liveMedia/include/MultiFramedRTPSink.hh

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-2013 Live Networks, Inc.  All rights reserved.
00018 // RTP sink for a common kind of payload format: Those which pack multiple,
00019 // complete codec frames (as many as possible) into each RTP packet.
00020 // C++ header
00021 
00022 #ifndef _MULTI_FRAMED_RTP_SINK_HH
00023 #define _MULTI_FRAMED_RTP_SINK_HH
00024 
00025 #ifndef _RTP_SINK_HH
00026 #include "RTPSink.hh"
00027 #endif
00028 
00029 class MultiFramedRTPSink: public RTPSink {
00030 public:
00031   void setPacketSizes(unsigned preferredPacketSize, unsigned maxPacketSize);
00032 
00033   typedef void (onSendErrorFunc)(void* clientData);
00034   void setOnSendErrorFunc(onSendErrorFunc* onSendErrorFunc, void* onSendErrorFuncData) {
00035     // Can be used to set a callback function to be called if there's an error sending RTP packets on our socket.
00036     fOnSendErrorFunc = onSendErrorFunc;
00037     fOnSendErrorData = onSendErrorFuncData;
00038   }
00039 
00040 protected:
00041   MultiFramedRTPSink(UsageEnvironment& env,
00042                      Groupsock* rtpgs, unsigned char rtpPayloadType,
00043                      unsigned rtpTimestampFrequency,
00044                      char const* rtpPayloadFormatName,
00045                      unsigned numChannels = 1);
00046         // we're a virtual base class
00047 
00048   virtual ~MultiFramedRTPSink();
00049 
00050   virtual void doSpecialFrameHandling(unsigned fragmentationOffset,
00051                                       unsigned char* frameStart,
00052                                       unsigned numBytesInFrame,
00053                                       struct timeval framePresentationTime,
00054                                       unsigned numRemainingBytes);
00055       // perform any processing specific to the particular payload format
00056   virtual Boolean allowFragmentationAfterStart() const;
00057       // whether a frame can be fragmented if other frame(s) appear earlier
00058       // in the packet (by default: False)
00059   virtual Boolean allowOtherFramesAfterLastFragment() const;
00060       // whether other frames can be packed into a packet following the
00061       // final fragment of a previous, fragmented frame (by default: False)
00062   virtual Boolean frameCanAppearAfterPacketStart(unsigned char const* frameStart,
00063                                                  unsigned numBytesInFrame) const;
00064       // whether this frame can appear in position >1 in a pkt (default: True)
00065   virtual unsigned specialHeaderSize() const;
00066       // returns the size of any special header used (following the RTP header) (default: 0)
00067   virtual unsigned frameSpecificHeaderSize() const;
00068       // returns the size of any frame-specific header used (before each frame
00069       // within the packet) (default: 0)
00070   virtual unsigned computeOverflowForNewFrame(unsigned newFrameSize) const;
00071       // returns the number of overflow bytes that would be produced by adding a new
00072       // frame of size "newFrameSize" to the current RTP packet.
00073       // (By default, this just calls "numOverflowBytes()", but subclasses can redefine
00074       // this to (e.g.) impose a granularity upon RTP payload fragments.)
00075 
00076   // Functions that might be called by doSpecialFrameHandling(), or other subclass virtual functions:
00077   Boolean isFirstPacket() const { return fIsFirstPacket; }
00078   Boolean isFirstFrameInPacket() const { return fNumFramesUsedSoFar == 0; }
00079   unsigned curFragmentationOffset() const { return fCurFragmentationOffset; }
00080   void setMarkerBit();
00081   void setTimestamp(struct timeval framePresentationTime);
00082   void setSpecialHeaderWord(unsigned word, /* 32 bits, in host order */
00083                             unsigned wordPosition = 0);
00084   void setSpecialHeaderBytes(unsigned char const* bytes, unsigned numBytes,
00085                              unsigned bytePosition = 0);
00086   void setFrameSpecificHeaderWord(unsigned word, /* 32 bits, in host order */
00087                                   unsigned wordPosition = 0);
00088   void setFrameSpecificHeaderBytes(unsigned char const* bytes, unsigned numBytes,
00089                                    unsigned bytePosition = 0);
00090   void setFramePadding(unsigned numPaddingBytes);
00091   unsigned numFramesUsedSoFar() const { return fNumFramesUsedSoFar; }
00092   unsigned ourMaxPacketSize() const { return fOurMaxPacketSize; }
00093 
00094 public: // redefined virtual functions:
00095   virtual void stopPlaying();
00096 
00097 protected: // redefined virtual functions:
00098   virtual Boolean continuePlaying();
00099 
00100 private:
00101   void buildAndSendPacket(Boolean isFirstPacket);
00102   void packFrame();
00103   void sendPacketIfNecessary();
00104   static void sendNext(void* firstArg);
00105   friend void sendNext(void*);
00106 
00107   static void afterGettingFrame(void* clientData,
00108                                 unsigned numBytesRead, unsigned numTruncatedBytes,
00109                                 struct timeval presentationTime,
00110                                 unsigned durationInMicroseconds);
00111   void afterGettingFrame1(unsigned numBytesRead, unsigned numTruncatedBytes,
00112                           struct timeval presentationTime,
00113                           unsigned durationInMicroseconds);
00114   Boolean isTooBigForAPacket(unsigned numBytes) const;
00115 
00116   static void ourHandleClosure(void* clientData);
00117 
00118 private:
00119   OutPacketBuffer* fOutBuf;
00120 
00121   Boolean fNoFramesLeft;
00122   unsigned fNumFramesUsedSoFar;
00123   unsigned fCurFragmentationOffset;
00124   Boolean fPreviousFrameEndedFragmentation;
00125 
00126   Boolean fIsFirstPacket;
00127   struct timeval fNextSendTime;
00128   unsigned fTimestampPosition;
00129   unsigned fSpecialHeaderPosition;
00130   unsigned fSpecialHeaderSize; // size in bytes of any special header used
00131   unsigned fCurFrameSpecificHeaderPosition;
00132   unsigned fCurFrameSpecificHeaderSize; // size in bytes of cur frame-specific header
00133   unsigned fTotalFrameSpecificHeaderSizes; // size of all frame-specific hdrs in pkt
00134   unsigned fOurMaxPacketSize;
00135 
00136   onSendErrorFunc* fOnSendErrorFunc;
00137   void* fOnSendErrorData;
00138 };
00139 
00140 #endif

Generated on Mon Apr 29 13:28:01 2013 for live by  doxygen 1.5.2