Various RTMP fixes.

This commit is contained in:
Thulinma 2014-06-07 01:02:56 +02:00
parent 4f217771f0
commit c6e86a697b
7 changed files with 289 additions and 238 deletions

View file

@ -78,6 +78,142 @@ namespace Mist {
isInitialized = false;
myConn.close();
}
void Output::negotiateWithBuffer(int tid){
//Check whether the track exists
if (!meta_out.tracks.count(tid)) {
return;
}
//Do not re-negotiate already confirmed tracks
if (trackMap.count(tid)){
return;
}
//Do not re-negotiate if already at maximum for push tracks
if (trackMap.size() >= 5){
DEBUG_MSG(DLVL_FAIL, "Failed to negotiate for incoming track %d, already at maximum number of tracks", tid);
return;
}
char * tmp = playerConn.getData();
if (!tmp){
DEBUG_MSG(DLVL_FAIL, "Failed to negotiate for incoming track %d, there does not seem to be a connection with the buffer", tid);
return;
}
int bufConnOffset = trackMap.size();
DEBUG_MSG(DLVL_DEVEL, "Starting negotiation for incoming track %d, at offset %d", tid, bufConnOffset);
memset(tmp + 6 * bufConnOffset, 0, 6);
tmp[6 * bufConnOffset] = 0x80;
tmp[6 * bufConnOffset + 4] = 0xFF;
tmp[6 * bufConnOffset + 5] = 0xFF;
playerConn.keepAlive();
int newTid = 0x80000000;
while (newTid == 0x80000000){
Util::sleep(100);
newTid = ((long)(tmp[6 * bufConnOffset]) << 24) | ((long)(tmp[6 * bufConnOffset + 1]) << 16) | ((long)(tmp[6 * bufConnOffset + 2]) << 8) | tmp[6 * bufConnOffset + 3];
}
DEBUG_MSG(DLVL_DEVEL, "Track %d temporarily mapped to %d", tid, newTid);
char pageName[100];
sprintf(pageName, "liveStream_%s%d", streamName.c_str(), newTid);
metaPages[newTid].init(pageName, 8 * 1024 * 1024);
DTSC::Meta tmpMeta = meta_out;
tmpMeta.tracks.clear();
tmpMeta.tracks[newTid] = meta_out.tracks[tid];
tmpMeta.tracks[newTid].trackID = newTid;
JSON::Value tmpVal = tmpMeta.toJSON();
std::string tmpStr = tmpVal.toNetPacked();
memcpy(metaPages[newTid].mapped, tmpStr.data(), tmpStr.size());
DEBUG_MSG(DLVL_DEVEL, "Temporary metadata written for incoming track %d, handling as track %d", tid, newTid);
unsigned short firstPage = 0xFFFF;
unsigned int finalTid = newTid;
while (firstPage == 0xFFFF){
DEBUG_MSG(DLVL_DEVEL, "Re-checking at offset %d", bufConnOffset);
Util::sleep(100);
finalTid = ((long)(tmp[6 * bufConnOffset]) << 24) | ((long)(tmp[6 * bufConnOffset + 1]) << 16) | ((long)(tmp[6 * bufConnOffset + 2]) << 8) | tmp[6 * bufConnOffset + 3];
firstPage = ((long)(tmp[6 * bufConnOffset + 4]) << 8) | tmp[6 * bufConnOffset + 5];
if (finalTid == 0xFFFFFFFF){
DEBUG_MSG(DLVL_DEVEL, "Buffer has declined incoming track %d", tid);
return;
}
}
//Reinitialize so we make sure we got the right values here
finalTid = ((long)(tmp[6 * bufConnOffset]) << 24) | ((long)(tmp[6 * bufConnOffset + 1]) << 16) | ((long)(tmp[6 * bufConnOffset + 2]) << 8) | tmp[6 * bufConnOffset + 3];
firstPage = ((long)(tmp[6 * bufConnOffset + 4]) << 8) | tmp[6 * bufConnOffset + 5];
if (finalTid == 0xFFFFFFFF){
DEBUG_MSG(DLVL_DEVEL, "Buffer has declined incoming track %d", tid);
memset(tmp + 6 * bufConnOffset, 0, 6);
return;
}
DEBUG_MSG(DLVL_DEVEL, "Buffer accepted incoming track %d, temporary mapping %d as final mapping %d", tid, newTid, finalTid);
DEBUG_MSG(DLVL_DEVEL, "Buffer has indicated that incoming track %d should start writing on track %d, page %d", tid, finalTid, firstPage);
memset(pageName, 0, 100);
sprintf(pageName, "%s%d_%d", streamName.c_str(), finalTid, firstPage);
curPages[finalTid].init(pageName, 8 * 1024 * 1024);
trackMap[tid] = finalTid;
bookKeeping[finalTid] = DTSCPageData();
DEBUG_MSG(DLVL_DEVEL, "Done negotiating for incoming track %d", tid);
}
void Output::negotiatePushTracks() {
int i = 0;
for (std::map<int, DTSC::Track>::iterator it = meta_out.tracks.begin(); it != meta_out.tracks.end() && i < 5; it++){
negotiateWithBuffer(it->first);
i++;
}
}
void Output::bufferPacket(JSON::Value & pack){
if (myMeta.tracks[pack["trackid"].asInt()].type != "video"){
if ((pack["time"].asInt() - bookKeeping[trackMap[pack["trackid"].asInt()]].lastKeyTime) >= 5000){
pack["keyframe"] = 1LL;
bookKeeping[trackMap[pack["trackid"].asInt()]].lastKeyTime = pack["time"].asInt();
}
}
if (pack["trackid"].asInt() == 0){
return;
}
//Re-negotiate declined tracks on each keyframe, to compensate for "broken" tracks
if (!trackMap.count(pack["trackid"].asInt()) || !trackMap[pack["trackid"].asInt()]){
if (pack.isMember("keyframe") && pack["keyframe"]){
negotiateWithBuffer(pack["trackid"].asInt());
}
}
if (!trackMap.count(pack["trackid"].asInt()) || !trackMap[pack["trackid"].asInt()]){
//declined track;
return;
}
pack["trackid"] = trackMap[pack["trackid"].asInt()];
long long unsigned int tNum = pack["trackid"].asInt();
if (!bookKeeping.count(tNum)){
return;
}
int pageNum = bookKeeping[tNum].pageNum;
std::string tmp = pack.toNetPacked();
if (bookKeeping[tNum].curOffset > 8388608 && pack.isMember("keyframe") && pack["keyframe"]){
Util::sleep(500);
//open new page
char nextPage[100];
sprintf(nextPage, "%s%llu_%d", streamName.c_str(), tNum, bookKeeping[tNum].pageNum + bookKeeping[tNum].keyNum);
curPages[tNum].init(nextPage, 26 * 1024 * 1024);
bookKeeping[tNum].pageNum += bookKeeping[tNum].keyNum;
bookKeeping[tNum].keyNum = 0;
bookKeeping[tNum].curOffset = 0;
}
if (bookKeeping[tNum].curOffset + tmp.size() < curPages[tNum].len){
bookKeeping[tNum].keyNum += (pack.isMember("keyframe") && pack["keyframe"]);
memcpy(curPages[tNum].mapped + bookKeeping[tNum].curOffset, tmp.data(), tmp.size());
bookKeeping[tNum].curOffset += tmp.size();
}else{
bookKeeping[tNum].curOffset += tmp.size();
DEBUG_MSG(DLVL_WARN, "Can't buffer frame on page %d, track %llu, time %lld, keyNum %d, offset %llu", pageNum, tNum, pack["time"].asInt(), bookKeeping[tNum].pageNum + bookKeeping[tNum].keyNum, bookKeeping[tNum].curOffset);
}
playerConn.keepAlive();
}
void Output::initialize(){
if (isInitialized){
@ -392,6 +528,7 @@ namespace Mist {
}
}
DEBUG_MSG(DLVL_MEDIUM, "MistOut client handler shutting down: %s, %s, %s", myConn.connected() ? "conn_active" : "conn_closed", wantRequest ? "want_request" : "no_want_request", parseData ? "parsing_data" : "not_parsing_data");
playerConn.finish();
myConn.close();
return 0;
}

