blob: e4878e7d64811ca8ab82f4c2ad0c08046038fc52 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/buffer.h"
35#include "webrtc/base/byteorder.h"
36#include "webrtc/base/criticalsection.h"
37#include "webrtc/base/dscp.h"
38#include "webrtc/base/messagehandler.h"
39#include "webrtc/base/messagequeue.h"
40#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/media/base/mediachannel.h"
42#include "talk/media/base/rtputils.h"
43
44namespace cricket {
45
46// Fake NetworkInterface that sends/receives RTP/RTCP packets.
47class FakeNetworkInterface : public MediaChannel::NetworkInterface,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000048 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 public:
50 FakeNetworkInterface()
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051 : thread_(rtc::Thread::Current()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 dest_(NULL),
53 conf_(false),
54 sendbuf_size_(-1),
wu@webrtc.orgde305012013-10-31 15:40:38 +000055 recvbuf_size_(-1),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000056 dscp_(rtc::DSCP_NO_CHANGE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 }
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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 conf_ = conf;
67 conf_sent_ssrcs_ = ssrcs;
68 }
69
70 int NumRtpBytes() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 int bytes = 0;
73 for (size_t i = 0; i < rtp_packets_.size(); ++i) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +000074 bytes += static_cast<int>(rtp_packets_[i].length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 }
76 return bytes;
77 }
78
79 int NumRtpBytes(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 int bytes = 0;
82 GetNumRtpBytesAndPackets(ssrc, &bytes, NULL);
83 return bytes;
84 }
85
86 int NumRtpPackets() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000087 rtc::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +000088 return static_cast<int>(rtp_packets_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 }
90
91 int NumRtpPackets(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 int packets = 0;
94 GetNumRtpBytesAndPackets(ssrc, NULL, &packets);
95 return packets;
96 }
97
98 int NumSentSsrcs() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099 rtc::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000100 return static_cast<int>(sent_ssrcs_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 }
102
103 // Note: callers are responsible for deleting the returned buffer.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 const rtc::Buffer* GetRtpPacket(int index) {
105 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 if (index >= NumRtpPackets()) {
107 return NULL;
108 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109 return new rtc::Buffer(rtp_packets_[index]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 }
111
112 int NumRtcpPackets() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000113 rtc::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000114 return static_cast<int>(rtcp_packets_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 }
116
117 // Note: callers are responsible for deleting the returned buffer.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000118 const rtc::Buffer* GetRtcpPacket(int index) {
119 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 if (index >= NumRtcpPackets()) {
121 return NULL;
122 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000123 return new rtc::Buffer(rtcp_packets_[index]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 }
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_; }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000133 rtc::DiffServCodePoint dscp() const { return dscp_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134
135 protected:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000136 virtual bool SendPacket(rtc::Buffer* packet,
137 rtc::DiffServCodePoint dscp) {
138 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
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_) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000157 rtc::Buffer buffer_copy(*packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000171 virtual bool SendRtcp(rtc::Buffer* packet,
172 rtc::DiffServCodePoint dscp) {
173 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000182 virtual int SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000184 if (opt == rtc::Socket::OPT_SNDBUF) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 sendbuf_size_ = option;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000186 } else if (opt == rtc::Socket::OPT_RCVBUF) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 recvbuf_size_ = option;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188 } else if (opt == rtc::Socket::OPT_DSCP) {
189 dscp_ = static_cast<rtc::DiffServCodePoint>(option);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 }
191 return 0;
192 }
193
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000194 void PostMessage(int id, const rtc::Buffer& packet) {
195 thread_->Post(this, id, rtc::WrapMessageData(packet));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 }
197
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 virtual void OnMessage(rtc::Message* msg) {
199 rtc::TypedMessageData<rtc::Buffer>* msg_data =
200 static_cast<rtc::TypedMessageData<rtc::Buffer>*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 msg->pdata);
202 if (dest_) {
203 if (msg->message_id == ST_RTP) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000204 dest_->OnPacketReceived(&msg_data->data(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000205 rtc::CreatePacketTime(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000207 dest_->OnRtcpReceived(&msg_data->data(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000208 rtc::CreatePacketTime(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 }
210 }
211 delete msg_data;
212 }
213
214 private:
215 void GetNumRtpBytesAndPackets(uint32 ssrc, int* bytes, int* packets) {
216 if (bytes) {
217 *bytes = 0;
218 }
219 if (packets) {
220 *packets = 0;
221 }
222 uint32 cur_ssrc = 0;
223 for (size_t i = 0; i < rtp_packets_.size(); ++i) {
224 if (!GetRtpSsrc(rtp_packets_[i].data(),
225 rtp_packets_[i].length(), &cur_ssrc)) {
226 return;
227 }
228 if (ssrc == cur_ssrc) {
229 if (bytes) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000230 *bytes += static_cast<int>(rtp_packets_[i].length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 }
232 if (packets) {
233 ++(*packets);
234 }
235 }
236 }
237 }
238
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000239 rtc::Thread* thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 MediaChannel* dest_;
241 bool conf_;
242 // The ssrcs used in sending out packets in conference mode.
243 std::vector<uint32> conf_sent_ssrcs_;
244 // Map to track counts of packets that have been sent per ssrc.
245 // This includes packets that are dropped.
246 std::map<uint32, uint32> sent_ssrcs_;
247 // Map to track packet-number that needs to be dropped per ssrc.
248 std::map<uint32, std::set<uint32> > drop_map_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000249 rtc::CriticalSection crit_;
250 std::vector<rtc::Buffer> rtp_packets_;
251 std::vector<rtc::Buffer> rtcp_packets_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 int sendbuf_size_;
253 int recvbuf_size_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000254 rtc::DiffServCodePoint dscp_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255};
256
257} // namespace cricket
258
259#endif // TALK_MEDIA_BASE_FAKENETWORKINTERFACE_H_