2003-02-03 10:27:50 +00:00
|
|
|
////////// Routines (with C-linkage) that interface between "MPlayer"
|
2005-09-23 22:35:04 +00:00
|
|
|
////////// and the "LIVE555 Streaming Media" libraries:
|
2003-02-03 10:27:50 +00:00
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
extern "C" {
|
2004-06-03 13:30:55 +00:00
|
|
|
// on MinGW, we must include windows.h before the things it conflicts
|
|
|
|
#ifdef __MINGW32__ // with. they are each protected from
|
|
|
|
#include <windows.h> // windows.h, but not the other way around.
|
|
|
|
#endif
|
2002-08-05 00:39:07 +00:00
|
|
|
#include "demux_rtp.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
}
|
2003-02-03 10:27:50 +00:00
|
|
|
#include "demux_rtp_internal.h"
|
2002-08-05 00:39:07 +00:00
|
|
|
|
|
|
|
#include "BasicUsageEnvironment.hh"
|
|
|
|
#include "liveMedia.hh"
|
2003-03-11 19:08:31 +00:00
|
|
|
#include "GroupsockHelper.hh"
|
2002-08-05 00:39:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
// A data structure representing input data for each stream:
|
2002-08-05 00:39:07 +00:00
|
|
|
class ReadBufferQueue {
|
|
|
|
public:
|
|
|
|
ReadBufferQueue(MediaSubsession* subsession, demuxer_t* demuxer,
|
|
|
|
char const* tag);
|
|
|
|
virtual ~ReadBufferQueue();
|
|
|
|
|
|
|
|
FramedSource* readSource() const { return fReadSource; }
|
2002-09-28 18:51:44 +00:00
|
|
|
RTPSource* rtpSource() const { return fRTPSource; }
|
2002-08-05 00:39:07 +00:00
|
|
|
demuxer_t* ourDemuxer() const { return fOurDemuxer; }
|
|
|
|
char const* tag() const { return fTag; }
|
|
|
|
|
|
|
|
char blockingFlag; // used to implement synchronous reads
|
2003-03-11 19:08:31 +00:00
|
|
|
|
|
|
|
// For A/V synchronization:
|
|
|
|
Boolean prevPacketWasSynchronized;
|
|
|
|
float prevPacketPTS;
|
|
|
|
ReadBufferQueue** otherQueue;
|
|
|
|
|
|
|
|
// The 'queue' actually consists of just a single "demux_packet_t"
|
|
|
|
// (because the underlying OS does the actual queueing/buffering):
|
|
|
|
demux_packet_t* dp;
|
|
|
|
|
|
|
|
// However, we sometimes inspect buffers before delivering them.
|
|
|
|
// For this, we maintain a queue of pending buffers:
|
|
|
|
void savePendingBuffer(demux_packet_t* dp);
|
|
|
|
demux_packet_t* getPendingBuffer();
|
|
|
|
|
2007-04-01 03:03:05 +00:00
|
|
|
// For H264 over rtsp using AVParser, the next packet has to be saved
|
|
|
|
demux_packet_t* nextpacket;
|
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
private:
|
2003-03-11 19:08:31 +00:00
|
|
|
demux_packet_t* pendingDPHead;
|
|
|
|
demux_packet_t* pendingDPTail;
|
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
FramedSource* fReadSource;
|
2002-09-28 18:51:44 +00:00
|
|
|
RTPSource* fRTPSource;
|
2002-08-05 00:39:07 +00:00
|
|
|
demuxer_t* fOurDemuxer;
|
|
|
|
char const* fTag; // used for debugging
|
|
|
|
};
|
|
|
|
|
|
|
|
// A structure of RTP-specific state, kept so that we can cleanly
|
|
|
|
// reclaim it:
|
|
|
|
typedef struct RTPState {
|
|
|
|
char const* sdpDescription;
|
|
|
|
RTSPClient* rtspClient;
|
2003-05-03 06:13:11 +00:00
|
|
|
SIPClient* sipClient;
|
2002-08-05 00:39:07 +00:00
|
|
|
MediaSession* mediaSession;
|
|
|
|
ReadBufferQueue* audioBufferQueue;
|
|
|
|
ReadBufferQueue* videoBufferQueue;
|
2003-02-03 10:27:50 +00:00
|
|
|
unsigned flags;
|
2002-09-28 18:51:44 +00:00
|
|
|
struct timeval firstSyncTime;
|
2002-08-05 00:39:07 +00:00
|
|
|
};
|
|
|
|
|
2003-04-11 02:35:01 +00:00
|
|
|
extern "C" char* network_username;
|
|
|
|
extern "C" char* network_password;
|
2003-05-03 06:13:11 +00:00
|
|
|
static char* openURL_rtsp(RTSPClient* client, char const* url) {
|
|
|
|
// If we were given a user name (and optional password), then use them:
|
|
|
|
if (network_username != NULL) {
|
|
|
|
char const* password = network_password == NULL ? "" : network_password;
|
|
|
|
return client->describeWithPassword(url, network_username, password);
|
|
|
|
} else {
|
|
|
|
return client->describeURL(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* openURL_sip(SIPClient* client, char const* url) {
|
|
|
|
// If we were given a user name (and optional password), then use them:
|
|
|
|
if (network_username != NULL) {
|
|
|
|
char const* password = network_password == NULL ? "" : network_password;
|
|
|
|
return client->inviteWithPassword(url, network_username, password);
|
|
|
|
} else {
|
|
|
|
return client->invite(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-08 05:46:23 +00:00
|
|
|
int rtspStreamOverTCP = 0;
|
2006-06-26 22:00:44 +00:00
|
|
|
extern int rtsp_port;
|
2002-10-08 05:46:23 +00:00
|
|
|
|
2004-06-02 06:48:25 +00:00
|
|
|
extern "C" int audio_id, video_id, dvdsub_id;
|
2004-02-22 06:20:08 +00:00
|
|
|
extern "C" demuxer_t* demux_open_rtp(demuxer_t* demuxer) {
|
2004-03-19 10:15:41 +00:00
|
|
|
Boolean success = False;
|
2002-08-05 00:39:07 +00:00
|
|
|
do {
|
|
|
|
TaskScheduler* scheduler = BasicTaskScheduler::createNew();
|
|
|
|
if (scheduler == NULL) break;
|
|
|
|
UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
|
|
|
|
if (env == NULL) break;
|
|
|
|
|
|
|
|
RTSPClient* rtspClient = NULL;
|
2003-05-03 06:13:11 +00:00
|
|
|
SIPClient* sipClient = NULL;
|
2002-08-05 00:39:07 +00:00
|
|
|
|
2003-02-18 22:33:44 +00:00
|
|
|
if (demuxer == NULL || demuxer->stream == NULL) break; // shouldn't happen
|
|
|
|
demuxer->stream->eof = 0; // just in case
|
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
// Look at the stream's 'priv' field to see if we were initiated
|
|
|
|
// via a SDP description:
|
|
|
|
char* sdpDescription = (char*)(demuxer->stream->priv);
|
|
|
|
if (sdpDescription == NULL) {
|
|
|
|
// We weren't given a SDP description directly, so assume that
|
2003-05-03 06:13:11 +00:00
|
|
|
// we were given a RTSP or SIP URL:
|
|
|
|
char const* protocol = demuxer->stream->streaming_ctrl->url->protocol;
|
2002-08-05 00:39:07 +00:00
|
|
|
char const* url = demuxer->stream->streaming_ctrl->url->url;
|
|
|
|
extern int verbose;
|
2003-05-03 06:13:11 +00:00
|
|
|
if (strcmp(protocol, "rtsp") == 0) {
|
|
|
|
rtspClient = RTSPClient::createNew(*env, verbose, "MPlayer");
|
|
|
|
if (rtspClient == NULL) {
|
|
|
|
fprintf(stderr, "Failed to create RTSP client: %s\n",
|
|
|
|
env->getResultMsg());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sdpDescription = openURL_rtsp(rtspClient, url);
|
|
|
|
} else { // SIP
|
|
|
|
unsigned char desiredAudioType = 0; // PCMU (use 3 for GSM)
|
|
|
|
sipClient = SIPClient::createNew(*env, desiredAudioType, NULL,
|
|
|
|
verbose, "MPlayer");
|
|
|
|
if (sipClient == NULL) {
|
|
|
|
fprintf(stderr, "Failed to create SIP client: %s\n",
|
|
|
|
env->getResultMsg());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sipClient->setClientStartPortNum(8000);
|
|
|
|
sdpDescription = openURL_sip(sipClient, url);
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sdpDescription == NULL) {
|
|
|
|
fprintf(stderr, "Failed to get a SDP description from URL \"%s\": %s\n",
|
|
|
|
url, env->getResultMsg());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have a SDP description, create a MediaSession from it:
|
|
|
|
MediaSession* mediaSession = MediaSession::createNew(*env, sdpDescription);
|
|
|
|
if (mediaSession == NULL) break;
|
|
|
|
|
2003-02-03 10:27:50 +00:00
|
|
|
|
|
|
|
// Create a 'RTPState' structure containing the state that we just created,
|
|
|
|
// and store it in the demuxer's 'priv' field, for future reference:
|
|
|
|
RTPState* rtpState = new RTPState;
|
|
|
|
rtpState->sdpDescription = sdpDescription;
|
|
|
|
rtpState->rtspClient = rtspClient;
|
2003-05-03 06:13:11 +00:00
|
|
|
rtpState->sipClient = sipClient;
|
2003-02-03 10:27:50 +00:00
|
|
|
rtpState->mediaSession = mediaSession;
|
2003-02-09 17:06:38 +00:00
|
|
|
rtpState->audioBufferQueue = rtpState->videoBufferQueue = NULL;
|
2003-03-11 19:08:31 +00:00
|
|
|
rtpState->flags = 0;
|
2003-02-03 10:27:50 +00:00
|
|
|
rtpState->firstSyncTime.tv_sec = rtpState->firstSyncTime.tv_usec = 0;
|
|
|
|
demuxer->priv = rtpState;
|
|
|
|
|
2007-12-02 18:04:14 +00:00
|
|
|
int audiofound = 0, videofound = 0;
|
2002-08-05 00:39:07 +00:00
|
|
|
// Create RTP receivers (sources) for each subsession:
|
|
|
|
MediaSubsessionIterator iter(*mediaSession);
|
|
|
|
MediaSubsession* subsession;
|
2003-03-11 19:08:31 +00:00
|
|
|
unsigned desiredReceiveBufferSize;
|
2002-08-05 00:39:07 +00:00
|
|
|
while ((subsession = iter.next()) != NULL) {
|
|
|
|
// Ignore any subsession that's not audio or video:
|
|
|
|
if (strcmp(subsession->mediumName(), "audio") == 0) {
|
2007-09-25 22:34:01 +00:00
|
|
|
if (audiofound) {
|
|
|
|
fprintf(stderr, "Additional subsession \"audio/%s\" skipped\n", subsession->codecName());
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
desiredReceiveBufferSize = 100000;
|
2002-08-05 00:39:07 +00:00
|
|
|
} else if (strcmp(subsession->mediumName(), "video") == 0) {
|
2007-12-02 18:04:14 +00:00
|
|
|
if (videofound) {
|
|
|
|
fprintf(stderr, "Additional subsession \"video/%s\" skipped\n", subsession->codecName());
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
desiredReceiveBufferSize = 2000000;
|
2002-08-05 00:39:07 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-02-19 13:27:27 +00:00
|
|
|
if (rtsp_port)
|
|
|
|
subsession->setClientPortNum (rtsp_port);
|
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
if (!subsession->initiate()) {
|
|
|
|
fprintf(stderr, "Failed to initiate \"%s/%s\" RTP subsession: %s\n", subsession->mediumName(), subsession->codecName(), env->getResultMsg());
|
|
|
|
} else {
|
2006-02-19 13:27:27 +00:00
|
|
|
fprintf(stderr, "Initiated \"%s/%s\" RTP subsession on port %d\n", subsession->mediumName(), subsession->codecName(), subsession->clientPortNum());
|
2002-08-05 00:39:07 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
// Set the OS's socket receive buffer sufficiently large to avoid
|
|
|
|
// incoming packets getting dropped between successive reads from this
|
|
|
|
// subsession's demuxer. Depending on the bitrate(s) that you expect,
|
|
|
|
// you may wish to tweak the "desiredReceiveBufferSize" values above.
|
|
|
|
int rtpSocketNum = subsession->rtpSource()->RTPgs()->socketNum();
|
|
|
|
int receiveBufferSize
|
|
|
|
= increaseReceiveBufferTo(*env, rtpSocketNum,
|
|
|
|
desiredReceiveBufferSize);
|
|
|
|
if (verbose > 0) {
|
|
|
|
fprintf(stderr, "Increased %s socket receive buffer to %d bytes \n",
|
|
|
|
subsession->mediumName(), receiveBufferSize);
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
if (rtspClient != NULL) {
|
2003-03-11 19:08:31 +00:00
|
|
|
// Issue a RTSP "SETUP" command on the chosen subsession:
|
2002-10-08 05:46:23 +00:00
|
|
|
if (!rtspClient->setupMediaSubsession(*subsession, False,
|
|
|
|
rtspStreamOverTCP)) break;
|
2007-09-25 22:34:01 +00:00
|
|
|
if (!strcmp(subsession->mediumName(), "audio"))
|
|
|
|
audiofound = 1;
|
2007-12-02 18:04:14 +00:00
|
|
|
if (!strcmp(subsession->mediumName(), "video"))
|
|
|
|
videofound = 1;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-05 00:39:07 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
if (rtspClient != NULL) {
|
|
|
|
// Issue a RTSP aggregate "PLAY" command on the whole session:
|
|
|
|
if (!rtspClient->playMediaSession(*mediaSession)) break;
|
2003-05-03 06:13:11 +00:00
|
|
|
} else if (sipClient != NULL) {
|
|
|
|
sipClient->sendACK(); // to start the stream flowing
|
2003-03-11 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the session is ready to be read, do additional
|
|
|
|
// MPlayer codec-specific initialization on each subsession:
|
|
|
|
iter.reset();
|
|
|
|
while ((subsession = iter.next()) != NULL) {
|
|
|
|
if (subsession->readSource() == NULL) continue; // not reading this
|
|
|
|
|
|
|
|
unsigned flags = 0;
|
|
|
|
if (strcmp(subsession->mediumName(), "audio") == 0) {
|
|
|
|
rtpState->audioBufferQueue
|
|
|
|
= new ReadBufferQueue(subsession, demuxer, "audio");
|
|
|
|
rtpState->audioBufferQueue->otherQueue = &(rtpState->videoBufferQueue);
|
|
|
|
rtpCodecInitialize_audio(demuxer, subsession, flags);
|
|
|
|
} else if (strcmp(subsession->mediumName(), "video") == 0) {
|
|
|
|
rtpState->videoBufferQueue
|
|
|
|
= new ReadBufferQueue(subsession, demuxer, "video");
|
|
|
|
rtpState->videoBufferQueue->otherQueue = &(rtpState->audioBufferQueue);
|
|
|
|
rtpCodecInitialize_video(demuxer, subsession, flags);
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
rtpState->flags |= flags;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
2004-03-19 10:15:41 +00:00
|
|
|
success = True;
|
2002-08-05 00:39:07 +00:00
|
|
|
} while (0);
|
2004-03-19 10:15:41 +00:00
|
|
|
if (!success) return NULL; // an error occurred
|
2004-02-22 06:20:08 +00:00
|
|
|
|
|
|
|
// Hack: If audio and video are demuxed together on a single RTP stream,
|
|
|
|
// then create a new "demuxer_t" structure to allow the higher-level
|
|
|
|
// code to recognize this:
|
|
|
|
if (demux_is_multiplexed_rtp_stream(demuxer)) {
|
|
|
|
stream_t* s = new_ds_stream(demuxer->video);
|
2004-06-02 06:48:25 +00:00
|
|
|
demuxer_t* od = demux_open(s, DEMUXER_TYPE_UNKNOWN,
|
|
|
|
audio_id, video_id, dvdsub_id, NULL);
|
2004-02-22 06:20:08 +00:00
|
|
|
demuxer = new_demuxers_demuxer(od, od, od);
|
|
|
|
}
|
|
|
|
|
|
|
|
return demuxer;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int demux_is_mpeg_rtp_stream(demuxer_t* demuxer) {
|
|
|
|
// Get the RTP state that was stored in the demuxer's 'priv' field:
|
|
|
|
RTPState* rtpState = (RTPState*)(demuxer->priv);
|
2003-02-03 10:27:50 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
return (rtpState->flags&RTPSTATE_IS_MPEG12_VIDEO) != 0;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
2003-07-27 10:15:10 +00:00
|
|
|
extern "C" int demux_is_multiplexed_rtp_stream(demuxer_t* demuxer) {
|
|
|
|
// Get the RTP state that was stored in the demuxer's 'priv' field:
|
|
|
|
RTPState* rtpState = (RTPState*)(demuxer->priv);
|
|
|
|
|
|
|
|
return (rtpState->flags&RTPSTATE_IS_MULTIPLEXED) != 0;
|
|
|
|
}
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
|
|
|
|
Boolean mustGetNewData,
|
|
|
|
float& ptsBehind); // forward
|
2002-08-05 00:39:07 +00:00
|
|
|
|
|
|
|
extern "C" int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds) {
|
|
|
|
// Get a filled-in "demux_packet" from the RTP source, and deliver it.
|
|
|
|
// Note that this is called as a synchronous read operation, so it needs
|
|
|
|
// to block in the (hopefully infrequent) case where no packet is
|
|
|
|
// immediately available.
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
while (1) {
|
|
|
|
float ptsBehind;
|
|
|
|
demux_packet_t* dp = getBuffer(demuxer, ds, False, ptsBehind); // blocking
|
|
|
|
if (dp == NULL) return 0;
|
2002-08-05 00:39:07 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
if (demuxer->stream->eof) return 0; // source stream has closed down
|
2002-08-05 00:39:07 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
// Before using this packet, check to make sure that its presentation
|
|
|
|
// time is not far behind the other stream (if any). If it is,
|
|
|
|
// then we discard this packet, and get another instead. (The rest of
|
|
|
|
// MPlayer doesn't always do a good job of synchronizing when the
|
|
|
|
// audio and video streams get this far apart.)
|
|
|
|
// (We don't do this when streaming over TCP, because then the audio and
|
|
|
|
// video streams are interleaved.)
|
2004-03-02 08:52:59 +00:00
|
|
|
// (Also, if the stream is *excessively* far behind, then we allow
|
|
|
|
// the packet, because in this case it probably means that there was
|
|
|
|
// an error in the source's timestamp synchronization.)
|
2003-03-11 19:08:31 +00:00
|
|
|
const float ptsBehindThreshold = 1.0; // seconds
|
2004-03-02 08:52:59 +00:00
|
|
|
const float ptsBehindLimit = 60.0; // seconds
|
|
|
|
if (ptsBehind < ptsBehindThreshold ||
|
|
|
|
ptsBehind > ptsBehindLimit ||
|
|
|
|
rtspStreamOverTCP) { // packet's OK
|
2003-03-11 19:08:31 +00:00
|
|
|
ds_add_packet(ds, dp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-04-02 01:38:07 +00:00
|
|
|
#ifdef DEBUG_PRINT_DISCARDED_PACKETS
|
|
|
|
RTPState* rtpState = (RTPState*)(demuxer->priv);
|
|
|
|
ReadBufferQueue* bufferQueue = ds == demuxer->video ? rtpState->videoBufferQueue : rtpState->audioBufferQueue;
|
|
|
|
fprintf(stderr, "Discarding %s packet (%fs behind)\n", bufferQueue->tag(), ptsBehind);
|
|
|
|
#endif
|
2003-03-11 19:08:31 +00:00
|
|
|
free_demux_packet(dp); // give back this packet, and get another one
|
|
|
|
}
|
2003-02-03 10:27:50 +00:00
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
Boolean awaitRTPPacket(demuxer_t* demuxer, demux_stream_t* ds,
|
|
|
|
unsigned char*& packetData, unsigned& packetDataLen,
|
|
|
|
float& pts) {
|
|
|
|
// Similar to "demux_rtp_fill_buffer()", except that the "demux_packet"
|
|
|
|
// is not delivered to the "demux_stream".
|
|
|
|
float ptsBehind;
|
|
|
|
demux_packet_t* dp = getBuffer(demuxer, ds, True, ptsBehind); // blocking
|
|
|
|
if (dp == NULL) return False;
|
|
|
|
|
|
|
|
packetData = dp->buffer;
|
|
|
|
packetDataLen = dp->len;
|
|
|
|
pts = dp->pts;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2003-05-03 06:13:11 +00:00
|
|
|
static void teardownRTSPorSIPSession(RTPState* rtpState); // forward
|
2003-03-11 19:08:31 +00:00
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
extern "C" void demux_close_rtp(demuxer_t* demuxer) {
|
|
|
|
// Reclaim all RTP-related state:
|
|
|
|
|
|
|
|
// Get the RTP state that was stored in the demuxer's 'priv' field:
|
|
|
|
RTPState* rtpState = (RTPState*)(demuxer->priv);
|
2002-08-29 20:19:33 +00:00
|
|
|
if (rtpState == NULL) return;
|
2003-03-11 19:08:31 +00:00
|
|
|
|
2003-05-03 06:13:11 +00:00
|
|
|
teardownRTSPorSIPSession(rtpState);
|
2003-03-11 19:08:31 +00:00
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
UsageEnvironment* env = NULL;
|
|
|
|
TaskScheduler* scheduler = NULL;
|
|
|
|
if (rtpState->mediaSession != NULL) {
|
|
|
|
env = &(rtpState->mediaSession->envir());
|
|
|
|
scheduler = &(env->taskScheduler());
|
|
|
|
}
|
|
|
|
Medium::close(rtpState->mediaSession);
|
|
|
|
Medium::close(rtpState->rtspClient);
|
2003-05-03 06:13:11 +00:00
|
|
|
Medium::close(rtpState->sipClient);
|
2002-08-05 00:39:07 +00:00
|
|
|
delete rtpState->audioBufferQueue;
|
|
|
|
delete rtpState->videoBufferQueue;
|
|
|
|
delete rtpState->sdpDescription;
|
|
|
|
delete rtpState;
|
|
|
|
|
2004-07-20 02:12:08 +00:00
|
|
|
env->reclaim(); delete scheduler;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////// Extra routines that help implement the above interface functions:
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
#define MAX_RTP_FRAME_SIZE 50000
|
|
|
|
// >= the largest conceivable frame composed from one or more RTP packets
|
2002-08-05 00:39:07 +00:00
|
|
|
|
|
|
|
static void afterReading(void* clientData, unsigned frameSize,
|
2004-01-06 09:30:27 +00:00
|
|
|
unsigned /*numTruncatedBytes*/,
|
|
|
|
struct timeval presentationTime,
|
|
|
|
unsigned /*durationInMicroseconds*/) {
|
2007-03-06 22:53:52 +00:00
|
|
|
int headersize = 0;
|
2003-03-11 19:08:31 +00:00
|
|
|
if (frameSize >= MAX_RTP_FRAME_SIZE) {
|
|
|
|
fprintf(stderr, "Saw an input frame too large (>=%d). Increase MAX_RTP_FRAME_SIZE in \"demux_rtp.cpp\".\n",
|
|
|
|
MAX_RTP_FRAME_SIZE);
|
|
|
|
}
|
|
|
|
ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
|
2002-08-05 00:39:07 +00:00
|
|
|
demuxer_t* demuxer = bufferQueue->ourDemuxer();
|
2002-09-28 18:51:44 +00:00
|
|
|
RTPState* rtpState = (RTPState*)(demuxer->priv);
|
2002-08-05 00:39:07 +00:00
|
|
|
|
|
|
|
if (frameSize > 0) demuxer->stream->eof = 0;
|
|
|
|
|
2007-04-01 03:00:47 +00:00
|
|
|
demux_packet_t* dp = bufferQueue->dp;
|
|
|
|
|
2007-03-06 22:53:52 +00:00
|
|
|
if (bufferQueue->readSource()->isAMRAudioSource())
|
|
|
|
headersize = 1;
|
2007-04-01 03:03:05 +00:00
|
|
|
else if (bufferQueue == rtpState->videoBufferQueue &&
|
|
|
|
((sh_video_t*)demuxer->video->sh)->format == mmioFOURCC('H','2','6','4')) {
|
|
|
|
dp->buffer[0]=0x00;
|
|
|
|
dp->buffer[1]=0x00;
|
|
|
|
dp->buffer[2]=0x01;
|
|
|
|
headersize = 3;
|
|
|
|
}
|
2007-03-06 22:53:52 +00:00
|
|
|
|
|
|
|
resize_demux_packet(dp, frameSize + headersize);
|
2002-09-28 18:51:44 +00:00
|
|
|
|
|
|
|
// Set the packet's presentation time stamp, depending on whether or
|
|
|
|
// not our RTP source's timestamps have been synchronized yet:
|
2003-03-11 19:08:31 +00:00
|
|
|
Boolean hasBeenSynchronized
|
|
|
|
= bufferQueue->rtpSource()->hasBeenSynchronizedUsingRTCP();
|
|
|
|
if (hasBeenSynchronized) {
|
|
|
|
if (verbose > 0 && !bufferQueue->prevPacketWasSynchronized) {
|
|
|
|
fprintf(stderr, "%s stream has been synchronized using RTCP \n",
|
|
|
|
bufferQueue->tag());
|
|
|
|
}
|
2002-09-28 18:51:44 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
struct timeval* fst = &(rtpState->firstSyncTime); // abbrev
|
|
|
|
if (fst->tv_sec == 0 && fst->tv_usec == 0) {
|
|
|
|
*fst = presentationTime;
|
2002-09-28 18:51:44 +00:00
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
|
|
|
|
// For the "pts" field, use the time differential from the first
|
|
|
|
// synchronized time, rather than absolute time, in order to avoid
|
|
|
|
// round-off errors when converting to a float:
|
|
|
|
dp->pts = presentationTime.tv_sec - fst->tv_sec
|
|
|
|
+ (presentationTime.tv_usec - fst->tv_usec)/1000000.0;
|
|
|
|
bufferQueue->prevPacketPTS = dp->pts;
|
|
|
|
} else {
|
|
|
|
if (verbose > 0 && bufferQueue->prevPacketWasSynchronized) {
|
|
|
|
fprintf(stderr, "%s stream is no longer RTCP-synchronized \n",
|
|
|
|
bufferQueue->tag());
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the previous packet's "pts" once again:
|
|
|
|
dp->pts = bufferQueue->prevPacketPTS;
|
2002-09-28 18:51:44 +00:00
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
bufferQueue->prevPacketWasSynchronized = hasBeenSynchronized;
|
2002-09-28 18:51:44 +00:00
|
|
|
|
2002-08-05 00:39:07 +00:00
|
|
|
dp->pos = demuxer->filepos;
|
2007-03-22 10:14:32 +00:00
|
|
|
demuxer->filepos += frameSize + headersize;
|
2002-08-05 00:39:07 +00:00
|
|
|
|
2003-02-03 10:27:50 +00:00
|
|
|
// Signal any pending 'doEventLoop()' call on this queue:
|
2002-08-05 00:39:07 +00:00
|
|
|
bufferQueue->blockingFlag = ~0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onSourceClosure(void* clientData) {
|
2003-03-11 19:08:31 +00:00
|
|
|
ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
|
2002-08-05 00:39:07 +00:00
|
|
|
demuxer_t* demuxer = bufferQueue->ourDemuxer();
|
|
|
|
|
|
|
|
demuxer->stream->eof = 1;
|
|
|
|
|
2003-02-03 10:27:50 +00:00
|
|
|
// Signal any pending 'doEventLoop()' call on this queue:
|
2002-08-05 00:39:07 +00:00
|
|
|
bufferQueue->blockingFlag = ~0;
|
|
|
|
}
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
|
|
|
|
Boolean mustGetNewData,
|
|
|
|
float& ptsBehind) {
|
|
|
|
// Begin by finding the buffer queue that we want to read from:
|
|
|
|
// (Get this from the RTP state, which we stored in
|
|
|
|
// the demuxer's 'priv' field)
|
|
|
|
RTPState* rtpState = (RTPState*)(demuxer->priv);
|
|
|
|
ReadBufferQueue* bufferQueue = NULL;
|
2007-03-12 22:13:48 +00:00
|
|
|
int headersize = 0;
|
2007-06-18 08:53:53 +00:00
|
|
|
TaskToken task;
|
|
|
|
|
|
|
|
if (demuxer->stream->eof) return NULL;
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
if (ds == demuxer->video) {
|
|
|
|
bufferQueue = rtpState->videoBufferQueue;
|
2007-04-01 03:03:05 +00:00
|
|
|
if (((sh_video_t*)ds->sh)->format == mmioFOURCC('H','2','6','4'))
|
|
|
|
headersize = 3;
|
2003-03-11 19:08:31 +00:00
|
|
|
} else if (ds == demuxer->audio) {
|
|
|
|
bufferQueue = rtpState->audioBufferQueue;
|
2007-03-06 22:53:52 +00:00
|
|
|
if (bufferQueue->readSource()->isAMRAudioSource())
|
2007-03-12 22:13:48 +00:00
|
|
|
headersize = 1;
|
2003-03-11 19:08:31 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "(demux_rtp)getBuffer: internal error: unknown stream\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-03 10:27:50 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
if (bufferQueue == NULL || bufferQueue->readSource() == NULL) {
|
|
|
|
fprintf(stderr, "(demux_rtp)getBuffer failed: no appropriate RTP subsession has been set up\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-18 09:15:32 +00:00
|
|
|
demux_packet_t* dp = NULL;
|
2003-03-11 19:08:31 +00:00
|
|
|
if (!mustGetNewData) {
|
|
|
|
// Check whether we have a previously-saved buffer that we can use:
|
|
|
|
dp = bufferQueue->getPendingBuffer();
|
2003-04-12 09:30:19 +00:00
|
|
|
if (dp != NULL) {
|
|
|
|
ptsBehind = 0.0; // so that we always accept this data
|
|
|
|
return dp;
|
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
}
|
2003-02-03 10:27:50 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
// Allocate a new packet buffer, and arrange to read into it:
|
2007-04-01 03:03:05 +00:00
|
|
|
if (!bufferQueue->nextpacket) {
|
2003-03-11 19:08:31 +00:00
|
|
|
dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
|
|
|
|
bufferQueue->dp = dp;
|
|
|
|
if (dp == NULL) return NULL;
|
2007-04-01 03:03:05 +00:00
|
|
|
}
|
2003-02-03 10:27:50 +00:00
|
|
|
|
2007-04-01 03:03:05 +00:00
|
|
|
#ifdef USE_LIBAVCODEC
|
|
|
|
extern AVCodecParserContext * h264parserctx;
|
|
|
|
int consumed, poutbuf_size = 1;
|
2007-05-07 08:13:22 +00:00
|
|
|
const uint8_t *poutbuf = NULL;
|
2007-06-18 09:15:32 +00:00
|
|
|
float lastpts = 0.0;
|
2007-04-01 03:03:05 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (!bufferQueue->nextpacket) {
|
|
|
|
#endif
|
2003-03-11 19:08:31 +00:00
|
|
|
// Schedule the read operation:
|
2003-02-03 10:27:50 +00:00
|
|
|
bufferQueue->blockingFlag = 0;
|
2007-03-12 22:13:48 +00:00
|
|
|
bufferQueue->readSource()->getNextFrame(&dp->buffer[headersize], MAX_RTP_FRAME_SIZE - headersize,
|
2003-03-11 19:08:31 +00:00
|
|
|
afterReading, bufferQueue,
|
|
|
|
onSourceClosure, bufferQueue);
|
|
|
|
// Block ourselves until data becomes available:
|
|
|
|
TaskScheduler& scheduler
|
|
|
|
= bufferQueue->readSource()->envir().taskScheduler();
|
2007-06-20 10:39:57 +00:00
|
|
|
int delay = 10000000;
|
|
|
|
if (bufferQueue->prevPacketPTS * 1.05 > rtpState->mediaSession->playEndTime())
|
|
|
|
delay /= 10;
|
2007-06-18 08:53:53 +00:00
|
|
|
task = scheduler.scheduleDelayedTask(delay, onSourceClosure, bufferQueue);
|
2003-03-11 19:08:31 +00:00
|
|
|
scheduler.doEventLoop(&bufferQueue->blockingFlag);
|
2007-06-18 08:53:53 +00:00
|
|
|
scheduler.unscheduleDelayedTask(task);
|
|
|
|
if (demuxer->stream->eof) {
|
|
|
|
free_demux_packet(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
|
2007-03-12 22:13:48 +00:00
|
|
|
if (headersize == 1) // amr
|
2007-03-06 22:53:52 +00:00
|
|
|
dp->buffer[0] =
|
|
|
|
((AMRAudioSource*)bufferQueue->readSource())->lastFrameHeader();
|
2007-04-01 03:03:05 +00:00
|
|
|
#ifdef USE_LIBAVCODEC
|
|
|
|
} else {
|
|
|
|
bufferQueue->dp = dp = bufferQueue->nextpacket;
|
|
|
|
bufferQueue->nextpacket = NULL;
|
|
|
|
}
|
|
|
|
if (headersize == 3 && h264parserctx) { // h264
|
|
|
|
consumed = h264parserctx->parser->parser_parse(h264parserctx,
|
|
|
|
NULL,
|
|
|
|
&poutbuf, &poutbuf_size,
|
|
|
|
dp->buffer, dp->len);
|
|
|
|
|
|
|
|
if (!consumed && !poutbuf_size)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!poutbuf_size) {
|
|
|
|
lastpts=dp->pts;
|
|
|
|
free_demux_packet(dp);
|
|
|
|
bufferQueue->dp = dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
|
|
|
|
} else {
|
|
|
|
bufferQueue->nextpacket = dp;
|
|
|
|
bufferQueue->dp = dp = new_demux_packet(poutbuf_size);
|
|
|
|
memcpy(dp->buffer, poutbuf, poutbuf_size);
|
|
|
|
dp->pts=lastpts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!poutbuf_size);
|
|
|
|
#endif
|
2007-03-06 22:53:52 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
// Set the "ptsBehind" result parameter:
|
2003-09-24 08:41:57 +00:00
|
|
|
if (bufferQueue->prevPacketPTS != 0.0
|
|
|
|
&& bufferQueue->prevPacketWasSynchronized
|
|
|
|
&& *(bufferQueue->otherQueue) != NULL
|
|
|
|
&& (*(bufferQueue->otherQueue))->prevPacketPTS != 0.0
|
|
|
|
&& (*(bufferQueue->otherQueue))->prevPacketWasSynchronized) {
|
2003-03-11 19:08:31 +00:00
|
|
|
ptsBehind = (*(bufferQueue->otherQueue))->prevPacketPTS
|
|
|
|
- bufferQueue->prevPacketPTS;
|
|
|
|
} else {
|
|
|
|
ptsBehind = 0.0;
|
2003-02-03 10:27:50 +00:00
|
|
|
}
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
if (mustGetNewData) {
|
|
|
|
// Save this buffer for future reads:
|
|
|
|
bufferQueue->savePendingBuffer(dp);
|
|
|
|
}
|
2002-08-05 00:39:07 +00:00
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
return dp;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
2003-05-03 06:13:11 +00:00
|
|
|
static void teardownRTSPorSIPSession(RTPState* rtpState) {
|
2003-03-11 19:08:31 +00:00
|
|
|
MediaSession* mediaSession = rtpState->mediaSession;
|
2003-05-03 06:13:11 +00:00
|
|
|
if (mediaSession == NULL) return;
|
|
|
|
if (rtpState->rtspClient != NULL) {
|
2007-04-03 17:00:27 +00:00
|
|
|
rtpState->rtspClient->teardownMediaSession(*mediaSession);
|
2003-05-03 06:13:11 +00:00
|
|
|
} else if (rtpState->sipClient != NULL) {
|
|
|
|
rtpState->sipClient->sendBYE();
|
2003-03-11 19:08:31 +00:00
|
|
|
}
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
////////// "ReadBuffer" and "ReadBufferQueue" implementation:
|
2002-08-05 00:39:07 +00:00
|
|
|
|
|
|
|
ReadBufferQueue::ReadBufferQueue(MediaSubsession* subsession,
|
|
|
|
demuxer_t* demuxer, char const* tag)
|
2003-03-11 19:08:31 +00:00
|
|
|
: prevPacketWasSynchronized(False), prevPacketPTS(0.0), otherQueue(NULL),
|
2007-06-18 09:15:32 +00:00
|
|
|
dp(NULL), nextpacket(NULL),
|
|
|
|
pendingDPHead(NULL), pendingDPTail(NULL),
|
2002-08-05 00:39:07 +00:00
|
|
|
fReadSource(subsession == NULL ? NULL : subsession->readSource()),
|
2002-09-28 18:51:44 +00:00
|
|
|
fRTPSource(subsession == NULL ? NULL : subsession->rtpSource()),
|
2002-08-05 00:39:07 +00:00
|
|
|
fOurDemuxer(demuxer), fTag(strdup(tag)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadBufferQueue::~ReadBufferQueue() {
|
|
|
|
delete fTag;
|
2003-03-11 19:08:31 +00:00
|
|
|
|
|
|
|
// Free any pending buffers (that never got delivered):
|
|
|
|
demux_packet_t* dp = pendingDPHead;
|
|
|
|
while (dp != NULL) {
|
|
|
|
demux_packet_t* dpNext = dp->next;
|
|
|
|
dp->next = NULL;
|
|
|
|
free_demux_packet(dp);
|
|
|
|
dp = dpNext;
|
|
|
|
}
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
2003-03-11 19:08:31 +00:00
|
|
|
void ReadBufferQueue::savePendingBuffer(demux_packet_t* dp) {
|
|
|
|
// Keep this buffer around, until MPlayer asks for it later:
|
|
|
|
if (pendingDPTail == NULL) {
|
|
|
|
pendingDPHead = pendingDPTail = dp;
|
|
|
|
} else {
|
|
|
|
pendingDPTail->next = dp;
|
|
|
|
pendingDPTail = dp;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
2003-03-11 19:08:31 +00:00
|
|
|
dp->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
demux_packet_t* ReadBufferQueue::getPendingBuffer() {
|
|
|
|
demux_packet_t* dp = pendingDPHead;
|
|
|
|
if (dp != NULL) {
|
|
|
|
pendingDPHead = dp->next;
|
|
|
|
if (pendingDPHead == NULL) pendingDPTail = NULL;
|
|
|
|
|
|
|
|
dp->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dp;
|
2002-08-05 00:39:07 +00:00
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2007-02-27 22:25:00 +00:00
|
|
|
static int demux_rtp_control(struct demuxer_st *demuxer, int cmd, void *arg) {
|
|
|
|
double endpts = ((RTPState*)demuxer->priv)->mediaSession->playEndTime();
|
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH:
|
|
|
|
if (endpts <= 0)
|
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
*((double *)arg) = endpts;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
|
|
|
case DEMUXER_CTRL_GET_PERCENT_POS:
|
|
|
|
if (endpts <= 0)
|
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
*((int *)arg) = (int)(((RTPState*)demuxer->priv)->videoBufferQueue->prevPacketPTS*100/endpts);
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
|
|
|
demuxer_desc_t demuxer_desc_rtp = {
|
2005-09-23 22:35:04 +00:00
|
|
|
"LIVE555 RTP demuxer",
|
2007-12-04 15:26:56 +00:00
|
|
|
"live555",
|
2005-08-05 19:57:47 +00:00
|
|
|
"",
|
|
|
|
"Ross Finlayson",
|
2005-09-23 22:35:04 +00:00
|
|
|
"requires LIVE555 Streaming Media library",
|
2005-08-05 19:57:47 +00:00
|
|
|
DEMUXER_TYPE_RTP,
|
|
|
|
0, // no autodetect
|
|
|
|
NULL,
|
|
|
|
demux_rtp_fill_buffer,
|
|
|
|
demux_open_rtp,
|
|
|
|
demux_close_rtp,
|
|
|
|
NULL,
|
2007-02-27 22:25:00 +00:00
|
|
|
demux_rtp_control
|
2005-08-05 19:57:47 +00:00
|
|
|
};
|