henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_MEDIA_BASE_RTPDATAENGINE_H_ |
| 29 | #define TALK_MEDIA_BASE_RTPDATAENGINE_H_ |
| 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #include "talk/media/base/constants.h" |
| 35 | #include "talk/media/base/mediachannel.h" |
| 36 | #include "talk/media/base/mediaengine.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 37 | #include "webrtc/base/timing.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | |
| 39 | namespace cricket { |
| 40 | |
| 41 | struct DataCodec; |
| 42 | |
| 43 | class RtpDataEngine : public DataEngineInterface { |
| 44 | public: |
| 45 | RtpDataEngine(); |
| 46 | |
| 47 | virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type); |
| 48 | |
| 49 | virtual const std::vector<DataCodec>& data_codecs() { |
| 50 | return data_codecs_; |
| 51 | } |
| 52 | |
| 53 | // Mostly for testing with a fake clock. Ownership is passed in. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 54 | void SetTiming(rtc::Timing* timing) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | timing_.reset(timing); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | std::vector<DataCodec> data_codecs_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 60 | rtc::scoped_ptr<rtc::Timing> timing_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // Keep track of sequence number and timestamp of an RTP stream. The |
| 64 | // sequence number starts with a "random" value and increments. The |
| 65 | // timestamp starts with a "random" value and increases monotonically |
| 66 | // according to the clockrate. |
| 67 | class RtpClock { |
| 68 | public: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 69 | RtpClock(int clockrate, uint16_t first_seq_num, uint32_t timestamp_offset) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | : clockrate_(clockrate), |
| 71 | last_seq_num_(first_seq_num), |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 72 | timestamp_offset_(timestamp_offset) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | |
| 74 | // Given the current time (in number of seconds which must be |
| 75 | // monotonically increasing), Return the next sequence number and |
| 76 | // timestamp. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 77 | void Tick(double now, int* seq_num, uint32_t* timestamp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | int clockrate_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 81 | uint16_t last_seq_num_; |
| 82 | uint32_t timestamp_offset_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | class RtpDataMediaChannel : public DataMediaChannel { |
| 86 | public: |
| 87 | // Timing* Used for the RtpClock |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 88 | explicit RtpDataMediaChannel(rtc::Timing* timing); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | // Sets Timing == NULL, so you'll need to call set_timer() before |
| 90 | // using it. This is needed by FakeMediaEngine. |
| 91 | RtpDataMediaChannel(); |
| 92 | virtual ~RtpDataMediaChannel(); |
| 93 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 94 | void set_timing(rtc::Timing* timing) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | timing_ = timing; |
| 96 | } |
| 97 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 98 | virtual bool SetSendParameters(const DataSendParameters& params); |
| 99 | virtual bool SetRecvParameters(const DataRecvParameters& params); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | virtual bool AddSendStream(const StreamParams& sp); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 101 | virtual bool RemoveSendStream(uint32_t ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | virtual bool AddRecvStream(const StreamParams& sp); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 103 | virtual bool RemoveRecvStream(uint32_t ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | virtual bool SetSend(bool send) { |
| 105 | sending_ = send; |
| 106 | return true; |
| 107 | } |
| 108 | virtual bool SetReceive(bool receive) { |
| 109 | receiving_ = receive; |
| 110 | return true; |
| 111 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 112 | virtual void OnPacketReceived(rtc::Buffer* packet, |
| 113 | const rtc::PacketTime& packet_time); |
| 114 | virtual void OnRtcpReceived(rtc::Buffer* packet, |
| 115 | const rtc::PacketTime& packet_time) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | virtual void OnReadyToSend(bool ready) {} |
| 117 | virtual bool SendData( |
| 118 | const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 119 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | SendDataResult* result); |
| 121 | |
| 122 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 123 | void Construct(rtc::Timing* timing); |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 124 | bool SetMaxSendBandwidth(int bps); |
| 125 | bool SetSendCodecs(const std::vector<DataCodec>& codecs); |
| 126 | bool SetRecvCodecs(const std::vector<DataCodec>& codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | |
| 128 | bool sending_; |
| 129 | bool receiving_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 130 | rtc::Timing* timing_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | std::vector<DataCodec> send_codecs_; |
| 132 | std::vector<DataCodec> recv_codecs_; |
| 133 | std::vector<StreamParams> send_streams_; |
| 134 | std::vector<StreamParams> recv_streams_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 135 | std::map<uint32_t, RtpClock*> rtp_clock_by_send_ssrc_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 136 | rtc::scoped_ptr<rtc::RateLimiter> send_limiter_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // namespace cricket |
| 140 | |
| 141 | #endif // TALK_MEDIA_BASE_RTPDATAENGINE_H_ |