1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/threading/thread.h"
17 #include "base/threading/thread_checker.h"
18 #include "content/common/content_export.h"
19 #include "content/renderer/media/webrtc/media_stream_track_metrics.h"
20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
21 #include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandler.h"
22 #include "third_party/WebKit/public/platform/WebRTCStatsRequest.h"
23 #include "third_party/WebKit/public/platform/WebRTCStatsResponse.h"
27 class WebRTCDataChannelHandler;
28 class WebRTCOfferOptions;
33 class PeerConnectionDependencyFactory;
34 class PeerConnectionTracker;
35 class RemoteMediaStreamImpl;
36 class RtcDataChannelHandler;
37 class RTCMediaConstraints;
38 class WebRtcMediaStreamAdapter;
40 // Mockable wrapper for blink::WebRTCStatsResponse
41 class CONTENT_EXPORT LocalRTCStatsResponse
42 : public NON_EXPORTED_BASE(rtc::RefCountInterface) {
44 explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse& impl)
48 virtual blink::WebRTCStatsResponse webKitStatsResponse() const;
49 virtual size_t addReport(blink::WebString type, blink::WebString id,
51 virtual void addStatistic(size_t report,
52 blink::WebString name, blink::WebString value);
55 ~LocalRTCStatsResponse() override {}
56 // Constructor for creating mocks.
57 LocalRTCStatsResponse() {}
60 blink::WebRTCStatsResponse impl_;
63 // Mockable wrapper for blink::WebRTCStatsRequest
64 class CONTENT_EXPORT LocalRTCStatsRequest
65 : public NON_EXPORTED_BASE(rtc::RefCountInterface) {
67 explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl);
68 // Constructor for testing.
69 LocalRTCStatsRequest();
71 virtual bool hasSelector() const;
72 virtual blink::WebMediaStreamTrack component() const;
73 virtual void requestSucceeded(const LocalRTCStatsResponse* response);
74 virtual scoped_refptr<LocalRTCStatsResponse> createResponse();
77 ~LocalRTCStatsRequest() override;
80 blink::WebRTCStatsRequest impl_;
83 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API
84 // messages going between WebKit and native PeerConnection in libjingle. It's
86 // WebKit calls all of these methods on the main render thread.
87 // Callbacks to the webrtc::PeerConnectionObserver implementation also occur on
88 // the main render thread.
89 class CONTENT_EXPORT RTCPeerConnectionHandler
90 : NON_EXPORTED_BASE(public blink::WebRTCPeerConnectionHandler) {
92 RTCPeerConnectionHandler(
93 blink::WebRTCPeerConnectionHandlerClient* client,
94 PeerConnectionDependencyFactory* dependency_factory);
95 virtual ~RTCPeerConnectionHandler();
97 // Destroy all existing RTCPeerConnectionHandler objects.
98 static void DestructAllHandlers();
100 static void ConvertOfferOptionsToConstraints(
101 const blink::WebRTCOfferOptions& options,
102 RTCMediaConstraints* output);
104 void associateWithFrame(blink::WebFrame* frame);
106 // Initialize method only used for unit test.
107 bool InitializeForTest(
108 const blink::WebRTCConfiguration& server_configuration,
109 const blink::WebMediaConstraints& options,
110 const base::WeakPtr<PeerConnectionTracker>& peer_connection_tracker);
112 // blink::WebRTCPeerConnectionHandler implementation
113 virtual bool initialize(
114 const blink::WebRTCConfiguration& server_configuration,
115 const blink::WebMediaConstraints& options) override;
117 virtual void createOffer(
118 const blink::WebRTCSessionDescriptionRequest& request,
119 const blink::WebMediaConstraints& options) override;
120 virtual void createOffer(
121 const blink::WebRTCSessionDescriptionRequest& request,
122 const blink::WebRTCOfferOptions& options) override;
124 virtual void createAnswer(
125 const blink::WebRTCSessionDescriptionRequest& request,
126 const blink::WebMediaConstraints& options) override;
128 virtual void setLocalDescription(
129 const blink::WebRTCVoidRequest& request,
130 const blink::WebRTCSessionDescription& description) override;
131 virtual void setRemoteDescription(
132 const blink::WebRTCVoidRequest& request,
133 const blink::WebRTCSessionDescription& description) override;
135 virtual blink::WebRTCSessionDescription localDescription()
137 virtual blink::WebRTCSessionDescription remoteDescription()
140 virtual bool updateICE(
141 const blink::WebRTCConfiguration& server_configuration,
142 const blink::WebMediaConstraints& options) override;
143 virtual bool addICECandidate(
144 const blink::WebRTCICECandidate& candidate) override;
145 virtual bool addICECandidate(
146 const blink::WebRTCVoidRequest& request,
147 const blink::WebRTCICECandidate& candidate) override;
148 virtual void OnaddICECandidateResult(const blink::WebRTCVoidRequest& request,
151 virtual bool addStream(
152 const blink::WebMediaStream& stream,
153 const blink::WebMediaConstraints& options) override;
154 virtual void removeStream(
155 const blink::WebMediaStream& stream) override;
156 virtual void getStats(
157 const blink::WebRTCStatsRequest& request) override;
158 virtual blink::WebRTCDataChannelHandler* createDataChannel(
159 const blink::WebString& label,
160 const blink::WebRTCDataChannelInit& init) override;
161 virtual blink::WebRTCDTMFSenderHandler* createDTMFSender(
162 const blink::WebMediaStreamTrack& track) override;
163 virtual void stop() override;
165 // Delegate functions to allow for mocking of WebKit interfaces.
166 // getStats takes ownership of request parameter.
167 virtual void getStats(const scoped_refptr<LocalRTCStatsRequest>& request);
169 // Asynchronously calls native_peer_connection_->getStats on the signaling
170 // thread. If the |track_id| is empty, the |track_type| parameter is ignored.
171 void GetStats(webrtc::StatsObserver* observer,
172 webrtc::PeerConnectionInterface::StatsOutputLevel level,
173 const std::string& track_id,
174 blink::WebMediaStreamSource::Type track_type);
176 // Tells the |client_| to close RTCPeerConnection.
177 void CloseClientPeerConnection();
180 webrtc::PeerConnectionInterface* native_peer_connection() {
181 return native_peer_connection_.get();
185 friend class Observer;
187 void OnSignalingChange(
188 webrtc::PeerConnectionInterface::SignalingState new_state);
189 void OnIceConnectionChange(
190 webrtc::PeerConnectionInterface::IceConnectionState new_state);
191 void OnIceGatheringChange(
192 webrtc::PeerConnectionInterface::IceGatheringState new_state);
193 void OnRenegotiationNeeded();
194 void OnAddStream(scoped_ptr<RemoteMediaStreamImpl> stream);
196 const scoped_refptr<webrtc::MediaStreamInterface>& stream);
197 void OnDataChannel(scoped_ptr<RtcDataChannelHandler> handler);
198 void OnIceCandidate(const std::string& sdp, const std::string& sdp_mid,
199 int sdp_mline_index, int component, int address_family);
202 webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
203 const std::string& sdp, const std::string& type,
204 webrtc::SdpParseError* error);
206 // Virtual to allow mocks to override.
207 virtual scoped_refptr<base::SingleThreadTaskRunner> signaling_thread() const;
209 void RunSynchronousClosureOnSignalingThread(const base::Closure& closure,
210 const char* trace_event_name);
212 base::ThreadChecker thread_checker_;
214 // |client_| is a weak pointer, and is valid until stop() has returned.
215 blink::WebRTCPeerConnectionHandlerClient* client_;
217 // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
219 PeerConnectionDependencyFactory* const dependency_factory_;
221 blink::WebFrame* frame_;
223 ScopedVector<WebRtcMediaStreamAdapter> local_streams_;
225 base::WeakPtr<PeerConnectionTracker> peer_connection_tracker_;
227 MediaStreamTrackMetrics track_metrics_;
229 // Counter for a UMA stat reported at destruction time.
230 int num_data_channels_created_;
232 // Counter for number of IPv4 and IPv6 local candidates.
233 int num_local_candidates_ipv4_;
234 int num_local_candidates_ipv6_;
236 // To make sure the observer is released after the native_peer_connection_,
237 // it has to come first.
238 scoped_refptr<Observer> peer_connection_observer_;
240 // |native_peer_connection_| is the libjingle native PeerConnection object.
241 scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection_;
243 typedef std::map<webrtc::MediaStreamInterface*,
244 content::RemoteMediaStreamImpl*> RemoteStreamMap;
245 RemoteStreamMap remote_streams_;
246 scoped_refptr<webrtc::UMAObserver> uma_observer_;
247 base::TimeTicks ice_connection_checking_start_;
248 base::WeakPtrFactory<RTCPeerConnectionHandler> weak_factory_;
250 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
253 } // namespace content
255 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_