liveMedia/include/RTPSink.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-2012 Live Networks, Inc.  All rights reserved.
00018 // RTP Sinks
00019 // C++ header
00020 
00021 #ifndef _RTP_SINK_HH
00022 #define _RTP_SINK_HH
00023 
00024 #ifndef _MEDIA_SINK_HH
00025 #include "MediaSink.hh"
00026 #endif
00027 #ifndef _RTP_INTERFACE_HH
00028 #include "RTPInterface.hh"
00029 #endif
00030 
00031 class RTPTransmissionStatsDB; // forward
00032 
00033 class RTPSink: public MediaSink {
00034 public:
00035   static Boolean lookupByName(UsageEnvironment& env, char const* sinkName,
00036                               RTPSink*& resultSink);
00037 
00038   // used by RTCP:
00039   u_int32_t SSRC() const {return fSSRC;}
00040      // later need a means of changing the SSRC if there's a collision #####
00041   u_int32_t convertToRTPTimestamp(struct timeval tv);
00042   unsigned packetCount() const {return fPacketCount;}
00043   unsigned octetCount() const {return fOctetCount;}
00044 
00045   // used by RTSP servers:
00046   Groupsock const& groupsockBeingUsed() const { return *(fRTPInterface.gs()); }
00047   Groupsock& groupsockBeingUsed() { return *(fRTPInterface.gs()); }
00048 
00049   unsigned char rtpPayloadType() const { return fRTPPayloadType; }
00050   unsigned rtpTimestampFrequency() const { return fTimestampFrequency; }
00051   void setRTPTimestampFrequency(unsigned freq) {
00052     fTimestampFrequency = freq;
00053   }
00054   char const* rtpPayloadFormatName() const {return fRTPPayloadFormatName;}
00055 
00056   unsigned numChannels() const { return fNumChannels; }
00057 
00058   virtual char const* sdpMediaType() const; // for use in SDP m= lines
00059   virtual char* rtpmapLine() const; // returns a string to be delete[]d
00060   virtual char const* auxSDPLine();
00061       // optional SDP line (e.g. a=fmtp:...)
00062 
00063   u_int16_t currentSeqNo() const { return fSeqNo; }
00064   u_int32_t presetNextTimestamp();
00065       // ensures that the next timestamp to be used will correspond to
00066       // the current 'wall clock' time.
00067 
00068   RTPTransmissionStatsDB& transmissionStatsDB() const {
00069     return *fTransmissionStatsDB;
00070   }
00071 
00072   Boolean nextTimestampHasBeenPreset() const { return fNextTimestampHasBeenPreset; }
00073 
00074   void setStreamSocket(int sockNum, unsigned char streamChannelId) {
00075     fRTPInterface.setStreamSocket(sockNum, streamChannelId);
00076   }
00077   void addStreamSocket(int sockNum, unsigned char streamChannelId) {
00078     fRTPInterface.addStreamSocket(sockNum, streamChannelId);
00079   }
00080   void removeStreamSocket(int sockNum, unsigned char streamChannelId) {
00081     fRTPInterface.removeStreamSocket(sockNum, streamChannelId);
00082   }
00083   void setServerRequestAlternativeByteHandler(int socketNum, ServerRequestAlternativeByteHandler* handler, void* clientData) {
00084     fRTPInterface.setServerRequestAlternativeByteHandler(socketNum, handler, clientData);
00085   }
00086     // hacks to allow sending RTP over TCP (RFC 2236, section 10.12)
00087 
00088   void getTotalBitrate(unsigned& outNumBytes, double& outElapsedTime);
00089       // returns the number of bytes sent since the last time that we
00090       // were called, and resets the counter.
00091 
00092 protected:
00093   RTPSink(UsageEnvironment& env,
00094           Groupsock* rtpGS, unsigned char rtpPayloadType,
00095           u_int32_t rtpTimestampFrequency,
00096           char const* rtpPayloadFormatName,
00097           unsigned numChannels);
00098         // abstract base class
00099 
00100   virtual ~RTPSink();
00101 
00102   RTPInterface fRTPInterface;
00103   unsigned char fRTPPayloadType;
00104   unsigned fPacketCount, fOctetCount, fTotalOctetCount /*incl RTP hdr*/;
00105   struct timeval fTotalOctetCountStartTime;
00106   u_int32_t fCurrentTimestamp;
00107   u_int16_t fSeqNo;
00108 
00109 private:
00110   // redefined virtual functions:
00111   virtual Boolean isRTPSink() const;
00112 
00113 private:
00114   u_int32_t fSSRC, fTimestampBase;
00115   unsigned fTimestampFrequency;
00116   Boolean fNextTimestampHasBeenPreset;
00117   char const* fRTPPayloadFormatName;
00118   unsigned fNumChannels;
00119   struct timeval fCreationTime;
00120 
00121   RTPTransmissionStatsDB* fTransmissionStatsDB;
00122 };
00123 
00124 
00125 class RTPTransmissionStats; // forward
00126 
00127 class RTPTransmissionStatsDB {
00128 public:
00129   unsigned numReceivers() const { return fNumReceivers; }
00130 
00131   class Iterator {
00132   public:
00133     Iterator(RTPTransmissionStatsDB& receptionStatsDB);
00134     virtual ~Iterator();
00135 
00136     RTPTransmissionStats* next();
00137         // NULL if none
00138 
00139   private:
00140     HashTable::Iterator* fIter;
00141   };
00142 
00143   // The following is called whenever a RTCP RR packet is received:
00144   void noteIncomingRR(u_int32_t SSRC, struct sockaddr_in const& lastFromAddress,
00145                       unsigned lossStats, unsigned lastPacketNumReceived,
00146                       unsigned jitter, unsigned lastSRTime, unsigned diffSR_RRTime);
00147 
00148   // The following is called when a RTCP BYE packet is received:
00149   void removeRecord(u_int32_t SSRC);
00150 
00151   RTPTransmissionStats* lookup(u_int32_t SSRC) const;
00152 
00153 private: // constructor and destructor, called only by RTPSink:
00154   friend class RTPSink;
00155   RTPTransmissionStatsDB(RTPSink& rtpSink);
00156   virtual ~RTPTransmissionStatsDB();
00157 
00158 private:
00159   void add(u_int32_t SSRC, RTPTransmissionStats* stats);
00160 
00161 private:
00162   friend class Iterator;
00163   unsigned fNumReceivers;
00164     RTPSink& fOurRTPSink;
00165   HashTable* fTable;
00166 };
00167 
00168 class RTPTransmissionStats {
00169 public:
00170   u_int32_t SSRC() const {return fSSRC;}
00171   struct sockaddr_in const& lastFromAddress() const {return fLastFromAddress;}
00172   unsigned lastPacketNumReceived() const {return fLastPacketNumReceived;}
00173   unsigned firstPacketNumReported() const {return fFirstPacketNumReported;}
00174   unsigned totNumPacketsLost() const {return fTotNumPacketsLost;}
00175   unsigned jitter() const {return fJitter;}
00176   unsigned lastSRTime() const { return fLastSRTime; }
00177   unsigned diffSR_RRTime() const { return fDiffSR_RRTime; }
00178   unsigned roundTripDelay() const;
00179       // The round-trip delay (in units of 1/65536 seconds) computed from
00180       // the most recently-received RTCP RR packet.
00181   struct timeval timeCreated() const {return fTimeCreated;}
00182   struct timeval lastTimeReceived() const {return fTimeReceived;}
00183   void getTotalOctetCount(u_int32_t& hi, u_int32_t& lo);
00184   void getTotalPacketCount(u_int32_t& hi, u_int32_t& lo);
00185 
00186   // Information which requires at least two RRs to have been received:
00187   Boolean oldValid() const {return fOldValid;} // Have two RRs been received?
00188   unsigned packetsReceivedSinceLastRR() const;
00189   u_int8_t packetLossRatio() const { return fPacketLossRatio; }
00190      // as an 8-bit fixed-point number
00191   int packetsLostBetweenRR() const;
00192 
00193 private:
00194   // called only by RTPTransmissionStatsDB:
00195   friend class RTPTransmissionStatsDB;
00196   RTPTransmissionStats(RTPSink& rtpSink, u_int32_t SSRC);
00197   virtual ~RTPTransmissionStats();
00198 
00199   void noteIncomingRR(struct sockaddr_in const& lastFromAddress,
00200                       unsigned lossStats, unsigned lastPacketNumReceived,
00201                       unsigned jitter,
00202                       unsigned lastSRTime, unsigned diffSR_RRTime);
00203 
00204 private:
00205   RTPSink& fOurRTPSink;
00206   u_int32_t fSSRC;
00207   struct sockaddr_in fLastFromAddress;
00208   unsigned fLastPacketNumReceived;
00209   u_int8_t fPacketLossRatio;
00210   unsigned fTotNumPacketsLost;
00211   unsigned fJitter;
00212   unsigned fLastSRTime;
00213   unsigned fDiffSR_RRTime;
00214   struct timeval fTimeCreated, fTimeReceived;
00215   Boolean fOldValid;
00216   unsigned fOldLastPacketNumReceived;
00217   unsigned fOldTotNumPacketsLost;
00218   Boolean fFirstPacket;
00219   unsigned fFirstPacketNumReported;
00220   u_int32_t fLastOctetCount, fTotalOctetCount_hi, fTotalOctetCount_lo;
00221   u_int32_t fLastPacketCount, fTotalPacketCount_hi, fTotalPacketCount_lo;
00222 };
00223 
00224 #endif

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