| 1 | /* | 
| 2 |  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 
| 3 |  * | 
| 4 |  *  Use of this source code is governed by a BSD-style license | 
| 5 |  *  that can be found in the LICENSE file in the root of the source | 
| 6 |  *  tree. An additional intellectual property rights grant can be found | 
| 7 |  *  in the file PATENTS.  All contributing project authors may | 
| 8 |  *  be found in the AUTHORS file in the root of the source tree. | 
| 9 |  */ | 
| 10 |  | 
| 11 | #ifndef MEDIA_BASE_RTPDATAENGINE_H_ | 
| 12 | #define MEDIA_BASE_RTPDATAENGINE_H_ | 
| 13 |  | 
| 14 | #include <map> | 
| 15 | #include <memory> | 
| 16 | #include <string> | 
| 17 | #include <vector> | 
| 18 |  | 
| 19 | #include "media/base/mediachannel.h" | 
| 20 | #include "media/base/mediaconstants.h" | 
| 21 | #include "media/base/mediaengine.h" | 
| 22 |  | 
| 23 | namespace rtc { | 
| 24 | class DataRateLimiter; | 
| 25 | } | 
| 26 |  | 
| 27 | namespace cricket { | 
| 28 |  | 
| 29 | struct DataCodec; | 
| 30 |  | 
| 31 | class RtpDataEngine : public DataEngineInterface { | 
| 32 |  public: | 
| 33 |   RtpDataEngine(); | 
| 34 |  | 
| 35 |   virtual DataMediaChannel* CreateChannel(const MediaConfig& config); | 
| 36 |  | 
| 37 |   virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; } | 
| 38 |  | 
| 39 |  private: | 
| 40 |   std::vector<DataCodec> data_codecs_; | 
| 41 | }; | 
| 42 |  | 
| 43 | // Keep track of sequence number and timestamp of an RTP stream.  The | 
| 44 | // sequence number starts with a "random" value and increments.  The | 
| 45 | // timestamp starts with a "random" value and increases monotonically | 
| 46 | // according to the clockrate. | 
| 47 | class RtpClock { | 
| 48 |  public: | 
| 49 |   RtpClock(int clockrate, uint16_t first_seq_num, uint32_t timestamp_offset) | 
| 50 |       : clockrate_(clockrate), | 
| 51 |         last_seq_num_(first_seq_num), | 
| 52 |         timestamp_offset_(timestamp_offset) {} | 
| 53 |  | 
| 54 |   // Given the current time (in number of seconds which must be | 
| 55 |   // monotonically increasing), Return the next sequence number and | 
| 56 |   // timestamp. | 
| 57 |   void Tick(double now, int* seq_num, uint32_t* timestamp); | 
| 58 |  | 
| 59 |  private: | 
| 60 |   int clockrate_; | 
| 61 |   uint16_t last_seq_num_; | 
| 62 |   uint32_t timestamp_offset_; | 
| 63 | }; | 
| 64 |  | 
| 65 | class RtpDataMediaChannel : public DataMediaChannel { | 
| 66 |  public: | 
| 67 |   explicit RtpDataMediaChannel(const MediaConfig& config); | 
| 68 |   virtual ~RtpDataMediaChannel(); | 
| 69 |  | 
| 70 |   virtual bool SetSendParameters(const DataSendParameters& params); | 
| 71 |   virtual bool SetRecvParameters(const DataRecvParameters& params); | 
| 72 |   virtual bool AddSendStream(const StreamParams& sp); | 
| 73 |   virtual bool RemoveSendStream(uint32_t ssrc); | 
| 74 |   virtual bool AddRecvStream(const StreamParams& sp); | 
| 75 |   virtual bool RemoveRecvStream(uint32_t ssrc); | 
| 76 |   virtual bool SetSend(bool send) { | 
| 77 |     sending_ = send; | 
| 78 |     return true; | 
| 79 |   } | 
| 80 |   virtual bool SetReceive(bool receive) { | 
| 81 |     receiving_ = receive; | 
| 82 |     return true; | 
| 83 |   } | 
| 84 |   virtual void OnPacketReceived(rtc::CopyOnWriteBuffer* packet, | 
| 85 |                                 int64_t packet_time_us); | 
| 86 |   virtual void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet, | 
| 87 |                               int64_t packet_time_us) {} | 
| 88 |   virtual void OnReadyToSend(bool ready) {} | 
| 89 |   virtual bool SendData(const SendDataParams& params, | 
| 90 |                         const rtc::CopyOnWriteBuffer& payload, | 
| 91 |                         SendDataResult* result); | 
| 92 |   virtual rtc::DiffServCodePoint PreferredDscp() const; | 
| 93 |  | 
| 94 |  private: | 
| 95 |   void Construct(); | 
| 96 |   bool SetMaxSendBandwidth(int bps); | 
| 97 |   bool SetSendCodecs(const std::vector<DataCodec>& codecs); | 
| 98 |   bool SetRecvCodecs(const std::vector<DataCodec>& codecs); | 
| 99 |  | 
| 100 |   bool sending_; | 
| 101 |   bool receiving_; | 
| 102 |   std::vector<DataCodec> send_codecs_; | 
| 103 |   std::vector<DataCodec> recv_codecs_; | 
| 104 |   std::vector<StreamParams> send_streams_; | 
| 105 |   std::vector<StreamParams> recv_streams_; | 
| 106 |   std::map<uint32_t, RtpClock*> rtp_clock_by_send_ssrc_; | 
| 107 |   std::unique_ptr<rtc::DataRateLimiter> send_limiter_; | 
| 108 | }; | 
| 109 |  | 
| 110 | }  // namespace cricket | 
| 111 |  | 
| 112 | #endif  // MEDIA_BASE_RTPDATAENGINE_H_ | 
| 113 |  |