View file

@ -25,7 +25,18 @@ namespace Mist {
unsigned int offset;
};
/// The output class is intended to be inherited by MistOut process classes.
struct DTSCPageData {
DTSCPageData() : pageNum(0), keyNum(0), partNum(0), dataSize(0), curOffset(0), firstTime(0), lastKeyTime(-5000){}
int pageNum;///<The current page number
int keyNum;///<The number of keyframes in this page.
int partNum;///<The number of parts in this page.
unsigned long long int dataSize;///<The full size this page should be.
unsigned long long int curOffset;///<The current write offset in the page.
unsigned long long int firstTime;///<The first timestamp of the page.
long long int lastKeyTime;///<The time of the last keyframe of the page.
};
/// The output class is intended to be inherited by MistOut process classes.
/// It contains all generic code and logic, while the child classes implement
/// anything specific to particular protocols or containers.
/// It contains several virtual functions, that may be overridden to "hook" into
@ -93,6 +104,18 @@ namespace Mist {
//Read-only stream data variables
DTSC::Packet currentPacket;///< The packet that is ready for sending now.
DTSC::Meta myMeta;///< Up to date stream metadata
//For pushing data through an output into the buffer process
void negotiateWithBuffer(int tid);
void negotiatePushTracks();
void bufferPacket(JSON::Value & pack);
DTSC::Meta meta_out;
std::deque<JSON::Value> preBuf;
std::map<int,int> trackMap;
std::map<int,IPC::sharedPage> metaPages;
std::map<int,DTSCPageData> bookKeeping;
};
}

