SRT output now displays an error on non-subtitle requested tracks, and fixed a bug in the JSON connector

This commit is contained in:
ThatGuy 2013-08-27 12:02:06 +02:00
parent 1c93131dd9
commit 3a4726d817
2 changed files with 113 additions and 140 deletions

View file

@ -90,98 +90,88 @@ namespace Connector_HTTP {
jsondata.clear(); jsondata.clear();
jsondata << "["; jsondata << "[";
//if (ready4data){
// if ( !inited){
//we are ready, connect the socket! //we are ready, connect the socket!
if ( !ss.connected()){ if ( !ss.connected()){
ss = Util::Stream::getStream(streamname); ss = Util::Stream::getStream(streamname);
} }
if ( !ss.connected()){ if ( !ss.connected()){
#if DEBUG >= 1 #if DEBUG >= 1
fprintf(stderr, "Could not connect to server for %s!\n", streamname.c_str()); fprintf(stderr, "Could not connect to server for %s!\n", streamname.c_str());
#endif #endif
ss.close(); ss.close();
HTTP_S.Clean(); HTTP_S.Clean();
HTTP_S.SetBody("No such stream is available on the system. Please try again.\n"); HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
conn.SendNow(HTTP_S.BuildResponse("404", "Not found")); conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
//ready4data = false; //ready4data = false;
inited = false; inited = false;
continue; continue;
}
//wait until we have a header
while ( !Strm.metadata && ss.connected()){
if (ss.spool()){
Strm.parsePacket(ss.Received()); //read the metadata
}else{
Util::sleep(5);
} }
} //wait until we have a header
while ( !Strm.metadata && ss.connected()){
if (ss.spool()){
seek_sec = seek_byte; Strm.parsePacket(ss.Received()); //read the metadata
}else{
std::stringstream cmd; Util::sleep(5);
cmd << "t";
if (Strm.metadata["tracks"].size()){
for (JSON::ObjIter objIt = Strm.metadata["tracks"].ObjBegin(); objIt != Strm.metadata["tracks"].ObjEnd(); objIt++){
if ( objIt->second["type"].asStringRef() == "meta" ){
std::cout << "selecting track " << objIt->second["trackid"].asInt() << std::endl;
cmd << " " << objIt->second["trackid"].asInt();
} }
} }
}
if( cmd.str() == "t" ){
cmd.str("");
cmd.clear();
}
int maxTime = Strm.metadata["lastms"].asInt(); seek_sec = seek_byte;
std::stringstream cmd;
cmd << "t";
if (Strm.metadata["tracks"].size()){
for (JSON::ObjIter objIt = Strm.metadata["tracks"].ObjBegin(); objIt != Strm.metadata["tracks"].ObjEnd(); objIt++){
if ( objIt->second["type"].asStringRef() == "meta" ){
std::cout << "selecting track " << objIt->second["trackid"].asInt() << std::endl;
cmd << " " << objIt->second["trackid"].asInt();
}
}
}
if( cmd.str() == "t" ){
cmd.str("");
cmd.clear();
}
int maxTime = Strm.metadata["lastms"].asInt();
cmd << "\ns " << seek_sec << "\np " << maxTime << "\n";
ss.SendNow(cmd.str().c_str(), cmd.str().size());
std::cout << "sending command " << cmd.str() << std::endl;
inited = true;
cmd << "\ns " << seek_sec << "\np " << maxTime << "\n";
ss.SendNow(cmd.str().c_str(), cmd.str().size());
std::cout << "sending command " << cmd.str() << std::endl;
inited = true;
} }
} }
} }
if (inited){ if (inited){
unsigned int now = Util::epoch(); unsigned int now = Util::epoch();
if (now != lastStats){ if (now != lastStats){
lastStats = now; lastStats = now;
ss.SendNow(conn.getStats("HTTP_JSON").c_str()); ss.SendNow(conn.getStats("HTTP_JSON").c_str());
} }
if (ss.spool()){ if (ss.spool()){
while (Strm.parsePacket(ss.Received())){ while (Strm.parsePacket(ss.Received())){
if(Strm.lastType() == DTSC::PAUSEMARK){
HTTP_S.Clean(); //make sure no parts of old requests are left in any buffers
HTTP_S.SetHeader("Content-Type", "application/json"); //Send the correct content-type for FLV files
jsondata << "]";
HTTP_S.SetBody(jsondata.str());
conn.SendNow(HTTP_S.BuildResponse("200", "OK")); //no SetBody = unknown length - this is intentional, we will stream the entire file
inited = false;
jsondata.str(""); // totally do this
jsondata.clear();
break;
}
if(Strm.lastType() == DTSC::META){ if (jsondata.str().length() > 1){
if(jsondata.str().length() > 1) jsondata << ",";
{ }
jsondata << ",";
} jsondata << Strm.getPacket().toString();
jsondata << Strm.getPacket().toString();
}else if(Strm.lastType() == DTSC::PAUSEMARK){
HTTP_S.Clean(); //make sure no parts of old requests are left in any buffers
HTTP_S.SetHeader("Content-Type", "application/json"); //Send the correct content-type for FLV files
jsondata << "]";
HTTP_S.SetBody(jsondata.str());
conn.SendNow(HTTP_S.BuildResponse("200", "OK")); //no SetBody = unknown length - this is intentional, we will stream the entire file
inited = false;
jsondata.str(""); // totally do this
jsondata.clear();
}
} }
}else{ }else{
Util::sleep(1); Util::sleep(1);
@ -189,7 +179,7 @@ namespace Connector_HTTP {
if ( !ss.connected()){ if ( !ss.connected()){
break; break;
} }
} }
} }
conn.close(); conn.close();

View file

@ -37,28 +37,18 @@ namespace Connector_HTTP {
std::string streamname;//Will contain the name of the stream. std::string streamname;//Will contain the name of the stream.
unsigned int lastStats = 0;//Indicates the last time that we have sent stats to the server socket. unsigned int lastStats = 0;//Indicates the last time that we have sent stats to the server socket.
unsigned int seek_sec = 0;//Seek position in ms unsigned int seek_time = 0;//Seek position in ms
unsigned int seek_byte = 0;//Seek position in bytes int trackID = -1; // the track to be selected
int curIndex; // SRT index
int curIndex; bool subtitleTrack = false; // check whether the requested track is a srt track
std::stringstream srtdata; std::stringstream srtdata; // ss output data
while (conn.connected()){ while (conn.connected()){
//Only attempt to parse input when not yet init'ed. //Only attempt to parse input when not yet init'ed.
if ( !inited){ if ( !inited){
if (conn.Received().size() || conn.spool()){ if (conn.Received().size() || conn.spool()){
//make sure it ends in a \n if (HTTP_R.Read(conn)){
if ( *(conn.Received().get().rbegin()) != '\n'){
std::string tmp = conn.Received().get();
conn.Received().get().clear();
if (conn.Received().size()){
conn.Received().get().insert(0, tmp);
}else{
conn.Received().append(tmp);
}
}
if (HTTP_R.Read(conn.Received().get())){
#if DEBUG >= 5 #if DEBUG >= 5
std::cout << "Received request: " << HTTP_R.getUrl() << std::endl; std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
#endif #endif
@ -81,11 +71,14 @@ namespace Connector_HTTP {
if ( !HTTP_R.GetVar("fs").empty()){ if ( !HTTP_R.GetVar("fs").empty()){
start = atoi(HTTP_R.GetVar("fs").c_str()); start = atoi(HTTP_R.GetVar("fs").c_str());
} }
if ( !HTTP_R.GetVar("trackid").empty()){
trackID = atoi(HTTP_R.GetVar("trackid").c_str());
}
//under 3 hours we assume seconds, otherwise byte position //under 3 hours we assume seconds, otherwise byte position
if (start < 10800){ if (start < 10800){
seek_byte = start * 1000; //ms, not s seek_time = start * 1000; //ms, not s
}else{ }else{
seek_byte = start * 1000; //divide by 1mbit, then *1000 for ms. seek_time = start * 1000; //divide by 1mbit, then *1000 for ms.
} }
//we are ready, connect the socket! //we are ready, connect the socket!
@ -104,75 +97,72 @@ namespace Connector_HTTP {
//std::cout << "CONTINUE? Y/N J/K" << std::endl; //std::cout << "CONTINUE? Y/N J/K" << std::endl;
continue; continue;
} }
//wait until we have a header
while ( !Strm.metadata && ss.connected()){
if (ss.spool()){
Strm.parsePacket(ss.Received()); //read the metadata
}else{
Util::sleep(5);
}
}
int subtrackID = -1; Strm.waitForMeta(ss);
if(trackID == -1){
for (JSON::ObjIter objIt = Strm.metadata["tracks"].ObjBegin(); objIt != Strm.metadata["tracks"].ObjEnd(); objIt++){ // no track was given. Fetch the first track that has SRT data
if (subtrackID == -1 && objIt->second["type"].asStringRef() == "meta" && objIt->second["codec"].asStringRef() == "srt"){ for (JSON::ObjIter objIt = Strm.metadata["tracks"].ObjBegin(); objIt != Strm.metadata["tracks"].ObjEnd(); objIt++){
subtrackID = objIt->second["trackid"].asInt(); if (objIt->second["codec"].asStringRef() == "srt"){
trackID = objIt->second["trackid"].asInt();
subtitleTrack = true;
break;
}
}
}else{
std::cout << "TRACKID YO " << trackID << std::endl;
// track *was* given, but we have to check whether it's an actual srt track
for (JSON::ObjIter objIt = Strm.metadata["tracks"].ObjBegin(); objIt != Strm.metadata["tracks"].ObjEnd(); objIt++){
if (objIt->second["trackid"].asInt() == trackID){
std::cout << " trackID matches with objIt, codec = srt" << std::endl;
subtitleTrack = (objIt->second["codec"].asStringRef() == "srt");
break;
}else{
subtitleTrack = false;
}
} }
} }
seek_sec = seek_byte;
if(!subtitleTrack){
std::stringstream cmd;
cmd << "t";
if (subtrackID != -1){
cmd << " " << subtrackID;
}
if( cmd.str() == "t" ){
// no srt track: build http error
std::cout << "srt: no track, sending 404" << std::endl;
ss.close();
HTTP_S.Clean(); HTTP_S.Clean();
HTTP_S.SetBody("No subtitles found for this stream.\n"); HTTP_S.SetBody("# This track doesn't contain subtitle data.\n");
conn.SendNow(HTTP_S.BuildResponse("404", "Not found")); conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
subtitleTrack = false;
HTTP_R.Clean();
continue;
} }
std::stringstream cmd;
cmd << "t " << trackID;
int maxTime = Strm.metadata["lastms"].asInt(); int maxTime = Strm.metadata["lastms"].asInt();
cmd << "\ns " << seek_sec << "\np " << maxTime << "\n"; cmd << "\ns " << seek_time << "\np " << maxTime << "\n";
ss.SendNow(cmd.str().c_str(), cmd.str().size()); ss.SendNow(cmd.str().c_str(), cmd.str().size());
inited = true; inited = true;
HTTP_R.Clean(); //clean for any possible next requests HTTP_R.Clean(); //clean for any possible next requests
srtdata.clear(); srtdata.clear();
curIndex = 1; // set to 1, first srt 'track' curIndex = 1; // set to 1, first srt 'track'
} }
} }
} }
unsigned int now = Util::epoch();
if (now != lastStats){
lastStats = now;
ss.SendNow(conn.getStats("HTTP_SRT").c_str());
}
if (inited){ if (inited){
unsigned int now = Util::epoch();
if (now != lastStats){
lastStats = now;
ss.SendNow(conn.getStats("HTTP_SRT").c_str());
}
if (ss.spool()){ if (ss.spool()){
while (Strm.parsePacket(ss.Received())){ while (Strm.parsePacket(ss.Received())){
if(Strm.lastType() == DTSC::META){ if(Strm.lastType() == DTSC::META){
srtdata << curIndex++ << std::endl; srtdata << curIndex++ << std::endl;
long long unsigned int time = Strm.getPacket()["time"].asInt(); long long unsigned int time = Strm.getPacket()["time"].asInt();
srtdata << std::setfill('0') << std::setw(2) << (time / 3600000) << ":"; srtdata << std::setfill('0') << std::setw(2) << (time / 3600000) << ":";
@ -185,10 +175,8 @@ namespace Connector_HTTP {
srtdata << std::setfill('0') << std::setw(2) << (((time % 3600000) % 60000) / 1000) << ","; srtdata << std::setfill('0') << std::setw(2) << (((time % 3600000) % 60000) / 1000) << ",";
srtdata << std::setfill('0') << std::setw(3) << time % 1000 << std::endl; srtdata << std::setfill('0') << std::setw(3) << time % 1000 << std::endl;
srtdata << Strm.lastData() << std::endl; srtdata << Strm.lastData() << std::endl;
} }
if( Strm.lastType() == DTSC::PAUSEMARK){ if( Strm.lastType() == DTSC::PAUSEMARK){
HTTP_S.Clean(); //make sure no parts of old requests are left in any buffers HTTP_S.Clean(); //make sure no parts of old requests are left in any buffers
HTTP_S.SetHeader("Content-Type", "text/plain"); //Send the correct content-type for FLV files HTTP_S.SetHeader("Content-Type", "text/plain"); //Send the correct content-type for FLV files
@ -197,16 +185,11 @@ namespace Connector_HTTP {
inited = false; inited = false;
srtdata.str(""); srtdata.str("");
srtdata.clear(); // :) srtdata.clear();
//conn.close();
//ss.close();
} }
} }
}else{ }else{
Util::sleep(1); Util::sleep(200);
} }
if ( !ss.connected()){ if ( !ss.connected()){
break; break;