Added a JSON and SRT progressive HTTP output
This commit is contained in:
parent
0fa966906d
commit
8b2c4e55aa
3 changed files with 511 additions and 1 deletions
|
@ -20,6 +20,8 @@ bin_PROGRAMS+=MistConnHTTPProgressiveFLV
|
|||
bin_PROGRAMS+=MistConnHTTPProgressiveMP3
|
||||
bin_PROGRAMS+=MistConnHTTPProgressiveMP4
|
||||
bin_PROGRAMS+=MistConnHTTPProgressiveOGG
|
||||
bin_PROGRAMS+=MistConnHTTPSRT
|
||||
bin_PROGRAMS+=MistConnHTTPJSON
|
||||
bin_PROGRAMS+=MistConnHTTPDynamic
|
||||
bin_PROGRAMS+=MistConnHTTPSmooth
|
||||
bin_PROGRAMS+=MistConnHTTPLive
|
||||
|
@ -61,10 +63,12 @@ MistConnHTTPProgressiveFLV_SOURCES=connectors/conn_http_progressive_flv.cpp ../V
|
|||
MistConnHTTPProgressiveMP3_SOURCES=connectors/conn_http_progressive_mp3.cpp ../VERSION
|
||||
MistConnHTTPProgressiveMP4_SOURCES=connectors/conn_http_progressive_mp4.cpp ../VERSION
|
||||
MistConnHTTPProgressiveOGG_SOURCES=connectors/conn_http_progressive_ogg.cpp ../VERSION
|
||||
MistConnHTTPDynamic_SOURCES=connectors/conn_http_dynamic.cpp ../VERSION
|
||||
MistConnHTTPSRT_SOURCES=connectors/conn_http_srt.cpp ../VERSIONMistConnHTTPDynamic_SOURCES=connectors/conn_http_dynamic.cpp ../VERSION
|
||||
MistConnHTTPSmooth_SOURCES=connectors/conn_http_smooth.cpp ../VERSION
|
||||
MistConnHTTPLive_SOURCES=connectors/conn_http_live.cpp ../VERSION
|
||||
MistConnTS_SOURCES=connectors/conn_ts.cpp ../VERSION
|
||||
MistConnHTTPSRT_SOURCES=connectors/conn_http_srt.cpp ../VERSION
|
||||
MistConnHTTPJSON_SOURCES=connectors/conn_http_json.cpp ../VERSION
|
||||
|
||||
#converters directory (Mist*2*)
|
||||
MistDTSC2FLV_SOURCES=converters/dtsc2flv.cpp
|
||||
|
|
242
src/connectors/conn_http_json.cpp
Normal file
242
src/connectors/conn_http_json.cpp
Normal file
|
@ -0,0 +1,242 @@
|
|||
///\file conn_http_progressive.cpp
|
||||
///\brief Contains the main code for the HTTP Progressive Connector
|
||||
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <mist/socket.h>
|
||||
#include <mist/http_parser.h>
|
||||
#include <mist/dtsc.h>
|
||||
#include <mist/flv_tag.h>
|
||||
#include <mist/amf.h>
|
||||
#include <mist/config.h>
|
||||
#include <mist/stream.h>
|
||||
#include <mist/timing.h>
|
||||
|
||||
///\brief Holds everything unique to HTTP Connectors.
|
||||
namespace Connector_HTTP {
|
||||
///\brief Main function for the HTTP Progressive Connector
|
||||
///\param conn A socket describing the connection the client.
|
||||
///\return The exit code of the connector.
|
||||
int JSONConnector(Socket::Connection conn){
|
||||
bool progressive_has_sent_header = false;//Indicates whether we have sent a header.
|
||||
DTSC::Stream Strm; //Incoming stream buffer.
|
||||
HTTP::Parser HTTP_R, HTTP_S;//HTTP Receiver en HTTP Sender.
|
||||
bool inited = false;//Whether the stream is initialized
|
||||
Socket::Connection ss( -1);//The Stream Socket, used to connect to the desired 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 seek_sec = 0;//Seek position in ms
|
||||
unsigned int seek_byte = 0;//Seek position in bytes
|
||||
|
||||
std::stringstream jsondata;
|
||||
|
||||
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 DEBUG >= 5
|
||||
std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
|
||||
#endif
|
||||
conn.setHost(HTTP_R.GetHeader("X-Origin"));
|
||||
streamname = HTTP_R.GetHeader("X-Stream");
|
||||
|
||||
int start = 0;
|
||||
if ( !HTTP_R.GetVar("start").empty()){
|
||||
start = atoi(HTTP_R.GetVar("start").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("starttime").empty()){
|
||||
start = atoi(HTTP_R.GetVar("starttime").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("apstart").empty()){
|
||||
start = atoi(HTTP_R.GetVar("apstart").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("ec_seek").empty()){
|
||||
start = atoi(HTTP_R.GetVar("ec_seek").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("fs").empty()){
|
||||
start = atoi(HTTP_R.GetVar("fs").c_str());
|
||||
}
|
||||
//under 3 hours we assume seconds, otherwise byte position
|
||||
if (start < 10800){
|
||||
seek_byte = start * 1000; //ms, not s
|
||||
}else{
|
||||
seek_byte = start * 1000; //divide by 1mbit, then *1000 for ms.
|
||||
}
|
||||
// ready4data = true;
|
||||
HTTP_R.Clean(); //clean for any possible next requests
|
||||
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
|
||||
fprintf(stderr, "Could not connect to server for %s!\n", streamname.c_str());
|
||||
#endif
|
||||
ss.close();
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
|
||||
conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
|
||||
//ready4data = false;
|
||||
inited = false;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
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() != "video" && objIt->second["type"].asStringRef() != "audio"){
|
||||
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());
|
||||
|
||||
inited = true;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inited){
|
||||
|
||||
unsigned int now = Util::epoch();
|
||||
if (now != lastStats){
|
||||
lastStats = now;
|
||||
ss.SendNow(conn.getStats("HTTP_JSON").c_str());
|
||||
}
|
||||
|
||||
|
||||
if (ss.spool()){
|
||||
while (Strm.parsePacket(ss.Received())){
|
||||
|
||||
if(Strm.lastType() != DTSC::PAUSEMARK){
|
||||
if(jsondata.str().length() > 1)
|
||||
{
|
||||
jsondata << ",";
|
||||
}
|
||||
|
||||
jsondata << Strm.getPacket().toString();
|
||||
|
||||
}else{
|
||||
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{
|
||||
Util::sleep(1);
|
||||
}
|
||||
if ( !ss.connected()){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
conn.close();
|
||||
ss.SendNow(conn.getStats("HTTP_JSON").c_str());
|
||||
ss.close();
|
||||
return 0;
|
||||
} //SRT main function
|
||||
|
||||
} //Connector_HTTP namespace
|
||||
|
||||
///\brief The standard process-spawning main function.
|
||||
int main(int argc, char ** argv){
|
||||
Util::Config conf(argv[0], PACKAGE_VERSION);
|
||||
JSON::Value capa;
|
||||
capa["desc"] = "Enables HTTP protocol JSON streaming.";
|
||||
capa["deps"] = "HTTP";
|
||||
capa["url_rel"] = "/$.json";
|
||||
capa["url_match"] = "/$.json";
|
||||
capa["url_handler"] = "http";
|
||||
capa["url_type"] = "json";
|
||||
capa["socket"] = "http_json";
|
||||
conf.addBasicConnectorOptions(capa);
|
||||
conf.parseArgs(argc, argv);
|
||||
|
||||
if (conf.getBool("json")){
|
||||
std::cout << capa.toString() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Socket::Server server_socket = Socket::Server("/tmp/mist/http_json");
|
||||
if ( !server_socket.connected()){
|
||||
return 1;
|
||||
}
|
||||
conf.activate();
|
||||
|
||||
while (server_socket.connected() && conf.is_active){
|
||||
Socket::Connection S = server_socket.accept();
|
||||
if (S.connected()){ //check if the new connection is valid
|
||||
pid_t myid = fork();
|
||||
if (myid == 0){ //if new child, start MAINHANDLER
|
||||
return Connector_HTTP::JSONConnector(S);
|
||||
}else{ //otherwise, do nothing or output debugging text
|
||||
#if DEBUG >= 5
|
||||
fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} //while connected
|
||||
server_socket.close();
|
||||
return 0;
|
||||
} //main
|
264
src/connectors/conn_http_srt.cpp
Normal file
264
src/connectors/conn_http_srt.cpp
Normal file
|
@ -0,0 +1,264 @@
|
|||
///\file conn_http_progressive.cpp
|
||||
///\brief Contains the main code for the HTTP Progressive Connector
|
||||
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <mist/socket.h>
|
||||
#include <mist/http_parser.h>
|
||||
#include <mist/dtsc.h>
|
||||
#include <mist/flv_tag.h>
|
||||
#include <mist/amf.h>
|
||||
#include <mist/config.h>
|
||||
#include <mist/stream.h>
|
||||
#include <mist/timing.h>
|
||||
|
||||
///\brief Holds everything unique to HTTP Connectors.
|
||||
namespace Connector_HTTP {
|
||||
///\brief Main function for the HTTP Progressive Connector
|
||||
///\param conn A socket describing the connection the client.
|
||||
///\return The exit code of the connector.
|
||||
int SRTConnector(Socket::Connection conn){
|
||||
bool progressive_has_sent_header = false;//Indicates whether we have sent a header.
|
||||
DTSC::Stream Strm; //Incoming stream buffer.
|
||||
HTTP::Parser HTTP_R, HTTP_S;//HTTP Receiver en HTTP Sender.
|
||||
bool inited = false;//Whether the stream is initialized
|
||||
Socket::Connection ss( -1);//The Stream Socket, used to connect to the desired 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 seek_sec = 0;//Seek position in ms
|
||||
unsigned int seek_byte = 0;//Seek position in bytes
|
||||
|
||||
int curIndex;
|
||||
|
||||
std::stringstream srtdata;
|
||||
|
||||
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 DEBUG >= 5
|
||||
std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
|
||||
#endif
|
||||
conn.setHost(HTTP_R.GetHeader("X-Origin"));
|
||||
streamname = HTTP_R.GetHeader("X-Stream");
|
||||
|
||||
int start = 0;
|
||||
if ( !HTTP_R.GetVar("start").empty()){
|
||||
start = atoi(HTTP_R.GetVar("start").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("starttime").empty()){
|
||||
start = atoi(HTTP_R.GetVar("starttime").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("apstart").empty()){
|
||||
start = atoi(HTTP_R.GetVar("apstart").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("ec_seek").empty()){
|
||||
start = atoi(HTTP_R.GetVar("ec_seek").c_str());
|
||||
}
|
||||
if ( !HTTP_R.GetVar("fs").empty()){
|
||||
start = atoi(HTTP_R.GetVar("fs").c_str());
|
||||
}
|
||||
//under 3 hours we assume seconds, otherwise byte position
|
||||
if (start < 10800){
|
||||
seek_byte = start * 1000; //ms, not s
|
||||
}else{
|
||||
seek_byte = start * 1000; //divide by 1mbit, then *1000 for ms.
|
||||
}
|
||||
|
||||
//we are ready, connect the socket!
|
||||
if ( !ss.connected()){
|
||||
ss = Util::Stream::getStream(streamname);
|
||||
}
|
||||
if ( !ss.connected()){
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not connect to server for %s!\n", streamname.c_str());
|
||||
#endif
|
||||
ss.close();
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
|
||||
conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
|
||||
inited = false;
|
||||
//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;
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
seek_sec = seek_byte;
|
||||
|
||||
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"));
|
||||
|
||||
}
|
||||
|
||||
int maxTime = Strm.metadata["lastms"].asInt();
|
||||
|
||||
cmd << "\ns " << seek_sec << "\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){
|
||||
lastStats = now;
|
||||
ss.SendNow(conn.getStats("HTTP_SRT").c_str());
|
||||
}
|
||||
|
||||
|
||||
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) << ":";
|
||||
srtdata << std::setfill('0') << std::setw(2) << ((time % 3600000) / 60000) << ":";
|
||||
srtdata << std::setfill('0') << std::setw(2) << (((time % 3600000) % 60000) / 1000) << ",";
|
||||
srtdata << std::setfill('0') << std::setw(3) << time % 1000 << " --> ";
|
||||
time += Strm.getPacket()["duration"].asInt();
|
||||
srtdata << std::setfill('0') << std::setw(2) << (time / 3600000) << ":";
|
||||
srtdata << std::setfill('0') << std::setw(2) << ((time % 3600000) / 60000) << ":";
|
||||
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
|
||||
HTTP_S.SetBody(srtdata.str());
|
||||
conn.SendNow(HTTP_S.BuildResponse("200", "OK")); //no SetBody = unknown length - this is intentional, we will stream the entire file
|
||||
inited = false;
|
||||
|
||||
srtdata.str("");
|
||||
srtdata.clear(); // :)
|
||||
//conn.close();
|
||||
//ss.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}else{
|
||||
Util::sleep(1);
|
||||
}
|
||||
if ( !ss.connected()){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
conn.close();
|
||||
ss.SendNow(conn.getStats("HTTP_SRT").c_str());
|
||||
ss.close();
|
||||
return 0;
|
||||
} //SRT main function
|
||||
|
||||
} //Connector_HTTP namespace
|
||||
|
||||
///\brief The standard process-spawning main function.
|
||||
int main(int argc, char ** argv){
|
||||
Util::Config conf(argv[0], PACKAGE_VERSION);
|
||||
JSON::Value capa;
|
||||
capa["desc"] = "Enables HTTP protocol subtitle streaming.";
|
||||
capa["deps"] = "HTTP";
|
||||
capa["url_rel"] = "/$.srt";
|
||||
capa["url_match"] = "/$.srt";
|
||||
capa["url_handler"] = "http";
|
||||
capa["url_type"] = "subtitle";
|
||||
capa["socket"] = "http_srt";
|
||||
conf.addBasicConnectorOptions(capa);
|
||||
conf.parseArgs(argc, argv);
|
||||
|
||||
if (conf.getBool("json")){
|
||||
std::cout << capa.toString() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Socket::Server server_socket = Socket::Server("/tmp/mist/http_srt");
|
||||
if ( !server_socket.connected()){
|
||||
return 1;
|
||||
}
|
||||
conf.activate();
|
||||
|
||||
while (server_socket.connected() && conf.is_active){
|
||||
Socket::Connection S = server_socket.accept();
|
||||
if (S.connected()){ //check if the new connection is valid
|
||||
pid_t myid = fork();
|
||||
if (myid == 0){ //if new child, start MAINHANDLER
|
||||
return Connector_HTTP::SRTConnector(S);
|
||||
}else{ //otherwise, do nothing or output debugging text
|
||||
#if DEBUG >= 5
|
||||
fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} //while connected
|
||||
server_socket.close();
|
||||
return 0;
|
||||
} //main
|
Loading…
Add table
Reference in a new issue