OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <errno.h> | 11 #include <errno.h> |
12 #include <stdarg.h> | 12 #include <stdarg.h> |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
15 #include <memory> | 15 #include <memory> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "webrtc/base/bind.h" | 19 #include "webrtc/base/bind.h" |
20 #include "webrtc/base/buffer.h" | 20 #include "webrtc/base/copyonwritebuffer.h" |
21 #include "webrtc/base/criticalsection.h" | 21 #include "webrtc/base/criticalsection.h" |
22 #include "webrtc/base/gunit.h" | 22 #include "webrtc/base/gunit.h" |
23 #include "webrtc/base/helpers.h" | 23 #include "webrtc/base/helpers.h" |
24 #include "webrtc/base/messagehandler.h" | 24 #include "webrtc/base/messagehandler.h" |
25 #include "webrtc/base/messagequeue.h" | 25 #include "webrtc/base/messagequeue.h" |
26 #include "webrtc/base/ssladapter.h" | 26 #include "webrtc/base/ssladapter.h" |
27 #include "webrtc/base/thread.h" | 27 #include "webrtc/base/thread.h" |
28 #include "webrtc/media/base/mediachannel.h" | 28 #include "webrtc/media/base/mediachannel.h" |
29 #include "webrtc/media/base/mediaconstants.h" | 29 #include "webrtc/media/base/mediaconstants.h" |
30 #include "webrtc/media/sctp/sctpdataengine.h" | 30 #include "webrtc/media/sctp/sctpdataengine.h" |
31 | 31 |
32 enum { | 32 enum { |
33 MSG_PACKET = 1, | 33 MSG_PACKET = 1, |
34 }; | 34 }; |
35 | 35 |
36 // Fake NetworkInterface that sends/receives sctp packets. The one in | 36 // Fake NetworkInterface that sends/receives sctp packets. The one in |
37 // webrtc/media/base/fakenetworkinterface.h only works with rtp/rtcp. | 37 // webrtc/media/base/fakenetworkinterface.h only works with rtp/rtcp. |
38 class SctpFakeNetworkInterface : public cricket::MediaChannel::NetworkInterface, | 38 class SctpFakeNetworkInterface : public cricket::MediaChannel::NetworkInterface, |
39 public rtc::MessageHandler { | 39 public rtc::MessageHandler { |
40 public: | 40 public: |
41 explicit SctpFakeNetworkInterface(rtc::Thread* thread) | 41 explicit SctpFakeNetworkInterface(rtc::Thread* thread) |
42 : thread_(thread), | 42 : thread_(thread), |
43 dest_(NULL) { | 43 dest_(NULL) { |
44 } | 44 } |
45 | 45 |
46 void SetDestination(cricket::DataMediaChannel* dest) { dest_ = dest; } | 46 void SetDestination(cricket::DataMediaChannel* dest) { dest_ = dest; } |
47 | 47 |
48 protected: | 48 protected: |
49 // Called to send raw packet down the wire (e.g. SCTP an packet). | 49 // Called to send raw packet down the wire (e.g. SCTP an packet). |
50 virtual bool SendPacket(rtc::Buffer* packet, | 50 virtual bool SendPacket(rtc::CopyOnWriteBuffer* packet, |
51 const rtc::PacketOptions& options) { | 51 const rtc::PacketOptions& options) { |
52 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket"; | 52 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket"; |
53 | 53 |
54 // TODO(ldixon): Can/should we use Buffer.TransferTo here? | 54 rtc::CopyOnWriteBuffer* buffer = new rtc::CopyOnWriteBuffer(*packet); |
55 // Note: this assignment does a deep copy of data from packet. | |
56 rtc::Buffer* buffer = new rtc::Buffer(packet->data(), packet->size()); | |
57 thread_->Post(this, MSG_PACKET, rtc::WrapMessageData(buffer)); | 55 thread_->Post(this, MSG_PACKET, rtc::WrapMessageData(buffer)); |
58 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket, Posted message."; | 56 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket, Posted message."; |
59 return true; | 57 return true; |
60 } | 58 } |
61 | 59 |
62 // Called when a raw packet has been recieved. This passes the data to the | 60 // Called when a raw packet has been recieved. This passes the data to the |
63 // code that will interpret the packet. e.g. to get the content payload from | 61 // code that will interpret the packet. e.g. to get the content payload from |
64 // an SCTP packet. | 62 // an SCTP packet. |
65 virtual void OnMessage(rtc::Message* msg) { | 63 virtual void OnMessage(rtc::Message* msg) { |
66 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::OnMessage"; | 64 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::OnMessage"; |
67 std::unique_ptr<rtc::Buffer> buffer( | 65 std::unique_ptr<rtc::CopyOnWriteBuffer> buffer( |
68 static_cast<rtc::TypedMessageData<rtc::Buffer*>*>( | 66 static_cast<rtc::TypedMessageData<rtc::CopyOnWriteBuffer*>*>( |
69 msg->pdata)->data()); | 67 msg->pdata)->data()); |
70 if (dest_) { | 68 if (dest_) { |
71 dest_->OnPacketReceived(buffer.get(), rtc::PacketTime()); | 69 dest_->OnPacketReceived(buffer.get(), rtc::PacketTime()); |
72 } | 70 } |
73 delete msg->pdata; | 71 delete msg->pdata; |
74 } | 72 } |
75 | 73 |
76 // Unsupported functions required to exist by NetworkInterface. | 74 // Unsupported functions required to exist by NetworkInterface. |
77 // TODO(ldixon): Refactor parent NetworkInterface class so these are not | 75 // TODO(ldixon): Refactor parent NetworkInterface class so these are not |
78 // required. They are RTC specific and should be in an appropriate subclass. | 76 // required. They are RTC specific and should be in an appropriate subclass. |
79 virtual bool SendRtcp(rtc::Buffer* packet, | 77 virtual bool SendRtcp(rtc::CopyOnWriteBuffer* packet, |
80 const rtc::PacketOptions& options) { | 78 const rtc::PacketOptions& options) { |
81 LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SendRtcp."; | 79 LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SendRtcp."; |
82 return false; | 80 return false; |
83 } | 81 } |
84 virtual int SetOption(SocketType type, rtc::Socket::Option opt, | 82 virtual int SetOption(SocketType type, rtc::Socket::Option opt, |
85 int option) { | 83 int option) { |
86 LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SetOption."; | 84 LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SetOption."; |
87 return 0; | 85 return 0; |
88 } | 86 } |
89 virtual void SetDefaultDSCPCode(rtc::DiffServCodePoint dscp) { | 87 virtual void SetDefaultDSCPCode(rtc::DiffServCodePoint dscp) { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 return channel; | 274 return channel; |
277 } | 275 } |
278 | 276 |
279 bool SendData(cricket::SctpDataMediaChannel* chan, | 277 bool SendData(cricket::SctpDataMediaChannel* chan, |
280 uint32_t ssrc, | 278 uint32_t ssrc, |
281 const std::string& msg, | 279 const std::string& msg, |
282 cricket::SendDataResult* result) { | 280 cricket::SendDataResult* result) { |
283 cricket::SendDataParams params; | 281 cricket::SendDataParams params; |
284 params.ssrc = ssrc; | 282 params.ssrc = ssrc; |
285 | 283 |
286 return chan->SendData(params, rtc::Buffer( | 284 return chan->SendData(params, rtc::CopyOnWriteBuffer( |
287 &msg[0], msg.length()), result); | 285 &msg[0], msg.length()), result); |
288 } | 286 } |
289 | 287 |
290 bool ReceivedData(const SctpFakeDataReceiver* recv, | 288 bool ReceivedData(const SctpFakeDataReceiver* recv, |
291 uint32_t ssrc, | 289 uint32_t ssrc, |
292 const std::string& msg) { | 290 const std::string& msg) { |
293 return (recv->received() && | 291 return (recv->received() && |
294 recv->last_params().ssrc == ssrc && | 292 recv->last_params().ssrc == ssrc && |
295 recv->last_data() == msg); | 293 recv->last_data() == msg); |
296 } | 294 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 SetupConnectedChannels(); | 393 SetupConnectedChannels(); |
396 | 394 |
397 cricket::SendDataResult result; | 395 cricket::SendDataResult result; |
398 cricket::SendDataParams params; | 396 cricket::SendDataParams params; |
399 params.ssrc = 1; | 397 params.ssrc = 1; |
400 | 398 |
401 std::vector<char> buffer(1024 * 64, 0); | 399 std::vector<char> buffer(1024 * 64, 0); |
402 | 400 |
403 for (size_t i = 0; i < 100; ++i) { | 401 for (size_t i = 0; i < 100; ++i) { |
404 channel1()->SendData( | 402 channel1()->SendData( |
405 params, rtc::Buffer(&buffer[0], buffer.size()), &result); | 403 params, rtc::CopyOnWriteBuffer(&buffer[0], buffer.size()), &result); |
406 if (result == cricket::SDR_BLOCK) | 404 if (result == cricket::SDR_BLOCK) |
407 break; | 405 break; |
408 } | 406 } |
409 | 407 |
410 EXPECT_EQ(cricket::SDR_BLOCK, result); | 408 EXPECT_EQ(cricket::SDR_BLOCK, result); |
411 } | 409 } |
412 | 410 |
413 TEST_F(SctpDataMediaChannelTest, ClosesRemoteStream) { | 411 TEST_F(SctpDataMediaChannelTest, ClosesRemoteStream) { |
414 SetupConnectedChannels(); | 412 SetupConnectedChannels(); |
415 SignalChannelClosedObserver chan_1_sig_receiver, chan_2_sig_receiver; | 413 SignalChannelClosedObserver chan_1_sig_receiver, chan_2_sig_receiver; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 // Channel 1 is gone now. | 516 // Channel 1 is gone now. |
519 | 517 |
520 // Create a new channel 1. | 518 // Create a new channel 1. |
521 AddStream(1); | 519 AddStream(1); |
522 ASSERT_TRUE(SendData(channel1(), 1, "hi?", &result)); | 520 ASSERT_TRUE(SendData(channel1(), 1, "hi?", &result)); |
523 EXPECT_EQ(cricket::SDR_SUCCESS, result); | 521 EXPECT_EQ(cricket::SDR_SUCCESS, result); |
524 EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hi?"), 1000); | 522 EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hi?"), 1000); |
525 channel1()->RemoveSendStream(1); | 523 channel1()->RemoveSendStream(1); |
526 EXPECT_TRUE_WAIT(chan_2_sig_receiver.StreamCloseCount(1) == 2, 1000); | 524 EXPECT_TRUE_WAIT(chan_2_sig_receiver.StreamCloseCount(1) == 2, 1000); |
527 } | 525 } |
OLD | NEW |