aboutsummaryrefslogtreecommitdiff
path: root/host/frontend/webrtc/libcommon/connection_controller.cpp
blob: cc22e862a70d3b30912c105eca592ec7bc2b3d96 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "host/frontend/webrtc/libcommon/connection_controller.h"

#include <algorithm>
#include <vector>

#include <android-base/logging.h>

#include "host/frontend/webrtc/libcommon/audio_device.h"
#include "host/frontend/webrtc/libcommon/utils.h"

namespace cuttlefish {
namespace webrtc_streaming {

// Different classes are needed because all the interfaces inherit from
// classes providing the methods AddRef and Release, needed by scoped_ptr, which
// cause ambiguity when a single class (i.e ConnectionController) implements all
// of them.
// It's safe for these classes to hold a reference to the ConnectionController
// because it owns the peer connection, so it will never be destroyed before
// these observers.
class CreateSessionDescriptionObserverIntermediate
    : public webrtc::CreateSessionDescriptionObserver {
 public:
  CreateSessionDescriptionObserverIntermediate(ConnectionController& controller)
      : controller_(controller) {}

  void OnSuccess(webrtc::SessionDescriptionInterface* desc) override {
    controller_.OnCreateSDPSuccess(desc);
  }
  void OnFailure(webrtc::RTCError error) override {
    controller_.OnCreateSDPFailure(error);
  }

 private:
  ConnectionController& controller_;
};

class SetSessionDescriptionObserverIntermediate
    : public webrtc::SetSessionDescriptionObserver {
 public:
  SetSessionDescriptionObserverIntermediate(ConnectionController& controller)
      : controller_(controller) {}

  void OnSuccess() override { controller_.OnSetLocalDescriptionSuccess(); }
  void OnFailure(webrtc::RTCError error) override {
    controller_.OnSetLocalDescriptionFailure(error);
  }

 private:
  ConnectionController& controller_;
};

class SetRemoteDescriptionObserverIntermediate
    : public webrtc::SetRemoteDescriptionObserverInterface {
 public:
  SetRemoteDescriptionObserverIntermediate(ConnectionController& controller)
      : controller_(controller) {}

  void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override {
    controller_.OnSetRemoteDescriptionComplete(error);
  }

 private:
  ConnectionController& controller_;
};

ConnectionController::ConnectionController(
    PeerSignalingHandler& sig_handler,
    PeerConnectionBuilder& connection_builder,
    ConnectionController::Observer& observer)
    : sig_handler_(sig_handler),
      connection_builder_(connection_builder),
      observer_(observer) {}

void ConnectionController::CreateOffer() {
  // No memory leak here because this is a ref counted object and the
  // peer connection immediately wraps it with a scoped_refptr
  peer_connection_->CreateOffer(ThisAsCreateSDPObserver(), {} /*options*/);
}

Result<void> ConnectionController::RequestOffer(
    const std::vector<webrtc::PeerConnectionInterface::IceServer>&
        ice_servers) {
  observer_.OnConnectionStateChange(
      webrtc::PeerConnectionInterface::PeerConnectionState::kNew);
  Json::Value msg;
  msg["type"] = "request-offer";
  if (!ice_servers.empty()) {
    // Only include the ice servers in the message if non empty
    msg["ice_servers"] = GenerateIceServersMessage(ice_servers);
  }
  CF_EXPECT(sig_handler_.SendMessage(msg),
            "Failed to send the request-offer message to the device");
  return {};
}

void ConnectionController::FailConnection(const std::string& message) {
  Json::Value reply;
  reply["type"] = "error";
  reply["error"] = message;
  sig_handler_.SendMessage(reply);
  observer_.OnConnectionStateChange(CF_ERR(message));
}

void ConnectionController::AddPendingIceCandidates() {
  // Add any ice candidates that arrived before the remote description
  for (auto& candidate : pending_ice_candidates_) {
    peer_connection_->AddIceCandidate(
        std::move(candidate), [this](webrtc::RTCError error) {
          if (!error.ok()) {
            FailConnection(ToString(error.type()) + std::string(": ") +
                           error.message());
          }
        });
  }
  pending_ice_candidates_.clear();
}

Result<void> ConnectionController::OnOfferRequestMsg(
    const std::vector<webrtc::PeerConnectionInterface::IceServer>&
        ice_servers) {
  peer_connection_ = CF_EXPECT(connection_builder_.Build(*this, ice_servers),
                               "Failed to create peer connection");
  CreateOffer();
  return {};
}

Result<void> ConnectionController::OnOfferMsg(
    std::unique_ptr<webrtc::SessionDescriptionInterface> offer) {
  peer_connection_->SetRemoteDescription(std::move(offer),
                                         ThisAsSetRemoteSDPObserver());
  return {};
}

Result<void> ConnectionController::OnAnswerMsg(
    std::unique_ptr<webrtc::SessionDescriptionInterface> answer) {
  peer_connection_->SetRemoteDescription(std::move(answer),
                                         ThisAsSetRemoteSDPObserver());
  return {};
}

Result<void> ConnectionController::OnIceCandidateMsg(
    std::unique_ptr<webrtc::IceCandidateInterface> candidate) {
  if (peer_connection_->remote_description()) {
    peer_connection_->AddIceCandidate(
        std::move(candidate), [this](webrtc::RTCError error) {
          if (!error.ok()) {
            FailConnection(ToString(error.type()) + std::string(": ") +
                           error.message());
          }
        });
  } else {
    // Store the ice candidate to be added later if it arrives before the
    // remote description. This could happen if the client uses polling
    // instead of websockets because the candidates are generated immediately
    // after the remote (offer) description is set and the events and the ajax
    // calls are asynchronous.
    pending_ice_candidates_.push_back(std::move(candidate));
  }
  return {};
}

Result<void> ConnectionController::OnErrorMsg(const std::string& msg) {
  LOG(ERROR) << "Received error message from peer: " << msg;
  return {};
}

void ConnectionController::OnCreateSDPSuccess(
    webrtc::SessionDescriptionInterface* desc) {
  std::string offer_str;
  desc->ToString(&offer_str);
  std::string sdp_type = desc->type();
  peer_connection_->SetLocalDescription(ThisAsSetSDPObserver(), desc);
  // The peer connection takes ownership of the description so it should not be
  // used after this
  desc = nullptr;

  Json::Value reply;
  reply["type"] = sdp_type;
  reply["sdp"] = offer_str;

  sig_handler_.SendMessage(reply);
}

void ConnectionController::OnCreateSDPFailure(const webrtc::RTCError& error) {
  FailConnection(ToString(error.type()) + std::string(": ") + error.message());
}

void ConnectionController::OnSetLocalDescriptionSuccess() {
  // local description set, nothing else to do
}

void ConnectionController::OnSetLocalDescriptionFailure(
    const webrtc::RTCError& error) {
  LOG(ERROR) << "Error setting local description: Either there is a bug in "
                "libwebrtc or the local description was (incorrectly) modified "
                "after creating it";
  FailConnection(ToString(error.type()) + std::string(": ") + error.message());
}

void ConnectionController::OnSetRemoteDescriptionComplete(
    const webrtc::RTCError& error) {
  if (!error.ok()) {
    // The remote description was rejected, can't connect to device.
    FailConnection(ToString(error.type()) + std::string(": ") + error.message());
    return;
  }
  AddPendingIceCandidates();
  auto remote_desc = peer_connection_->remote_description();
  CHECK(remote_desc) << "The remote description was just added successfully in "
                        "this thread, so it can't be nullptr";
  if (remote_desc->GetType() != webrtc::SdpType::kOffer) {
    // Only create and send answer when the remote description is an offer.
    return;
  }
  peer_connection_->CreateAnswer(ThisAsCreateSDPObserver(), {} /*options*/);
}

// No memory leaks with these because the peer_connection immediately wraps
// these pointers with scoped_refptr.
webrtc::CreateSessionDescriptionObserver*
ConnectionController::ThisAsCreateSDPObserver() {
  return new rtc::RefCountedObject<
      CreateSessionDescriptionObserverIntermediate>(*this);
}
webrtc::SetSessionDescriptionObserver*
ConnectionController::ThisAsSetSDPObserver() {
  return new rtc::RefCountedObject<SetSessionDescriptionObserverIntermediate>(
      *this);
}
rtc::scoped_refptr<webrtc::SetRemoteDescriptionObserverInterface>
ConnectionController::ThisAsSetRemoteSDPObserver() {
  return rtc::scoped_refptr<webrtc::SetRemoteDescriptionObserverInterface>(
      new rtc::RefCountedObject<SetRemoteDescriptionObserverIntermediate>(
          *this));
}

void ConnectionController::HandleSignalingMessage(const Json::Value& msg) {
  auto result = HandleSignalingMessageInner(msg);
  if (!result.ok()) {
    LOG(ERROR) << result.error().FormatForEnv();
    FailConnection(result.error().Message());
  }
}

Result<void> ConnectionController::HandleSignalingMessageInner(
    const Json::Value& message) {
  CF_EXPECT(ValidateJsonObject(message, "",
                               {{"type", Json::ValueType::stringValue}}));
  auto type = message["type"].asString();

  if (type == "request-offer") {
    auto ice_servers = CF_EXPECT(ParseIceServersMessage(message),
                                 "Error parsing ice-servers field");
    return OnOfferRequestMsg(ice_servers);
  } else if (type == "offer") {
    auto remote_desc = CF_EXPECT(
        ParseSessionDescription(type, message, webrtc::SdpType::kOffer));
    return OnOfferMsg(std::move(remote_desc));
  } else if (type == "answer") {
    auto remote_desc = CF_EXPECT(
        ParseSessionDescription(type, message, webrtc::SdpType::kAnswer));
    return OnAnswerMsg(std::move(remote_desc));
  } else if (type == "ice-candidate") {
    auto candidate = CF_EXPECT(ParseIceCandidate(type, message));
    return OnIceCandidateMsg(std::move(candidate));
  } else if (type == "error") {
    return OnErrorMsg(CF_EXPECT(ParseError(type, message)));
  } else {
    return CF_ERR("Unknown client message type: " + type);
  }
}

// Triggered when the SignalingState changed.
void ConnectionController::OnSignalingChange(
    webrtc::PeerConnectionInterface::SignalingState new_state) {
  LOG(VERBOSE) << "Signaling state changed: " << new_state;
}

// Triggered when media is received on a new stream from remote peer.
void ConnectionController::OnAddStream(
    rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
  LOG(VERBOSE) << "Stream added: " << stream->id();
}

// Triggered when a remote peer closes a stream.
void ConnectionController::OnRemoveStream(
    rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
  LOG(VERBOSE) << "Stream removed: " << stream->id();
}

// Triggered when a remote peer opens a data channel.
void ConnectionController::OnDataChannel(
    rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
  observer_.OnDataChannel(data_channel);
}

// Triggered when renegotiation is needed. For example, an ICE restart
// has begun.
void ConnectionController::OnRenegotiationNeeded() {
  if (!peer_connection_) {
    return;
  }
  CreateOffer();
}

// Called any time the standards-compliant IceConnectionState changes.
void ConnectionController::OnStandardizedIceConnectionChange(
    webrtc::PeerConnectionInterface::IceConnectionState new_state) {
  switch (new_state) {
    case webrtc::PeerConnectionInterface::kIceConnectionNew:
      LOG(DEBUG) << "ICE connection state: New";
      break;
    case webrtc::PeerConnectionInterface::kIceConnectionChecking:
      LOG(DEBUG) << "ICE connection state: Checking";
      break;
    case webrtc::PeerConnectionInterface::kIceConnectionConnected:
      LOG(DEBUG) << "ICE connection state: Connected";
      break;
    case webrtc::PeerConnectionInterface::kIceConnectionCompleted:
      LOG(DEBUG) << "ICE connection state: Completed";
      break;
    case webrtc::PeerConnectionInterface::kIceConnectionFailed:
      LOG(DEBUG) << "ICE connection state: Failed";
      break;
    case webrtc::PeerConnectionInterface::kIceConnectionDisconnected:
      LOG(DEBUG) << "ICE connection state: Disconnected";
      break;
    case webrtc::PeerConnectionInterface::kIceConnectionClosed:
      LOG(DEBUG) << "ICE connection state: Closed";
      break;
    case webrtc::PeerConnectionInterface::kIceConnectionMax:
      LOG(DEBUG) << "ICE connection state: Max";
      break;
    default:
      LOG(DEBUG) << "ICE connection state: " << new_state;
  }
}

// Called any time the PeerConnectionState changes.
void ConnectionController::OnConnectionChange(
    webrtc::PeerConnectionInterface::PeerConnectionState new_state) {
  observer_.OnConnectionStateChange(new_state);
}

// Called any time the IceGatheringState changes.
void ConnectionController::OnIceGatheringChange(
    webrtc::PeerConnectionInterface::IceGatheringState new_state) {
  std::string state_str;
  switch (new_state) {
    case webrtc::PeerConnectionInterface::IceGatheringState::kIceGatheringNew:
      state_str = "NEW";
      break;
    case webrtc::PeerConnectionInterface::IceGatheringState::
        kIceGatheringGathering:
      state_str = "GATHERING";
      break;
    case webrtc::PeerConnectionInterface::IceGatheringState::
        kIceGatheringComplete:
      state_str = "COMPLETE";
      break;
    default:
      state_str = "UNKNOWN";
  }
  LOG(VERBOSE) << "ICE Gathering state set to: " << state_str;
}

// A new ICE candidate has been gathered.
void ConnectionController::OnIceCandidate(
    const webrtc::IceCandidateInterface* candidate) {
  std::string candidate_sdp;
  candidate->ToString(&candidate_sdp);
  auto sdp_mid = candidate->sdp_mid();
  auto line_index = candidate->sdp_mline_index();

  Json::Value reply;
  reply["type"] = "ice-candidate";
  reply["mid"] = sdp_mid;
  reply["mLineIndex"] = static_cast<Json::UInt64>(line_index);
  reply["candidate"] = candidate_sdp;

  sig_handler_.SendMessage(reply);
}

// Gathering of an ICE candidate failed.
// See https://w3c.github.io/webrtc-pc/#event-icecandidateerror
void ConnectionController::OnIceCandidateError(const std::string& address,
                                               int port, const std::string& url,
                                               int error_code,
                                               const std::string& error_text) {
  LOG(VERBOSE) << "Gathering of an ICE candidate (address: " << address
               << ", port: " << port << ", url: " << url
               << ") failed: " << error_text;
}

// Ice candidates have been removed.
void ConnectionController::OnIceCandidatesRemoved(
    const std::vector<cricket::Candidate>&) {
  // ignore
}

// This is called when signaling indicates a transceiver will be receiving
// media from the remote endpoint. This is fired during a call to
// SetRemoteDescription. The receiving track can be accessed by:
// ConnectionController::|transceiver->receiver()->track()| and its
// associated streams by |transceiver->receiver()->streams()|. Note: This will
// only be called if Unified Plan semantics are specified. This behavior is
// specified in section 2.2.8.2.5 of the "Set the RTCSessionDescription"
// algorithm: https://w3c.github.io/webrtc-pc/#set-description
void ConnectionController::OnTrack(
    rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) {
  observer_.OnTrack(transceiver);
}

// Called when signaling indicates that media will no longer be received on a
// track.
// With Plan B semantics, the given receiver will have been removed from the
// PeerConnection and the track muted.
// With Unified Plan semantics, the receiver will remain but the transceiver
// will have changed direction to either sendonly or inactive.
// https://w3c.github.io/webrtc-pc/#process-remote-track-removal
void ConnectionController::OnRemoveTrack(
    rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) {
  observer_.OnRemoveTrack(receiver);
}

}  // namespace webrtc_streaming
}  // namespace cuttlefish