OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 13 matching lines...) Expand all Loading... |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include "talk/app/webrtc/datachannel.h" | 28 #include "talk/app/webrtc/datachannel.h" |
29 | 29 |
30 #include <string> | 30 #include <string> |
31 | 31 |
32 #include "talk/app/webrtc/mediastreamprovider.h" | 32 #include "talk/app/webrtc/mediastreamprovider.h" |
33 #include "talk/app/webrtc/sctputils.h" | 33 #include "talk/app/webrtc/sctputils.h" |
34 #include "talk/media/sctp/sctpdataengine.h" | |
35 #include "webrtc/base/logging.h" | 34 #include "webrtc/base/logging.h" |
36 #include "webrtc/base/refcount.h" | 35 #include "webrtc/base/refcount.h" |
37 | 36 |
38 namespace webrtc { | 37 namespace webrtc { |
39 | 38 |
40 static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; | 39 static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; |
41 static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; | 40 static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; |
42 | 41 |
43 enum { | 42 enum { |
44 MSG_CHANNELREADY, | 43 MSG_CHANNELREADY, |
45 }; | 44 }; |
46 | 45 |
47 bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) { | |
48 int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1; | |
49 while (!IsSidAvailable(potential_sid)) { | |
50 potential_sid += 2; | |
51 if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) { | |
52 return false; | |
53 } | |
54 } | |
55 | |
56 *sid = potential_sid; | |
57 used_sids_.insert(potential_sid); | |
58 return true; | |
59 } | |
60 | |
61 bool SctpSidAllocator::ReserveSid(int sid) { | |
62 if (!IsSidAvailable(sid)) { | |
63 return false; | |
64 } | |
65 used_sids_.insert(sid); | |
66 return true; | |
67 } | |
68 | |
69 void SctpSidAllocator::ReleaseSid(int sid) { | |
70 auto it = used_sids_.find(sid); | |
71 if (it != used_sids_.end()) { | |
72 used_sids_.erase(it); | |
73 } | |
74 } | |
75 | |
76 bool SctpSidAllocator::IsSidAvailable(int sid) const { | |
77 if (sid < 0 || sid > static_cast<int>(cricket::kMaxSctpSid)) { | |
78 return false; | |
79 } | |
80 return used_sids_.find(sid) == used_sids_.end(); | |
81 } | |
82 | |
83 DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} | 46 DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} |
84 | 47 |
85 DataChannel::PacketQueue::~PacketQueue() { | 48 DataChannel::PacketQueue::~PacketQueue() { |
86 Clear(); | 49 Clear(); |
87 } | 50 } |
88 | 51 |
89 bool DataChannel::PacketQueue::Empty() const { | 52 bool DataChannel::PacketQueue::Empty() const { |
90 return packets_.empty(); | 53 return packets_.empty(); |
91 } | 54 } |
92 | 55 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 UpdateState(); | 250 UpdateState(); |
288 } | 251 } |
289 | 252 |
290 // The remote peer request that this channel shall be closed. | 253 // The remote peer request that this channel shall be closed. |
291 void DataChannel::RemotePeerRequestClose() { | 254 void DataChannel::RemotePeerRequestClose() { |
292 DoClose(); | 255 DoClose(); |
293 } | 256 } |
294 | 257 |
295 void DataChannel::SetSctpSid(int sid) { | 258 void DataChannel::SetSctpSid(int sid) { |
296 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP); | 259 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP); |
297 if (config_.id == sid) { | 260 if (config_.id == sid) |
298 return; | 261 return; |
299 } | |
300 | 262 |
301 config_.id = sid; | 263 config_.id = sid; |
302 provider_->AddSctpDataStream(sid); | 264 provider_->AddSctpDataStream(sid); |
303 } | 265 } |
304 | 266 |
305 void DataChannel::OnTransportChannelCreated() { | 267 void DataChannel::OnTransportChannelCreated() { |
306 ASSERT(data_channel_type_ == cricket::DCT_SCTP); | 268 ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
307 if (!connected_to_provider_) { | 269 if (!connected_to_provider_) { |
308 connected_to_provider_ = provider_->ConnectDataChannel(this); | 270 connected_to_provider_ = provider_->ConnectDataChannel(this); |
309 } | 271 } |
310 // The sid may have been unassigned when provider_->ConnectDataChannel was | 272 // The sid may have been unassigned when provider_->ConnectDataChannel was |
311 // done. So always add the streams even if connected_to_provider_ is true. | 273 // done. So always add the streams even if connected_to_provider_ is true. |
312 if (config_.id >= 0) { | 274 if (config_.id >= 0) { |
313 provider_->AddSctpDataStream(config_.id); | 275 provider_->AddSctpDataStream(config_.id); |
314 } | 276 } |
315 } | 277 } |
316 | 278 |
317 // The underlying transport channel was destroyed. | |
318 // This function makes sure the DataChannel is disconnected and changes state to | |
319 // kClosed. | |
320 void DataChannel::OnTransportChannelDestroyed() { | |
321 DoClose(); | |
322 } | |
323 | |
324 void DataChannel::SetSendSsrc(uint32_t send_ssrc) { | 279 void DataChannel::SetSendSsrc(uint32_t send_ssrc) { |
325 ASSERT(data_channel_type_ == cricket::DCT_RTP); | 280 ASSERT(data_channel_type_ == cricket::DCT_RTP); |
326 if (send_ssrc_set_) { | 281 if (send_ssrc_set_) { |
327 return; | 282 return; |
328 } | 283 } |
329 send_ssrc_ = send_ssrc; | 284 send_ssrc_ = send_ssrc; |
330 send_ssrc_set_ = true; | 285 send_ssrc_set_ = true; |
331 UpdateState(); | 286 UpdateState(); |
332 } | 287 } |
333 | 288 |
334 void DataChannel::OnMessage(rtc::Message* msg) { | 289 void DataChannel::OnMessage(rtc::Message* msg) { |
335 switch (msg->message_id) { | 290 switch (msg->message_id) { |
336 case MSG_CHANNELREADY: | 291 case MSG_CHANNELREADY: |
337 OnChannelReady(true); | 292 OnChannelReady(true); |
338 break; | 293 break; |
339 } | 294 } |
340 } | 295 } |
341 | 296 |
| 297 // The underlaying data engine is closing. |
| 298 // This function makes sure the DataChannel is disconnected and changes state to |
| 299 // kClosed. |
| 300 void DataChannel::OnDataEngineClose() { |
| 301 DoClose(); |
| 302 } |
| 303 |
342 void DataChannel::OnDataReceived(cricket::DataChannel* channel, | 304 void DataChannel::OnDataReceived(cricket::DataChannel* channel, |
343 const cricket::ReceiveDataParams& params, | 305 const cricket::ReceiveDataParams& params, |
344 const rtc::Buffer& payload) { | 306 const rtc::Buffer& payload) { |
345 uint32_t expected_ssrc = | 307 uint32_t expected_ssrc = |
346 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; | 308 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; |
347 if (params.ssrc != expected_ssrc) { | 309 if (params.ssrc != expected_ssrc) { |
348 return; | 310 return; |
349 } | 311 } |
350 | 312 |
351 if (params.type == cricket::DMT_CONTROL) { | 313 if (params.type == cricket::DMT_CONTROL) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 if (data_channel_type_ != cricket::DCT_RTP) { | 354 if (data_channel_type_ != cricket::DCT_RTP) { |
393 Close(); | 355 Close(); |
394 } | 356 } |
395 | 357 |
396 return; | 358 return; |
397 } | 359 } |
398 queued_received_data_.Push(buffer.release()); | 360 queued_received_data_.Push(buffer.release()); |
399 } | 361 } |
400 } | 362 } |
401 | 363 |
402 void DataChannel::OnStreamClosedRemotely(uint32_t sid) { | |
403 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) { | |
404 Close(); | |
405 } | |
406 } | |
407 | |
408 void DataChannel::OnChannelReady(bool writable) { | 364 void DataChannel::OnChannelReady(bool writable) { |
409 writable_ = writable; | 365 writable_ = writable; |
410 if (!writable) { | 366 if (!writable) { |
411 return; | 367 return; |
412 } | 368 } |
413 | 369 |
414 SendQueuedControlMessages(); | 370 SendQueuedControlMessages(); |
415 SendQueuedDataMessages(); | 371 SendQueuedDataMessages(); |
416 UpdateState(); | 372 UpdateState(); |
417 } | 373 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 } | 429 } |
474 } | 430 } |
475 break; | 431 break; |
476 } | 432 } |
477 case kClosed: | 433 case kClosed: |
478 break; | 434 break; |
479 } | 435 } |
480 } | 436 } |
481 | 437 |
482 void DataChannel::SetState(DataState state) { | 438 void DataChannel::SetState(DataState state) { |
483 if (state_ == state) { | 439 if (state_ == state) |
484 return; | 440 return; |
485 } | |
486 | 441 |
487 state_ = state; | 442 state_ = state; |
488 if (observer_) { | 443 if (observer_) { |
489 observer_->OnStateChange(); | 444 observer_->OnStateChange(); |
490 } | 445 } |
491 if (state_ == kClosed) { | |
492 SignalClosed(this); | |
493 } | |
494 } | 446 } |
495 | 447 |
496 void DataChannel::DisconnectFromProvider() { | 448 void DataChannel::DisconnectFromProvider() { |
497 if (!connected_to_provider_) | 449 if (!connected_to_provider_) |
498 return; | 450 return; |
499 | 451 |
500 provider_->DisconnectDataChannel(this); | 452 provider_->DisconnectDataChannel(this); |
501 connected_to_provider_ = false; | 453 connected_to_provider_ = false; |
502 | 454 |
503 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) { | 455 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 QueueControlMessage(buffer); | 598 QueueControlMessage(buffer); |
647 } else { | 599 } else { |
648 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" | 600 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
649 << " the CONTROL message, send_result = " << send_result; | 601 << " the CONTROL message, send_result = " << send_result; |
650 Close(); | 602 Close(); |
651 } | 603 } |
652 return retval; | 604 return retval; |
653 } | 605 } |
654 | 606 |
655 } // namespace webrtc | 607 } // namespace webrtc |
OLD | NEW |