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_RTP_DATA_ENGINE_H_ 12 #define MEDIA_BASE_RTP_DATA_ENGINE_H_ 13 14 #include <map> 15 #include <memory> 16 #include <string> 17 #include <vector> 18 19 #include "media/base/codec.h" 20 #include "media/base/media_channel.h" 21 #include "media/base/media_constants.h" 22 #include "media/base/media_engine.h" 23 24 namespace rtc { 25 class DataRateLimiter; 26 } 27 28 namespace cricket { 29 30 class RtpDataEngine : public DataEngineInterface { 31 public: 32 RtpDataEngine(); 33 34 virtual DataMediaChannel* CreateChannel(const MediaConfig& config); 35 data_codecs()36 virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; } 37 38 private: 39 std::vector<DataCodec> data_codecs_; 40 }; 41 42 // Keep track of sequence number and timestamp of an RTP stream. The 43 // sequence number starts with a "random" value and increments. The 44 // timestamp starts with a "random" value and increases monotonically 45 // according to the clockrate. 46 class RtpClock { 47 public: RtpClock(int clockrate,uint16_t first_seq_num,uint32_t timestamp_offset)48 RtpClock(int clockrate, uint16_t first_seq_num, uint32_t timestamp_offset) 49 : clockrate_(clockrate), 50 last_seq_num_(first_seq_num), 51 timestamp_offset_(timestamp_offset) {} 52 53 // Given the current time (in number of seconds which must be 54 // monotonically increasing), Return the next sequence number and 55 // timestamp. 56 void Tick(double now, int* seq_num, uint32_t* timestamp); 57 58 private: 59 int clockrate_; 60 uint16_t last_seq_num_; 61 uint32_t timestamp_offset_; 62 }; 63 64 class RtpDataMediaChannel : public DataMediaChannel { 65 public: 66 explicit RtpDataMediaChannel(const MediaConfig& config); 67 virtual ~RtpDataMediaChannel(); 68 69 virtual bool SetSendParameters(const DataSendParameters& params); 70 virtual bool SetRecvParameters(const DataRecvParameters& params); 71 virtual bool AddSendStream(const StreamParams& sp); 72 virtual bool RemoveSendStream(uint32_t ssrc); 73 virtual bool AddRecvStream(const StreamParams& sp); 74 virtual bool RemoveRecvStream(uint32_t ssrc); 75 virtual void ResetUnsignaledRecvStream(); SetSend(bool send)76 virtual bool SetSend(bool send) { 77 sending_ = send; 78 return true; 79 } SetReceive(bool receive)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); OnReadyToSend(bool ready)86 virtual void OnReadyToSend(bool ready) {} 87 virtual bool SendData(const SendDataParams& params, 88 const rtc::CopyOnWriteBuffer& payload, 89 SendDataResult* result); 90 91 private: 92 void Construct(); 93 bool SetMaxSendBandwidth(int bps); 94 bool SetSendCodecs(const std::vector<DataCodec>& codecs); 95 bool SetRecvCodecs(const std::vector<DataCodec>& codecs); 96 97 bool sending_; 98 bool receiving_; 99 std::vector<DataCodec> send_codecs_; 100 std::vector<DataCodec> recv_codecs_; 101 std::vector<StreamParams> send_streams_; 102 std::vector<StreamParams> recv_streams_; 103 std::map<uint32_t, RtpClock*> rtp_clock_by_send_ssrc_; 104 std::unique_ptr<rtc::DataRateLimiter> send_limiter_; 105 }; 106 107 } // namespace cricket 108 109 #endif // MEDIA_BASE_RTP_DATA_ENGINE_H_ 110