summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/webrtc_data_stream_adapter.cc
blob: 0a0ef01dda0a75fc9b5fabe8291bc794736dc1c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/webrtc_data_stream_adapter.h"

#include <stdint.h>

#include "base/bind.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/thread_task_runner_handle.h"
#include "net/base/net_errors.h"
#include "remoting/base/compound_buffer.h"
#include "remoting/protocol/message_pipe.h"
#include "remoting/protocol/message_serialization.h"

namespace remoting {
namespace protocol {

class WebrtcDataStreamAdapter::Channel : public MessagePipe,
                                         public webrtc::DataChannelObserver {
 public:
  explicit Channel(base::WeakPtr<WebrtcDataStreamAdapter> adapter);
  ~Channel() override;

  void Start(rtc::scoped_refptr<webrtc::DataChannelInterface> channel);

  std::string name() { return channel_->label(); }

  // MessagePipe interface.
  void StartReceiving(const MessageReceivedCallback& callback) override;
  void Send(google::protobuf::MessageLite* message,
            const base::Closure& done) override;

 private:
  enum class State { CONNECTING, OPEN, CLOSED };

  // webrtc::DataChannelObserver interface.
  void OnStateChange() override;
  void OnMessage(const webrtc::DataBuffer& buffer) override;

  void OnConnected();

  void OnError();

  base::WeakPtr<WebrtcDataStreamAdapter> adapter_;

  rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;

  MessageReceivedCallback message_received_callback_;

  State state_ = State::CONNECTING;

  DISALLOW_COPY_AND_ASSIGN(Channel);
};

WebrtcDataStreamAdapter::Channel::Channel(
    base::WeakPtr<WebrtcDataStreamAdapter> adapter)
    : adapter_(adapter) {}

WebrtcDataStreamAdapter::Channel::~Channel() {
  if (channel_) {
    channel_->UnregisterObserver();
    channel_->Close();
  }
}

void WebrtcDataStreamAdapter::Channel::Start(
    rtc::scoped_refptr<webrtc::DataChannelInterface> channel) {
  DCHECK(!channel_);

  channel_ = channel;
  channel_->RegisterObserver(this);

   if (channel_->state() == webrtc::DataChannelInterface::kOpen) {
    OnConnected();
  } else {
    DCHECK_EQ(channel_->state(), webrtc::DataChannelInterface::kConnecting);
  }
}

void WebrtcDataStreamAdapter::Channel::StartReceiving(
    const MessageReceivedCallback& callback) {
  DCHECK(message_received_callback_.is_null());
  DCHECK(!callback.is_null());

  message_received_callback_ = callback;
}

void WebrtcDataStreamAdapter::Channel::Send(
    google::protobuf::MessageLite* message,
    const base::Closure& done) {
  rtc::CopyOnWriteBuffer buffer;
  buffer.SetSize(message->ByteSize());
  message->SerializeWithCachedSizesToArray(
      reinterpret_cast<uint8_t*>(buffer.data()));
  webrtc::DataBuffer data_buffer(std::move(buffer), true /* binary */);
  if (!channel_->Send(data_buffer)) {
    OnError();
    return;
  }

  if (!done.is_null())
    done.Run();
}

void WebrtcDataStreamAdapter::Channel::OnStateChange() {
  switch (channel_->state()) {
    case webrtc::DataChannelInterface::kOpen:
      OnConnected();
      break;

    case webrtc::DataChannelInterface::kClosing:
      // Currently channels are not expected to be closed.
      OnError();
      break;

    case webrtc::DataChannelInterface::kConnecting:
    case webrtc::DataChannelInterface::kClosed:
      break;
  }
}

void WebrtcDataStreamAdapter::Channel::OnConnected() {
  CHECK(state_ == State::CONNECTING);
  state_ = State::OPEN;
  adapter_->OnChannelConnected(this);
}

void WebrtcDataStreamAdapter::Channel::OnError() {
  if (state_ == State::CLOSED)
    return;

  state_ = State::CLOSED;

  // Notify the adapter about the error asychronously.
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE,
      base::Bind(&WebrtcDataStreamAdapter::OnChannelError, adapter_));
}

void WebrtcDataStreamAdapter::Channel::OnMessage(
    const webrtc::DataBuffer& rtc_buffer) {
  scoped_ptr<CompoundBuffer> buffer(new CompoundBuffer());
  buffer->AppendCopyOf(reinterpret_cast<const char*>(rtc_buffer.data.data()),
                       rtc_buffer.data.size());
  buffer->Lock();
  message_received_callback_.Run(std::move(buffer));
}

struct WebrtcDataStreamAdapter::PendingChannel {
  PendingChannel() {}
  PendingChannel(scoped_ptr<Channel> channel,
                 const ChannelCreatedCallback& connected_callback)
      : channel(std::move(channel)), connected_callback(connected_callback) {}
  PendingChannel(PendingChannel&& other)
      : channel(std::move(other.channel)),
        connected_callback(std::move(other.connected_callback)) {}
  PendingChannel& operator=(PendingChannel&& other) {
    channel = std::move(other.channel);
    connected_callback = std::move(other.connected_callback);
    return *this;
  }

  scoped_ptr<Channel> channel;
  ChannelCreatedCallback connected_callback;
};

WebrtcDataStreamAdapter::WebrtcDataStreamAdapter(
    bool outgoing,
    const ErrorCallback& error_callback)
    : outgoing_(outgoing),
      error_callback_(error_callback),
      weak_factory_(this) {}

WebrtcDataStreamAdapter::~WebrtcDataStreamAdapter() {
  DCHECK(pending_channels_.empty());
}

void WebrtcDataStreamAdapter::Initialize(
    rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection) {
  peer_connection_ = peer_connection;
}

void WebrtcDataStreamAdapter::OnIncomingDataChannel(
    webrtc::DataChannelInterface* data_channel) {
  DCHECK(!outgoing_);

  auto it = pending_channels_.find(data_channel->label());
  if (it == pending_channels_.end()) {
    LOG(ERROR) << "Received unexpected data channel " << data_channel->label();
    return;
  }
  it->second.channel->Start(data_channel);
}

void WebrtcDataStreamAdapter::CreateChannel(
    const std::string& name,
    const ChannelCreatedCallback& callback) {
  DCHECK(peer_connection_);
  DCHECK(pending_channels_.find(name) == pending_channels_.end());

  Channel* channel = new Channel(weak_factory_.GetWeakPtr());
  pending_channels_[name] = PendingChannel(make_scoped_ptr(channel), callback);

  if (outgoing_) {
    webrtc::DataChannelInit config;
    config.reliable = true;
    channel->Start(peer_connection_->CreateDataChannel(name, &config));
  }
}

void WebrtcDataStreamAdapter::CancelChannelCreation(const std::string& name) {
  auto it = pending_channels_.find(name);
  DCHECK(it != pending_channels_.end());
  pending_channels_.erase(it);
}

void WebrtcDataStreamAdapter::OnChannelConnected(Channel* channel) {
  auto it = pending_channels_.find(channel->name());
  DCHECK(it != pending_channels_.end());
  PendingChannel pending_channel = std::move(it->second);
  pending_channels_.erase(it);

  // Once the channel is connected its ownership  is passed to the
  // |connected_callback|.
  pending_channel.connected_callback.Run(std::move(pending_channel.channel));
}

void WebrtcDataStreamAdapter::OnChannelError() {
  error_callback_.Run(CHANNEL_CONNECTION_ERROR);
}

}  // namespace protocol
}  // namespace remoting