SRT output now displays an error on non-subtitle requested tracks, and fixed a bug in the JSON connector
This commit is contained in:
parent
1c93131dd9
commit
3a4726d817
2 changed files with 113 additions and 140 deletions
|
@ -90,16 +90,14 @@ namespace Connector_HTTP {
|
|||
jsondata.clear();
|
||||
jsondata << "[";
|
||||
|
||||
//if (ready4data){
|
||||
// if ( !inited){
|
||||
//we are ready, connect the socket!
|
||||
if ( !ss.connected()){
|
||||
ss = Util::Stream::getStream(streamname);
|
||||
}
|
||||
if ( !ss.connected()){
|
||||
#if DEBUG >= 1
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not connect to server for %s!\n", streamname.c_str());
|
||||
#endif
|
||||
#endif
|
||||
ss.close();
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
|
||||
|
@ -108,6 +106,7 @@ namespace Connector_HTTP {
|
|||
inited = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
//wait until we have a header
|
||||
while ( !Strm.metadata && ss.connected()){
|
||||
if (ss.spool()){
|
||||
|
@ -115,10 +114,8 @@ namespace Connector_HTTP {
|
|||
}else{
|
||||
Util::sleep(5);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
seek_sec = seek_byte;
|
||||
|
||||
std::stringstream cmd;
|
||||
|
@ -145,7 +142,6 @@ namespace Connector_HTTP {
|
|||
std::cout << "sending command " << cmd.str() << std::endl;
|
||||
inited = true;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,19 +153,9 @@ namespace Connector_HTTP {
|
|||
ss.SendNow(conn.getStats("HTTP_JSON").c_str());
|
||||
}
|
||||
|
||||
|
||||
if (ss.spool()){
|
||||
while (Strm.parsePacket(ss.Received())){
|
||||
|
||||
if(Strm.lastType() == DTSC::META){
|
||||
if(jsondata.str().length() > 1)
|
||||
{
|
||||
jsondata << ",";
|
||||
}
|
||||
|
||||
jsondata << Strm.getPacket().toString();
|
||||
|
||||
}else 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.SetHeader("Content-Type", "application/json"); //Send the correct content-type for FLV files
|
||||
jsondata << "]";
|
||||
|
@ -178,10 +164,14 @@ namespace Connector_HTTP {
|
|||
inited = false;
|
||||
jsondata.str(""); // totally do this
|
||||
jsondata.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
if (jsondata.str().length() > 1){
|
||||
jsondata << ",";
|
||||
}
|
||||
|
||||
|
||||
jsondata << Strm.getPacket().toString();
|
||||
}
|
||||
}else{
|
||||
Util::sleep(1);
|
||||
|
|
|
@ -37,28 +37,18 @@ namespace Connector_HTTP {
|
|||
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 seek_sec = 0;//Seek position in ms
|
||||
unsigned int seek_byte = 0;//Seek position in bytes
|
||||
unsigned int seek_time = 0;//Seek position in ms
|
||||
int trackID = -1; // the track to be selected
|
||||
int curIndex; // SRT index
|
||||
bool subtitleTrack = false; // check whether the requested track is a srt track
|
||||
|
||||
int curIndex;
|
||||
|
||||
std::stringstream srtdata;
|
||||
std::stringstream srtdata; // ss output data
|
||||
|
||||
while (conn.connected()){
|
||||
//Only attempt to parse input when not yet init'ed.
|
||||
if ( !inited){
|
||||
if (conn.Received().size() || conn.spool()){
|
||||
//make sure it ends in a \n
|
||||
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 (HTTP_R.Read(conn)){
|
||||
#if DEBUG >= 5
|
||||
std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
|
||||
#endif
|
||||
|
@ -81,11 +71,14 @@ namespace Connector_HTTP {
|
|||
if ( !HTTP_R.GetVar("fs").empty()){
|
||||
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
|
||||
if (start < 10800){
|
||||
seek_byte = start * 1000; //ms, not s
|
||||
seek_time = start * 1000; //ms, not s
|
||||
}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!
|
||||
|
@ -104,59 +97,58 @@ namespace Connector_HTTP {
|
|||
//std::cout << "CONTINUE? Y/N J/K" << std::endl;
|
||||
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){
|
||||
// no track was given. Fetch the first track that has SRT data
|
||||
for (JSON::ObjIter objIt = Strm.metadata["tracks"].ObjBegin(); objIt != Strm.metadata["tracks"].ObjEnd(); objIt++){
|
||||
if (subtrackID == -1 && objIt->second["type"].asStringRef() == "meta" && objIt->second["codec"].asStringRef() == "srt"){
|
||||
subtrackID = objIt->second["trackid"].asInt();
|
||||
if (objIt->second["codec"].asStringRef() == "srt"){
|
||||
trackID = objIt->second["trackid"].asInt();
|
||||
subtitleTrack = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
seek_sec = seek_byte;
|
||||
}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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!subtitleTrack){
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetBody("# This track doesn't contain subtitle data.\n");
|
||||
conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
|
||||
subtitleTrack = false;
|
||||
HTTP_R.Clean();
|
||||
continue;
|
||||
}
|
||||
|
||||
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.SetBody("No subtitles found for this stream.\n");
|
||||
conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
|
||||
|
||||
}
|
||||
cmd << "t " << trackID;
|
||||
|
||||
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());
|
||||
|
||||
inited = true;
|
||||
|
||||
|
||||
|
||||
HTTP_R.Clean(); //clean for any possible next requests
|
||||
srtdata.clear();
|
||||
curIndex = 1; // set to 1, first srt 'track'
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inited){
|
||||
|
||||
unsigned int now = Util::epoch();
|
||||
if (now != lastStats){
|
||||
|
@ -164,15 +156,13 @@ namespace Connector_HTTP {
|
|||
ss.SendNow(conn.getStats("HTTP_SRT").c_str());
|
||||
}
|
||||
|
||||
if (inited){
|
||||
|
||||
if (ss.spool()){
|
||||
while (Strm.parsePacket(ss.Received())){
|
||||
|
||||
|
||||
|
||||
if(Strm.lastType() == DTSC::META){
|
||||
|
||||
|
||||
srtdata << curIndex++ << std::endl;
|
||||
long long unsigned int time = Strm.getPacket()["time"].asInt();
|
||||
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(3) << time % 1000 << std::endl;
|
||||
srtdata << Strm.lastData() << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
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", "text/plain"); //Send the correct content-type for FLV files
|
||||
|
@ -197,16 +185,11 @@ namespace Connector_HTTP {
|
|||
inited = false;
|
||||
|
||||
srtdata.str("");
|
||||
srtdata.clear(); // :)
|
||||
//conn.close();
|
||||
//ss.close();
|
||||
srtdata.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}else{
|
||||
Util::sleep(1);
|
||||
Util::sleep(200);
|
||||
}
|
||||
if ( !ss.connected()){
|
||||
break;
|
||||
|
|
Loading…
Add table
Reference in a new issue