Do not call rgb2rgbWrapper() for rgb48*, there is no special converter
[mplayer/glamo.git] / libmpdemux / demux_rtp.cpp
blobc59bf5bf7ea587c7708da893e9b7d4b224f5d769
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"
30 #include "demux_rtp_internal.h"
32 #include "BasicUsageEnvironment.hh"
33 #include "liveMedia.hh"
34 #include "GroupsockHelper.hh"
35 #include <unistd.h>
37 // A data structure representing input data for each stream:
38 class ReadBufferQueue {
39 public:
40 ReadBufferQueue(MediaSubsession* subsession, demuxer_t* demuxer,
41 char const* tag);
42 virtual ~ReadBufferQueue();
44 FramedSource* readSource() const { return fReadSource; }
45 RTPSource* rtpSource() const { return fRTPSource; }
46 demuxer_t* ourDemuxer() const { return fOurDemuxer; }
47 char const* tag() const { return fTag; }
49 char blockingFlag; // used to implement synchronous reads
51 // For A/V synchronization:
52 Boolean prevPacketWasSynchronized;
53 float prevPacketPTS;
54 ReadBufferQueue** otherQueue;
56 // The 'queue' actually consists of just a single "demux_packet_t"
57 // (because the underlying OS does the actual queueing/buffering):
58 demux_packet_t* dp;
60 // However, we sometimes inspect buffers before delivering them.
61 // For this, we maintain a queue of pending buffers:
62 void savePendingBuffer(demux_packet_t* dp);
63 demux_packet_t* getPendingBuffer();
65 // For H264 over rtsp using AVParser, the next packet has to be saved
66 demux_packet_t* nextpacket;
68 private:
69 demux_packet_t* pendingDPHead;
70 demux_packet_t* pendingDPTail;
72 FramedSource* fReadSource;
73 RTPSource* fRTPSource;
74 demuxer_t* fOurDemuxer;
75 char const* fTag; // used for debugging
78 // A structure of RTP-specific state, kept so that we can cleanly
79 // reclaim it:
80 typedef struct RTPState {
81 char const* sdpDescription;
82 RTSPClient* rtspClient;
83 SIPClient* sipClient;
84 MediaSession* mediaSession;
85 ReadBufferQueue* audioBufferQueue;
86 ReadBufferQueue* videoBufferQueue;
87 unsigned flags;
88 struct timeval firstSyncTime;
91 extern "C" char* network_username;
92 extern "C" char* network_password;
93 static char* openURL_rtsp(RTSPClient* client, char const* url) {
94 // If we were given a user name (and optional password), then use them:
95 if (network_username != NULL) {
96 char const* password = network_password == NULL ? "" : network_password;
97 return client->describeWithPassword(url, network_username, password);
98 } else {
99 return client->describeURL(url);
103 static char* openURL_sip(SIPClient* client, char const* url) {
104 // If we were given a user name (and optional password), then use them:
105 if (network_username != NULL) {
106 char const* password = network_password == NULL ? "" : network_password;
107 return client->inviteWithPassword(url, network_username, password);
108 } else {
109 return client->invite(url);
113 #ifdef CONFIG_LIBNEMESI
114 extern int rtsp_transport_tcp;
115 #else
116 int rtsp_transport_tcp = 0;
117 #endif
119 extern int rtsp_port;
121 extern "C" int audio_id, video_id, dvdsub_id;
122 extern "C" demuxer_t* demux_open_rtp(demuxer_t* demuxer) {
123 Boolean success = False;
124 do {
125 TaskScheduler* scheduler = BasicTaskScheduler::createNew();
126 if (scheduler == NULL) break;
127 UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
128 if (env == NULL) break;
130 RTSPClient* rtspClient = NULL;
131 SIPClient* sipClient = NULL;
133 if (demuxer == NULL || demuxer->stream == NULL) break; // shouldn't happen
134 demuxer->stream->eof = 0; // just in case
136 // Look at the stream's 'priv' field to see if we were initiated
137 // via a SDP description:
138 char* sdpDescription = (char*)(demuxer->stream->priv);
139 if (sdpDescription == NULL) {
140 // We weren't given a SDP description directly, so assume that
141 // we were given a RTSP or SIP URL:
142 char const* protocol = demuxer->stream->streaming_ctrl->url->protocol;
143 char const* url = demuxer->stream->streaming_ctrl->url->url;
144 extern int verbose;
145 if (strcmp(protocol, "rtsp") == 0) {
146 rtspClient = RTSPClient::createNew(*env, verbose, "MPlayer");
147 if (rtspClient == NULL) {
148 fprintf(stderr, "Failed to create RTSP client: %s\n",
149 env->getResultMsg());
150 break;
152 sdpDescription = openURL_rtsp(rtspClient, url);
153 } else { // SIP
154 unsigned char desiredAudioType = 0; // PCMU (use 3 for GSM)
155 sipClient = SIPClient::createNew(*env, desiredAudioType, NULL,
156 verbose, "MPlayer");
157 if (sipClient == NULL) {
158 fprintf(stderr, "Failed to create SIP client: %s\n",
159 env->getResultMsg());
160 break;
162 sipClient->setClientStartPortNum(8000);
163 sdpDescription = openURL_sip(sipClient, url);
166 if (sdpDescription == NULL) {
167 fprintf(stderr, "Failed to get a SDP description from URL \"%s\": %s\n",
168 url, env->getResultMsg());
169 break;
173 // Now that we have a SDP description, create a MediaSession from it:
174 MediaSession* mediaSession = MediaSession::createNew(*env, sdpDescription);
175 if (mediaSession == NULL) break;
178 // Create a 'RTPState' structure containing the state that we just created,
179 // and store it in the demuxer's 'priv' field, for future reference:
180 RTPState* rtpState = new RTPState;
181 rtpState->sdpDescription = sdpDescription;
182 rtpState->rtspClient = rtspClient;
183 rtpState->sipClient = sipClient;
184 rtpState->mediaSession = mediaSession;
185 rtpState->audioBufferQueue = rtpState->videoBufferQueue = NULL;
186 rtpState->flags = 0;
187 rtpState->firstSyncTime.tv_sec = rtpState->firstSyncTime.tv_usec = 0;
188 demuxer->priv = rtpState;
190 int audiofound = 0, videofound = 0;
191 // Create RTP receivers (sources) for each subsession:
192 MediaSubsessionIterator iter(*mediaSession);
193 MediaSubsession* subsession;
194 unsigned desiredReceiveBufferSize;
195 while ((subsession = iter.next()) != NULL) {
196 // Ignore any subsession that's not audio or video:
197 if (strcmp(subsession->mediumName(), "audio") == 0) {
198 if (audiofound) {
199 fprintf(stderr, "Additional subsession \"audio/%s\" skipped\n", subsession->codecName());
200 continue;
202 desiredReceiveBufferSize = 100000;
203 } else if (strcmp(subsession->mediumName(), "video") == 0) {
204 if (videofound) {
205 fprintf(stderr, "Additional subsession \"video/%s\" skipped\n", subsession->codecName());
206 continue;
208 desiredReceiveBufferSize = 2000000;
209 } else {
210 continue;
213 if (rtsp_port)
214 subsession->setClientPortNum (rtsp_port);
216 if (!subsession->initiate()) {
217 fprintf(stderr, "Failed to initiate \"%s/%s\" RTP subsession: %s\n", subsession->mediumName(), subsession->codecName(), env->getResultMsg());
218 } else {
219 fprintf(stderr, "Initiated \"%s/%s\" RTP subsession on port %d\n", subsession->mediumName(), subsession->codecName(), subsession->clientPortNum());
221 // Set the OS's socket receive buffer sufficiently large to avoid
222 // incoming packets getting dropped between successive reads from this
223 // subsession's demuxer. Depending on the bitrate(s) that you expect,
224 // you may wish to tweak the "desiredReceiveBufferSize" values above.
225 int rtpSocketNum = subsession->rtpSource()->RTPgs()->socketNum();
226 int receiveBufferSize
227 = increaseReceiveBufferTo(*env, rtpSocketNum,
228 desiredReceiveBufferSize);
229 if (verbose > 0) {
230 fprintf(stderr, "Increased %s socket receive buffer to %d bytes \n",
231 subsession->mediumName(), receiveBufferSize);
234 if (rtspClient != NULL) {
235 // Issue a RTSP "SETUP" command on the chosen subsession:
236 if (!rtspClient->setupMediaSubsession(*subsession, False,
237 rtsp_transport_tcp)) break;
238 if (!strcmp(subsession->mediumName(), "audio"))
239 audiofound = 1;
240 if (!strcmp(subsession->mediumName(), "video"))
241 videofound = 1;
246 if (rtspClient != NULL) {
247 // Issue a RTSP aggregate "PLAY" command on the whole session:
248 if (!rtspClient->playMediaSession(*mediaSession)) break;
249 } else if (sipClient != NULL) {
250 sipClient->sendACK(); // to start the stream flowing
253 // Now that the session is ready to be read, do additional
254 // MPlayer codec-specific initialization on each subsession:
255 iter.reset();
256 while ((subsession = iter.next()) != NULL) {
257 if (subsession->readSource() == NULL) continue; // not reading this
259 unsigned flags = 0;
260 if (strcmp(subsession->mediumName(), "audio") == 0) {
261 rtpState->audioBufferQueue
262 = new ReadBufferQueue(subsession, demuxer, "audio");
263 rtpState->audioBufferQueue->otherQueue = &(rtpState->videoBufferQueue);
264 rtpCodecInitialize_audio(demuxer, subsession, flags);
265 } else if (strcmp(subsession->mediumName(), "video") == 0) {
266 rtpState->videoBufferQueue
267 = new ReadBufferQueue(subsession, demuxer, "video");
268 rtpState->videoBufferQueue->otherQueue = &(rtpState->audioBufferQueue);
269 rtpCodecInitialize_video(demuxer, subsession, flags);
271 rtpState->flags |= flags;
273 success = True;
274 } while (0);
275 if (!success) return NULL; // an error occurred
277 // Hack: If audio and video are demuxed together on a single RTP stream,
278 // then create a new "demuxer_t" structure to allow the higher-level
279 // code to recognize this:
280 if (demux_is_multiplexed_rtp_stream(demuxer)) {
281 stream_t* s = new_ds_stream(demuxer->video);
282 demuxer_t* od = demux_open(s, DEMUXER_TYPE_UNKNOWN,
283 audio_id, video_id, dvdsub_id, NULL);
284 demuxer = new_demuxers_demuxer(od, od, od);
287 return demuxer;
290 extern "C" int demux_is_mpeg_rtp_stream(demuxer_t* demuxer) {
291 // Get the RTP state that was stored in the demuxer's 'priv' field:
292 RTPState* rtpState = (RTPState*)(demuxer->priv);
294 return (rtpState->flags&RTPSTATE_IS_MPEG12_VIDEO) != 0;
297 extern "C" int demux_is_multiplexed_rtp_stream(demuxer_t* demuxer) {
298 // Get the RTP state that was stored in the demuxer's 'priv' field:
299 RTPState* rtpState = (RTPState*)(demuxer->priv);
301 return (rtpState->flags&RTPSTATE_IS_MULTIPLEXED) != 0;
304 static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
305 Boolean mustGetNewData,
306 float& ptsBehind); // forward
308 extern "C" int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds) {
309 // Get a filled-in "demux_packet" from the RTP source, and deliver it.
310 // Note that this is called as a synchronous read operation, so it needs
311 // to block in the (hopefully infrequent) case where no packet is
312 // immediately available.
314 while (1) {
315 float ptsBehind;
316 demux_packet_t* dp = getBuffer(demuxer, ds, False, ptsBehind); // blocking
317 if (dp == NULL) return 0;
319 if (demuxer->stream->eof) return 0; // source stream has closed down
321 // Before using this packet, check to make sure that its presentation
322 // time is not far behind the other stream (if any). If it is,
323 // then we discard this packet, and get another instead. (The rest of
324 // MPlayer doesn't always do a good job of synchronizing when the
325 // audio and video streams get this far apart.)
326 // (We don't do this when streaming over TCP, because then the audio and
327 // video streams are interleaved.)
328 // (Also, if the stream is *excessively* far behind, then we allow
329 // the packet, because in this case it probably means that there was
330 // an error in the source's timestamp synchronization.)
331 const float ptsBehindThreshold = 1.0; // seconds
332 const float ptsBehindLimit = 60.0; // seconds
333 if (ptsBehind < ptsBehindThreshold ||
334 ptsBehind > ptsBehindLimit ||
335 rtsp_transport_tcp) { // packet's OK
336 ds_add_packet(ds, dp);
337 break;
340 #ifdef DEBUG_PRINT_DISCARDED_PACKETS
341 RTPState* rtpState = (RTPState*)(demuxer->priv);
342 ReadBufferQueue* bufferQueue = ds == demuxer->video ? rtpState->videoBufferQueue : rtpState->audioBufferQueue;
343 fprintf(stderr, "Discarding %s packet (%fs behind)\n", bufferQueue->tag(), ptsBehind);
344 #endif
345 free_demux_packet(dp); // give back this packet, and get another one
348 return 1;
351 Boolean awaitRTPPacket(demuxer_t* demuxer, demux_stream_t* ds,
352 unsigned char*& packetData, unsigned& packetDataLen,
353 float& pts) {
354 // Similar to "demux_rtp_fill_buffer()", except that the "demux_packet"
355 // is not delivered to the "demux_stream".
356 float ptsBehind;
357 demux_packet_t* dp = getBuffer(demuxer, ds, True, ptsBehind); // blocking
358 if (dp == NULL) return False;
360 packetData = dp->buffer;
361 packetDataLen = dp->len;
362 pts = dp->pts;
364 return True;
367 static void teardownRTSPorSIPSession(RTPState* rtpState); // forward
369 extern "C" void demux_close_rtp(demuxer_t* demuxer) {
370 // Reclaim all RTP-related state:
372 // Get the RTP state that was stored in the demuxer's 'priv' field:
373 RTPState* rtpState = (RTPState*)(demuxer->priv);
374 if (rtpState == NULL) return;
376 teardownRTSPorSIPSession(rtpState);
378 UsageEnvironment* env = NULL;
379 TaskScheduler* scheduler = NULL;
380 if (rtpState->mediaSession != NULL) {
381 env = &(rtpState->mediaSession->envir());
382 scheduler = &(env->taskScheduler());
384 Medium::close(rtpState->mediaSession);
385 Medium::close(rtpState->rtspClient);
386 Medium::close(rtpState->sipClient);
387 delete rtpState->audioBufferQueue;
388 delete rtpState->videoBufferQueue;
389 delete rtpState->sdpDescription;
390 delete rtpState;
392 env->reclaim(); delete scheduler;
395 ////////// Extra routines that help implement the above interface functions:
397 #define MAX_RTP_FRAME_SIZE 5000000
398 // >= the largest conceivable frame composed from one or more RTP packets
400 static void afterReading(void* clientData, unsigned frameSize,
401 unsigned /*numTruncatedBytes*/,
402 struct timeval presentationTime,
403 unsigned /*durationInMicroseconds*/) {
404 int headersize = 0;
405 if (frameSize >= MAX_RTP_FRAME_SIZE) {
406 fprintf(stderr, "Saw an input frame too large (>=%d). Increase MAX_RTP_FRAME_SIZE in \"demux_rtp.cpp\".\n",
407 MAX_RTP_FRAME_SIZE);
409 ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
410 demuxer_t* demuxer = bufferQueue->ourDemuxer();
411 RTPState* rtpState = (RTPState*)(demuxer->priv);
413 if (frameSize > 0) demuxer->stream->eof = 0;
415 demux_packet_t* dp = bufferQueue->dp;
417 if (bufferQueue->readSource()->isAMRAudioSource())
418 headersize = 1;
419 else if (bufferQueue == rtpState->videoBufferQueue &&
420 ((sh_video_t*)demuxer->video->sh)->format == mmioFOURCC('H','2','6','4')) {
421 dp->buffer[0]=0x00;
422 dp->buffer[1]=0x00;
423 dp->buffer[2]=0x01;
424 headersize = 3;
427 resize_demux_packet(dp, frameSize + headersize);
429 // Set the packet's presentation time stamp, depending on whether or
430 // not our RTP source's timestamps have been synchronized yet:
431 Boolean hasBeenSynchronized
432 = bufferQueue->rtpSource()->hasBeenSynchronizedUsingRTCP();
433 if (hasBeenSynchronized) {
434 if (verbose > 0 && !bufferQueue->prevPacketWasSynchronized) {
435 fprintf(stderr, "%s stream has been synchronized using RTCP \n",
436 bufferQueue->tag());
439 struct timeval* fst = &(rtpState->firstSyncTime); // abbrev
440 if (fst->tv_sec == 0 && fst->tv_usec == 0) {
441 *fst = presentationTime;
444 // For the "pts" field, use the time differential from the first
445 // synchronized time, rather than absolute time, in order to avoid
446 // round-off errors when converting to a float:
447 dp->pts = presentationTime.tv_sec - fst->tv_sec
448 + (presentationTime.tv_usec - fst->tv_usec)/1000000.0;
449 bufferQueue->prevPacketPTS = dp->pts;
450 } else {
451 if (verbose > 0 && bufferQueue->prevPacketWasSynchronized) {
452 fprintf(stderr, "%s stream is no longer RTCP-synchronized \n",
453 bufferQueue->tag());
456 // use the previous packet's "pts" once again:
457 dp->pts = bufferQueue->prevPacketPTS;
459 bufferQueue->prevPacketWasSynchronized = hasBeenSynchronized;
461 dp->pos = demuxer->filepos;
462 demuxer->filepos += frameSize + headersize;
464 // Signal any pending 'doEventLoop()' call on this queue:
465 bufferQueue->blockingFlag = ~0;
468 static void onSourceClosure(void* clientData) {
469 ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
470 demuxer_t* demuxer = bufferQueue->ourDemuxer();
472 demuxer->stream->eof = 1;
474 // Signal any pending 'doEventLoop()' call on this queue:
475 bufferQueue->blockingFlag = ~0;
478 static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
479 Boolean mustGetNewData,
480 float& ptsBehind) {
481 // Begin by finding the buffer queue that we want to read from:
482 // (Get this from the RTP state, which we stored in
483 // the demuxer's 'priv' field)
484 RTPState* rtpState = (RTPState*)(demuxer->priv);
485 ReadBufferQueue* bufferQueue = NULL;
486 int headersize = 0;
487 TaskToken task;
489 if (demuxer->stream->eof) return NULL;
491 if (ds == demuxer->video) {
492 bufferQueue = rtpState->videoBufferQueue;
493 if (((sh_video_t*)ds->sh)->format == mmioFOURCC('H','2','6','4'))
494 headersize = 3;
495 } else if (ds == demuxer->audio) {
496 bufferQueue = rtpState->audioBufferQueue;
497 if (bufferQueue->readSource()->isAMRAudioSource())
498 headersize = 1;
499 } else {
500 fprintf(stderr, "(demux_rtp)getBuffer: internal error: unknown stream\n");
501 return NULL;
504 if (bufferQueue == NULL || bufferQueue->readSource() == NULL) {
505 fprintf(stderr, "(demux_rtp)getBuffer failed: no appropriate RTP subsession has been set up\n");
506 return NULL;
509 demux_packet_t* dp = NULL;
510 if (!mustGetNewData) {
511 // Check whether we have a previously-saved buffer that we can use:
512 dp = bufferQueue->getPendingBuffer();
513 if (dp != NULL) {
514 ptsBehind = 0.0; // so that we always accept this data
515 return dp;
519 // Allocate a new packet buffer, and arrange to read into it:
520 if (!bufferQueue->nextpacket) {
521 dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
522 bufferQueue->dp = dp;
523 if (dp == NULL) return NULL;
526 #ifdef CONFIG_LIBAVCODEC
527 extern AVCodecParserContext * h264parserctx;
528 int consumed, poutbuf_size = 1;
529 const uint8_t *poutbuf = NULL;
530 float lastpts = 0.0;
532 do {
533 if (!bufferQueue->nextpacket) {
534 #endif
535 // Schedule the read operation:
536 bufferQueue->blockingFlag = 0;
537 bufferQueue->readSource()->getNextFrame(&dp->buffer[headersize], MAX_RTP_FRAME_SIZE - headersize,
538 afterReading, bufferQueue,
539 onSourceClosure, bufferQueue);
540 // Block ourselves until data becomes available:
541 TaskScheduler& scheduler
542 = bufferQueue->readSource()->envir().taskScheduler();
543 int delay = 10000000;
544 if (bufferQueue->prevPacketPTS * 1.05 > rtpState->mediaSession->playEndTime())
545 delay /= 10;
546 task = scheduler.scheduleDelayedTask(delay, onSourceClosure, bufferQueue);
547 scheduler.doEventLoop(&bufferQueue->blockingFlag);
548 scheduler.unscheduleDelayedTask(task);
549 if (demuxer->stream->eof) {
550 free_demux_packet(dp);
551 return NULL;
554 if (headersize == 1) // amr
555 dp->buffer[0] =
556 ((AMRAudioSource*)bufferQueue->readSource())->lastFrameHeader();
557 #ifdef CONFIG_LIBAVCODEC
558 } else {
559 bufferQueue->dp = dp = bufferQueue->nextpacket;
560 bufferQueue->nextpacket = NULL;
562 if (headersize == 3 && h264parserctx) { // h264
563 consumed = h264parserctx->parser->parser_parse(h264parserctx,
564 NULL,
565 &poutbuf, &poutbuf_size,
566 dp->buffer, dp->len);
568 if (!consumed && !poutbuf_size)
569 return NULL;
571 if (!poutbuf_size) {
572 lastpts=dp->pts;
573 free_demux_packet(dp);
574 bufferQueue->dp = dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
575 } else {
576 bufferQueue->nextpacket = dp;
577 bufferQueue->dp = dp = new_demux_packet(poutbuf_size);
578 memcpy(dp->buffer, poutbuf, poutbuf_size);
579 dp->pts=lastpts;
582 } while (!poutbuf_size);
583 #endif
585 // Set the "ptsBehind" result parameter:
586 if (bufferQueue->prevPacketPTS != 0.0
587 && bufferQueue->prevPacketWasSynchronized
588 && *(bufferQueue->otherQueue) != NULL
589 && (*(bufferQueue->otherQueue))->prevPacketPTS != 0.0
590 && (*(bufferQueue->otherQueue))->prevPacketWasSynchronized) {
591 ptsBehind = (*(bufferQueue->otherQueue))->prevPacketPTS
592 - bufferQueue->prevPacketPTS;
593 } else {
594 ptsBehind = 0.0;
597 if (mustGetNewData) {
598 // Save this buffer for future reads:
599 bufferQueue->savePendingBuffer(dp);
602 return dp;
605 static void teardownRTSPorSIPSession(RTPState* rtpState) {
606 MediaSession* mediaSession = rtpState->mediaSession;
607 if (mediaSession == NULL) return;
608 if (rtpState->rtspClient != NULL) {
609 rtpState->rtspClient->teardownMediaSession(*mediaSession);
610 } else if (rtpState->sipClient != NULL) {
611 rtpState->sipClient->sendBYE();
615 ////////// "ReadBuffer" and "ReadBufferQueue" implementation:
617 ReadBufferQueue::ReadBufferQueue(MediaSubsession* subsession,
618 demuxer_t* demuxer, char const* tag)
619 : prevPacketWasSynchronized(False), prevPacketPTS(0.0), otherQueue(NULL),
620 dp(NULL), nextpacket(NULL),
621 pendingDPHead(NULL), pendingDPTail(NULL),
622 fReadSource(subsession == NULL ? NULL : subsession->readSource()),
623 fRTPSource(subsession == NULL ? NULL : subsession->rtpSource()),
624 fOurDemuxer(demuxer), fTag(strdup(tag)) {
627 ReadBufferQueue::~ReadBufferQueue() {
628 delete fTag;
630 // Free any pending buffers (that never got delivered):
631 demux_packet_t* dp = pendingDPHead;
632 while (dp != NULL) {
633 demux_packet_t* dpNext = dp->next;
634 dp->next = NULL;
635 free_demux_packet(dp);
636 dp = dpNext;
640 void ReadBufferQueue::savePendingBuffer(demux_packet_t* dp) {
641 // Keep this buffer around, until MPlayer asks for it later:
642 if (pendingDPTail == NULL) {
643 pendingDPHead = pendingDPTail = dp;
644 } else {
645 pendingDPTail->next = dp;
646 pendingDPTail = dp;
648 dp->next = NULL;
651 demux_packet_t* ReadBufferQueue::getPendingBuffer() {
652 demux_packet_t* dp = pendingDPHead;
653 if (dp != NULL) {
654 pendingDPHead = dp->next;
655 if (pendingDPHead == NULL) pendingDPTail = NULL;
657 dp->next = NULL;
660 return dp;
663 static int demux_rtp_control(struct demuxer_st *demuxer, int cmd, void *arg) {
664 double endpts = ((RTPState*)demuxer->priv)->mediaSession->playEndTime();
666 switch(cmd) {
667 case DEMUXER_CTRL_GET_TIME_LENGTH:
668 if (endpts <= 0)
669 return DEMUXER_CTRL_DONTKNOW;
670 *((double *)arg) = endpts;
671 return DEMUXER_CTRL_OK;
673 case DEMUXER_CTRL_GET_PERCENT_POS:
674 if (endpts <= 0)
675 return DEMUXER_CTRL_DONTKNOW;
676 *((int *)arg) = (int)(((RTPState*)demuxer->priv)->videoBufferQueue->prevPacketPTS*100/endpts);
677 return DEMUXER_CTRL_OK;
679 default:
680 return DEMUXER_CTRL_NOTIMPL;
684 demuxer_desc_t demuxer_desc_rtp = {
685 "LIVE555 RTP demuxer",
686 "live555",
688 "Ross Finlayson",
689 "requires LIVE555 Streaming Media library",
690 DEMUXER_TYPE_RTP,
691 0, // no autodetect
692 NULL,
693 demux_rtp_fill_buffer,
694 demux_open_rtp,
695 demux_close_rtp,
696 NULL,
697 demux_rtp_control