demux_mkv: Make seeks more precise in some cases
[mplayer/glamo.git] / libmpdemux / demux_rtp.cpp
blobafe550b284d8be1f3d0e8474404b88f0dfdd5379
1 /*
2 * routines (with C-linkage) that interface between MPlayer
3 * and the "LIVE555 Streaming Media" libraries
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 extern "C" {
23 // on MinGW, we must include windows.h before the things it conflicts
24 #ifdef __MINGW32__ // with. they are each protected from
25 #include <windows.h> // windows.h, but not the other way around.
26 #endif
27 #include "demux_rtp.h"
28 #include "stheader.h"
29 #include "options.h"
31 #include "demux_rtp_internal.h"
33 #include "BasicUsageEnvironment.hh"
34 #include "liveMedia.hh"
35 #include "GroupsockHelper.hh"
36 #include <unistd.h>
38 // A data structure representing input data for each stream:
39 class ReadBufferQueue {
40 public:
41 ReadBufferQueue(MediaSubsession* subsession, demuxer_t* demuxer,
42 char const* tag);
43 virtual ~ReadBufferQueue();
45 FramedSource* readSource() const { return fReadSource; }
46 RTPSource* rtpSource() const { return fRTPSource; }
47 demuxer_t* ourDemuxer() const { return fOurDemuxer; }
48 char const* tag() const { return fTag; }
50 char blockingFlag; // used to implement synchronous reads
52 // For A/V synchronization:
53 Boolean prevPacketWasSynchronized;
54 float prevPacketPTS;
55 ReadBufferQueue** otherQueue;
57 // The 'queue' actually consists of just a single "demux_packet_t"
58 // (because the underlying OS does the actual queueing/buffering):
59 demux_packet_t* dp;
61 // However, we sometimes inspect buffers before delivering them.
62 // For this, we maintain a queue of pending buffers:
63 void savePendingBuffer(demux_packet_t* dp);
64 demux_packet_t* getPendingBuffer();
66 // For H264 over rtsp using AVParser, the next packet has to be saved
67 demux_packet_t* nextpacket;
69 private:
70 demux_packet_t* pendingDPHead;
71 demux_packet_t* pendingDPTail;
73 FramedSource* fReadSource;
74 RTPSource* fRTPSource;
75 demuxer_t* fOurDemuxer;
76 char const* fTag; // used for debugging
79 // A structure of RTP-specific state, kept so that we can cleanly
80 // reclaim it:
81 typedef struct RTPState {
82 char const* sdpDescription;
83 RTSPClient* rtspClient;
84 SIPClient* sipClient;
85 MediaSession* mediaSession;
86 ReadBufferQueue* audioBufferQueue;
87 ReadBufferQueue* videoBufferQueue;
88 unsigned flags;
89 struct timeval firstSyncTime;
92 extern "C" char* network_username;
93 extern "C" char* network_password;
94 static char* openURL_rtsp(RTSPClient* client, char const* url) {
95 // If we were given a user name (and optional password), then use them:
96 if (network_username != NULL) {
97 char const* password = network_password == NULL ? "" : network_password;
98 return client->describeWithPassword(url, network_username, password);
99 } else {
100 return client->describeURL(url);
104 static char* openURL_sip(SIPClient* client, char const* url) {
105 // If we were given a user name (and optional password), then use them:
106 if (network_username != NULL) {
107 char const* password = network_password == NULL ? "" : network_password;
108 return client->inviteWithPassword(url, network_username, password);
109 } else {
110 return client->invite(url);
114 #ifdef CONFIG_LIBNEMESI
115 extern int rtsp_transport_tcp;
116 #else
117 int rtsp_transport_tcp = 0;
118 #endif
120 extern int rtsp_port;
122 extern "C" demuxer_t* demux_open_rtp(demuxer_t* demuxer) {
123 struct MPOpts *opts = demuxer->opts;
124 Boolean success = False;
125 do {
126 TaskScheduler* scheduler = BasicTaskScheduler::createNew();
127 if (scheduler == NULL) break;
128 UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
129 if (env == NULL) break;
131 RTSPClient* rtspClient = NULL;
132 SIPClient* sipClient = NULL;
134 if (demuxer == NULL || demuxer->stream == NULL) break; // shouldn't happen
135 demuxer->stream->eof = 0; // just in case
137 // Look at the stream's 'priv' field to see if we were initiated
138 // via a SDP description:
139 char* sdpDescription = (char*)(demuxer->stream->priv);
140 if (sdpDescription == NULL) {
141 // We weren't given a SDP description directly, so assume that
142 // we were given a RTSP or SIP URL:
143 char const* protocol = demuxer->stream->streaming_ctrl->url->protocol;
144 char const* url = demuxer->stream->streaming_ctrl->url->url;
145 extern int verbose;
146 if (strcmp(protocol, "rtsp") == 0) {
147 rtspClient = RTSPClient::createNew(*env, verbose, "MPlayer");
148 if (rtspClient == NULL) {
149 fprintf(stderr, "Failed to create RTSP client: %s\n",
150 env->getResultMsg());
151 break;
153 sdpDescription = openURL_rtsp(rtspClient, url);
154 } else { // SIP
155 unsigned char desiredAudioType = 0; // PCMU (use 3 for GSM)
156 sipClient = SIPClient::createNew(*env, desiredAudioType, NULL,
157 verbose, "MPlayer");
158 if (sipClient == NULL) {
159 fprintf(stderr, "Failed to create SIP client: %s\n",
160 env->getResultMsg());
161 break;
163 sipClient->setClientStartPortNum(8000);
164 sdpDescription = openURL_sip(sipClient, url);
167 if (sdpDescription == NULL) {
168 fprintf(stderr, "Failed to get a SDP description from URL \"%s\": %s\n",
169 url, env->getResultMsg());
170 break;
174 // Now that we have a SDP description, create a MediaSession from it:
175 MediaSession* mediaSession = MediaSession::createNew(*env, sdpDescription);
176 if (mediaSession == NULL) break;
179 // Create a 'RTPState' structure containing the state that we just created,
180 // and store it in the demuxer's 'priv' field, for future reference:
181 RTPState* rtpState = new RTPState;
182 rtpState->sdpDescription = sdpDescription;
183 rtpState->rtspClient = rtspClient;
184 rtpState->sipClient = sipClient;
185 rtpState->mediaSession = mediaSession;
186 rtpState->audioBufferQueue = rtpState->videoBufferQueue = NULL;
187 rtpState->flags = 0;
188 rtpState->firstSyncTime.tv_sec = rtpState->firstSyncTime.tv_usec = 0;
189 demuxer->priv = rtpState;
191 int audiofound = 0, videofound = 0;
192 // Create RTP receivers (sources) for each subsession:
193 MediaSubsessionIterator iter(*mediaSession);
194 MediaSubsession* subsession;
195 unsigned desiredReceiveBufferSize;
196 while ((subsession = iter.next()) != NULL) {
197 // Ignore any subsession that's not audio or video:
198 if (strcmp(subsession->mediumName(), "audio") == 0) {
199 if (audiofound) {
200 fprintf(stderr, "Additional subsession \"audio/%s\" skipped\n", subsession->codecName());
201 continue;
203 desiredReceiveBufferSize = 100000;
204 } else if (strcmp(subsession->mediumName(), "video") == 0) {
205 if (videofound) {
206 fprintf(stderr, "Additional subsession \"video/%s\" skipped\n", subsession->codecName());
207 continue;
209 desiredReceiveBufferSize = 2000000;
210 } else {
211 continue;
214 if (rtsp_port)
215 subsession->setClientPortNum (rtsp_port);
217 if (!subsession->initiate()) {
218 fprintf(stderr, "Failed to initiate \"%s/%s\" RTP subsession: %s\n", subsession->mediumName(), subsession->codecName(), env->getResultMsg());
219 } else {
220 fprintf(stderr, "Initiated \"%s/%s\" RTP subsession on port %d\n", subsession->mediumName(), subsession->codecName(), subsession->clientPortNum());
222 // Set the OS's socket receive buffer sufficiently large to avoid
223 // incoming packets getting dropped between successive reads from this
224 // subsession's demuxer. Depending on the bitrate(s) that you expect,
225 // you may wish to tweak the "desiredReceiveBufferSize" values above.
226 int rtpSocketNum = subsession->rtpSource()->RTPgs()->socketNum();
227 int receiveBufferSize
228 = increaseReceiveBufferTo(*env, rtpSocketNum,
229 desiredReceiveBufferSize);
230 if (verbose > 0) {
231 fprintf(stderr, "Increased %s socket receive buffer to %d bytes \n",
232 subsession->mediumName(), receiveBufferSize);
235 if (rtspClient != NULL) {
236 // Issue a RTSP "SETUP" command on the chosen subsession:
237 if (!rtspClient->setupMediaSubsession(*subsession, False,
238 rtsp_transport_tcp)) break;
239 if (!strcmp(subsession->mediumName(), "audio"))
240 audiofound = 1;
241 if (!strcmp(subsession->mediumName(), "video"))
242 videofound = 1;
247 if (rtspClient != NULL) {
248 // Issue a RTSP aggregate "PLAY" command on the whole session:
249 if (!rtspClient->playMediaSession(*mediaSession)) break;
250 } else if (sipClient != NULL) {
251 sipClient->sendACK(); // to start the stream flowing
254 // Now that the session is ready to be read, do additional
255 // MPlayer codec-specific initialization on each subsession:
256 iter.reset();
257 while ((subsession = iter.next()) != NULL) {
258 if (subsession->readSource() == NULL) continue; // not reading this
260 unsigned flags = 0;
261 if (strcmp(subsession->mediumName(), "audio") == 0) {
262 rtpState->audioBufferQueue
263 = new ReadBufferQueue(subsession, demuxer, "audio");
264 rtpState->audioBufferQueue->otherQueue = &(rtpState->videoBufferQueue);
265 rtpCodecInitialize_audio(demuxer, subsession, flags);
266 } else if (strcmp(subsession->mediumName(), "video") == 0) {
267 rtpState->videoBufferQueue
268 = new ReadBufferQueue(subsession, demuxer, "video");
269 rtpState->videoBufferQueue->otherQueue = &(rtpState->audioBufferQueue);
270 rtpCodecInitialize_video(demuxer, subsession, flags);
272 rtpState->flags |= flags;
274 success = True;
275 } while (0);
276 if (!success) return NULL; // an error occurred
278 // Hack: If audio and video are demuxed together on a single RTP stream,
279 // then create a new "demuxer_t" structure to allow the higher-level
280 // code to recognize this:
281 if (demux_is_multiplexed_rtp_stream(demuxer)) {
282 stream_t* s = new_ds_stream(demuxer->video);
283 demuxer_t* od = demux_open(opts, s, DEMUXER_TYPE_UNKNOWN,
284 opts->audio_id, opts->video_id, opts->sub_id,
285 NULL);
286 demuxer = new_demuxers_demuxer(od, od, od);
289 return demuxer;
292 extern "C" int demux_is_mpeg_rtp_stream(demuxer_t* demuxer) {
293 // Get the RTP state that was stored in the demuxer's 'priv' field:
294 RTPState* rtpState = (RTPState*)(demuxer->priv);
296 return (rtpState->flags&RTPSTATE_IS_MPEG12_VIDEO) != 0;
299 extern "C" int demux_is_multiplexed_rtp_stream(demuxer_t* demuxer) {
300 // Get the RTP state that was stored in the demuxer's 'priv' field:
301 RTPState* rtpState = (RTPState*)(demuxer->priv);
303 return (rtpState->flags&RTPSTATE_IS_MULTIPLEXED) != 0;
306 static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
307 Boolean mustGetNewData,
308 float& ptsBehind); // forward
310 extern "C" int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds) {
311 // Get a filled-in "demux_packet" from the RTP source, and deliver it.
312 // Note that this is called as a synchronous read operation, so it needs
313 // to block in the (hopefully infrequent) case where no packet is
314 // immediately available.
316 while (1) {
317 float ptsBehind;
318 demux_packet_t* dp = getBuffer(demuxer, ds, False, ptsBehind); // blocking
319 if (dp == NULL) return 0;
321 if (demuxer->stream->eof) return 0; // source stream has closed down
323 // Before using this packet, check to make sure that its presentation
324 // time is not far behind the other stream (if any). If it is,
325 // then we discard this packet, and get another instead. (The rest of
326 // MPlayer doesn't always do a good job of synchronizing when the
327 // audio and video streams get this far apart.)
328 // (We don't do this when streaming over TCP, because then the audio and
329 // video streams are interleaved.)
330 // (Also, if the stream is *excessively* far behind, then we allow
331 // the packet, because in this case it probably means that there was
332 // an error in the source's timestamp synchronization.)
333 const float ptsBehindThreshold = 1.0; // seconds
334 const float ptsBehindLimit = 60.0; // seconds
335 if (ptsBehind < ptsBehindThreshold ||
336 ptsBehind > ptsBehindLimit ||
337 rtsp_transport_tcp) { // packet's OK
338 ds_add_packet(ds, dp);
339 break;
342 #ifdef DEBUG_PRINT_DISCARDED_PACKETS
343 RTPState* rtpState = (RTPState*)(demuxer->priv);
344 ReadBufferQueue* bufferQueue = ds == demuxer->video ? rtpState->videoBufferQueue : rtpState->audioBufferQueue;
345 fprintf(stderr, "Discarding %s packet (%fs behind)\n", bufferQueue->tag(), ptsBehind);
346 #endif
347 free_demux_packet(dp); // give back this packet, and get another one
350 return 1;
353 Boolean awaitRTPPacket(demuxer_t* demuxer, demux_stream_t* ds,
354 unsigned char*& packetData, unsigned& packetDataLen,
355 float& pts) {
356 // Similar to "demux_rtp_fill_buffer()", except that the "demux_packet"
357 // is not delivered to the "demux_stream".
358 float ptsBehind;
359 demux_packet_t* dp = getBuffer(demuxer, ds, True, ptsBehind); // blocking
360 if (dp == NULL) return False;
362 packetData = dp->buffer;
363 packetDataLen = dp->len;
364 pts = dp->pts;
366 return True;
369 static void teardownRTSPorSIPSession(RTPState* rtpState); // forward
371 extern "C" void demux_close_rtp(demuxer_t* demuxer) {
372 // Reclaim all RTP-related state:
374 // Get the RTP state that was stored in the demuxer's 'priv' field:
375 RTPState* rtpState = (RTPState*)(demuxer->priv);
376 if (rtpState == NULL) return;
378 teardownRTSPorSIPSession(rtpState);
380 UsageEnvironment* env = NULL;
381 TaskScheduler* scheduler = NULL;
382 if (rtpState->mediaSession != NULL) {
383 env = &(rtpState->mediaSession->envir());
384 scheduler = &(env->taskScheduler());
386 Medium::close(rtpState->mediaSession);
387 Medium::close(rtpState->rtspClient);
388 Medium::close(rtpState->sipClient);
389 delete rtpState->audioBufferQueue;
390 delete rtpState->videoBufferQueue;
391 delete rtpState->sdpDescription;
392 delete rtpState;
394 env->reclaim(); delete scheduler;
397 ////////// Extra routines that help implement the above interface functions:
399 #define MAX_RTP_FRAME_SIZE 5000000
400 // >= the largest conceivable frame composed from one or more RTP packets
402 static void afterReading(void* clientData, unsigned frameSize,
403 unsigned /*numTruncatedBytes*/,
404 struct timeval presentationTime,
405 unsigned /*durationInMicroseconds*/) {
406 int headersize = 0;
407 if (frameSize >= MAX_RTP_FRAME_SIZE) {
408 fprintf(stderr, "Saw an input frame too large (>=%d). Increase MAX_RTP_FRAME_SIZE in \"demux_rtp.cpp\".\n",
409 MAX_RTP_FRAME_SIZE);
411 ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
412 demuxer_t* demuxer = bufferQueue->ourDemuxer();
413 RTPState* rtpState = (RTPState*)(demuxer->priv);
415 if (frameSize > 0) demuxer->stream->eof = 0;
417 demux_packet_t* dp = bufferQueue->dp;
419 if (bufferQueue->readSource()->isAMRAudioSource())
420 headersize = 1;
421 else if (bufferQueue == rtpState->videoBufferQueue &&
422 ((sh_video_t*)demuxer->video->sh)->format == mmioFOURCC('H','2','6','4')) {
423 dp->buffer[0]=0x00;
424 dp->buffer[1]=0x00;
425 dp->buffer[2]=0x01;
426 headersize = 3;
429 resize_demux_packet(dp, frameSize + headersize);
431 // Set the packet's presentation time stamp, depending on whether or
432 // not our RTP source's timestamps have been synchronized yet:
433 Boolean hasBeenSynchronized
434 = bufferQueue->rtpSource()->hasBeenSynchronizedUsingRTCP();
435 if (hasBeenSynchronized) {
436 if (verbose > 0 && !bufferQueue->prevPacketWasSynchronized) {
437 fprintf(stderr, "%s stream has been synchronized using RTCP \n",
438 bufferQueue->tag());
441 struct timeval* fst = &(rtpState->firstSyncTime); // abbrev
442 if (fst->tv_sec == 0 && fst->tv_usec == 0) {
443 *fst = presentationTime;
446 // For the "pts" field, use the time differential from the first
447 // synchronized time, rather than absolute time, in order to avoid
448 // round-off errors when converting to a float:
449 dp->pts = presentationTime.tv_sec - fst->tv_sec
450 + (presentationTime.tv_usec - fst->tv_usec)/1000000.0;
451 bufferQueue->prevPacketPTS = dp->pts;
452 } else {
453 if (verbose > 0 && bufferQueue->prevPacketWasSynchronized) {
454 fprintf(stderr, "%s stream is no longer RTCP-synchronized \n",
455 bufferQueue->tag());
458 // use the previous packet's "pts" once again:
459 dp->pts = bufferQueue->prevPacketPTS;
461 bufferQueue->prevPacketWasSynchronized = hasBeenSynchronized;
463 dp->pos = demuxer->filepos;
464 demuxer->filepos += frameSize + headersize;
466 // Signal any pending 'doEventLoop()' call on this queue:
467 bufferQueue->blockingFlag = ~0;
470 static void onSourceClosure(void* clientData) {
471 ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
472 demuxer_t* demuxer = bufferQueue->ourDemuxer();
474 demuxer->stream->eof = 1;
476 // Signal any pending 'doEventLoop()' call on this queue:
477 bufferQueue->blockingFlag = ~0;
480 static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
481 Boolean mustGetNewData,
482 float& ptsBehind) {
483 // Begin by finding the buffer queue that we want to read from:
484 // (Get this from the RTP state, which we stored in
485 // the demuxer's 'priv' field)
486 RTPState* rtpState = (RTPState*)(demuxer->priv);
487 ReadBufferQueue* bufferQueue = NULL;
488 int headersize = 0;
489 TaskToken task;
491 if (demuxer->stream->eof) return NULL;
493 if (ds == demuxer->video) {
494 bufferQueue = rtpState->videoBufferQueue;
495 if (((sh_video_t*)ds->sh)->format == mmioFOURCC('H','2','6','4'))
496 headersize = 3;
497 } else if (ds == demuxer->audio) {
498 bufferQueue = rtpState->audioBufferQueue;
499 if (bufferQueue->readSource()->isAMRAudioSource())
500 headersize = 1;
501 } else {
502 fprintf(stderr, "(demux_rtp)getBuffer: internal error: unknown stream\n");
503 return NULL;
506 if (bufferQueue == NULL || bufferQueue->readSource() == NULL) {
507 fprintf(stderr, "(demux_rtp)getBuffer failed: no appropriate RTP subsession has been set up\n");
508 return NULL;
511 demux_packet_t* dp = NULL;
512 if (!mustGetNewData) {
513 // Check whether we have a previously-saved buffer that we can use:
514 dp = bufferQueue->getPendingBuffer();
515 if (dp != NULL) {
516 ptsBehind = 0.0; // so that we always accept this data
517 return dp;
521 // Allocate a new packet buffer, and arrange to read into it:
522 if (!bufferQueue->nextpacket) {
523 dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
524 bufferQueue->dp = dp;
525 if (dp == NULL) return NULL;
528 #ifdef CONFIG_LIBAVCODEC
529 extern AVCodecParserContext * h264parserctx;
530 int consumed, poutbuf_size = 1;
531 const uint8_t *poutbuf = NULL;
532 float lastpts = 0.0;
534 do {
535 if (!bufferQueue->nextpacket) {
536 #endif
537 // Schedule the read operation:
538 bufferQueue->blockingFlag = 0;
539 bufferQueue->readSource()->getNextFrame(&dp->buffer[headersize], MAX_RTP_FRAME_SIZE - headersize,
540 afterReading, bufferQueue,
541 onSourceClosure, bufferQueue);
542 // Block ourselves until data becomes available:
543 TaskScheduler& scheduler
544 = bufferQueue->readSource()->envir().taskScheduler();
545 int delay = 10000000;
546 if (bufferQueue->prevPacketPTS * 1.05 > rtpState->mediaSession->playEndTime())
547 delay /= 10;
548 task = scheduler.scheduleDelayedTask(delay, onSourceClosure, bufferQueue);
549 scheduler.doEventLoop(&bufferQueue->blockingFlag);
550 scheduler.unscheduleDelayedTask(task);
551 if (demuxer->stream->eof) {
552 free_demux_packet(dp);
553 return NULL;
556 if (headersize == 1) // amr
557 dp->buffer[0] =
558 ((AMRAudioSource*)bufferQueue->readSource())->lastFrameHeader();
559 #ifdef CONFIG_LIBAVCODEC
560 } else {
561 bufferQueue->dp = dp = bufferQueue->nextpacket;
562 bufferQueue->nextpacket = NULL;
564 if (headersize == 3 && h264parserctx) { // h264
565 consumed = h264parserctx->parser->parser_parse(h264parserctx,
566 NULL,
567 &poutbuf, &poutbuf_size,
568 dp->buffer, dp->len);
570 if (!consumed && !poutbuf_size)
571 return NULL;
573 if (!poutbuf_size) {
574 lastpts=dp->pts;
575 free_demux_packet(dp);
576 bufferQueue->dp = dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
577 } else {
578 bufferQueue->nextpacket = dp;
579 bufferQueue->dp = dp = new_demux_packet(poutbuf_size);
580 memcpy(dp->buffer, poutbuf, poutbuf_size);
581 dp->pts=lastpts;
584 } while (!poutbuf_size);
585 #endif
587 // Set the "ptsBehind" result parameter:
588 if (bufferQueue->prevPacketPTS != 0.0
589 && bufferQueue->prevPacketWasSynchronized
590 && *(bufferQueue->otherQueue) != NULL
591 && (*(bufferQueue->otherQueue))->prevPacketPTS != 0.0
592 && (*(bufferQueue->otherQueue))->prevPacketWasSynchronized) {
593 ptsBehind = (*(bufferQueue->otherQueue))->prevPacketPTS
594 - bufferQueue->prevPacketPTS;
595 } else {
596 ptsBehind = 0.0;
599 if (mustGetNewData) {
600 // Save this buffer for future reads:
601 bufferQueue->savePendingBuffer(dp);
604 return dp;
607 static void teardownRTSPorSIPSession(RTPState* rtpState) {
608 MediaSession* mediaSession = rtpState->mediaSession;
609 if (mediaSession == NULL) return;
610 if (rtpState->rtspClient != NULL) {
611 rtpState->rtspClient->teardownMediaSession(*mediaSession);
612 } else if (rtpState->sipClient != NULL) {
613 rtpState->sipClient->sendBYE();
617 ////////// "ReadBuffer" and "ReadBufferQueue" implementation:
619 ReadBufferQueue::ReadBufferQueue(MediaSubsession* subsession,
620 demuxer_t* demuxer, char const* tag)
621 : prevPacketWasSynchronized(False), prevPacketPTS(0.0), otherQueue(NULL),
622 dp(NULL), nextpacket(NULL),
623 pendingDPHead(NULL), pendingDPTail(NULL),
624 fReadSource(subsession == NULL ? NULL : subsession->readSource()),
625 fRTPSource(subsession == NULL ? NULL : subsession->rtpSource()),
626 fOurDemuxer(demuxer), fTag(strdup(tag)) {
629 ReadBufferQueue::~ReadBufferQueue() {
630 delete fTag;
632 // Free any pending buffers (that never got delivered):
633 demux_packet_t* dp = pendingDPHead;
634 while (dp != NULL) {
635 demux_packet_t* dpNext = dp->next;
636 dp->next = NULL;
637 free_demux_packet(dp);
638 dp = dpNext;
642 void ReadBufferQueue::savePendingBuffer(demux_packet_t* dp) {
643 // Keep this buffer around, until MPlayer asks for it later:
644 if (pendingDPTail == NULL) {
645 pendingDPHead = pendingDPTail = dp;
646 } else {
647 pendingDPTail->next = dp;
648 pendingDPTail = dp;
650 dp->next = NULL;
653 demux_packet_t* ReadBufferQueue::getPendingBuffer() {
654 demux_packet_t* dp = pendingDPHead;
655 if (dp != NULL) {
656 pendingDPHead = dp->next;
657 if (pendingDPHead == NULL) pendingDPTail = NULL;
659 dp->next = NULL;
662 return dp;
665 static int demux_rtp_control(struct demuxer *demuxer, int cmd, void *arg) {
666 double endpts = ((RTPState*)demuxer->priv)->mediaSession->playEndTime();
668 switch(cmd) {
669 case DEMUXER_CTRL_GET_TIME_LENGTH:
670 if (endpts <= 0)
671 return DEMUXER_CTRL_DONTKNOW;
672 *((double *)arg) = endpts;
673 return DEMUXER_CTRL_OK;
675 case DEMUXER_CTRL_GET_PERCENT_POS:
676 if (endpts <= 0)
677 return DEMUXER_CTRL_DONTKNOW;
678 *((int *)arg) = (int)(((RTPState*)demuxer->priv)->videoBufferQueue->prevPacketPTS*100/endpts);
679 return DEMUXER_CTRL_OK;
681 default:
682 return DEMUXER_CTRL_NOTIMPL;
686 demuxer_desc_t demuxer_desc_rtp = {
687 "LIVE555 RTP demuxer",
688 "live555",
690 "Ross Finlayson",
691 "requires LIVE555 Streaming Media library",
692 DEMUXER_TYPE_RTP,
693 0, // no autodetect
694 NULL,
695 demux_rtp_fill_buffer,
696 demux_open_rtp,
697 demux_close_rtp,
698 NULL,
699 demux_rtp_control