You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
7.5 KiB

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