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" |
34 #include "webrtc/base/logging.h" | 35 #include "webrtc/base/logging.h" |
35 #include "webrtc/base/refcount.h" | 36 #include "webrtc/base/refcount.h" |
36 | 37 |
37 namespace webrtc { | 38 namespace webrtc { |
38 | 39 |
39 static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; | 40 static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; |
40 static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; | 41 static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; |
41 | 42 |
42 enum { | 43 enum { |
43 MSG_CHANNELREADY, | 44 MSG_CHANNELREADY, |
44 }; | 45 }; |
45 | 46 |
| 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 |
46 DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} | 83 DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} |
47 | 84 |
48 DataChannel::PacketQueue::~PacketQueue() { | 85 DataChannel::PacketQueue::~PacketQueue() { |
49 Clear(); | 86 Clear(); |
50 } | 87 } |
51 | 88 |
52 bool DataChannel::PacketQueue::Empty() const { | 89 bool DataChannel::PacketQueue::Empty() const { |
53 return packets_.empty(); | 90 return packets_.empty(); |
54 } | 91 } |
55 | 92 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 UpdateState(); | 287 UpdateState(); |
251 } | 288 } |
252 | 289 |
253 // The remote peer request that this channel shall be closed. | 290 // The remote peer request that this channel shall be closed. |
254 void DataChannel::RemotePeerRequestClose() { | 291 void DataChannel::RemotePeerRequestClose() { |
255 DoClose(); | 292 DoClose(); |
256 } | 293 } |
257 | 294 |
258 void DataChannel::SetSctpSid(int sid) { | 295 void DataChannel::SetSctpSid(int sid) { |
259 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP); | 296 ASSERT(config_.id < 0 && sid >= 0 && data_channel_type_ == cricket::DCT_SCTP); |
260 if (config_.id == sid) | 297 if (config_.id == sid) { |
261 return; | 298 return; |
| 299 } |
262 | 300 |
263 config_.id = sid; | 301 config_.id = sid; |
264 provider_->AddSctpDataStream(sid); | 302 provider_->AddSctpDataStream(sid); |
265 } | 303 } |
266 | 304 |
267 void DataChannel::OnTransportChannelCreated() { | 305 void DataChannel::OnTransportChannelCreated() { |
268 ASSERT(data_channel_type_ == cricket::DCT_SCTP); | 306 ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
269 if (!connected_to_provider_) { | 307 if (!connected_to_provider_) { |
270 connected_to_provider_ = provider_->ConnectDataChannel(this); | 308 connected_to_provider_ = provider_->ConnectDataChannel(this); |
271 } | 309 } |
272 // The sid may have been unassigned when provider_->ConnectDataChannel was | 310 // The sid may have been unassigned when provider_->ConnectDataChannel was |
273 // done. So always add the streams even if connected_to_provider_ is true. | 311 // done. So always add the streams even if connected_to_provider_ is true. |
274 if (config_.id >= 0) { | 312 if (config_.id >= 0) { |
275 provider_->AddSctpDataStream(config_.id); | 313 provider_->AddSctpDataStream(config_.id); |
276 } | 314 } |
277 } | 315 } |
278 | 316 |
| 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 |
279 void DataChannel::SetSendSsrc(uint32_t send_ssrc) { | 324 void DataChannel::SetSendSsrc(uint32_t send_ssrc) { |
280 ASSERT(data_channel_type_ == cricket::DCT_RTP); | 325 ASSERT(data_channel_type_ == cricket::DCT_RTP); |
281 if (send_ssrc_set_) { | 326 if (send_ssrc_set_) { |
282 return; | 327 return; |
283 } | 328 } |
284 send_ssrc_ = send_ssrc; | 329 send_ssrc_ = send_ssrc; |
285 send_ssrc_set_ = true; | 330 send_ssrc_set_ = true; |
286 UpdateState(); | 331 UpdateState(); |
287 } | 332 } |
288 | 333 |
289 void DataChannel::OnMessage(rtc::Message* msg) { | 334 void DataChannel::OnMessage(rtc::Message* msg) { |
290 switch (msg->message_id) { | 335 switch (msg->message_id) { |
291 case MSG_CHANNELREADY: | 336 case MSG_CHANNELREADY: |
292 OnChannelReady(true); | 337 OnChannelReady(true); |
293 break; | 338 break; |
294 } | 339 } |
295 } | 340 } |
296 | 341 |
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 | |
304 void DataChannel::OnDataReceived(cricket::DataChannel* channel, | 342 void DataChannel::OnDataReceived(cricket::DataChannel* channel, |
305 const cricket::ReceiveDataParams& params, | 343 const cricket::ReceiveDataParams& params, |
306 const rtc::Buffer& payload) { | 344 const rtc::Buffer& payload) { |
307 uint32_t expected_ssrc = | 345 uint32_t expected_ssrc = |
308 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; | 346 (data_channel_type_ == cricket::DCT_RTP) ? receive_ssrc_ : config_.id; |
309 if (params.ssrc != expected_ssrc) { | 347 if (params.ssrc != expected_ssrc) { |
310 return; | 348 return; |
311 } | 349 } |
312 | 350 |
313 if (params.type == cricket::DMT_CONTROL) { | 351 if (params.type == cricket::DMT_CONTROL) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 if (data_channel_type_ != cricket::DCT_RTP) { | 392 if (data_channel_type_ != cricket::DCT_RTP) { |
355 Close(); | 393 Close(); |
356 } | 394 } |
357 | 395 |
358 return; | 396 return; |
359 } | 397 } |
360 queued_received_data_.Push(buffer.release()); | 398 queued_received_data_.Push(buffer.release()); |
361 } | 399 } |
362 } | 400 } |
363 | 401 |
| 402 void DataChannel::OnStreamClosedRemotely(uint32_t sid) { |
| 403 if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) { |
| 404 Close(); |
| 405 } |
| 406 } |
| 407 |
364 void DataChannel::OnChannelReady(bool writable) { | 408 void DataChannel::OnChannelReady(bool writable) { |
365 writable_ = writable; | 409 writable_ = writable; |
366 if (!writable) { | 410 if (!writable) { |
367 return; | 411 return; |
368 } | 412 } |
369 | 413 |
370 SendQueuedControlMessages(); | 414 SendQueuedControlMessages(); |
371 SendQueuedDataMessages(); | 415 SendQueuedDataMessages(); |
372 UpdateState(); | 416 UpdateState(); |
373 } | 417 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 } | 473 } |
430 } | 474 } |
431 break; | 475 break; |
432 } | 476 } |
433 case kClosed: | 477 case kClosed: |
434 break; | 478 break; |
435 } | 479 } |
436 } | 480 } |
437 | 481 |
438 void DataChannel::SetState(DataState state) { | 482 void DataChannel::SetState(DataState state) { |
439 if (state_ == state) | 483 if (state_ == state) { |
440 return; | 484 return; |
| 485 } |
441 | 486 |
442 state_ = state; | 487 state_ = state; |
443 if (observer_) { | 488 if (observer_) { |
444 observer_->OnStateChange(); | 489 observer_->OnStateChange(); |
445 } | 490 } |
| 491 if (state_ == kClosed) { |
| 492 SignalClosed(this); |
| 493 } |
446 } | 494 } |
447 | 495 |
448 void DataChannel::DisconnectFromProvider() { | 496 void DataChannel::DisconnectFromProvider() { |
449 if (!connected_to_provider_) | 497 if (!connected_to_provider_) |
450 return; | 498 return; |
451 | 499 |
452 provider_->DisconnectDataChannel(this); | 500 provider_->DisconnectDataChannel(this); |
453 connected_to_provider_ = false; | 501 connected_to_provider_ = false; |
454 | 502 |
455 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) { | 503 if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 QueueControlMessage(buffer); | 646 QueueControlMessage(buffer); |
599 } else { | 647 } else { |
600 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" | 648 LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
601 << " the CONTROL message, send_result = " << send_result; | 649 << " the CONTROL message, send_result = " << send_result; |
602 Close(); | 650 Close(); |
603 } | 651 } |
604 return retval; | 652 return retval; |
605 } | 653 } |
606 | 654 |
607 } // namespace webrtc | 655 } // namespace webrtc |
OLD | NEW |