liveMedia/MPEG4ESVideoRTPSink.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2012 Live Networks, Inc.  All rights reserved.
00018 // RTP sink for MPEG-4 Elementary Stream video (RFC 3016)
00019 // Implementation
00020 
00021 #include "MPEG4ESVideoRTPSink.hh"
00022 #include "MPEG4VideoStreamFramer.hh"
00023 #include "MPEG4LATMAudioRTPSource.hh" // for "parseGeneralConfigStr()"
00024 
00025 MPEG4ESVideoRTPSink
00026 ::MPEG4ESVideoRTPSink(UsageEnvironment& env, Groupsock* RTPgs, unsigned char rtpPayloadFormat, u_int32_t rtpTimestampFrequency,
00027                       u_int8_t profileAndLevelIndication, char const* configStr)
00028   : VideoRTPSink(env, RTPgs, rtpPayloadFormat, rtpTimestampFrequency, "MP4V-ES"),
00029     fVOPIsPresent(False), fProfileAndLevelIndication(profileAndLevelIndication), fFmtpSDPLine(NULL) {
00030   fConfigBytes = parseGeneralConfigStr(configStr, fNumConfigBytes);
00031 }
00032 
00033 MPEG4ESVideoRTPSink::~MPEG4ESVideoRTPSink() {
00034   delete[] fFmtpSDPLine;
00035   delete[] fConfigBytes;
00036 }
00037 
00038 MPEG4ESVideoRTPSink*
00039 MPEG4ESVideoRTPSink::createNew(UsageEnvironment& env,
00040                                Groupsock* RTPgs, unsigned char rtpPayloadFormat,
00041                                u_int32_t rtpTimestampFrequency) {
00042   return new MPEG4ESVideoRTPSink(env, RTPgs, rtpPayloadFormat, rtpTimestampFrequency);
00043 }
00044 
00045 MPEG4ESVideoRTPSink*
00046 MPEG4ESVideoRTPSink::createNew(UsageEnvironment& env,
00047                                Groupsock* RTPgs, unsigned char rtpPayloadFormat, u_int32_t rtpTimestampFrequency,
00048                                u_int8_t profileAndLevelIndication, char const* configStr) {
00049   return new MPEG4ESVideoRTPSink(env, RTPgs, rtpPayloadFormat, rtpTimestampFrequency, profileAndLevelIndication, configStr);
00050 }
00051 
00052 Boolean MPEG4ESVideoRTPSink::sourceIsCompatibleWithUs(MediaSource& source) {
00053   // Our source must be an appropriate framer:
00054   return source.isMPEG4VideoStreamFramer();
00055 }
00056 
00057 #define VOP_START_CODE                    0x000001B6
00058 
00059 void MPEG4ESVideoRTPSink
00060 ::doSpecialFrameHandling(unsigned fragmentationOffset,
00061                          unsigned char* frameStart,
00062                          unsigned numBytesInFrame,
00063                          struct timeval framePresentationTime,
00064                          unsigned numRemainingBytes) {
00065   if (fragmentationOffset == 0) {
00066     // Begin by inspecting the 4-byte code at the start of the frame:
00067     if (numBytesInFrame < 4) return; // shouldn't happen
00068     unsigned startCode = (frameStart[0]<<24) | (frameStart[1]<<16)
00069       | (frameStart[2]<<8) | frameStart[3];
00070 
00071     fVOPIsPresent = startCode == VOP_START_CODE;
00072   }
00073 
00074   // Set the RTP 'M' (marker) bit iff this frame ends a VOP
00075   // (and there are no fragments remaining).
00076   // This relies on the source being a "MPEG4VideoStreamFramer".
00077   MPEG4VideoStreamFramer* framerSource = (MPEG4VideoStreamFramer*)fSource;
00078   if (framerSource != NULL && framerSource->pictureEndMarker()
00079       && numRemainingBytes == 0) {
00080     setMarkerBit();
00081     framerSource->pictureEndMarker() = False;
00082   }
00083 
00084   // Also set the RTP timestamp.  (We do this for each frame
00085   // in the packet, to ensure that the timestamp of the VOP (if present)
00086   // gets used.)
00087   setTimestamp(framePresentationTime);
00088 }
00089 
00090 Boolean MPEG4ESVideoRTPSink::allowFragmentationAfterStart() const {
00091   return True;
00092 }
00093 
00094 Boolean MPEG4ESVideoRTPSink
00095 ::frameCanAppearAfterPacketStart(unsigned char const* /*frameStart*/,
00096                                  unsigned /*numBytesInFrame*/) const {
00097   // Once we've packed a VOP into the packet, then no other
00098   // frame can be packed into it:
00099   return !fVOPIsPresent;
00100 }
00101 
00102 char const* MPEG4ESVideoRTPSink::auxSDPLine() {
00103   // Generate a new "a=fmtp:" line each time, using our own 'configuration' information (if we have it),
00104   // otherwise parameters from our framer source (in case they've changed since the last time that
00105   // we were called):
00106   unsigned configLength = fNumConfigBytes;
00107   unsigned char* config = fConfigBytes;
00108   if (fProfileAndLevelIndication == 0 || config == NULL) {
00109     // We need to get this information from our framer source:
00110     MPEG4VideoStreamFramer* framerSource = (MPEG4VideoStreamFramer*)fSource;
00111     if (framerSource == NULL) return NULL; // we don't yet have a source
00112 
00113     fProfileAndLevelIndication = framerSource->profile_and_level_indication();
00114     if (fProfileAndLevelIndication == 0) return NULL; // our source isn't ready
00115 
00116     config = framerSource->getConfigBytes(configLength);
00117     if (config == NULL) return NULL; // our source isn't ready
00118   }
00119 
00120   char const* fmtpFmt =
00121     "a=fmtp:%d "
00122     "profile-level-id=%d;"
00123     "config=";
00124   unsigned fmtpFmtSize = strlen(fmtpFmt)
00125     + 3 /* max char len */
00126     + 3 /* max char len */
00127     + 2*configLength /* 2*, because each byte prints as 2 chars */
00128     + 2 /* trailing \r\n */;
00129   char* fmtp = new char[fmtpFmtSize];
00130   sprintf(fmtp, fmtpFmt, rtpPayloadType(), fProfileAndLevelIndication);
00131   char* endPtr = &fmtp[strlen(fmtp)];
00132   for (unsigned i = 0; i < configLength; ++i) {
00133     sprintf(endPtr, "%02X", config[i]);
00134     endPtr += 2;
00135   }
00136   sprintf(endPtr, "\r\n");
00137 
00138   delete[] fFmtpSDPLine;
00139   fFmtpSDPLine = strDup(fmtp);
00140   delete[] fmtp;
00141   return fFmtpSDPLine;
00142 }

Generated on Thu May 17 07:11:47 2012 for live by  doxygen 1.5.2