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
;
29 class WebRTCPeerConnectionHandlerClient
;
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
) {
45 explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse
& impl
)
49 virtual blink::WebRTCStatsResponse
webKitStatsResponse() const;
50 virtual size_t addReport(blink::WebString type
, blink::WebString id
,
52 virtual void addStatistic(size_t report
,
53 blink::WebString name
, blink::WebString value
);
56 ~LocalRTCStatsResponse() override
{}
57 // Constructor for creating mocks.
58 LocalRTCStatsResponse() {}
61 blink::WebRTCStatsResponse impl_
;
64 // Mockable wrapper for blink::WebRTCStatsRequest
65 class CONTENT_EXPORT LocalRTCStatsRequest
66 : public NON_EXPORTED_BASE(rtc::RefCountInterface
) {
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();
78 ~LocalRTCStatsRequest() override
;
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
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
) {
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()
138 virtual blink::WebRTCSessionDescription
remoteDescription()
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
,
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();
181 webrtc::PeerConnectionInterface
* native_peer_connection() {
182 return native_peer_connection_
.get();
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
);
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
);
203 webrtc::SessionDescriptionInterface
* CreateNativeSessionDescription(
204 const std::string
& sdp
, const std::string
& type
,
205 webrtc::SdpParseError
* error
);
207 // Virtual to allow mocks to override.
208 virtual scoped_refptr
<base::SingleThreadTaskRunner
> signaling_thread() const;
210 void RunSynchronousClosureOnSignalingThread(const base::Closure
& closure
,
211 const char* trace_event_name
);
213 base::ThreadChecker thread_checker_
;
215 // |client_| is a weak pointer, and is valid until stop() has returned.
216 blink::WebRTCPeerConnectionHandlerClient
* client_
;
218 // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
220 PeerConnectionDependencyFactory
* const dependency_factory_
;
222 blink::WebFrame
* frame_
;
224 ScopedVector
<WebRtcMediaStreamAdapter
> local_streams_
;
226 base::WeakPtr
<PeerConnectionTracker
> peer_connection_tracker_
;
228 MediaStreamTrackMetrics track_metrics_
;
230 // Counter for a UMA stat reported at destruction time.
231 int num_data_channels_created_
;
233 // Counter for number of IPv4 and IPv6 local candidates.
234 int num_local_candidates_ipv4_
;
235 int num_local_candidates_ipv6_
;
237 // To make sure the observer is released after the native_peer_connection_,
238 // it has to come first.
239 scoped_refptr
<Observer
> peer_connection_observer_
;
241 // |native_peer_connection_| is the libjingle native PeerConnection object.
242 scoped_refptr
<webrtc::PeerConnectionInterface
> native_peer_connection_
;
244 typedef std::map
<webrtc::MediaStreamInterface
*,
245 content::RemoteMediaStreamImpl
*> RemoteStreamMap
;
246 RemoteStreamMap remote_streams_
;
247 scoped_refptr
<webrtc::UMAObserver
> uma_observer_
;
248 base::TimeTicks ice_connection_checking_start_
;
249 base::WeakPtrFactory
<RTCPeerConnectionHandler
> weak_factory_
;
251 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler
);
254 } // namespace content
256 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_