| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/webrtc_data_stream_adapter.h" | 5 #include "remoting/protocol/webrtc_data_stream_adapter.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "remoting/base/compound_buffer.h" | 17 #include "remoting/base/compound_buffer.h" |
| 18 #include "remoting/protocol/message_pipe.h" | |
| 19 #include "remoting/protocol/message_serialization.h" | 18 #include "remoting/protocol/message_serialization.h" |
| 20 | 19 |
| 21 namespace remoting { | 20 namespace remoting { |
| 22 namespace protocol { | 21 namespace protocol { |
| 23 | 22 |
| 24 namespace { | 23 WebrtcDataStreamAdapter::WebrtcDataStreamAdapter( |
| 25 | |
| 26 class WebrtcDataChannel : public MessagePipe, | |
| 27 public webrtc::DataChannelObserver { | |
| 28 public: | |
| 29 explicit WebrtcDataChannel( | |
| 30 rtc::scoped_refptr<webrtc::DataChannelInterface> channel); | |
| 31 ~WebrtcDataChannel() override; | |
| 32 | |
| 33 std::string name() { return channel_->label(); } | |
| 34 | |
| 35 // MessagePipe interface. | |
| 36 void Start(EventHandler* event_handler) override; | |
| 37 void Send(google::protobuf::MessageLite* message, | |
| 38 const base::Closure& done) override; | |
| 39 | |
| 40 private: | |
| 41 enum class State { CONNECTING, OPEN, CLOSED }; | |
| 42 | |
| 43 // webrtc::DataChannelObserver interface. | |
| 44 void OnStateChange() override; | |
| 45 void OnMessage(const webrtc::DataBuffer& buffer) override; | |
| 46 | |
| 47 void OnConnected(); | |
| 48 | |
| 49 void OnClosed(); | |
| 50 | |
| 51 rtc::scoped_refptr<webrtc::DataChannelInterface> channel_; | |
| 52 | |
| 53 EventHandler* event_handler_ = nullptr; | |
| 54 | |
| 55 State state_ = State::CONNECTING; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(WebrtcDataChannel); | |
| 58 }; | |
| 59 | |
| 60 WebrtcDataChannel::WebrtcDataChannel( | |
| 61 rtc::scoped_refptr<webrtc::DataChannelInterface> channel) | 24 rtc::scoped_refptr<webrtc::DataChannelInterface> channel) |
| 62 : channel_(channel) { | 25 : channel_(channel) { |
| 63 channel_->RegisterObserver(this); | 26 channel_->RegisterObserver(this); |
| 64 DCHECK_EQ(channel_->state(), webrtc::DataChannelInterface::kConnecting); | 27 DCHECK_EQ(channel_->state(), webrtc::DataChannelInterface::kConnecting); |
| 65 } | 28 } |
| 66 | 29 |
| 67 WebrtcDataChannel::~WebrtcDataChannel() { | 30 WebrtcDataStreamAdapter::~WebrtcDataStreamAdapter() { |
| 68 if (channel_) { | 31 if (channel_) { |
| 69 channel_->UnregisterObserver(); | 32 channel_->UnregisterObserver(); |
| 70 channel_->Close(); | 33 channel_->Close(); |
| 71 } | 34 } |
| 72 } | 35 } |
| 73 | 36 |
| 74 void WebrtcDataChannel::Start(EventHandler* event_handler) { | 37 void WebrtcDataStreamAdapter::Start(EventHandler* event_handler) { |
| 75 DCHECK(!event_handler_); | 38 DCHECK(!event_handler_); |
| 76 DCHECK(event_handler); | 39 DCHECK(event_handler); |
| 77 | 40 |
| 78 event_handler_ = event_handler; | 41 event_handler_ = event_handler; |
| 79 } | 42 } |
| 80 | 43 |
| 81 void WebrtcDataChannel::Send(google::protobuf::MessageLite* message, | 44 void WebrtcDataStreamAdapter::Send(google::protobuf::MessageLite* message, |
| 82 const base::Closure& done) { | 45 const base::Closure& done) { |
| 83 DCHECK(state_ == State::OPEN); | 46 DCHECK(state_ == State::OPEN); |
| 84 | 47 |
| 85 rtc::CopyOnWriteBuffer buffer; | 48 rtc::CopyOnWriteBuffer buffer; |
| 86 buffer.SetSize(message->ByteSize()); | 49 buffer.SetSize(message->ByteSize()); |
| 87 message->SerializeWithCachedSizesToArray( | 50 message->SerializeWithCachedSizesToArray( |
| 88 reinterpret_cast<uint8_t*>(buffer.data())); | 51 reinterpret_cast<uint8_t*>(buffer.data())); |
| 89 webrtc::DataBuffer data_buffer(std::move(buffer), true /* binary */); | 52 webrtc::DataBuffer data_buffer(std::move(buffer), true /* binary */); |
| 90 if (!channel_->Send(data_buffer)) { | 53 if (!channel_->Send(data_buffer)) { |
| 91 LOG(ERROR) << "Send failed on data channel " << channel_->label(); | 54 LOG(ERROR) << "Send failed on data channel " << channel_->label(); |
| 92 channel_->Close(); | 55 channel_->Close(); |
| 93 return; | 56 return; |
| 94 } | 57 } |
| 95 | 58 |
| 96 if (!done.is_null()) | 59 if (!done.is_null()) |
| 97 done.Run(); | 60 done.Run(); |
| 98 } | 61 } |
| 99 | 62 |
| 100 void WebrtcDataChannel::OnStateChange() { | 63 void WebrtcDataStreamAdapter::OnStateChange() { |
| 101 switch (channel_->state()) { | 64 switch (channel_->state()) { |
| 102 case webrtc::DataChannelInterface::kOpen: | 65 case webrtc::DataChannelInterface::kOpen: |
| 103 DCHECK(state_ == State::CONNECTING); | 66 DCHECK(state_ == State::CONNECTING); |
| 104 state_ = State::OPEN; | 67 state_ = State::OPEN; |
| 105 event_handler_->OnMessagePipeOpen(); | 68 event_handler_->OnMessagePipeOpen(); |
| 106 break; | 69 break; |
| 107 | 70 |
| 108 case webrtc::DataChannelInterface::kClosing: | 71 case webrtc::DataChannelInterface::kClosing: |
| 109 if (state_ != State::CLOSED) { | 72 if (state_ != State::CLOSED) { |
| 110 state_ = State::CLOSED; | 73 state_ = State::CLOSED; |
| 111 event_handler_->OnMessagePipeClosed(); | 74 event_handler_->OnMessagePipeClosed(); |
| 112 } | 75 } |
| 113 break; | 76 break; |
| 114 | 77 |
| 115 case webrtc::DataChannelInterface::kConnecting: | 78 case webrtc::DataChannelInterface::kConnecting: |
| 116 case webrtc::DataChannelInterface::kClosed: | 79 case webrtc::DataChannelInterface::kClosed: |
| 117 break; | 80 break; |
| 118 } | 81 } |
| 119 } | 82 } |
| 120 | 83 |
| 121 void WebrtcDataChannel::OnMessage(const webrtc::DataBuffer& rtc_buffer) { | 84 void WebrtcDataStreamAdapter::OnMessage(const webrtc::DataBuffer& rtc_buffer) { |
| 122 if (state_ != State::OPEN) { | 85 if (state_ != State::OPEN) { |
| 123 LOG(ERROR) << "Dropping a message received when the channel is not open."; | 86 LOG(ERROR) << "Dropping a message received when the channel is not open."; |
| 124 return; | 87 return; |
| 125 } | 88 } |
| 126 | 89 |
| 127 std::unique_ptr<CompoundBuffer> buffer(new CompoundBuffer()); | 90 std::unique_ptr<CompoundBuffer> buffer(new CompoundBuffer()); |
| 128 buffer->AppendCopyOf(reinterpret_cast<const char*>(rtc_buffer.data.data()), | 91 buffer->AppendCopyOf(reinterpret_cast<const char*>(rtc_buffer.data.data()), |
| 129 rtc_buffer.data.size()); | 92 rtc_buffer.data.size()); |
| 130 buffer->Lock(); | 93 buffer->Lock(); |
| 131 event_handler_->OnMessageReceived(std::move(buffer)); | 94 event_handler_->OnMessageReceived(std::move(buffer)); |
| 132 } | 95 } |
| 133 | 96 |
| 134 } // namespace | |
| 135 | |
| 136 WebrtcDataStreamAdapter::WebrtcDataStreamAdapter( | |
| 137 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection) | |
| 138 : peer_connection_(peer_connection) {} | |
| 139 WebrtcDataStreamAdapter::~WebrtcDataStreamAdapter() {} | |
| 140 | |
| 141 std::unique_ptr<MessagePipe> WebrtcDataStreamAdapter::CreateOutgoingChannel( | |
| 142 const std::string& name) { | |
| 143 webrtc::DataChannelInit config; | |
| 144 config.reliable = true; | |
| 145 return base::WrapUnique(new WebrtcDataChannel( | |
| 146 peer_connection_->CreateDataChannel(name, &config))); | |
| 147 } | |
| 148 | |
| 149 std::unique_ptr<MessagePipe> WebrtcDataStreamAdapter::WrapIncomingDataChannel( | |
| 150 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) { | |
| 151 return base::WrapUnique(new WebrtcDataChannel(data_channel)); | |
| 152 } | |
| 153 | |
| 154 } // namespace protocol | 97 } // namespace protocol |
| 155 } // namespace remoting | 98 } // namespace remoting |
| OLD | NEW |