Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / libmpdemux / demux_rtp.cpp
blob52af89b0ee1a28cbbd130c2eab853b3c2b22756f
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;
120 #ifdef CONFIG_LIBAVCODEC
121 extern AVCodecContext *avcctx;
122 #endif
124 extern "C" int audio_id, video_id, dvdsub_id;
125 extern "C" demuxer_t* demux_open_rtp(demuxer_t* demuxer) {
126 Boolean success = False;
127 do {
128 TaskScheduler* scheduler = BasicTaskScheduler::createNew();
129 if (scheduler == NULL) break;
130 UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
131 if (env == NULL) break;
133 RTSPClient* rtspClient = NULL;
134 SIPClient* sipClient = NULL;
136 if (demuxer == NULL || demuxer->stream == NULL) break; // shouldn't happen
137 demuxer->stream->eof = 0; // just in case
139 // Look at the stream's 'priv' field to see if we were initiated
140 // via a SDP description:
141 char* sdpDescription = (char*)(demuxer->stream->priv);
142 if (sdpDescription == NULL) {
143 // We weren't given a SDP description directly, so assume that
144 // we were given a RTSP or SIP URL:
145 char const* protocol = demuxer->stream->streaming_ctrl->url->protocol;
146 char const* url = demuxer->stream->streaming_ctrl->url->url;
147 extern int verbose;
148 if (strcmp(protocol, "rtsp") == 0) {
149 rtspClient = RTSPClient::createNew(*env, verbose, "MPlayer");
150 if (rtspClient == NULL) {
151 fprintf(stderr, "Failed to create RTSP client: %s\n",
152 env->getResultMsg());
153 break;
155 sdpDescription = openURL_rtsp(rtspClient, url);
156 } else { // SIP
157 unsigned char desiredAudioType = 0; // PCMU (use 3 for GSM)
158 sipClient = SIPClient::createNew(*env, desiredAudioType, NULL,
159 verbose, "MPlayer");
160 if (sipClient == NULL) {
161 fprintf(stderr, "Failed to create SIP client: %s\n",
162 env->getResultMsg());
163 break;
165 sipClient->setClientStartPortNum(8000);
166 sdpDescription = openURL_sip(sipClient, url);
169 if (sdpDescription == NULL) {
170 fprintf(stderr, "Failed to get a SDP description from URL \"%s\": %s\n",
171 url, env->getResultMsg());
172 break;
176 // Now that we have a SDP description, create a MediaSession from it:
177 MediaSession* mediaSession = MediaSession::createNew(*env, sdpDescription);
178 if (mediaSession == NULL) break;
181 // Create a 'RTPState' structure containing the state that we just created,
182 // and store it in the demuxer's 'priv' field, for future reference:
183 RTPState* rtpState = new RTPState;
184 rtpState->sdpDescription = sdpDescription;
185 rtpState->rtspClient = rtspClient;
186 rtpState->sipClient = sipClient;
187 rtpState->mediaSession = mediaSession;
188 rtpState->audioBufferQueue = rtpState->videoBufferQueue = NULL;
189 rtpState->flags = 0;
190 rtpState->firstSyncTime.tv_sec = rtpState->firstSyncTime.tv_usec = 0;
191 demuxer->priv = rtpState;
193 int audiofound = 0, videofound = 0;
194 // Create RTP receivers (sources) for each subsession:
195 MediaSubsessionIterator iter(*mediaSession);
196 MediaSubsession* subsession;
197 unsigned desiredReceiveBufferSize;
198 while ((subsession = iter.next()) != NULL) {
199 // Ignore any subsession that's not audio or video:
200 if (strcmp(subsession->mediumName(), "audio") == 0) {
201 if (audiofound) {
202 fprintf(stderr, "Additional subsession \"audio/%s\" skipped\n", subsession->codecName());
203 continue;
205 desiredReceiveBufferSize = 100000;
206 } else if (strcmp(subsession->mediumName(), "video") == 0) {
207 if (videofound) {
208 fprintf(stderr, "Additional subsession \"video/%s\" skipped\n", subsession->codecName());
209 continue;
211 desiredReceiveBufferSize = 2000000;
212 } else {
213 continue;
216 if (rtsp_port)
217 subsession->setClientPortNum (rtsp_port);
219 if (!subsession->initiate()) {
220 fprintf(stderr, "Failed to initiate \"%s/%s\" RTP subsession: %s\n", subsession->mediumName(), subsession->codecName(), env->getResultMsg());
221 } else {
222 fprintf(stderr, "Initiated \"%s/%s\" RTP subsession on port %d\n", subsession->mediumName(), subsession->codecName(), subsession->clientPortNum());
224 // Set the OS's socket receive buffer sufficiently large to avoid
225 // incoming packets getting dropped between successive reads from this
226 // subsession's demuxer. Depending on the bitrate(s) that you expect,
227 // you may wish to tweak the "desiredReceiveBufferSize" values above.
228 int rtpSocketNum = subsession->rtpSource()->RTPgs()->socketNum();
229 int receiveBufferSize
230 = increaseReceiveBufferTo(*env, rtpSocketNum,
231 desiredReceiveBufferSize);
232 if (verbose > 0) {
233 fprintf(stderr, "Increased %s socket receive buffer to %d bytes \n",
234 subsession->mediumName(), receiveBufferSize);
237 if (rtspClient != NULL) {
238 // Issue a RTSP "SETUP" command on the chosen subsession:
239 if (!rtspClient->setupMediaSubsession(*subsession, False,
240 rtsp_transport_tcp)) break;
241 if (!strcmp(subsession->mediumName(), "audio"))
242 audiofound = 1;
243 if (!strcmp(subsession->mediumName(), "video"))
244 videofound = 1;
249 if (rtspClient != NULL) {
250 // Issue a RTSP aggregate "PLAY" command on the whole session:
251 if (!rtspClient->playMediaSession(*mediaSession)) break;
252 } else if (sipClient != NULL) {
253 sipClient->sendACK(); // to start the stream flowing
256 // Now that the session is ready to be read, do additional
257 // MPlayer codec-specific initialization on each subsession:
258 iter.reset();
259 while ((subsession = iter.next()) != NULL) {
260 if (subsession->readSource() == NULL) continue; // not reading this
262 unsigned flags = 0;
263 if (strcmp(subsession->mediumName(), "audio") == 0) {
264 rtpState->audioBufferQueue
265 = new ReadBufferQueue(subsession, demuxer, "audio");
266 rtpState->audioBufferQueue->otherQueue = &(rtpState->videoBufferQueue);
267 rtpCodecInitialize_audio(demuxer, subsession, flags);
268 } else if (strcmp(subsession->mediumName(), "video") == 0) {
269 rtpState->videoBufferQueue
270 = new ReadBufferQueue(subsession, demuxer, "video");
271 rtpState->videoBufferQueue->otherQueue = &(rtpState->audioBufferQueue);
272 rtpCodecInitialize_video(demuxer, subsession, flags);
274 rtpState->flags |= flags;
276 success = True;
277 } while (0);
278 if (!success) return NULL; // an error occurred
280 // Hack: If audio and video are demuxed together on a single RTP stream,
281 // then create a new "demuxer_t" structure to allow the higher-level
282 // code to recognize this:
283 if (demux_is_multiplexed_rtp_stream(demuxer)) {
284 stream_t* s = new_ds_stream(demuxer->video);
285 demuxer_t* od = demux_open(s, DEMUXER_TYPE_UNKNOWN,
286 audio_id, video_id, dvdsub_id, NULL);
287 demuxer = new_demuxers_demuxer(od, od, od);
290 return demuxer;
293 extern "C" int demux_is_mpeg_rtp_stream(demuxer_t* demuxer) {
294 // Get the RTP state that was stored in the demuxer's 'priv' field:
295 RTPState* rtpState = (RTPState*)(demuxer->priv);
297 return (rtpState->flags&RTPSTATE_IS_MPEG12_VIDEO) != 0;
300 extern "C" int demux_is_multiplexed_rtp_stream(demuxer_t* demuxer) {
301 // Get the RTP state that was stored in the demuxer's 'priv' field:
302 RTPState* rtpState = (RTPState*)(demuxer->priv);
304 return (rtpState->flags&RTPSTATE_IS_MULTIPLEXED) != 0;
307 static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
308 Boolean mustGetNewData,
309 float& ptsBehind); // forward
311 extern "C" int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds) {
312 // Get a filled-in "demux_packet" from the RTP source, and deliver it.
313 // Note that this is called as a synchronous read operation, so it needs
314 // to block in the (hopefully infrequent) case where no packet is
315 // immediately available.
317 while (1) {
318 float ptsBehind;
319 demux_packet_t* dp = getBuffer(demuxer, ds, False, ptsBehind); // blocking
320 if (dp == NULL) return 0;
322 if (demuxer->stream->eof) return 0; // source stream has closed down
324 // Before using this packet, check to make sure that its presentation
325 // time is not far behind the other stream (if any). If it is,
326 // then we discard this packet, and get another instead. (The rest of
327 // MPlayer doesn't always do a good job of synchronizing when the
328 // audio and video streams get this far apart.)
329 // (We don't do this when streaming over TCP, because then the audio and
330 // video streams are interleaved.)
331 // (Also, if the stream is *excessively* far behind, then we allow
332 // the packet, because in this case it probably means that there was
333 // an error in the source's timestamp synchronization.)
334 const float ptsBehindThreshold = 1.0; // seconds
335 const float ptsBehindLimit = 60.0; // seconds
336 if (ptsBehind < ptsBehindThreshold ||
337 ptsBehind > ptsBehindLimit ||
338 rtsp_transport_tcp) { // packet's OK
339 ds_add_packet(ds, dp);
340 break;
343 #ifdef DEBUG_PRINT_DISCARDED_PACKETS
344 RTPState* rtpState = (RTPState*)(demuxer->priv);
345 ReadBufferQueue* bufferQueue = ds == demuxer->video ? rtpState->videoBufferQueue : rtpState->audioBufferQueue;
346 fprintf(stderr, "Discarding %s packet (%fs behind)\n", bufferQueue->tag(), ptsBehind);
347 #endif
348 free_demux_packet(dp); // give back this packet, and get another one
351 return 1;
354 Boolean awaitRTPPacket(demuxer_t* demuxer, demux_stream_t* ds,
355 unsigned char*& packetData, unsigned& packetDataLen,
356 float& pts) {
357 // Similar to "demux_rtp_fill_buffer()", except that the "demux_packet"
358 // is not delivered to the "demux_stream".
359 float ptsBehind;
360 demux_packet_t* dp = getBuffer(demuxer, ds, True, ptsBehind); // blocking
361 if (dp == NULL) return False;
363 packetData = dp->buffer;
364 packetDataLen = dp->len;
365 pts = dp->pts;
367 return True;
370 static void teardownRTSPorSIPSession(RTPState* rtpState); // forward
372 extern "C" void demux_close_rtp(demuxer_t* demuxer) {
373 // Reclaim all RTP-related state:
375 // Get the RTP state that was stored in the demuxer's 'priv' field:
376 RTPState* rtpState = (RTPState*)(demuxer->priv);
377 if (rtpState == NULL) return;
379 teardownRTSPorSIPSession(rtpState);
381 UsageEnvironment* env = NULL;
382 TaskScheduler* scheduler = NULL;
383 if (rtpState->mediaSession != NULL) {
384 env = &(rtpState->mediaSession->envir());
385 scheduler = &(env->taskScheduler());
387 Medium::close(rtpState->mediaSession);
388 Medium::close(rtpState->rtspClient);
389 Medium::close(rtpState->sipClient);
390 delete rtpState->audioBufferQueue;
391 delete rtpState->videoBufferQueue;
392 delete[] rtpState->sdpDescription;
393 delete rtpState;
394 #ifdef CONFIG_LIBAVCODEC
395 av_freep(&avcctx);
396 #endif
398 env->reclaim(); delete scheduler;
401 ////////// Extra routines that help implement the above interface functions:
403 #define MAX_RTP_FRAME_SIZE 5000000
404 // >= the largest conceivable frame composed from one or more RTP packets
406 static void afterReading(void* clientData, unsigned frameSize,
407 unsigned /*numTruncatedBytes*/,
408 struct timeval presentationTime,
409 unsigned /*durationInMicroseconds*/) {
410 int headersize = 0;
411 if (frameSize >= MAX_RTP_FRAME_SIZE) {
412 fprintf(stderr, "Saw an input frame too large (>=%d). Increase MAX_RTP_FRAME_SIZE in \"demux_rtp.cpp\".\n",
413 MAX_RTP_FRAME_SIZE);
415 ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
416 demuxer_t* demuxer = bufferQueue->ourDemuxer();
417 RTPState* rtpState = (RTPState*)(demuxer->priv);
419 if (frameSize > 0) demuxer->stream->eof = 0;
421 demux_packet_t* dp = bufferQueue->dp;
423 if (bufferQueue->readSource()->isAMRAudioSource())
424 headersize = 1;
425 else if (bufferQueue == rtpState->videoBufferQueue &&
426 ((sh_video_t*)demuxer->video->sh)->format == mmioFOURCC('H','2','6','4')) {
427 dp->buffer[0]=0x00;
428 dp->buffer[1]=0x00;
429 dp->buffer[2]=0x01;
430 headersize = 3;
433 resize_demux_packet(dp, frameSize + headersize);
435 // Set the packet's presentation time stamp, depending on whether or
436 // not our RTP source's timestamps have been synchronized yet:
437 Boolean hasBeenSynchronized
438 = bufferQueue->rtpSource()->hasBeenSynchronizedUsingRTCP();
439 if (hasBeenSynchronized) {
440 if (verbose > 0 && !bufferQueue->prevPacketWasSynchronized) {
441 fprintf(stderr, "%s stream has been synchronized using RTCP \n",
442 bufferQueue->tag());
445 struct timeval* fst = &(rtpState->firstSyncTime); // abbrev
446 if (fst->tv_sec == 0 && fst->tv_usec == 0) {
447 *fst = presentationTime;
450 // For the "pts" field, use the time differential from the first
451 // synchronized time, rather than absolute time, in order to avoid
452 // round-off errors when converting to a float:
453 dp->pts = presentationTime.tv_sec - fst->tv_sec
454 + (presentationTime.tv_usec - fst->tv_usec)/1000000.0;
455 bufferQueue->prevPacketPTS = dp->pts;
456 } else {
457 if (verbose > 0 && bufferQueue->prevPacketWasSynchronized) {
458 fprintf(stderr, "%s stream is no longer RTCP-synchronized \n",
459 bufferQueue->tag());
462 // use the previous packet's "pts" once again:
463 dp->pts = bufferQueue->prevPacketPTS;
465 bufferQueue->prevPacketWasSynchronized = hasBeenSynchronized;
467 dp->pos = demuxer->filepos;
468 demuxer->filepos += frameSize + headersize;
470 // Signal any pending 'doEventLoop()' call on this queue:
471 bufferQueue->blockingFlag = ~0;
474 static void onSourceClosure(void* clientData) {
475 ReadBufferQueue* bufferQueue = (ReadBufferQueue*)clientData;
476 demuxer_t* demuxer = bufferQueue->ourDemuxer();
478 demuxer->stream->eof = 1;
480 // Signal any pending 'doEventLoop()' call on this queue:
481 bufferQueue->blockingFlag = ~0;
484 static demux_packet_t* getBuffer(demuxer_t* demuxer, demux_stream_t* ds,
485 Boolean mustGetNewData,
486 float& ptsBehind) {
487 // Begin by finding the buffer queue that we want to read from:
488 // (Get this from the RTP state, which we stored in
489 // the demuxer's 'priv' field)
490 RTPState* rtpState = (RTPState*)(demuxer->priv);
491 ReadBufferQueue* bufferQueue = NULL;
492 int headersize = 0;
493 TaskToken task;
495 if (demuxer->stream->eof) return NULL;
497 if (ds == demuxer->video) {
498 bufferQueue = rtpState->videoBufferQueue;
499 if (((sh_video_t*)ds->sh)->format == mmioFOURCC('H','2','6','4'))
500 headersize = 3;
501 } else if (ds == demuxer->audio) {
502 bufferQueue = rtpState->audioBufferQueue;
503 if (bufferQueue->readSource()->isAMRAudioSource())
504 headersize = 1;
505 } else {
506 fprintf(stderr, "(demux_rtp)getBuffer: internal error: unknown stream\n");
507 return NULL;
510 if (bufferQueue == NULL || bufferQueue->readSource() == NULL) {
511 fprintf(stderr, "(demux_rtp)getBuffer failed: no appropriate RTP subsession has been set up\n");
512 return NULL;
515 demux_packet_t* dp = NULL;
516 if (!mustGetNewData) {
517 // Check whether we have a previously-saved buffer that we can use:
518 dp = bufferQueue->getPendingBuffer();
519 if (dp != NULL) {
520 ptsBehind = 0.0; // so that we always accept this data
521 return dp;
525 // Allocate a new packet buffer, and arrange to read into it:
526 if (!bufferQueue->nextpacket) {
527 dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
528 bufferQueue->dp = dp;
529 if (dp == NULL) return NULL;
532 #ifdef CONFIG_LIBAVCODEC
533 extern AVCodecParserContext * h264parserctx;
534 int consumed, poutbuf_size = 1;
535 const uint8_t *poutbuf = NULL;
536 float lastpts = 0.0;
538 do {
539 if (!bufferQueue->nextpacket) {
540 #endif
541 // Schedule the read operation:
542 bufferQueue->blockingFlag = 0;
543 bufferQueue->readSource()->getNextFrame(&dp->buffer[headersize], MAX_RTP_FRAME_SIZE - headersize,
544 afterReading, bufferQueue,
545 onSourceClosure, bufferQueue);
546 // Block ourselves until data becomes available:
547 TaskScheduler& scheduler
548 = bufferQueue->readSource()->envir().taskScheduler();
549 int delay = 10000000;
550 if (bufferQueue->prevPacketPTS * 1.05 > rtpState->mediaSession->playEndTime())
551 delay /= 10;
552 task = scheduler.scheduleDelayedTask(delay, onSourceClosure, bufferQueue);
553 scheduler.doEventLoop(&bufferQueue->blockingFlag);
554 scheduler.unscheduleDelayedTask(task);
555 if (demuxer->stream->eof) {
556 free_demux_packet(dp);
557 return NULL;
560 if (headersize == 1) // amr
561 dp->buffer[0] =
562 ((AMRAudioSource*)bufferQueue->readSource())->lastFrameHeader();
563 #ifdef CONFIG_LIBAVCODEC
564 } else {
565 bufferQueue->dp = dp = bufferQueue->nextpacket;
566 bufferQueue->nextpacket = NULL;
568 if (headersize == 3 && h264parserctx) { // h264
569 consumed = h264parserctx->parser->parser_parse(h264parserctx,
570 avcctx,
571 &poutbuf, &poutbuf_size,
572 dp->buffer, dp->len);
574 if (!consumed && !poutbuf_size)
575 return NULL;
577 if (!poutbuf_size) {
578 lastpts=dp->pts;
579 free_demux_packet(dp);
580 bufferQueue->dp = dp = new_demux_packet(MAX_RTP_FRAME_SIZE);
581 } else {
582 bufferQueue->nextpacket = dp;
583 bufferQueue->dp = dp = new_demux_packet(poutbuf_size);
584 memcpy(dp->buffer, poutbuf, poutbuf_size);
585 dp->pts=lastpts;
588 } while (!poutbuf_size);
589 #endif
591 // Set the "ptsBehind" result parameter:
592 if (bufferQueue->prevPacketPTS != 0.0
593 && bufferQueue->prevPacketWasSynchronized
594 && *(bufferQueue->otherQueue) != NULL
595 && (*(bufferQueue->otherQueue))->prevPacketPTS != 0.0
596 && (*(bufferQueue->otherQueue))->prevPacketWasSynchronized) {
597 ptsBehind = (*(bufferQueue->otherQueue))->prevPacketPTS
598 - bufferQueue->prevPacketPTS;
599 } else {
600 ptsBehind = 0.0;
603 if (mustGetNewData) {
604 // Save this buffer for future reads:
605 bufferQueue->savePendingBuffer(dp);
608 return dp;
611 static void teardownRTSPorSIPSession(RTPState* rtpState) {
612 MediaSession* mediaSession = rtpState->mediaSession;
613 if (mediaSession == NULL) return;
614 if (rtpState->rtspClient != NULL) {
615 rtpState->rtspClient->teardownMediaSession(*mediaSession);
616 } else if (rtpState->sipClient != NULL) {
617 rtpState->sipClient->sendBYE();
621 ////////// "ReadBuffer" and "ReadBufferQueue" implementation:
623 ReadBufferQueue::ReadBufferQueue(MediaSubsession* subsession,
624 demuxer_t* demuxer, char const* tag)
625 : prevPacketWasSynchronized(False), prevPacketPTS(0.0), otherQueue(NULL),
626 dp(NULL), nextpacket(NULL),
627 pendingDPHead(NULL), pendingDPTail(NULL),
628 fReadSource(subsession == NULL ? NULL : subsession->readSource()),
629 fRTPSource(subsession == NULL ? NULL : subsession->rtpSource()),
630 fOurDemuxer(demuxer), fTag(strdup(tag)) {
633 ReadBufferQueue::~ReadBufferQueue() {
634 free((void *)fTag);
636 // Free any pending buffers (that never got delivered):
637 demux_packet_t* dp = pendingDPHead;
638 while (dp != NULL) {
639 demux_packet_t* dpNext = dp->next;
640 dp->next = NULL;
641 free_demux_packet(dp);
642 dp = dpNext;
646 void ReadBufferQueue::savePendingBuffer(demux_packet_t* dp) {
647 // Keep this buffer around, until MPlayer asks for it later:
648 if (pendingDPTail == NULL) {
649 pendingDPHead = pendingDPTail = dp;
650 } else {
651 pendingDPTail->next = dp;
652 pendingDPTail = dp;
654 dp->next = NULL;
657 demux_packet_t* ReadBufferQueue::getPendingBuffer() {
658 demux_packet_t* dp = pendingDPHead;
659 if (dp != NULL) {
660 pendingDPHead = dp->next;
661 if (pendingDPHead == NULL) pendingDPTail = NULL;
663 dp->next = NULL;
666 return dp;
669 static int demux_rtp_control(struct demuxer *demuxer, int cmd, void *arg) {
670 double endpts = ((RTPState*)demuxer->priv)->mediaSession->playEndTime();
672 switch(cmd) {
673 case DEMUXER_CTRL_GET_TIME_LENGTH:
674 if (endpts <= 0)
675 return DEMUXER_CTRL_DONTKNOW;
676 *((double *)arg) = endpts;
677 return DEMUXER_CTRL_OK;
679 case DEMUXER_CTRL_GET_PERCENT_POS:
680 if (endpts <= 0)
681 return DEMUXER_CTRL_DONTKNOW;
682 *((int *)arg) = (int)(((RTPState*)demuxer->priv)->videoBufferQueue->prevPacketPTS*100/endpts);
683 return DEMUXER_CTRL_OK;
685 default:
686 return DEMUXER_CTRL_NOTIMPL;
690 demuxer_desc_t demuxer_desc_rtp = {
691 "LIVE555 RTP demuxer",
692 "live555",
694 "Ross Finlayson",
695 "requires LIVE555 Streaming Media library",
696 DEMUXER_TYPE_RTP,
697 0, // no autodetect
698 NULL,
699 demux_rtp_fill_buffer,
700 demux_open_rtp,
701 demux_close_rtp,
702 NULL,
703 demux_rtp_control