summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/webrtc_transport.h
blob: f480ee64d960d848d253ad4175c085ef40c751ae (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
// 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.

#ifndef REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_
#define REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread.h"
#include "base/threading/thread_checker.h"
#include "base/timer/timer.h"
#include "remoting/protocol/transport.h"
#include "remoting/protocol/webrtc_data_stream_adapter.h"
#include "remoting/signaling/signal_strategy.h"
#include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"

namespace webrtc {
class FakeAudioDeviceModule;
}  // namespace webrtc

namespace remoting {
namespace protocol {

class WebrtcTransport : public Transport,
                        public webrtc::PeerConnectionObserver {
 public:
  WebrtcTransport(
      rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
          port_allocator_factory,
      TransportRole role,
      scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner);
  ~WebrtcTransport() override;

  // Transport interface.
  void Start(EventHandler* event_handler,
             Authenticator* authenticator) override;
  bool ProcessTransportInfo(buzz::XmlElement* transport_info) override;
  DatagramChannelFactory* GetDatagramChannelFactory() override;
  StreamChannelFactory* GetStreamChannelFactory() override;
  StreamChannelFactory* GetMultiplexedChannelFactory() override;

 private:
  void DoStart(rtc::Thread* worker_thread);
  void OnLocalSessionDescriptionCreated(
      scoped_ptr<webrtc::SessionDescriptionInterface> description,
      const std::string& error);
  void OnLocalDescriptionSet(bool success, const std::string& error);
  void OnRemoteDescriptionSet(bool success, const std::string& error);

  // webrtc::PeerConnectionObserver interface.
  void OnSignalingChange(
      webrtc::PeerConnectionInterface::SignalingState new_state) override;
  void OnAddStream(webrtc::MediaStreamInterface* stream) override;
  void OnRemoveStream(webrtc::MediaStreamInterface* stream) override;
  void OnDataChannel(webrtc::DataChannelInterface* data_channel) override;
  void OnRenegotiationNeeded() override;
  void OnIceConnectionChange(
      webrtc::PeerConnectionInterface::IceConnectionState new_state) override;
  void OnIceGatheringChange(
      webrtc::PeerConnectionInterface::IceGatheringState new_state) override;
  void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;

  void EnsurePendingTransportInfoMessage();
  void SendTransportInfo();
  void AddPendingCandidatesIfPossible();

  void Close(ErrorCode error);

  base::ThreadChecker thread_checker_;

  rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
      port_allocator_factory_;
  TransportRole role_;
  EventHandler* event_handler_ = nullptr;
  scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_;

  scoped_ptr<webrtc::FakeAudioDeviceModule> fake_audio_device_module_;

  rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
      peer_connection_factory_;
  rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;

  scoped_ptr<buzz::XmlElement> pending_transport_info_message_;
  base::OneShotTimer transport_info_timer_;

  ScopedVector<webrtc::IceCandidateInterface> pending_incoming_candidates_;

  std::list<rtc::scoped_refptr<webrtc::MediaStreamInterface>>
      unclaimed_streams_;

  WebrtcDataStreamAdapter data_stream_adapter_;

  base::WeakPtrFactory<WebrtcTransport> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(WebrtcTransport);
};

class WebrtcTransportFactory : public TransportFactory {
 public:
  WebrtcTransportFactory(
      SignalStrategy* signal_strategy,
      rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
          port_allocator_factory,
      TransportRole role);
  ~WebrtcTransportFactory() override;

  // TransportFactory interface.
  scoped_ptr<Transport> CreateTransport() override;

 private:
  SignalStrategy* signal_strategy_;
  rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
      port_allocator_factory_;
  TransportRole role_;

  base::Thread worker_thread_;

  DISALLOW_COPY_AND_ASSIGN(WebrtcTransportFactory);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_