Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / rtc_peer_connection_handler.h
blob5959b08800ca7db7b7b01cdb6a9e4ddd84670696
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_
8 #include <map>
9 #include <string>
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"
25 namespace blink {
26 class WebFrame;
27 class WebRTCDataChannelHandler;
28 class WebRTCOfferOptions;
29 class WebRTCPeerConnectionHandlerClient;
32 namespace content {
34 class PeerConnectionDependencyFactory;
35 class PeerConnectionTracker;
36 class RemoteMediaStreamImpl;
37 class RtcDataChannelHandler;
38 class RTCMediaConstraints;
39 class WebRtcMediaStreamAdapter;
41 // Mockable wrapper for blink::WebRTCStatsResponse
42 class CONTENT_EXPORT LocalRTCStatsResponse
43 : public NON_EXPORTED_BASE(rtc::RefCountInterface) {
44 public:
45 explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse& impl)
46 : impl_(impl) {
49 virtual blink::WebRTCStatsResponse webKitStatsResponse() const;
50 virtual size_t addReport(blink::WebString type, blink::WebString id,
51 double timestamp);
52 virtual void addStatistic(size_t report,
53 blink::WebString name, blink::WebString value);
55 protected:
56 ~LocalRTCStatsResponse() override {}
57 // Constructor for creating mocks.
58 LocalRTCStatsResponse() {}
60 private:
61 blink::WebRTCStatsResponse impl_;
64 // Mockable wrapper for blink::WebRTCStatsRequest
65 class CONTENT_EXPORT LocalRTCStatsRequest
66 : public NON_EXPORTED_BASE(rtc::RefCountInterface) {
67 public:
68 explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl);
69 // Constructor for testing.
70 LocalRTCStatsRequest();
72 virtual bool hasSelector() const;
73 virtual blink::WebMediaStreamTrack component() const;
74 virtual void requestSucceeded(const LocalRTCStatsResponse* response);
75 virtual scoped_refptr<LocalRTCStatsResponse> createResponse();
77 protected:
78 ~LocalRTCStatsRequest() override;
80 private:
81 blink::WebRTCStatsRequest impl_;
84 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API
85 // messages going between WebKit and native PeerConnection in libjingle. It's
86 // owned by WebKit.
87 // WebKit calls all of these methods on the main render thread.
88 // Callbacks to the webrtc::PeerConnectionObserver implementation also occur on
89 // the main render thread.
90 class CONTENT_EXPORT RTCPeerConnectionHandler
91 : NON_EXPORTED_BASE(public blink::WebRTCPeerConnectionHandler) {
92 public:
93 RTCPeerConnectionHandler(
94 blink::WebRTCPeerConnectionHandlerClient* client,
95 PeerConnectionDependencyFactory* dependency_factory);
96 virtual ~RTCPeerConnectionHandler();
98 // Destroy all existing RTCPeerConnectionHandler objects.
99 static void DestructAllHandlers();
101 static void ConvertOfferOptionsToConstraints(
102 const blink::WebRTCOfferOptions& options,
103 RTCMediaConstraints* output);
105 void associateWithFrame(blink::WebFrame* frame);
107 // Initialize method only used for unit test.
108 bool InitializeForTest(
109 const blink::WebRTCConfiguration& server_configuration,
110 const blink::WebMediaConstraints& options,
111 const base::WeakPtr<PeerConnectionTracker>& peer_connection_tracker);
113 // blink::WebRTCPeerConnectionHandler implementation
114 virtual bool initialize(
115 const blink::WebRTCConfiguration& server_configuration,
116 const blink::WebMediaConstraints& options) override;
118 virtual void createOffer(
119 const blink::WebRTCSessionDescriptionRequest& request,
120 const blink::WebMediaConstraints& options) override;
121 virtual void createOffer(
122 const blink::WebRTCSessionDescriptionRequest& request,
123 const blink::WebRTCOfferOptions& options) override;
125 virtual void createAnswer(
126 const blink::WebRTCSessionDescriptionRequest& request,
127 const blink::WebMediaConstraints& options) override;
129 virtual void setLocalDescription(
130 const blink::WebRTCVoidRequest& request,
131 const blink::WebRTCSessionDescription& description) override;
132 virtual void setRemoteDescription(
133 const blink::WebRTCVoidRequest& request,
134 const blink::WebRTCSessionDescription& description) override;
136 virtual blink::WebRTCSessionDescription localDescription()
137 override;
138 virtual blink::WebRTCSessionDescription remoteDescription()
139 override;
141 virtual bool updateICE(
142 const blink::WebRTCConfiguration& server_configuration,
143 const blink::WebMediaConstraints& options) override;
144 virtual bool addICECandidate(
145 const blink::WebRTCICECandidate& candidate) override;
146 virtual bool addICECandidate(
147 const blink::WebRTCVoidRequest& request,
148 const blink::WebRTCICECandidate& candidate) override;
149 virtual void OnaddICECandidateResult(const blink::WebRTCVoidRequest& request,
150 bool result);
152 virtual bool addStream(
153 const blink::WebMediaStream& stream,
154 const blink::WebMediaConstraints& options) override;
155 virtual void removeStream(
156 const blink::WebMediaStream& stream) override;
157 virtual void getStats(
158 const blink::WebRTCStatsRequest& request) override;
159 virtual blink::WebRTCDataChannelHandler* createDataChannel(
160 const blink::WebString& label,
161 const blink::WebRTCDataChannelInit& init) override;
162 virtual blink::WebRTCDTMFSenderHandler* createDTMFSender(
163 const blink::WebMediaStreamTrack& track) override;
164 virtual void stop() override;
166 // Delegate functions to allow for mocking of WebKit interfaces.
167 // getStats takes ownership of request parameter.
168 virtual void getStats(const scoped_refptr<LocalRTCStatsRequest>& request);
170 // Asynchronously calls native_peer_connection_->getStats on the signaling
171 // thread. If the |track_id| is empty, the |track_type| parameter is ignored.
172 void GetStats(webrtc::StatsObserver* observer,
173 webrtc::PeerConnectionInterface::StatsOutputLevel level,
174 const std::string& track_id,
175 blink::WebMediaStreamSource::Type track_type);
177 // Tells the |client_| to close RTCPeerConnection.
178 void CloseClientPeerConnection();
180 protected:
181 webrtc::PeerConnectionInterface* native_peer_connection() {
182 return native_peer_connection_.get();
185 class Observer;
186 friend class Observer;
188 void OnSignalingChange(
189 webrtc::PeerConnectionInterface::SignalingState new_state);
190 void OnIceConnectionChange(
191 webrtc::PeerConnectionInterface::IceConnectionState new_state);
192 void OnIceGatheringChange(
193 webrtc::PeerConnectionInterface::IceGatheringState new_state);
194 void OnRenegotiationNeeded();
195 void OnAddStream(scoped_ptr<RemoteMediaStreamImpl> stream);
196 void OnRemoveStream(
197 const scoped_refptr<webrtc::MediaStreamInterface>& stream);
198 void OnDataChannel(scoped_ptr<RtcDataChannelHandler> handler);
199 void OnIceCandidate(const std::string& sdp, const std::string& sdp_mid,
200 int sdp_mline_index, int component, int address_family);
202 private:
203 // Record info about the first SessionDescription from the local and
204 // remote side to record UMA stats once both are set.
205 struct FirstSessionDescription {
206 FirstSessionDescription(const webrtc::SessionDescriptionInterface* desc);
208 bool audio = false;
209 bool video = false;
210 // If audio or video will use RTCP-mux (if there is no audio or
211 // video, then false).
212 bool rtcp_mux = false;
215 webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
216 const std::string& sdp, const std::string& type,
217 webrtc::SdpParseError* error);
219 // Report to UMA whether an IceConnectionState has occurred. It only records
220 // the first occurrence of a given state.
221 void ReportICEState(
222 webrtc::PeerConnectionInterface::IceConnectionState new_state);
224 // Reset UMA related members to the initial state. This is invoked at the
225 // constructor as well as after Ice Restart.
226 void ResetUMAStats();
228 void ReportFirstSessionDescriptions(
229 const FirstSessionDescription& local,
230 const FirstSessionDescription& remote);
232 // Virtual to allow mocks to override.
233 virtual scoped_refptr<base::SingleThreadTaskRunner> signaling_thread() const;
235 void RunSynchronousClosureOnSignalingThread(const base::Closure& closure,
236 const char* trace_event_name);
238 base::ThreadChecker thread_checker_;
240 // |client_| is a weak pointer, and is valid until stop() has returned.
241 blink::WebRTCPeerConnectionHandlerClient* client_;
243 // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
244 // RenderThreadImpl.
245 PeerConnectionDependencyFactory* const dependency_factory_;
247 blink::WebFrame* frame_ = nullptr;
249 ScopedVector<WebRtcMediaStreamAdapter> local_streams_;
251 base::WeakPtr<PeerConnectionTracker> peer_connection_tracker_;
253 MediaStreamTrackMetrics track_metrics_;
255 // Counter for a UMA stat reported at destruction time.
256 int num_data_channels_created_ = 0;
258 // Counter for number of IPv4 and IPv6 local candidates.
259 int num_local_candidates_ipv4_ = 0;
260 int num_local_candidates_ipv6_ = 0;
262 // To make sure the observer is released after the native_peer_connection_,
263 // it has to come first.
264 scoped_refptr<Observer> peer_connection_observer_;
266 // |native_peer_connection_| is the libjingle native PeerConnection object.
267 scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection_;
269 // Record info about the first SessionDescription from the local and
270 // remote side to record UMA stats once both are set. We only check
271 // for the first offer or answer. "pranswer"s and "unknown"s (from
272 // unit tests) are ignored.
273 scoped_ptr<FirstSessionDescription> first_local_description_;
274 scoped_ptr<FirstSessionDescription> first_remote_description_;
276 typedef std::map<webrtc::MediaStreamInterface*,
277 content::RemoteMediaStreamImpl*> RemoteStreamMap;
278 RemoteStreamMap remote_streams_;
279 scoped_refptr<webrtc::UMAObserver> uma_observer_;
280 base::TimeTicks ice_connection_checking_start_;
282 // Track which ICE Connection state that this PeerConnection has gone through.
283 bool ice_state_seen_[webrtc::PeerConnectionInterface::kIceConnectionMax] = {};
285 base::WeakPtrFactory<RTCPeerConnectionHandler> weak_factory_;
287 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
290 } // namespace content
292 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_