View file

@ -587,147 +587,6 @@ namespace Mist {
#endif
} //parseAMFCommand
void OutRTMP::bufferPacket(JSON::Value & pack){
if (!trackMap.count(pack["trackid"].asInt())){
//declined track;
return;
}
if (myMeta.tracks[pack["trackid"].asInt()].type != "video"){
if ((pack["time"].asInt() - bookKeeping[trackMap[pack["trackid"].asInt()]].lastKeyTime) >= 5000){
pack["keyframe"] = 1LL;
bookKeeping[trackMap[pack["trackid"].asInt()]].lastKeyTime = pack["time"].asInt();
}
}
pack["trackid"] = trackMap[pack["trackid"].asInt()];
long long unsigned int tNum = pack["trackid"].asInt();
if (!bookKeeping.count(tNum)){
return;
}
int pageNum = bookKeeping[tNum].pageNum;
std::string tmp = pack.toNetPacked();
if (bookKeeping[tNum].curOffset > 8388608 && pack.isMember("keyframe") && pack["keyframe"]){
Util::sleep(500);
//open new page
char nextPage[100];
sprintf(nextPage, "%s%llu_%d", streamName.c_str(), tNum, bookKeeping[tNum].pageNum + bookKeeping[tNum].keyNum);
curPages[tNum].init(nextPage, 26 * 1024 * 1024);
bookKeeping[tNum].pageNum += bookKeeping[tNum].keyNum;
bookKeeping[tNum].keyNum = 0;
bookKeeping[tNum].curOffset = 0;
}
if (bookKeeping[tNum].curOffset + tmp.size() < curPages[tNum].len){
bookKeeping[tNum].keyNum += (pack.isMember("keyframe") && pack["keyframe"]);
memcpy(curPages[tNum].mapped + bookKeeping[tNum].curOffset, tmp.data(), tmp.size());
bookKeeping[tNum].curOffset += tmp.size();
}else{
bookKeeping[tNum].curOffset += tmp.size();
DEBUG_MSG(DLVL_WARN, "Can't buffer frame on page %d, track %llu, time %lld, keyNum %d, offset %llu", pageNum, tNum, pack["time"].asInt(), bookKeeping[tNum].pageNum + bookKeeping[tNum].keyNum, bookKeeping[tNum].curOffset);
///\todo Open next page plx
}
playerConn.keepAlive();
}
void OutRTMP::negotiatePushTracks() {
char * tmp = playerConn.getData();
if (!tmp){
DEBUG_MSG(DLVL_FAIL, "No userpage allocated");
return;
}
memset(tmp, 0, 30);
unsigned int i = 0;
for (std::map<int, DTSC::Track>::iterator it = meta_out.tracks.begin(); it != meta_out.tracks.end() && i < 5; it++){
DEBUG_MSG(DLVL_DEVEL, "Negotiating tracknum for id %d", it->first);
(tmp + 6 * i)[0] = 0x80;
(tmp + 6 * i)[1] = 0x00;
(tmp + 6 * i)[2] = 0x00;
(tmp + 6 * i)[3] = 0x00;
(tmp + 6 * i)[4] = (it->first >> 8) & 0xFF;
(tmp + 6 * i)[5] = (it->first) & 0xFF;
i++;
}
playerConn.keepAlive();
bool gotAllNumbers = false;
while (!gotAllNumbers){
Util::sleep(100);
gotAllNumbers = true;
i = 0;
for (std::map<int, DTSC::Track>::iterator it = meta_out.tracks.begin(); it != meta_out.tracks.end() && i < 5; it++){
unsigned long tNum = (((long)(tmp + (6 * i))[0]) << 24) | (((long)(tmp + (6 * i))[1]) << 16) | (((long)(tmp + (6 * i))[2]) << 8) | (long)(tmp + (6 * i))[3];
unsigned short oldNum = (((long)(tmp + (6 * i))[4]) << 8) | (long)(tmp + (6 * i))[5];
if( tNum & 0x80000000){
gotAllNumbers = false;
break;
}else{
DEBUG_MSG(DLVL_DEVEL, "Mapped %d -> %lu", oldNum, tNum);
trackMap[oldNum] = tNum;
}
i++;
}
}
for (std::map<int, int>::iterator it = trackMap.begin(); it != trackMap.end(); it++){
char tmp[100];
sprintf( tmp, "liveStream_%s%d", streamName.c_str(), it->second);
metaPages[it->second].init(std::string(tmp), 8 * 1024 * 1024);
DTSC::Meta tmpMeta = meta_out;
tmpMeta.tracks.clear();
tmpMeta.tracks[it->second] = meta_out.tracks[it->first];
tmpMeta.tracks[it->second].trackID = it->second;
JSON::Value tmpVal = tmpMeta.toJSON();
std::string tmpStr = tmpVal.toNetPacked();
memcpy(metaPages[it->second].mapped, tmpStr.data(), tmpStr.size());
DEBUG_MSG(DLVL_DEVEL, "Written meta for track %d", it->second);
}
gotAllNumbers = false;
while (!gotAllNumbers){
Util::sleep(100);
gotAllNumbers = true;
i = 0;
unsigned int j = 0;
//update Metadata;
JSON::Value jsonMeta;
JSON::fromDTMI((const unsigned char*)streamIndex.mapped + 8, streamIndex.len - 8, j, jsonMeta);
myMeta = DTSC::Meta(jsonMeta);
tmp = playerConn.getData();
for (std::map<int, DTSC::Track>::iterator it = meta_out.tracks.begin(); it != meta_out.tracks.end() && i < 5; it++){
unsigned long tNum = (((long)(tmp + (6 * i))[0]) << 24) | (((long)(tmp + (6 * i))[1]) << 16) | (((long)(tmp + (6 * i))[2]) << 8) | (long)(tmp + (6 * i))[3];
if( tNum == 0xFFFFFFFF){
DEBUG_MSG(DLVL_DEVEL, "Skipping a declined track");
i++;
continue;
}
if(!myMeta.tracks.count(tNum)){
gotAllNumbers = false;
break;
}
i++;
}
}
i = 0;
tmp = playerConn.getData();
for (std::map<int, DTSC::Track>::iterator it = meta_out.tracks.begin(); it != meta_out.tracks.end() && i < 5; it++){
unsigned long tNum = ((long)(tmp[6*i]) << 24) | ((long)(tmp[6 * i + 1]) << 16) | ((long)(tmp[6 * i + 2]) << 8) | tmp[6 * i + 3];
if( tNum == 0xFFFFFFFF){
tNum = ((long)(tmp[6 * i + 4]) << 8) | (long)tmp[6 * i + 5];
DEBUG_MSG(DLVL_WARN, "Buffer declined track %i", trackMap[tNum]);
trackMap.erase(tNum);
tmp[6*i] = 0;
tmp[6*i+1] = 0;
tmp[6*i+2] = 0;
tmp[6*i+3] = 0;
tmp[6*i+4] = 0;
tmp[6*i+5] = 0;
}else{
char firstPage[100];
sprintf(firstPage, "%s%lu_%d", streamName.c_str(), tNum, 0);
curPages[tNum].init(firstPage, 8 * 1024 * 1024);
bookKeeping[tNum] = DTSCPageData();
DEBUG_MSG(DLVL_WARN, "Buffer accepted track %lu", tNum);
}
i++;
}
}
///\brief Gets and parses one RTMP chunk at a time.
///\param inputBuffer A buffer filled with chunk data.
void OutRTMP::parseChunk(Socket::Buffer & inputBuffer) {

View file

@ -5,18 +5,7 @@
namespace Mist {
struct DTSCPageData {
DTSCPageData() : pageNum(0), keyNum(0), partNum(0), dataSize(0), curOffset(0), firstTime(0), lastKeyTime(-5000){}
int pageNum;///<The current page number
int keyNum;///<The number of keyframes in this page.
int partNum;///<The number of parts in this page.
unsigned long long int dataSize;///<The full size this page should be.
unsigned long long int curOffset;///<The current write offset in the page.
unsigned long long int firstTime;///<The first timestamp of the page.
long long int lastKeyTime;///<The time of the last keyframe of the page.
};
class OutRTMP : public Output {
class OutRTMP : public Output {
public:
OutRTMP(Socket::Connection & conn);
~OutRTMP();
@ -24,10 +13,7 @@ namespace Mist {
void onRequest();
void sendNext();
void sendHeader();
void bufferPacket(JSON::Value & pack);
protected:
DTSC::Meta meta_out;
void negotiatePushTracks();
std::string app_name;
bool sending;
int counter;
@ -35,10 +21,6 @@ namespace Mist {
void parseChunk(Socket::Buffer & inputBuffer);
void parseAMFCommand(AMF::Object & amfData, int messageType, int streamId);
void sendCommand(AMF::Object & amfReply, int messageType, int streamId);
std::deque<JSON::Value> preBuf;
std::map<int,int> trackMap;
std::map<int,IPC::sharedPage> metaPages;
std::map<int,DTSCPageData> bookKeeping;
};
}