Fixes for everyone! *strooit met fixes als zwarte piet*

This commit is contained in:
Thulinma 2012-09-17 16:47:17 +02:00
parent 8ba5823e00
commit 802f8a22b4
6 changed files with 233 additions and 200 deletions

View file

@ -91,14 +91,14 @@ namespace Connector_HTTP{
H.Clean();
H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
H.SetBody("<!DOCTYPE html><html><head><title>Unsupported Media Type</title></head><body><h1>Unsupported Media Type</h1>The server isn't quite sure what you wanted to receive from it.</body></html>");
conn->Send(H.BuildResponse("415", "Unsupported Media Type"));
conn->SendNow(H.BuildResponse("415", "Unsupported Media Type"));
}
void Handle_Timeout(HTTP::Parser & H, Socket::Connection * conn){
H.Clean();
H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
H.SetBody("<!DOCTYPE html><html><head><title>Gateway timeout</title></head><body><h1>Gateway timeout</h1>Though the server understood your request and attempted to handle it, somehow handling it took longer than it should. Your request has been cancelled - please try again later.</body></html>");
conn->Send(H.BuildResponse("504", "Gateway Timeout"));
conn->SendNow(H.BuildResponse("504", "Gateway Timeout"));
}
/// Handles internal requests.
@ -111,7 +111,7 @@ namespace Connector_HTTP{
H.SetHeader("Content-Type", "text/xml");
H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
H.SetBody("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" /><site-control permitted-cross-domain-policies=\"all\"/></cross-domain-policy>");
conn->Send(H.BuildResponse("200", "OK"));
conn->SendNow(H.BuildResponse("200", "OK"));
return;
}//crossdomain.xml
@ -173,7 +173,7 @@ namespace Connector_HTTP{
response.append("(\"" + streamname + "\"));\n");
}
H.SetBody(response);
conn->Send(H.BuildResponse("200", "OK"));
conn->SendNow(H.BuildResponse("200", "OK"));
return;
}//embed code generator
@ -235,7 +235,7 @@ namespace Connector_HTTP{
return;
}
//forward the original request
connconn[uid]->conn->Send(request);
connconn[uid]->conn->SendNow(request);
connconn[uid]->lastuse = 0;
unsigned int timeout = 0;
//wait for a response
@ -267,13 +267,13 @@ namespace Connector_HTTP{
//known length - simply re-send the request with added headers and continue
H.SetHeader("X-UID", uid);
H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
conn->Send(H.BuildResponse("200", "OK"));
conn->SendNow(H.BuildResponse("200", "OK"));
conn->flush();
}else{
//unknown length
H.SetHeader("X-UID", uid);
H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
conn->Send(H.BuildResponse("200", "OK"));
conn->SendNow(H.BuildResponse("200", "OK"));
//switch out the connection for an empty one - it makes no sense to keep these globally
Socket::Connection * myConn = connconn[uid]->conn;
connconn[uid]->conn = new Socket::Connection();
@ -282,9 +282,8 @@ namespace Connector_HTTP{
while (myConn->connected() && conn->connected()){
if (myConn->Received().size() || myConn->spool()){
//forward any and all incoming data directly without parsing
conn->Send(myConn->Received().get());
conn->SendNow(myConn->Received().get());
myConn->Received().get().clear();
conn->flush();
}else{
usleep(30000);
}

View file

@ -26,21 +26,29 @@
/// Holds everything unique to HTTP Dynamic Connector.
namespace Connector_HTTP{
std::string GenerateBootstrap(std::string & MovieId, JSON::Value & metadata){
std::string GenerateBootstrap(std::string & MovieId, JSON::Value & metadata, int fragnum, int starttime){
MP4::AFRT afrt;
if (starttime == 0){
afrt.SetUpdate(false);
}else{
afrt.SetUpdate(true);
}
afrt.SetTimeScale(1000);
afrt.AddQualityEntry("");
if (!metadata.isMember("video") || !metadata["video"].isMember("keyms") || metadata["video"]["keyms"].asInt() == 0){
//metadata["lasttime"].asInt()?
afrt.AddFragmentRunEntry(1, 0, 2000); //FirstFragment, FirstFragmentTimestamp,Fragment Duration in milliseconds
afrt.AddFragmentRunEntry(fragnum, starttime, 2000); //FirstFragment, FirstFragmentTimestamp,Fragment Duration in milliseconds
}else{
afrt.AddFragmentRunEntry(1, 0, metadata["video"]["keyms"].asInt()); //FirstFragment, FirstFragmentTimestamp,Fragment Duration in milliseconds
afrt.AddFragmentRunEntry(fragnum, starttime, metadata["video"]["keyms"].asInt()); //FirstFragment, FirstFragmentTimestamp,Fragment Duration in milliseconds
}
afrt.WriteContent();
MP4::ASRT asrt;
if (starttime == 0){
asrt.SetUpdate(false);
}else{
asrt.SetUpdate(true);
}
asrt.AddQualityEntry("");
/// \todo Actually use correct number of fragments.
asrt.AddSegmentRunEntry(1, 20000);//1 Segment, 20000 Fragments
@ -58,7 +66,11 @@ namespace Connector_HTTP{
abst.SetLive(true);
abst.SetMediaTime(0xFFFFFFFF);//metadata["lasttime"].asInt()?
}
if (starttime == 0){
abst.SetUpdate(false);
}else{
abst.SetUpdate(true);
}
abst.SetTimeScale(1000);
abst.SetSMPTE(0);
abst.SetMovieIdentifier(MovieId);
@ -68,12 +80,10 @@ namespace Connector_HTTP{
abst.AddQualityEntry("");
abst.WriteContent();
std::string Result;
Result.append((char*)abst.GetBoxedData(), (int)abst.GetBoxedDataSize());
#if DEBUG >= 8
//#if DEBUG >= 8
std::cout << "Sending bootstrap:" << std::endl << abst.toPrettyString(0) << std::endl;
#endif
return Base64::encode(Result);
//#endif
return std::string((char*)abst.GetBoxedData(), (int)abst.GetBoxedDataSize());
}
@ -91,7 +101,7 @@ namespace Connector_HTTP{
"<streamType>recorded</streamType>\n"
"<deliveryType>streaming</deliveryType>\n"
"<bestEffortFetchInfo segmentDuration=\""+metadata["length"].asString()+".000\" fragmentDuration=\""+st.str()+"\" />\n"
"<bootstrapInfo profile=\"named\" id=\"bootstrap1\">" + GenerateBootstrap(MovieId, metadata) + "</bootstrapInfo>\n"
"<bootstrapInfo profile=\"named\" id=\"bootstrap1\">" + Base64::encode(GenerateBootstrap(MovieId, metadata, 1, 0)) + "</bootstrapInfo>\n"
"<media streamId=\"1\" bootstrapInfoId=\"bootstrap1\" url=\"" + MovieId + "/\"></media>\n"
"</manifest>\n";
}else{
@ -101,7 +111,7 @@ namespace Connector_HTTP{
"<mimeType>video/mp4</mimeType>\n"
"<streamType>live</streamType>\n"
"<deliveryType>streaming</deliveryType>\n"
"<bootstrapInfo profile=\"named\" id=\"bootstrap1\">" + GenerateBootstrap(MovieId, metadata) + "</bootstrapInfo>\n"
"<bootstrapInfo profile=\"named\" id=\"bootstrap1\">" + Base64::encode(GenerateBootstrap(MovieId, metadata, 1, 0)) + "</bootstrapInfo>\n"
"<media streamId=\"1\" bootstrapInfoId=\"bootstrap1\" url=\"" + MovieId + "/\"></media>\n"
"</manifest>\n";
}
@ -113,9 +123,10 @@ namespace Connector_HTTP{
/// Main function for Connector_HTTP_Dynamic
int Connector_HTTP_Dynamic(Socket::Connection conn){
std::stringstream FlashBuf;
int flashbuf_nonempty = 0;
FLV::Tag tmp;//temporary tag, for init data
std::deque<std::string> FlashBuf;
int FlashBufSize = 0;
long long int FlashBufTime = 0;
FLV::Tag tmp;//temporary tag
DTSC::Stream Strm;//Incoming stream buffer.
HTTP::Parser HTTP_R, HTTP_S;//HTTP Receiver en HTTP Sender.
@ -126,7 +137,6 @@ namespace Connector_HTTP{
bool inited = false;
Socket::Connection ss(-1);
std::string streamname;
FLV::Tag tag;///< Temporary tag buffer.
std::string recBuffer = "";
std::string Quality;
@ -155,7 +165,7 @@ namespace Connector_HTTP{
ss.close();
HTTP_S.Clean();
HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
conn.Send(HTTP_S.BuildResponse("404", "Not found"));
conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
ready4data = false;
continue;
}
@ -173,8 +183,7 @@ namespace Connector_HTTP{
#endif
std::stringstream sstream;
sstream << "f " << ReqFragment << "\no \n";
ss.Send(sstream.str().c_str());
ss.flush();
ss.SendNow(sstream.str().c_str());
Flash_RequestPending++;
}else{
streamname = HTTP_R.url.substr(1,HTTP_R.url.find("/",1)-1);
@ -185,7 +194,7 @@ namespace Connector_HTTP{
if (Strm.metadata.isMember("length")){receive_marks = true;}
std::string manifest = BuildManifest(streamname, Strm.metadata);
HTTP_S.SetBody(manifest);
conn.Send(HTTP_S.BuildResponse("200", "OK"));
conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
#if DEBUG >= 3
printf("Sent manifest\n");
#endif
@ -197,9 +206,13 @@ namespace Connector_HTTP{
ready4data = true;
HTTP_R.Clean(); //clean for any possible next requests
}
}else{
if (Flash_RequestPending){
usleep(1000);//sleep 1ms
}else{
usleep(10000);//sleep 10ms
}
}
if (ready4data){
if (!inited){
//we are ready, connect the socket!
@ -211,7 +224,7 @@ namespace Connector_HTTP{
ss.close();
HTTP_S.Clean();
HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
conn.Send(HTTP_S.BuildResponse("404", "Not found"));
conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
ready4data = false;
continue;
}
@ -225,10 +238,10 @@ namespace Connector_HTTP{
if (now != lastStats){
lastStats = now;
ss.Send("S ");
ss.Send(conn.getStats("HTTP_Dynamic").c_str());
ss.SendNow(conn.getStats("HTTP_Dynamic").c_str());
}
if (ss.spool() || ss.Received().size()){
if (Strm.parsePacket(ss.Received())){
if (ss.spool()){
while (Strm.parsePacket(ss.Received())){
if (Strm.getPacket(0).isMember("time")){
if (!Strm.metadata.isMember("firsttime")){
Strm.metadata["firsttime"] = Strm.getPacket(0)["time"];
@ -239,9 +252,6 @@ namespace Connector_HTTP{
}
Strm.metadata["lasttime"] = Strm.getPacket(0)["time"];
}
if (Strm.lastType() == DTSC::VIDEO || Strm.lastType() == DTSC::AUDIO){
tag.DTSCLoader(Strm);
}
if (pending_manifest){
HTTP_S.Clean();
HTTP_S.SetHeader("Content-Type","text/xml");
@ -249,7 +259,7 @@ namespace Connector_HTTP{
if (Strm.metadata.isMember("length")){receive_marks = true;}
std::string manifest = BuildManifest(streamname, Strm.metadata);
HTTP_S.SetBody(manifest);
conn.Send(HTTP_S.BuildResponse("200", "OK"));
conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
#if DEBUG >= 3
printf("Sent manifest\n");
#endif
@ -257,42 +267,68 @@ namespace Connector_HTTP{
}
if (!receive_marks && Strm.metadata.isMember("length")){receive_marks = true;}
if ((Strm.getPacket(0).isMember("keyframe") && !receive_marks) || Strm.lastType() == DTSC::PAUSEMARK){
if (flashbuf_nonempty || Strm.lastType() == DTSC::PAUSEMARK){
#if DEBUG >= 4
fprintf(stderr, "Received a %s fragment of %i packets.\n", Strm.getPacket(0)["datatype"].asString().c_str(), flashbuf_nonempty);
fprintf(stderr, "Received a %s fragment of %i bytes.\n", Strm.getPacket(0)["datatype"].asString().c_str(), FlashBufSize);
#endif
if (Flash_RequestPending > 0){
HTTP_S.Clean();
HTTP_S.SetHeader("Content-Type","video/mp4");
HTTP_S.SetBody(MP4::mdatFold(FlashBuf.str()));
conn.Send(HTTP_S.BuildResponse("200", "OK"));
Flash_RequestPending--;
if (Flash_RequestPending > 0 && FlashBufSize){
#if DEBUG >= 3
fprintf(stderr, "Sending a fragment...");
#endif
conn.flush();
static std::string btstrp;
btstrp = GenerateBootstrap(streamname, Strm.metadata, ReqFragment, FlashBufTime);
HTTP_S.Clean();
HTTP_S.SetHeader("Content-Type", "video/mp4");
HTTP_S.SetBody("");
HTTP_S.SetHeader("Content-Length", FlashBufSize+32+33+btstrp.size());
conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
conn.SendNow("\x00\x00\x00\x21" "afra\x00\x00\x00\x00\x00\x00\x00\x03\xE8\x00\x00\x00\x01", 21);
unsigned long tmptime = htonl(FlashBufTime << 32);
conn.SendNow((char*)&tmptime, 4);
tmptime = htonl(FlashBufTime & 0xFFFFFFFF);
conn.SendNow((char*)&tmptime, 4);
tmptime = htonl(65);
conn.SendNow((char*)&tmptime, 4);
conn.SendNow(btstrp);
conn.SendNow("\x00\x00\x00\x18moof\x00\x00\x00\x10mfhd\x00\x00\x00\x00", 20);
unsigned long fragno = htonl(ReqFragment);
conn.SendNow((char*)&fragno, 4);
unsigned long size = htonl(FlashBufSize+8);
conn.SendNow((char*)&size, 4);
conn.SendNow("mdat", 4);
while (FlashBuf.size() > 0){
conn.SendNow(FlashBuf.front());
FlashBuf.pop_front();
}
Flash_RequestPending--;
#if DEBUG >= 3
fprintf(stderr, "Done\n");
#endif
}
FlashBuf.clear();
FlashBufSize = 0;
}
FlashBuf.str("");
flashbuf_nonempty = 0;
if (Strm.lastType() == DTSC::VIDEO || Strm.lastType() == DTSC::AUDIO){
if (FlashBufSize == 0){
//fill buffer with init data, if needed.
if (Strm.metadata.isMember("audio") && Strm.metadata["audio"].isMember("init")){
tmp.DTSCAudioInit(Strm);
FlashBuf.write(tmp.data, tmp.len);
FlashBuf.push_back(std::string(tmp.data, tmp.len));
FlashBufSize += tmp.len;
}
if (Strm.metadata.isMember("video") && Strm.metadata["video"].isMember("init")){
tmp.DTSCVideoInit(Strm);
FlashBuf.write(tmp.data, tmp.len);
FlashBuf.push_back(std::string(tmp.data, tmp.len));
FlashBufSize += tmp.len;
}
FlashBufTime = Strm.getPacket(0)["time"].asInt();
}
tmp.DTSCLoader(Strm);
FlashBuf.push_back(std::string(tmp.data, tmp.len));
FlashBufSize += tmp.len;
}
}
if (Strm.lastType() == DTSC::VIDEO || Strm.lastType() == DTSC::AUDIO){
++flashbuf_nonempty;
FlashBuf.write(tag.data, tag.len);
}
}else{
if (pending_manifest && !Strm.metadata.isNull()){
HTTP_S.Clean();
HTTP_S.SetHeader("Content-Type","text/xml");
@ -300,21 +336,19 @@ namespace Connector_HTTP{
if (Strm.metadata.isMember("length")){receive_marks = true;}
std::string manifest = BuildManifest(streamname, Strm.metadata);
HTTP_S.SetBody(manifest);
conn.Send(HTTP_S.BuildResponse("200", "OK"));
conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
#if DEBUG >= 3
printf("Sent manifest\n");
#endif
pending_manifest = false;
}
}
}
if (!ss.connected()){break;}
}
}
conn.close();
ss.Send("S ");
ss.Send(conn.getStats("HTTP_Dynamic").c_str());
ss.flush();
ss.SendNow(conn.getStats("HTTP_Dynamic").c_str());
ss.close();
#if DEBUG >= 1
if (FLV::Parse_Error){fprintf(stderr, "FLV Parser Error: %s\n", FLV::Error_Str.c_str());}

View file

@ -55,7 +55,7 @@ namespace Connector_HTTP{
HTTP_R.Clean(); //clean for any possible next requests
}
}else{
usleep(10000);//sleep 10ms
usleep(1000);//sleep 1ms
}
if (ready4data){
if (!inited){
@ -68,58 +68,57 @@ namespace Connector_HTTP{
ss.close();
HTTP_S.Clean();
HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
conn.Send(HTTP_S.BuildResponse("404", "Not found"));
conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
ready4data = false;
continue;
}
if (seek_pos){
std::stringstream cmd;
cmd << "s " << seek_pos << "\n";
ss.Send(cmd.str().c_str());
ss.SendNow(cmd.str().c_str());
}
#if DEBUG >= 3
fprintf(stderr, "Everything connected, starting to send video data...\n");
#endif
ss.Send("p\n");
ss.flush();
ss.SendNow("p\n");
inited = true;
}
unsigned int now = time(0);
if (now != lastStats){
lastStats = now;
ss.Send("S ");
ss.Send(conn.getStats("HTTP_Progressive").c_str());
ss.SendNow(conn.getStats("HTTP_Progressive").c_str());
}
if (ss.spool() || ss.Received().size()){
if (Strm.parsePacket(ss.Received())){
tag.DTSCLoader(Strm);
if (ss.spool()){
while (Strm.parsePacket(ss.Received())){
if (!progressive_has_sent_header){
HTTP_S.Clean();//make sure no parts of old requests are left in any buffers
HTTP_S.SetHeader("Content-Type", "video/x-flv");//Send the correct content-type for FLV files
//HTTP_S.SetHeader("Transfer-Encoding", "chunked");
HTTP_S.protocol = "HTTP/1.0";
conn.Send(HTTP_S.BuildResponse("200", "OK"));//no SetBody = unknown length - this is intentional, we will stream the entire file
conn.Send(FLV::Header, 13);//write FLV header
conn.SendNow(HTTP_S.BuildResponse("200", "OK"));//no SetBody = unknown length - this is intentional, we will stream the entire file
conn.SendNow(FLV::Header, 13);//write FLV header
static FLV::Tag tmp;
//write metadata
tmp.DTSCMetaInit(Strm);
conn.Send(tmp.data, tmp.len);
conn.SendNow(tmp.data, tmp.len);
//write video init data, if needed
if (Strm.metadata.isMember("video") && Strm.metadata["video"].isMember("init")){
tmp.DTSCVideoInit(Strm);
conn.Send(tmp.data, tmp.len);
conn.SendNow(tmp.data, tmp.len);
}
//write audio init data, if needed
if (Strm.metadata.isMember("audio") && Strm.metadata["audio"].isMember("init")){
tmp.DTSCAudioInit(Strm);
conn.Send(tmp.data, tmp.len);
conn.SendNow(tmp.data, tmp.len);
}
progressive_has_sent_header = true;
#if DEBUG >= 1
fprintf(stderr, "Sent progressive FLV header\n");
#endif
}
conn.Send(tag.data, tag.len);//write the tag contents
tag.DTSCLoader(Strm);
conn.SendNow(tag.data, tag.len);//write the tag contents
}
}
if (!ss.connected()){break;}
@ -127,8 +126,7 @@ namespace Connector_HTTP{
}
conn.close();
ss.Send("S ");
ss.Send(conn.getStats("HTTP_Dynamic").c_str());
ss.flush();
ss.SendNow(conn.getStats("HTTP_Dynamic").c_str());
ss.close();
#if DEBUG >= 1
if (FLV::Parse_Error){fprintf(stderr, "FLV Parser Error: %s\n", FLV::Error_Str.c_str());}

View file

@ -38,15 +38,12 @@ int main(int argc, char ** argv) {
lastStats = now;
std::stringstream st;
st << "S localhost RAW " << (time(0) - started) << " " << S.dataDown() << " " << S.dataUp() << "\n";
std::string tmp = st.str();
S.Send(tmp);
S.SendNow(st.str().c_str());
}
}
std::stringstream st;
st << "S localhost RAW " << (time(0) - started) << " " << S.dataDown() << " " << S.dataUp() << "\n";
std::string tmp = st.str();
S.Send(tmp);
S.flush();
S.SendNow(st.str().c_str());
S.close();
return 0;
}

View file

@ -57,7 +57,7 @@ int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
RTMPStream::rec_cnt += 1537;
if (RTMPStream::doHandshake()){
Socket.Send(RTMPStream::handshake_out);
Socket.SendNow(RTMPStream::handshake_out);
while (!Socket.Received().available(1536) && Socket.connected()){Socket.spool(); usleep(5000);}
Socket.Received().remove(1536);
RTMPStream::rec_cnt += 1536;
@ -74,10 +74,12 @@ int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
unsigned int lastStats = 0;
while (Socket.connected()){
if (Socket.Received().size() || Socket.spool()){
if (Socket.spool() || Socket.Received().size()){
while (Socket.Received().size()){
parseChunk(Socket.Received().get());
}
}else{
usleep(10000);//sleep 10ms to prevent high CPU usage
usleep(1000);//sleep 1ms to prevent high CPU usage
}
if (ready4data){
if (!inited){
@ -94,7 +96,7 @@ int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
#if DEBUG >= 3
fprintf(stderr, "Everything connected, starting to send video data...\n");
#endif
SS.Send("p\n");SS.flush();
SS.SendNow("p\n");
inited = true;
}
if (inited && !nostats){
@ -102,11 +104,11 @@ int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
if (now != lastStats){
lastStats = now;
SS.Send("S ");
SS.Send(Socket.getStats("RTMP").c_str());
SS.SendNow(Socket.getStats("RTMP").c_str());
}
}
if (SS.spool() || SS.Received().size()){
if (Strm.parsePacket(SS.Received())){
if (SS.spool()){
while (Strm.parsePacket(SS.Received())){
if (play_trans != -1){
//send a status reply
AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
@ -148,20 +150,20 @@ int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
//sent init data if needed
if (!stream_inited){
init_tag.DTSCMetaInit(Strm);
Socket.Send(RTMPStream::SendMedia(init_tag));
Socket.SendNow(RTMPStream::SendMedia(init_tag));
if (Strm.metadata.isMember("audio") && Strm.metadata["audio"].isMember("init")){
init_tag.DTSCAudioInit(Strm);
Socket.Send(RTMPStream::SendMedia(init_tag));
Socket.SendNow(RTMPStream::SendMedia(init_tag));
}
if (Strm.metadata.isMember("video") && Strm.metadata["video"].isMember("init")){
init_tag.DTSCVideoInit(Strm);
Socket.Send(RTMPStream::SendMedia(init_tag));
Socket.SendNow(RTMPStream::SendMedia(init_tag));
}
stream_inited = true;
}
//sent a tag
tag.DTSCLoader(Strm);
Socket.Send(RTMPStream::SendMedia(tag));
Socket.SendNow(RTMPStream::SendMedia(tag));
#if DEBUG >= 8
fprintf(stderr, "Sent tag to %i: [%u] %s\n", Socket.getSocket(), tag.tagTime(), tag.tagType().c_str());
#endif
@ -171,8 +173,7 @@ int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
}
Socket.close();
SS.Send("S ");
SS.Send(Socket.getStats("RTMP").c_str());
SS.flush();
SS.SendNow(Socket.getStats("RTMP").c_str());
SS.close();
#if DEBUG >= 1
if (FLV::Parse_Error){fprintf(stderr, "FLV Parse Error: %s\n", FLV::Error_Str.c_str());}
@ -281,15 +282,15 @@ void Connector_RTMP::parseChunk(std::string & inbuffer){
counter++;
if (counter > 8){
sending = true;
SS.Send(meta_out.toNetPacked());
SS.Send(prebuffer.str().c_str());//write buffer
SS.SendNow(meta_out.toNetPacked());
SS.SendNow(prebuffer.str().c_str());//write buffer
prebuffer.str("");//clear buffer
SS.Send(pack_out.toNetPacked());
}else{
prebuffer << pack_out.toNetPacked();
}
}else{
SS.Send(pack_out.toNetPacked());
SS.SendNow(pack_out.toNetPacked());
}
}
}else{
@ -357,9 +358,9 @@ void Connector_RTMP::sendCommand(AMF::Object & amfreply, int messagetype, int st
std::cerr << amfreply.Print() << std::endl;
#endif
if (messagetype == 17){
Socket.Send(RTMPStream::SendChunk(3, messagetype, stream_id, (char)0+amfreply.Pack()));
Socket.SendNow(RTMPStream::SendChunk(3, messagetype, stream_id, (char)0+amfreply.Pack()));
}else{
Socket.Send(RTMPStream::SendChunk(3, messagetype, stream_id, amfreply.Pack()));
Socket.SendNow(RTMPStream::SendChunk(3, messagetype, stream_id, amfreply.Pack()));
}
}//sendCommand

View file

@ -70,7 +70,7 @@ int main(int argc, char** argv){
pausemark["time"] = (long long int)0;
Socket::Connection StatsSocket = Socket::Connection("/tmp/mist/statistics", true);
int lasttime = time(0);
int lasttime = time(0);//time last packet was sent
//send the header
{
@ -81,16 +81,21 @@ int main(int argc, char** argv){
}
JSON::Value meta = JSON::fromDTMI(meta_str);
if (meta["video"]["keyms"].asInt() < 11){
meta["video"]["keyms"] = (long long int)1000;
}
JSON::Value last_pack;
bool meta_sent = false;
long long now, timeDiff = 0, lastTime = 0;
long long now, lastTime = 0;//for timing of sending packets
long long bench = 0;//for benchmarking
Stats sts;
while (in_out.connected() && std::cin.good() && std::cout.good() && (time(0) - lasttime < 60)){
if (in_out.Received().size() || in_out.spool()){
if (in_out.spool()){
while (in_out.Received().size()){
//delete anything that doesn't end with a newline
if (!in_out.Received().get().empty() && *(in_out.Received().get().rbegin()) != '\n'){
if (*(in_out.Received().get().rbegin()) != '\n'){
in_out.Received().get().clear();
continue;
}
@ -142,6 +147,10 @@ int main(int argc, char** argv){
if (playing <= 0){playing = 1;}
++playing;
in_out.setBlocking(false);
#if DEBUG >= 4
std::cerr << "Playing one keyframe" << std::endl;
#endif
bench = getNowMS();
} break;
case 'q':{ //quit-playing
playing = 0;
@ -151,25 +160,21 @@ int main(int argc, char** argv){
in_out.Received().get().clear();
}
}
}
if (playing != 0){
now = getNowMS();
/// \todo This makes no sense. We're timing for packets here, but sending a whole keyframe. Fix. ASAP.
if (playing > 0 || now - timeDiff >= lastTime || lastTime - (now - timeDiff) > 15000) {
if (playing > 0 || meta["video"]["keyms"].asInt() <= now-lastTime) {
source.seekNext();
lastTime = source.getJSON()["time"].asInt();
if ((now - timeDiff - lastTime) > 5000 || (now - timeDiff - lastTime < -5000)){
timeDiff = now - lastTime;
}
if (source.getJSON().isMember("keyframe")){
lastTime = now;
if (playing > 0){--playing;}
if (playing == 0){
#if DEBUG >= 4
std::cerr << "Sending pause_marker" << std::endl;
std::cerr << "Sending pause_marker (" << (getNowMS() - bench) << "ms)" << std::endl;
#endif
pausemark["time"] = (long long int)now;
pausemark.toPacked();
in_out.Send(pausemark.toNetPacked());
in_out.flush();
in_out.SendNow(pausemark.toNetPacked());
in_out.setBlocking(true);
}
}
@ -180,11 +185,10 @@ int main(int argc, char** argv){
//insert the packet length
unsigned int size = htonl(source.getPacket().size());
in_out.Send((char*)&size, 4);
in_out.Send(source.getPacket());
in_out.flush();
in_out.SendNow(source.getPacket());
}
} else {
usleep(std::min(10000LL, lastTime - (now - timeDiff)) * 1000);
usleep((meta["video"]["keyms"].asInt()-(now-lastTime))*1000);
}
}else{
usleep(10000);//sleep 10ms