henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 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_FAKENETWORKINTERFACE_H_ |
| 29 | #define TALK_MEDIA_BASE_FAKENETWORKINTERFACE_H_ |
| 30 | |
| 31 | #include <vector> |
| 32 | #include <map> |
| 33 | |
| 34 | #include "talk/base/buffer.h" |
| 35 | #include "talk/base/byteorder.h" |
| 36 | #include "talk/base/criticalsection.h" |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame^] | 37 | #include "talk/base/dscp.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | #include "talk/base/messagehandler.h" |
| 39 | #include "talk/base/messagequeue.h" |
| 40 | #include "talk/base/thread.h" |
| 41 | #include "talk/media/base/mediachannel.h" |
| 42 | #include "talk/media/base/rtputils.h" |
| 43 | |
| 44 | namespace cricket { |
| 45 | |
| 46 | // Fake NetworkInterface that sends/receives RTP/RTCP packets. |
| 47 | class FakeNetworkInterface : public MediaChannel::NetworkInterface, |
| 48 | public talk_base::MessageHandler { |
| 49 | public: |
| 50 | FakeNetworkInterface() |
| 51 | : thread_(talk_base::Thread::Current()), |
| 52 | dest_(NULL), |
| 53 | conf_(false), |
| 54 | sendbuf_size_(-1), |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame^] | 55 | recvbuf_size_(-1), |
| 56 | dscp_(talk_base::DSCP_NO_CHANGE) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void SetDestination(MediaChannel* dest) { dest_ = dest; } |
| 60 | |
| 61 | // Conference mode is a mode where instead of simply forwarding the packets, |
| 62 | // the transport will send multiple copies of the packet with the specified |
| 63 | // SSRCs. This allows us to simulate receiving media from multiple sources. |
| 64 | void SetConferenceMode(bool conf, const std::vector<uint32>& ssrcs) { |
| 65 | talk_base::CritScope cs(&crit_); |
| 66 | conf_ = conf; |
| 67 | conf_sent_ssrcs_ = ssrcs; |
| 68 | } |
| 69 | |
| 70 | int NumRtpBytes() { |
| 71 | talk_base::CritScope cs(&crit_); |
| 72 | int bytes = 0; |
| 73 | for (size_t i = 0; i < rtp_packets_.size(); ++i) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 74 | bytes += static_cast<int>(rtp_packets_[i].length()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | } |
| 76 | return bytes; |
| 77 | } |
| 78 | |
| 79 | int NumRtpBytes(uint32 ssrc) { |
| 80 | talk_base::CritScope cs(&crit_); |
| 81 | int bytes = 0; |
| 82 | GetNumRtpBytesAndPackets(ssrc, &bytes, NULL); |
| 83 | return bytes; |
| 84 | } |
| 85 | |
| 86 | int NumRtpPackets() { |
| 87 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 88 | return static_cast<int>(rtp_packets_.size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int NumRtpPackets(uint32 ssrc) { |
| 92 | talk_base::CritScope cs(&crit_); |
| 93 | int packets = 0; |
| 94 | GetNumRtpBytesAndPackets(ssrc, NULL, &packets); |
| 95 | return packets; |
| 96 | } |
| 97 | |
| 98 | int NumSentSsrcs() { |
| 99 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 100 | return static_cast<int>(sent_ssrcs_.size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Note: callers are responsible for deleting the returned buffer. |
| 104 | const talk_base::Buffer* GetRtpPacket(int index) { |
| 105 | talk_base::CritScope cs(&crit_); |
| 106 | if (index >= NumRtpPackets()) { |
| 107 | return NULL; |
| 108 | } |
| 109 | return new talk_base::Buffer(rtp_packets_[index]); |
| 110 | } |
| 111 | |
| 112 | int NumRtcpPackets() { |
| 113 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 114 | return static_cast<int>(rtcp_packets_.size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Note: callers are responsible for deleting the returned buffer. |
| 118 | const talk_base::Buffer* GetRtcpPacket(int index) { |
| 119 | talk_base::CritScope cs(&crit_); |
| 120 | if (index >= NumRtcpPackets()) { |
| 121 | return NULL; |
| 122 | } |
| 123 | return new talk_base::Buffer(rtcp_packets_[index]); |
| 124 | } |
| 125 | |
| 126 | // Indicate that |n|'th packet for |ssrc| should be dropped. |
| 127 | void AddPacketDrop(uint32 ssrc, uint32 n) { |
| 128 | drop_map_[ssrc].insert(n); |
| 129 | } |
| 130 | |
| 131 | int sendbuf_size() const { return sendbuf_size_; } |
| 132 | int recvbuf_size() const { return recvbuf_size_; } |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame^] | 133 | talk_base::DiffServCodePoint dscp() const { return dscp_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | |
| 135 | protected: |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 136 | virtual bool SendPacket(talk_base::Buffer* packet, |
| 137 | talk_base::DiffServCodePoint dscp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | talk_base::CritScope cs(&crit_); |
| 139 | |
| 140 | uint32 cur_ssrc = 0; |
| 141 | if (!GetRtpSsrc(packet->data(), packet->length(), &cur_ssrc)) { |
| 142 | return false; |
| 143 | } |
| 144 | sent_ssrcs_[cur_ssrc]++; |
| 145 | |
| 146 | // Check if we need to drop this packet. |
| 147 | std::map<uint32, std::set<uint32> >::iterator itr = |
| 148 | drop_map_.find(cur_ssrc); |
| 149 | if (itr != drop_map_.end() && |
| 150 | itr->second.count(sent_ssrcs_[cur_ssrc]) > 0) { |
| 151 | // "Drop" the packet. |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | rtp_packets_.push_back(*packet); |
| 156 | if (conf_) { |
| 157 | talk_base::Buffer buffer_copy(*packet); |
| 158 | for (size_t i = 0; i < conf_sent_ssrcs_.size(); ++i) { |
| 159 | if (!SetRtpSsrc(buffer_copy.data(), buffer_copy.length(), |
| 160 | conf_sent_ssrcs_[i])) { |
| 161 | return false; |
| 162 | } |
| 163 | PostMessage(ST_RTP, buffer_copy); |
| 164 | } |
| 165 | } else { |
| 166 | PostMessage(ST_RTP, *packet); |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 171 | virtual bool SendRtcp(talk_base::Buffer* packet, |
| 172 | talk_base::DiffServCodePoint dscp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | talk_base::CritScope cs(&crit_); |
| 174 | rtcp_packets_.push_back(*packet); |
| 175 | if (!conf_) { |
| 176 | // don't worry about RTCP in conf mode for now |
| 177 | PostMessage(ST_RTCP, *packet); |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | virtual int SetOption(SocketType type, talk_base::Socket::Option opt, |
| 183 | int option) { |
| 184 | if (opt == talk_base::Socket::OPT_SNDBUF) { |
| 185 | sendbuf_size_ = option; |
| 186 | } else if (opt == talk_base::Socket::OPT_RCVBUF) { |
| 187 | recvbuf_size_ = option; |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame^] | 188 | } else if (opt == talk_base::Socket::OPT_DSCP) { |
| 189 | dscp_ = static_cast<talk_base::DiffServCodePoint>(option); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | void PostMessage(int id, const talk_base::Buffer& packet) { |
| 195 | thread_->Post(this, id, talk_base::WrapMessageData(packet)); |
| 196 | } |
| 197 | |
| 198 | virtual void OnMessage(talk_base::Message* msg) { |
| 199 | talk_base::TypedMessageData<talk_base::Buffer>* msg_data = |
| 200 | static_cast<talk_base::TypedMessageData<talk_base::Buffer>*>( |
| 201 | msg->pdata); |
| 202 | if (dest_) { |
| 203 | if (msg->message_id == ST_RTP) { |
| 204 | dest_->OnPacketReceived(&msg_data->data()); |
| 205 | } else { |
| 206 | dest_->OnRtcpReceived(&msg_data->data()); |
| 207 | } |
| 208 | } |
| 209 | delete msg_data; |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | void GetNumRtpBytesAndPackets(uint32 ssrc, int* bytes, int* packets) { |
| 214 | if (bytes) { |
| 215 | *bytes = 0; |
| 216 | } |
| 217 | if (packets) { |
| 218 | *packets = 0; |
| 219 | } |
| 220 | uint32 cur_ssrc = 0; |
| 221 | for (size_t i = 0; i < rtp_packets_.size(); ++i) { |
| 222 | if (!GetRtpSsrc(rtp_packets_[i].data(), |
| 223 | rtp_packets_[i].length(), &cur_ssrc)) { |
| 224 | return; |
| 225 | } |
| 226 | if (ssrc == cur_ssrc) { |
| 227 | if (bytes) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 228 | *bytes += static_cast<int>(rtp_packets_[i].length()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 229 | } |
| 230 | if (packets) { |
| 231 | ++(*packets); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | talk_base::Thread* thread_; |
| 238 | MediaChannel* dest_; |
| 239 | bool conf_; |
| 240 | // The ssrcs used in sending out packets in conference mode. |
| 241 | std::vector<uint32> conf_sent_ssrcs_; |
| 242 | // Map to track counts of packets that have been sent per ssrc. |
| 243 | // This includes packets that are dropped. |
| 244 | std::map<uint32, uint32> sent_ssrcs_; |
| 245 | // Map to track packet-number that needs to be dropped per ssrc. |
| 246 | std::map<uint32, std::set<uint32> > drop_map_; |
| 247 | talk_base::CriticalSection crit_; |
| 248 | std::vector<talk_base::Buffer> rtp_packets_; |
| 249 | std::vector<talk_base::Buffer> rtcp_packets_; |
| 250 | int sendbuf_size_; |
| 251 | int recvbuf_size_; |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame^] | 252 | talk_base::DiffServCodePoint dscp_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | } // namespace cricket |
| 256 | |
| 257 | #endif // TALK_MEDIA_BASE_FAKENETWORKINTERFACE_H_ |