Fingers crossed...

This commit is contained in:
Erik Zandvliet 2011-08-31 21:33:18 +02:00
parent 69a9048436
commit 8511419cb7

View file

@ -6,6 +6,7 @@
#include <ctime> #include <ctime>
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include <climits>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <unistd.h> #include <unistd.h>
@ -21,20 +22,42 @@
//JRTPLIB //JRTPLIB
#include "rtp.h" #include "rtp.h"
std::string ReadNALU( ) {
static char Separator[3] = { (char)0x00, (char)0x00, (char)0x01 };
std::string Buffer;
std::string Result;
do {
Buffer += std::cin.get();
} while ( std::cin.good() && ( Buffer.find( Separator,0,3 ) == std::string::npos ) );
if( !std::cin.good() ) { return ""; }
Result = Buffer.substr(0, Buffer.find( Separator,0,3 ) );
while( *(Result.end() - 1) == (char)0x00 ) { Result.erase( Result.end() - 1 ); }
if( Result.size() == 0 ) { Result = ReadNALU( ); }
return Result;
}
/// The main function of the connector /// The main function of the connector
/// \param conn A connection with the client /// \param conn A connection with the client
int RTSP_Handler( Socket::Connection conn ) { int RTSP_Handler( Socket::Connection conn ) {
bool ready4data = false; bool ready4data = false;
FLV::Tag tag;///< Temporary tag buffer for incoming video data. FLV::Tag tag;///< Temporary tag buffer for incoming video data.
bool PlayVideo = false;
bool PlayAudio = true;
bool InitVideo = false; bool InitVideo = false;
bool InitAudio = true; bool InitAudio = true;
bool VideoMeta = false; bool VideoMeta = false;
int RTPClientPort;
int RTCPClientPort;
bool inited;
jrtplib::RTPSession VideoSession;
jrtplib::RTPSessionParams VideoParams;
jrtplib::RTPUDPv4TransmissionParams VideoTransParams;
std::string PreviousRequest = ""; std::string PreviousRequest = "";
Socket::Connection ss(-1); Socket::Connection ss(-1);
HTTP::Parser HTTP_R, HTTP_S; HTTP::Parser HTTP_R, HTTP_S;
bool PerRequest = false; bool PerRequest = false;
while(conn.connected() && !FLV::Parse_Error) { while(conn.connected() && !FLV::Parse_Error) {
if( HTTP_R.Read(conn, ready4data) ) { if( HTTP_R.Read(conn ) ) {
fprintf( stderr, "REQUEST:\n%s\n", HTTP_R.BuildRequest().c_str() ); fprintf( stderr, "REQUEST:\n%s\n", HTTP_R.BuildRequest().c_str() );
if( HTTP_R.GetHeader( "User-Agent" ).find( "RealMedia Player Version" ) != std::string::npos) { if( HTTP_R.GetHeader( "User-Agent" ).find( "RealMedia Player Version" ) != std::string::npos) {
PerRequest = true; PerRequest = true;
@ -53,7 +76,7 @@ int RTSP_Handler( Socket::Connection conn ) {
} else { } else {
HTTP_S.SetHeader( "CSeq", HTTP_R.GetHeader( "CSeq" ).c_str() ); HTTP_S.SetHeader( "CSeq", HTTP_R.GetHeader( "CSeq" ).c_str() );
HTTP_S.SetHeader( "Content-Type", "application/sdp" ); HTTP_S.SetHeader( "Content-Type", "application/sdp" );
HTTP_S.SetBody( "v=0\r\no=- 0 0 IN IP4 ddvtech.com\r\ns=Fifa Test\r\nc=IN IP4 127.0.0.1\r\nt=0 0\r\na=recvonly\r\nm=video 0 RTP/AVP 98\r\na=rtpmap:98 H264/90000\na=fmtp:98 packetization-mode=1\r\n\r\n" ); HTTP_S.SetBody( "v=0\r\no=- 0 0 IN IP4 ddvtech.com\r\ns=Fifa Test\r\nc=IN IP4 127.0.0.1\r\nt=0 0\r\na=recvonly\r\nm=video 0 RTP/AVP 98\r\na=rtpmap:98 H264/700000\r\n\r\n" );//a=fmtp:98 packetization-mode=1\r\n\r\n" );
fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "200", "OK" ).c_str() ); fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "200", "OK" ).c_str() );
conn.write( HTTP_S.BuildResponse( "200", "OK" ) ); conn.write( HTTP_S.BuildResponse( "200", "OK" ) );
} }
@ -62,8 +85,8 @@ int RTSP_Handler( Socket::Connection conn ) {
int ClientRTPLoc = temp.find( "client_port=" ) + 12; int ClientRTPLoc = temp.find( "client_port=" ) + 12;
int PortSpacer = temp.find( "-", ClientRTPLoc ); int PortSpacer = temp.find( "-", ClientRTPLoc );
int PortEnd = ( temp.find( ";", PortSpacer ) ); int PortEnd = ( temp.find( ";", PortSpacer ) );
int RTPClientPort = atoi( temp.substr( ClientRTPLoc, ( PortSpacer - ClientRTPLoc ) ).c_str() ); RTPClientPort = atoi( temp.substr( ClientRTPLoc, ( PortSpacer - ClientRTPLoc ) ).c_str() );
int RTCPClientPort = atoi( temp.substr( PortSpacer + 1 , ( PortEnd - ( PortSpacer + 1 ) ) ).c_str() ); RTCPClientPort = atoi( temp.substr( PortSpacer + 1 , ( PortEnd - ( PortSpacer + 1 ) ) ).c_str() );
if( HTTP_S.GetHeader( "Session" ) != "" ) { if( HTTP_S.GetHeader( "Session" ) != "" ) {
fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "459", "Aggregate Operation Not Allowed" ).c_str() ); fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "459", "Aggregate Operation Not Allowed" ).c_str() );
conn.write( HTTP_S.BuildResponse( "459", "Aggregate Operation Not Allowed" ) ); conn.write( HTTP_S.BuildResponse( "459", "Aggregate Operation Not Allowed" ) );
@ -87,8 +110,14 @@ int RTSP_Handler( Socket::Connection conn ) {
HTTP_S.SetBody( "\r\n\r\n" ); HTTP_S.SetBody( "\r\n\r\n" );
fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "200", "OK" ).c_str() ); fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "200", "OK" ).c_str() );
conn.write( HTTP_S.BuildResponse( "200", "OK" ) ); conn.write( HTTP_S.BuildResponse( "200", "OK" ) );
InitVideo = true; PlayVideo = true;
} }
} else if( HTTP_R.method == "TEARDOWN" ) {
HTTP_S.SetHeader( "CSeq", HTTP_R.GetHeader( "CSeq" ).c_str() );
HTTP_S.SetBody( "\r\n\r\n" );
fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "200", "OK" ).c_str() );
conn.write( HTTP_S.BuildResponse( "200", "OK" ) );
PlayVideo = false;
} else { } else {
fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "501", "Not Implemented" ).c_str() ); fprintf( stderr, "RESPONSE:\n%s\n", HTTP_S.BuildResponse( "501", "Not Implemented" ).c_str() );
conn.write( HTTP_S.BuildResponse( "501", "Not Implemented" ) ); conn.write( HTTP_S.BuildResponse( "501", "Not Implemented" ) );
@ -98,25 +127,62 @@ int RTSP_Handler( Socket::Connection conn ) {
if( PerRequest ) { if( PerRequest ) {
conn.close(); conn.close();
} }
if( InitVideo ) { /* if( ( PlayVideo ) && !inited ) {
ss = Socket::Connection("/tmp/shared_socket_fifa");
} if (!ss.connected()){
} #if DEBUG >= 1
/* if( !inited ) { fprintf(stderr, "Could not connect to server!\n");
ss = Socket::Connection("/tmp/shared_socket_fifa"); #endif
if (!ss.connected()){ conn.close();
#if DEBUG >= 1 break;
fprintf(stderr, "Could not connect to server!\n"); }
#if DEBUG >= 3
fprintf(stderr, "Everything connected, starting to send video data...\n");
#endif #endif
conn.close(); inited = true;
break; }*/
}
#if DEBUG >= 3
fprintf(stderr, "Everything connected, starting to send video data...\n");
#endif
inited = true;
} }
if( PlayVideo ) {
if( !InitVideo ) {
VideoParams.SetOwnTimestampUnit( 1.0/90000.0 );
VideoParams.SetMaximumPacketSize( 10000 );
VideoTransParams.SetPortbase( 50000 );
int VideoStatus = VideoSession.Create( VideoParams, &VideoTransParams );
if( VideoStatus < 0 ) {
std::cerr << jrtplib::RTPGetErrorString( VideoStatus ) << std::endl;
exit( -1 );
} else {
std::cerr << "Created video session\n";
}
uint8_t localip[]={127,0,0,1};
jrtplib::RTPIPv4Address addr(localip,RTPClientPort);
VideoStatus = VideoSession.AddDestination(addr);
if (VideoStatus < 0) {
std::cerr << jrtplib::RTPGetErrorString(VideoStatus) << std::endl;
exit(-1);
} else {
std::cerr << "Destination Set\n";
}
VideoSession.SetDefaultPayloadType(98);
VideoSession.SetDefaultMark(false);
VideoSession.SetDefaultTimestampIncrement(0);
InitVideo = true;
}
// std::cerr << "Retrieving NALU from stdin\n";
std::string VideoBuf = ReadNALU( );
if( VideoBuf == "" ) {
jrtplib::RTPTime delay = jrtplib::RTPTime(10.0);
VideoSession.BYEDestroy(delay,"Out of data",11);
conn.close();
} else {
// std::cerr << "NALU Retrieved:\n";
// std::cerr << "\t" << (int)VideoBuf[0] << " " << (int)VideoBuf[2]
// << " " << (int)VideoBuf[3] << " " << (int)VideoBuf[4] << "\n";
VideoSession.SendPacket( VideoBuf.c_str(), VideoBuf.size(), 98, false, ( 700000.0 / VideoBuf.size() ) );
}
}
/*
switch (ss.ready()){ switch (ss.ready()){
case -1: case -1:
conn.close(); conn.close();