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 #ifndef REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_ | 5 #ifndef REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_ |
6 #define REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_ | 6 #define REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
17 #include "crypto/hmac.h" | 17 #include "crypto/hmac.h" |
18 #include "remoting/protocol/transport.h" | 18 #include "remoting/protocol/transport.h" |
19 #include "remoting/protocol/webrtc_data_stream_adapter.h" | 19 #include "remoting/protocol/webrtc_data_stream_adapter.h" |
20 #include "remoting/protocol/webrtc_video_encoder_factory.h" | 20 #include "remoting/protocol/webrtc_video_encoder_factory.h" |
21 #include "remoting/signaling/signal_strategy.h" | 21 #include "remoting/signaling/signal_strategy.h" |
22 #include "third_party/webrtc/api/peerconnectioninterface.h" | 22 #include "third_party/webrtc/api/peerconnectioninterface.h" |
23 | 23 |
24 namespace webrtc { | |
25 class FakeAudioDeviceModule; | |
26 } // namespace webrtc | |
27 | |
28 namespace remoting { | 24 namespace remoting { |
29 namespace protocol { | 25 namespace protocol { |
30 | 26 |
31 class TransportContext; | 27 class TransportContext; |
32 class MessagePipe; | 28 class MessagePipe; |
33 | 29 |
34 class WebrtcTransport : public Transport, | 30 class WebrtcTransport : public Transport { |
35 public webrtc::PeerConnectionObserver { | |
36 public: | 31 public: |
37 class EventHandler { | 32 class EventHandler { |
38 public: | 33 public: |
39 // Called after |peer_connection| has been created but before handshake. The | 34 // Called after |peer_connection| has been created but before handshake. The |
40 // handler should create data channels and media streams. Renegotiation will | 35 // handler should create data channels and media streams. Renegotiation will |
41 // be required in two cases after this method returns: | 36 // be required in two cases after this method returns: |
42 // 1. When the first data channel is created, if it wasn't created by this | 37 // 1. When the first data channel is created, if it wasn't created by this |
43 // event handler. | 38 // event handler. |
44 // 2. Whenever a media stream is added or removed. | 39 // 2. Whenever a media stream is added or removed. |
45 virtual void OnWebrtcTransportConnecting() = 0; | 40 virtual void OnWebrtcTransportConnecting() = 0; |
(...skipping 17 matching lines...) Expand all Loading... |
63 | 58 |
64 protected: | 59 protected: |
65 virtual ~EventHandler() {} | 60 virtual ~EventHandler() {} |
66 }; | 61 }; |
67 | 62 |
68 WebrtcTransport(rtc::Thread* worker_thread, | 63 WebrtcTransport(rtc::Thread* worker_thread, |
69 scoped_refptr<TransportContext> transport_context, | 64 scoped_refptr<TransportContext> transport_context, |
70 EventHandler* event_handler); | 65 EventHandler* event_handler); |
71 ~WebrtcTransport() override; | 66 ~WebrtcTransport() override; |
72 | 67 |
73 webrtc::PeerConnectionInterface* peer_connection() { | 68 webrtc::PeerConnectionInterface* peer_connection(); |
74 return peer_connection_; | 69 webrtc::PeerConnectionFactoryInterface* peer_connection_factory(); |
75 } | |
76 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() { | |
77 return peer_connection_factory_; | |
78 } | |
79 remoting::WebrtcVideoEncoderFactory* video_encoder_factory() { | 70 remoting::WebrtcVideoEncoderFactory* video_encoder_factory() { |
80 return video_encoder_factory_; | 71 return video_encoder_factory_; |
81 } | 72 } |
82 | 73 |
83 // Creates outgoing data channel. The channel is created in CONNECTING state. | 74 // Creates outgoing data channel. The channel is created in CONNECTING state. |
84 // The caller must wait for OnMessagePipeOpen() notification before sending | 75 // The caller must wait for OnMessagePipeOpen() notification before sending |
85 // any messages. | 76 // any messages. |
86 std::unique_ptr<MessagePipe> CreateOutgoingChannel(const std::string& name); | 77 std::unique_ptr<MessagePipe> CreateOutgoingChannel(const std::string& name); |
87 | 78 |
88 // Transport interface. | 79 // Transport interface. |
89 void Start(Authenticator* authenticator, | 80 void Start(Authenticator* authenticator, |
90 SendTransportInfoCallback send_transport_info_callback) override; | 81 SendTransportInfoCallback send_transport_info_callback) override; |
91 bool ProcessTransportInfo(buzz::XmlElement* transport_info) override; | 82 bool ProcessTransportInfo(buzz::XmlElement* transport_info) override; |
92 void Close(ErrorCode error); | 83 void Close(ErrorCode error); |
93 | 84 |
94 private: | 85 private: |
| 86 // PeerConnectionWrapper is responsible for PeerConnection creation, |
| 87 // ownership. It passes all events to the corresponding methods below. This is |
| 88 // necessary to make it possible to close and destroy PeerConnection |
| 89 // asynchronously, as it may be on stack when the transport is destroyed. |
| 90 class PeerConnectionWrapper; |
| 91 friend class PeerConnectionWrapper; |
| 92 |
95 void OnLocalSessionDescriptionCreated( | 93 void OnLocalSessionDescriptionCreated( |
96 std::unique_ptr<webrtc::SessionDescriptionInterface> description, | 94 std::unique_ptr<webrtc::SessionDescriptionInterface> description, |
97 const std::string& error); | 95 const std::string& error); |
98 void OnLocalDescriptionSet(bool success, const std::string& error); | 96 void OnLocalDescriptionSet(bool success, const std::string& error); |
99 void OnRemoteDescriptionSet(bool send_answer, | 97 void OnRemoteDescriptionSet(bool send_answer, |
100 bool success, | 98 bool success, |
101 const std::string& error); | 99 const std::string& error); |
102 | 100 |
103 // webrtc::PeerConnectionObserver interface. | 101 // PeerConnection event handlers, called by PeerConnectionWrapper. |
104 void OnSignalingChange( | 102 void OnSignalingChange( |
105 webrtc::PeerConnectionInterface::SignalingState new_state) override; | 103 webrtc::PeerConnectionInterface::SignalingState new_state); |
106 void OnAddStream( | 104 void OnAddStream( |
107 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override; | 105 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream); |
108 void OnRemoveStream( | 106 void OnRemoveStream( |
109 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override; | 107 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream); |
110 void OnDataChannel( | 108 void OnDataChannel( |
111 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override; | 109 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel); |
112 void OnRenegotiationNeeded() override; | 110 void OnRenegotiationNeeded(); |
113 void OnIceConnectionChange( | 111 void OnIceConnectionChange( |
114 webrtc::PeerConnectionInterface::IceConnectionState new_state) override; | 112 webrtc::PeerConnectionInterface::IceConnectionState new_state); |
115 void OnIceGatheringChange( | 113 void OnIceGatheringChange( |
116 webrtc::PeerConnectionInterface::IceGatheringState new_state) override; | 114 webrtc::PeerConnectionInterface::IceGatheringState new_state); |
117 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; | 115 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate); |
118 | 116 |
119 void RequestNegotiation(); | 117 void RequestNegotiation(); |
120 void SendOffer(); | 118 void SendOffer(); |
121 void EnsurePendingTransportInfoMessage(); | 119 void EnsurePendingTransportInfoMessage(); |
122 void SendTransportInfo(); | 120 void SendTransportInfo(); |
123 void AddPendingCandidatesIfPossible(); | 121 void AddPendingCandidatesIfPossible(); |
124 | 122 |
125 base::ThreadChecker thread_checker_; | 123 base::ThreadChecker thread_checker_; |
126 | 124 |
127 rtc::Thread* worker_thread_; | 125 rtc::Thread* worker_thread_; |
128 scoped_refptr<TransportContext> transport_context_; | 126 scoped_refptr<TransportContext> transport_context_; |
129 EventHandler* event_handler_ = nullptr; | 127 EventHandler* event_handler_ = nullptr; |
130 SendTransportInfoCallback send_transport_info_callback_; | 128 SendTransportInfoCallback send_transport_info_callback_; |
131 | 129 |
132 crypto::HMAC handshake_hmac_; | 130 crypto::HMAC handshake_hmac_; |
133 | 131 |
134 std::unique_ptr<webrtc::FakeAudioDeviceModule> fake_audio_device_module_; | 132 std::unique_ptr<PeerConnectionWrapper> peer_connection_wrapper_; |
135 | |
136 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> | |
137 peer_connection_factory_; | |
138 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; | |
139 | 133 |
140 remoting::WebrtcVideoEncoderFactory* video_encoder_factory_; | 134 remoting::WebrtcVideoEncoderFactory* video_encoder_factory_; |
141 | 135 |
142 bool negotiation_pending_ = false; | 136 bool negotiation_pending_ = false; |
143 | 137 |
144 bool connected_ = false; | 138 bool connected_ = false; |
145 | 139 |
146 std::unique_ptr<buzz::XmlElement> pending_transport_info_message_; | 140 std::unique_ptr<buzz::XmlElement> pending_transport_info_message_; |
147 base::OneShotTimer transport_info_timer_; | 141 base::OneShotTimer transport_info_timer_; |
148 | 142 |
149 ScopedVector<webrtc::IceCandidateInterface> pending_incoming_candidates_; | 143 ScopedVector<webrtc::IceCandidateInterface> pending_incoming_candidates_; |
150 | 144 |
151 base::WeakPtrFactory<WebrtcTransport> weak_factory_; | 145 base::WeakPtrFactory<WebrtcTransport> weak_factory_; |
152 | 146 |
153 DISALLOW_COPY_AND_ASSIGN(WebrtcTransport); | 147 DISALLOW_COPY_AND_ASSIGN(WebrtcTransport); |
154 }; | 148 }; |
155 | 149 |
156 } // namespace protocol | 150 } // namespace protocol |
157 } // namespace remoting | 151 } // namespace remoting |
158 | 152 |
159 #endif // REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_ | 153 #endif // REMOTING_PROTOCOL_WEBRTC_TRANSPORT_H_ |
OLD | NEW |