henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle SCTP |
| 3 | * Copyright 2013 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 | #include <errno.h> |
| 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string> |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 32 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 34 | #include "talk/app/webrtc/datachannelinterface.h" |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 35 | #include "talk/base/bind.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | #include "talk/base/buffer.h" |
| 37 | #include "talk/base/criticalsection.h" |
| 38 | #include "talk/base/gunit.h" |
| 39 | #include "talk/base/helpers.h" |
| 40 | #include "talk/base/messagehandler.h" |
| 41 | #include "talk/base/messagequeue.h" |
| 42 | #include "talk/base/scoped_ptr.h" |
| 43 | #include "talk/base/thread.h" |
| 44 | #include "talk/media/base/constants.h" |
| 45 | #include "talk/media/base/mediachannel.h" |
| 46 | #include "talk/media/sctp/sctpdataengine.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 47 | #include "talk/media/sctp/sctputils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | |
| 49 | enum { |
| 50 | MSG_PACKET = 1, |
| 51 | }; |
| 52 | |
| 53 | // Fake NetworkInterface that sends/receives sctp packets. The one in |
| 54 | // talk/media/base/fakenetworkinterface.h only works with rtp/rtcp. |
| 55 | class SctpFakeNetworkInterface : public cricket::MediaChannel::NetworkInterface, |
| 56 | public talk_base::MessageHandler { |
| 57 | public: |
| 58 | explicit SctpFakeNetworkInterface(talk_base::Thread* thread) |
| 59 | : thread_(thread), |
| 60 | dest_(NULL) { |
| 61 | } |
| 62 | |
| 63 | void SetDestination(cricket::DataMediaChannel* dest) { dest_ = dest; } |
| 64 | |
| 65 | protected: |
| 66 | // Called to send raw packet down the wire (e.g. SCTP an packet). |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 67 | virtual bool SendPacket(talk_base::Buffer* packet, |
| 68 | talk_base::DiffServCodePoint dscp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket"; |
| 70 | |
| 71 | // TODO(ldixon): Can/should we use Buffer.TransferTo here? |
| 72 | // Note: this assignment does a deep copy of data from packet. |
| 73 | talk_base::Buffer* buffer = new talk_base::Buffer(packet->data(), |
| 74 | packet->length()); |
| 75 | thread_->Post(this, MSG_PACKET, talk_base::WrapMessageData(buffer)); |
| 76 | LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket, Posted message."; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | // Called when a raw packet has been recieved. This passes the data to the |
| 81 | // code that will interpret the packet. e.g. to get the content payload from |
| 82 | // an SCTP packet. |
| 83 | virtual void OnMessage(talk_base::Message* msg) { |
| 84 | LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::OnMessage"; |
| 85 | talk_base::Buffer* buffer = |
| 86 | static_cast<talk_base::TypedMessageData<talk_base::Buffer*>*>( |
| 87 | msg->pdata)->data(); |
| 88 | if (dest_) { |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 89 | dest_->OnPacketReceived(buffer, talk_base::PacketTime()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | } |
| 91 | delete buffer; |
| 92 | } |
| 93 | |
| 94 | // Unsupported functions required to exist by NetworkInterface. |
| 95 | // TODO(ldixon): Refactor parent NetworkInterface class so these are not |
| 96 | // required. They are RTC specific and should be in an appropriate subclass. |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 97 | virtual bool SendRtcp(talk_base::Buffer* packet, |
| 98 | talk_base::DiffServCodePoint dscp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SendRtcp."; |
| 100 | return false; |
| 101 | } |
| 102 | virtual int SetOption(SocketType type, talk_base::Socket::Option opt, |
| 103 | int option) { |
| 104 | LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SetOption."; |
| 105 | return 0; |
| 106 | } |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 107 | virtual void SetDefaultDSCPCode(talk_base::DiffServCodePoint dscp) { |
| 108 | LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SetOption."; |
| 109 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | // Not owned by this class. |
| 113 | talk_base::Thread* thread_; |
| 114 | cricket::DataMediaChannel* dest_; |
| 115 | }; |
| 116 | |
| 117 | // This is essentially a buffer to hold recieved data. It stores only the last |
| 118 | // received data. Calling OnDataReceived twice overwrites old data with the |
| 119 | // newer one. |
| 120 | // TODO(ldixon): Implement constraints, and allow new data to be added to old |
| 121 | // instead of replacing it. |
| 122 | class SctpFakeDataReceiver : public sigslot::has_slots<> { |
| 123 | public: |
| 124 | SctpFakeDataReceiver() : received_(false) {} |
| 125 | |
| 126 | void Clear() { |
| 127 | received_ = false; |
| 128 | last_data_ = ""; |
| 129 | last_params_ = cricket::ReceiveDataParams(); |
| 130 | } |
| 131 | |
| 132 | virtual void OnDataReceived(const cricket::ReceiveDataParams& params, |
| 133 | const char* data, size_t length) { |
| 134 | received_ = true; |
| 135 | last_data_ = std::string(data, length); |
| 136 | last_params_ = params; |
| 137 | } |
| 138 | |
| 139 | bool received() const { return received_; } |
| 140 | std::string last_data() const { return last_data_; } |
| 141 | cricket::ReceiveDataParams last_params() const { return last_params_; } |
| 142 | |
| 143 | private: |
| 144 | bool received_; |
| 145 | std::string last_data_; |
| 146 | cricket::ReceiveDataParams last_params_; |
| 147 | }; |
| 148 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 149 | class SignalReadyToSendObserver : public sigslot::has_slots<> { |
| 150 | public: |
| 151 | SignalReadyToSendObserver() : signaled_(false), writable_(false) {} |
| 152 | |
| 153 | void OnSignaled(bool writable) { |
| 154 | signaled_ = true; |
| 155 | writable_ = writable; |
| 156 | } |
| 157 | |
| 158 | bool IsSignaled(bool writable) { |
| 159 | return signaled_ && (writable_ == writable); |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | bool signaled_; |
| 164 | bool writable_; |
| 165 | }; |
| 166 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 167 | class SignalChannelClosedObserver : public sigslot::has_slots<> { |
| 168 | public: |
| 169 | SignalChannelClosedObserver() {} |
| 170 | void BindSelf(cricket::SctpDataMediaChannel* channel) { |
| 171 | channel->SignalStreamClosed.connect( |
| 172 | this, &SignalChannelClosedObserver::OnStreamClosed); |
| 173 | } |
| 174 | void OnStreamClosed(int stream) { |
| 175 | streams_.push_back(stream); |
| 176 | } |
| 177 | |
| 178 | int StreamCloseCount(int stream) { |
| 179 | return std::count(streams_.begin(), streams_.end(), stream); |
| 180 | } |
| 181 | |
| 182 | bool WasStreamClosed(int stream) { |
| 183 | return std::find(streams_.begin(), streams_.end(), stream) |
| 184 | != streams_.end(); |
| 185 | } |
| 186 | |
| 187 | private: |
| 188 | std::vector<int> streams_; |
| 189 | }; |
| 190 | |
| 191 | class SignalChannelClosedReopener : public sigslot::has_slots<> { |
| 192 | public: |
| 193 | SignalChannelClosedReopener(cricket::SctpDataMediaChannel* channel, |
| 194 | cricket::SctpDataMediaChannel* peer) |
| 195 | : channel_(channel), peer_(peer) {} |
| 196 | |
| 197 | void OnStreamClosed(int stream) { |
| 198 | cricket::StreamParams p(cricket::StreamParams::CreateLegacy(stream)); |
| 199 | channel_->AddSendStream(p); |
| 200 | channel_->AddRecvStream(p); |
| 201 | peer_->AddSendStream(p); |
| 202 | peer_->AddRecvStream(p); |
| 203 | streams_.push_back(stream); |
| 204 | } |
| 205 | |
| 206 | int StreamCloseCount(int stream) { |
| 207 | return std::count(streams_.begin(), streams_.end(), stream); |
| 208 | } |
| 209 | |
| 210 | private: |
| 211 | cricket::SctpDataMediaChannel* channel_; |
| 212 | cricket::SctpDataMediaChannel* peer_; |
| 213 | std::vector<int> streams_; |
| 214 | }; |
| 215 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | // SCTP Data Engine testing framework. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 217 | class SctpDataMediaChannelTest : public testing::Test, |
| 218 | public sigslot::has_slots<> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | protected: |
| 220 | virtual void SetUp() { |
| 221 | engine_.reset(new cricket::SctpDataEngine()); |
| 222 | } |
| 223 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 224 | void SetupConnectedChannels() { |
| 225 | net1_.reset(new SctpFakeNetworkInterface(talk_base::Thread::Current())); |
| 226 | net2_.reset(new SctpFakeNetworkInterface(talk_base::Thread::Current())); |
| 227 | recv1_.reset(new SctpFakeDataReceiver()); |
| 228 | recv2_.reset(new SctpFakeDataReceiver()); |
| 229 | chan1_.reset(CreateChannel(net1_.get(), recv1_.get())); |
| 230 | chan1_->set_debug_name("chan1/connector"); |
| 231 | chan2_.reset(CreateChannel(net2_.get(), recv2_.get())); |
| 232 | chan2_->set_debug_name("chan2/listener"); |
| 233 | // Setup two connected channels ready to send and receive. |
| 234 | net1_->SetDestination(chan2_.get()); |
| 235 | net2_->SetDestination(chan1_.get()); |
| 236 | |
| 237 | LOG(LS_VERBOSE) << "Channel setup ----------------------------- "; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 238 | AddStream(1); |
| 239 | AddStream(2); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 240 | |
| 241 | LOG(LS_VERBOSE) << "Connect the channels -----------------------------"; |
| 242 | // chan1 wants to setup a data connection. |
| 243 | chan1_->SetReceive(true); |
| 244 | // chan1 will have sent chan2 a request to setup a data connection. After |
| 245 | // chan2 accepts the offer, chan2 connects to chan1 with the following. |
| 246 | chan2_->SetReceive(true); |
| 247 | chan2_->SetSend(true); |
| 248 | // Makes sure that network packets are delivered and simulates a |
| 249 | // deterministic and realistic small timing delay between the SetSend calls. |
| 250 | ProcessMessagesUntilIdle(); |
| 251 | |
| 252 | // chan1 and chan2 are now connected so chan1 enables sending to complete |
| 253 | // the creation of the connection. |
| 254 | chan1_->SetSend(true); |
| 255 | } |
| 256 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 257 | virtual void TearDown() { |
| 258 | channel1()->SetSend(false); |
| 259 | channel2()->SetSend(false); |
| 260 | } |
| 261 | |
| 262 | void AddStream(int ssrc) { |
| 263 | cricket::StreamParams p(cricket::StreamParams::CreateLegacy(ssrc)); |
| 264 | chan1_->AddSendStream(p); |
| 265 | chan1_->AddRecvStream(p); |
| 266 | chan2_->AddSendStream(p); |
| 267 | chan2_->AddRecvStream(p); |
| 268 | } |
| 269 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 270 | cricket::SctpDataMediaChannel* CreateChannel( |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 271 | SctpFakeNetworkInterface* net, SctpFakeDataReceiver* recv) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 272 | cricket::SctpDataMediaChannel* channel = |
| 273 | static_cast<cricket::SctpDataMediaChannel*>(engine_->CreateChannel( |
| 274 | cricket::DCT_SCTP)); |
| 275 | channel->SetInterface(net); |
| 276 | // When data is received, pass it to the SctpFakeDataReceiver. |
| 277 | channel->SignalDataReceived.connect( |
| 278 | recv, &SctpFakeDataReceiver::OnDataReceived); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 279 | channel->SignalNewStreamReceived.connect( |
| 280 | this, &SctpDataMediaChannelTest::OnNewStreamReceived); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 281 | return channel; |
| 282 | } |
| 283 | |
| 284 | bool SendData(cricket::SctpDataMediaChannel* chan, uint32 ssrc, |
| 285 | const std::string& msg, |
| 286 | cricket::SendDataResult* result) { |
| 287 | cricket::SendDataParams params; |
| 288 | params.ssrc = ssrc; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 289 | return chan->SendData(params, talk_base::Buffer( |
| 290 | msg.data(), msg.length()), result); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool ReceivedData(const SctpFakeDataReceiver* recv, uint32 ssrc, |
| 294 | const std::string& msg ) { |
| 295 | return (recv->received() && |
| 296 | recv->last_params().ssrc == ssrc && |
| 297 | recv->last_data() == msg); |
| 298 | } |
| 299 | |
| 300 | bool ProcessMessagesUntilIdle() { |
| 301 | talk_base::Thread* thread = talk_base::Thread::Current(); |
| 302 | while (!thread->empty()) { |
| 303 | talk_base::Message msg; |
| 304 | if (thread->Get(&msg, talk_base::kForever)) { |
| 305 | thread->Dispatch(&msg); |
| 306 | } |
| 307 | } |
| 308 | return !thread->IsQuitting(); |
| 309 | } |
| 310 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 311 | cricket::SctpDataMediaChannel* channel1() { return chan1_.get(); } |
| 312 | cricket::SctpDataMediaChannel* channel2() { return chan2_.get(); } |
| 313 | SctpFakeDataReceiver* receiver1() { return recv1_.get(); } |
| 314 | SctpFakeDataReceiver* receiver2() { return recv2_.get(); } |
| 315 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 316 | void OnNewStreamReceived(const std::string& label, |
| 317 | const webrtc::DataChannelInit& init) { |
| 318 | last_label_ = label; |
| 319 | last_dc_init_ = init; |
| 320 | } |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 321 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 322 | std::string last_label() { return last_label_; } |
| 323 | webrtc::DataChannelInit last_dc_init() { return last_dc_init_; } |
| 324 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 325 | private: |
| 326 | talk_base::scoped_ptr<cricket::SctpDataEngine> engine_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 327 | talk_base::scoped_ptr<SctpFakeNetworkInterface> net1_; |
| 328 | talk_base::scoped_ptr<SctpFakeNetworkInterface> net2_; |
| 329 | talk_base::scoped_ptr<SctpFakeDataReceiver> recv1_; |
| 330 | talk_base::scoped_ptr<SctpFakeDataReceiver> recv2_; |
| 331 | talk_base::scoped_ptr<cricket::SctpDataMediaChannel> chan1_; |
| 332 | talk_base::scoped_ptr<cricket::SctpDataMediaChannel> chan2_; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 333 | std::string last_label_; |
| 334 | webrtc::DataChannelInit last_dc_init_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 337 | // Verifies that SignalReadyToSend is fired. |
| 338 | TEST_F(SctpDataMediaChannelTest, SignalReadyToSend) { |
| 339 | SetupConnectedChannels(); |
| 340 | |
| 341 | SignalReadyToSendObserver signal_observer_1; |
| 342 | SignalReadyToSendObserver signal_observer_2; |
| 343 | |
| 344 | channel1()->SignalReadyToSend.connect(&signal_observer_1, |
| 345 | &SignalReadyToSendObserver::OnSignaled); |
| 346 | channel2()->SignalReadyToSend.connect(&signal_observer_2, |
| 347 | &SignalReadyToSendObserver::OnSignaled); |
| 348 | |
| 349 | cricket::SendDataResult result; |
| 350 | ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); |
| 351 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 352 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); |
| 353 | ASSERT_TRUE(SendData(channel2(), 2, "hi chan1", &result)); |
| 354 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 355 | EXPECT_TRUE_WAIT(ReceivedData(receiver1(), 2, "hi chan1"), 1000); |
| 356 | |
| 357 | EXPECT_TRUE_WAIT(signal_observer_1.IsSignaled(true), 1000); |
| 358 | EXPECT_TRUE_WAIT(signal_observer_2.IsSignaled(true), 1000); |
| 359 | } |
| 360 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 361 | TEST_F(SctpDataMediaChannelTest, SendData) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 362 | SetupConnectedChannels(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 363 | |
| 364 | cricket::SendDataResult result; |
| 365 | LOG(LS_VERBOSE) << "chan1 sending: 'hello?' -----------------------------"; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 366 | ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 368 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); |
| 369 | LOG(LS_VERBOSE) << "recv2.received=" << receiver2()->received() |
| 370 | << "recv2.last_params.ssrc=" |
| 371 | << receiver2()->last_params().ssrc |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 372 | << "recv2.last_params.timestamp=" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 373 | << receiver2()->last_params().ssrc |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 374 | << "recv2.last_params.seq_num=" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 375 | << receiver2()->last_params().seq_num |
| 376 | << "recv2.last_data=" << receiver2()->last_data(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 377 | |
| 378 | LOG(LS_VERBOSE) << "chan2 sending: 'hi chan1' -----------------------------"; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 379 | ASSERT_TRUE(SendData(channel2(), 2, "hi chan1", &result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 380 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 381 | EXPECT_TRUE_WAIT(ReceivedData(receiver1(), 2, "hi chan1"), 1000); |
| 382 | LOG(LS_VERBOSE) << "recv1.received=" << receiver1()->received() |
| 383 | << "recv1.last_params.ssrc=" |
| 384 | << receiver1()->last_params().ssrc |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 385 | << "recv1.last_params.timestamp=" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 386 | << receiver1()->last_params().ssrc |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | << "recv1.last_params.seq_num=" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 388 | << receiver1()->last_params().seq_num |
| 389 | << "recv1.last_data=" << receiver1()->last_data(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 390 | } |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 391 | |
| 392 | TEST_F(SctpDataMediaChannelTest, SendReceiveOpenMessage) { |
| 393 | SetupConnectedChannels(); |
| 394 | |
| 395 | std::string label("x"); |
| 396 | webrtc::DataChannelInit config; |
| 397 | config.id = 10; |
| 398 | |
| 399 | // Send the OPEN message on a unknown ssrc. |
| 400 | channel1()->AddSendStream(cricket::StreamParams::CreateLegacy(config.id)); |
| 401 | cricket::SendDataParams params; |
| 402 | params.ssrc = config.id; |
| 403 | params.type = cricket::DMT_CONTROL; |
| 404 | cricket::SendDataResult result; |
| 405 | talk_base::Buffer buffer; |
| 406 | ASSERT_TRUE(cricket::WriteDataChannelOpenMessage(label, config, &buffer)); |
| 407 | ASSERT_TRUE(channel1()->SendData(params, buffer, &result)); |
| 408 | // Send data on the new ssrc immediately after sending the OPEN message. |
| 409 | ASSERT_TRUE(SendData(channel1(), config.id, "hi chan2", &result)); |
| 410 | |
| 411 | // Verifies the received OPEN message. |
| 412 | EXPECT_TRUE_WAIT(last_label() == label, 1000); |
| 413 | EXPECT_EQ(config.id, last_dc_init().id); |
| 414 | EXPECT_EQ(true, last_dc_init().negotiated); |
| 415 | // Verifies the received data. |
| 416 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), config.id, "hi chan2"), 1000); |
| 417 | } |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame^] | 418 | |
| 419 | TEST_F(SctpDataMediaChannelTest, ClosesRemoteStream) { |
| 420 | SetupConnectedChannels(); |
| 421 | SignalChannelClosedObserver chan_1_sig_receiver, chan_2_sig_receiver; |
| 422 | chan_1_sig_receiver.BindSelf(channel1()); |
| 423 | chan_2_sig_receiver.BindSelf(channel2()); |
| 424 | |
| 425 | cricket::SendDataResult result; |
| 426 | ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); |
| 427 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 428 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); |
| 429 | ASSERT_TRUE(SendData(channel2(), 2, "hi chan1", &result)); |
| 430 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 431 | EXPECT_TRUE_WAIT(ReceivedData(receiver1(), 2, "hi chan1"), 1000); |
| 432 | |
| 433 | // Close channel 1. Channel 2 should notify us. |
| 434 | channel1()->RemoveSendStream(1); |
| 435 | EXPECT_TRUE_WAIT(chan_2_sig_receiver.WasStreamClosed(1), 1000); |
| 436 | } |
| 437 | |
| 438 | TEST_F(SctpDataMediaChannelTest, ClosesTwoRemoteStreams) { |
| 439 | SetupConnectedChannels(); |
| 440 | AddStream(3); |
| 441 | SignalChannelClosedObserver chan_1_sig_receiver, chan_2_sig_receiver; |
| 442 | chan_1_sig_receiver.BindSelf(channel1()); |
| 443 | chan_2_sig_receiver.BindSelf(channel2()); |
| 444 | |
| 445 | cricket::SendDataResult result; |
| 446 | ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); |
| 447 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 448 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); |
| 449 | ASSERT_TRUE(SendData(channel2(), 2, "hi chan1", &result)); |
| 450 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 451 | EXPECT_TRUE_WAIT(ReceivedData(receiver1(), 2, "hi chan1"), 1000); |
| 452 | |
| 453 | // Close two streams on one side. |
| 454 | channel2()->RemoveSendStream(2); |
| 455 | channel2()->RemoveSendStream(3); |
| 456 | EXPECT_TRUE_WAIT(chan_1_sig_receiver.WasStreamClosed(2), 1000); |
| 457 | EXPECT_TRUE_WAIT(chan_1_sig_receiver.WasStreamClosed(3), 1000); |
| 458 | } |
| 459 | |
| 460 | TEST_F(SctpDataMediaChannelTest, ClosesStreamsOnBothSides) { |
| 461 | SetupConnectedChannels(); |
| 462 | AddStream(3); |
| 463 | AddStream(4); |
| 464 | SignalChannelClosedObserver chan_1_sig_receiver, chan_2_sig_receiver; |
| 465 | chan_1_sig_receiver.BindSelf(channel1()); |
| 466 | chan_2_sig_receiver.BindSelf(channel2()); |
| 467 | |
| 468 | cricket::SendDataResult result; |
| 469 | ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); |
| 470 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 471 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); |
| 472 | ASSERT_TRUE(SendData(channel2(), 2, "hi chan1", &result)); |
| 473 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 474 | EXPECT_TRUE_WAIT(ReceivedData(receiver1(), 2, "hi chan1"), 1000); |
| 475 | |
| 476 | // Close one stream on channel1(), while closing three streams on |
| 477 | // channel2(). They will conflict (only one side can close anything at a |
| 478 | // time, apparently). Test the resolution of the conflict. |
| 479 | channel1()->RemoveSendStream(1); |
| 480 | |
| 481 | channel2()->RemoveSendStream(2); |
| 482 | channel2()->RemoveSendStream(3); |
| 483 | channel2()->RemoveSendStream(4); |
| 484 | EXPECT_TRUE_WAIT(chan_2_sig_receiver.WasStreamClosed(1), 1000); |
| 485 | EXPECT_TRUE_WAIT(chan_1_sig_receiver.WasStreamClosed(2), 1000); |
| 486 | EXPECT_TRUE_WAIT(chan_1_sig_receiver.WasStreamClosed(3), 1000); |
| 487 | EXPECT_TRUE_WAIT(chan_1_sig_receiver.WasStreamClosed(4), 1000); |
| 488 | } |
| 489 | |
| 490 | TEST_F(SctpDataMediaChannelTest, ReusesAStream) { |
| 491 | // Shut down channel 1, then open it up again for reuse. |
| 492 | SetupConnectedChannels(); |
| 493 | cricket::SendDataResult result; |
| 494 | SignalChannelClosedObserver chan_2_sig_receiver; |
| 495 | chan_2_sig_receiver.BindSelf(channel2()); |
| 496 | |
| 497 | ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); |
| 498 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 499 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); |
| 500 | |
| 501 | channel1()->RemoveSendStream(1); |
| 502 | EXPECT_TRUE_WAIT(chan_2_sig_receiver.WasStreamClosed(1), 1000); |
| 503 | // Channel 1 is gone now. |
| 504 | |
| 505 | // Create a new channel 1. |
| 506 | AddStream(1); |
| 507 | ASSERT_TRUE(SendData(channel1(), 1, "hi?", &result)); |
| 508 | EXPECT_EQ(cricket::SDR_SUCCESS, result); |
| 509 | EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hi?"), 1000); |
| 510 | channel1()->RemoveSendStream(1); |
| 511 | EXPECT_TRUE_WAIT(chan_2_sig_receiver.StreamCloseCount(1) == 2, 1000); |
| 512 | } |