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-2008 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     // hacks to allow sending RTP over TCP (RFC 2236, section 10.12)
00084 
00085   void getTotalBitrate(unsigned& outNumBytes, double& outElapsedTime);
00086       // returns the number of bytes sent since the last time that we
00087       // were called, and resets the counter.
00088 
00089 protected:
00090   RTPSink(UsageEnvironment& env,
00091           Groupsock* rtpGS, unsigned char rtpPayloadType,
00092           u_int32_t rtpTimestampFrequency,
00093           char const* rtpPayloadFormatName,
00094           unsigned numChannels);
00095         // abstract base class
00096 
00097   virtual ~RTPSink();
00098 
00099   RTPInterface fRTPInterface;
00100   unsigned char fRTPPayloadType;
00101   unsigned fPacketCount, fOctetCount, fTotalOctetCount /*incl RTP hdr*/;
00102   struct timeval fTotalOctetCountStartTime;
00103   u_int32_t fCurrentTimestamp;
00104   u_int16_t fSeqNo;
00105 
00106 private:
00107   // redefined virtual functions:
00108   virtual Boolean isRTPSink() const;
00109 
00110 private:
00111   u_int32_t fSSRC, fTimestampBase;
00112   unsigned fTimestampFrequency;
00113   Boolean fNextTimestampHasBeenPreset;
00114   char const* fRTPPayloadFormatName;
00115   unsigned fNumChannels;
00116   struct timeval fCreationTime;
00117 
00118   RTPTransmissionStatsDB* fTransmissionStatsDB;
00119 };
00120 
00121 
00122 class RTPTransmissionStats; // forward
00123 
00124 class RTPTransmissionStatsDB {
00125 public:
00126   unsigned numReceivers() const { return fNumReceivers; }
00127 
00128   class Iterator {
00129   public:
00130     Iterator(RTPTransmissionStatsDB& receptionStatsDB);
00131     virtual ~Iterator();
00132 
00133     RTPTransmissionStats* next();
00134         // NULL if none
00135 
00136   private:
00137     HashTable::Iterator* fIter;
00138   };
00139 
00140   // The following is called whenever a RTCP RR packet is received:
00141   void noteIncomingRR(u_int32_t SSRC, struct sockaddr_in const& lastFromAddress,
00142                       unsigned lossStats, unsigned lastPacketNumReceived,
00143                       unsigned jitter, unsigned lastSRTime, unsigned diffSR_RRTime);
00144 
00145   // The following is called when a RTCP BYE packet is received:
00146   void removeRecord(u_int32_t SSRC);
00147 
00148   RTPTransmissionStats* lookup(u_int32_t SSRC) const;
00149 
00150 private: // constructor and destructor, called only by RTPSink:
00151   friend class RTPSink;
00152   RTPTransmissionStatsDB(RTPSink& rtpSink);
00153   virtual ~RTPTransmissionStatsDB();
00154 
00155 private:
00156   void add(u_int32_t SSRC, RTPTransmissionStats* stats);
00157 
00158 private:
00159   friend class Iterator;
00160   unsigned fNumReceivers;
00161     RTPSink& fOurRTPSink;
00162   HashTable* fTable;
00163 };
00164 
00165 class RTPTransmissionStats {
00166 public:
00167   u_int32_t SSRC() const {return fSSRC;}
00168   struct sockaddr_in const& lastFromAddress() const {return fLastFromAddress;}
00169   unsigned lastPacketNumReceived() const {return fLastPacketNumReceived;}
00170   unsigned firstPacketNumReported() const {return fFirstPacketNumReported;}
00171   unsigned totNumPacketsLost() const {return fTotNumPacketsLost;}
00172   unsigned jitter() const {return fJitter;}
00173   unsigned lastSRTime() const { return fLastSRTime; }
00174   unsigned diffSR_RRTime() const { return fDiffSR_RRTime; }
00175   unsigned roundTripDelay() const;
00176       // The round-trip delay (in units of 1/65536 seconds) computed from
00177       // the most recently-received RTCP RR packet.
00178   struct timeval timeCreated() const {return fTimeCreated;}
00179   struct timeval lastTimeReceived() const {return fTimeReceived;}
00180   void getTotalOctetCount(u_int32_t& hi, u_int32_t& lo);
00181   void getTotalPacketCount(u_int32_t& hi, u_int32_t& lo);
00182 
00183   // Information which requires at least two RRs to have been received:
00184   Boolean oldValid() const {return fOldValid;} // Have two RRs been received?
00185   unsigned packetsReceivedSinceLastRR() const;
00186   u_int8_t packetLossRatio() const { return fPacketLossRatio; }
00187      // as an 8-bit fixed-point number
00188   int packetsLostBetweenRR() const;
00189 
00190 private:
00191   // called only by RTPTransmissionStatsDB:
00192   friend class RTPTransmissionStatsDB;
00193   RTPTransmissionStats(RTPSink& rtpSink, u_int32_t SSRC);
00194   virtual ~RTPTransmissionStats();
00195 
00196   void noteIncomingRR(struct sockaddr_in const& lastFromAddress,
00197                       unsigned lossStats, unsigned lastPacketNumReceived,
00198                       unsigned jitter,
00199                       unsigned lastSRTime, unsigned diffSR_RRTime);
00200 
00201 private:
00202   RTPSink& fOurRTPSink;
00203   u_int32_t fSSRC;
00204   struct sockaddr_in fLastFromAddress;
00205   unsigned fLastPacketNumReceived;
00206   u_int8_t fPacketLossRatio;
00207   unsigned fTotNumPacketsLost;
00208   unsigned fJitter;
00209   unsigned fLastSRTime;
00210   unsigned fDiffSR_RRTime;
00211   struct timeval fTimeCreated, fTimeReceived;
00212   Boolean fOldValid;
00213   unsigned fOldLastPacketNumReceived;
00214   unsigned fOldTotNumPacketsLost;
00215   Boolean fFirstPacket;
00216   unsigned fFirstPacketNumReported;
00217   u_int32_t fLastOctetCount, fTotalOctetCount_hi, fTotalOctetCount_lo;
00218   u_int32_t fLastPacketCount, fTotalPacketCount_hi, fTotalPacketCount_lo;
00219 };
00220 
00221 #endif

Generated on Tue Oct 7 15:38:09 2008 for live by  doxygen 1.5.2