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 "content/common/content_export.h"
15 #include "content/renderer/media/webrtc/media_stream_track_metrics.h"
16 #include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandler.h"
17 #include "third_party/WebKit/public/platform/WebRTCStatsRequest.h"
18 #include "third_party/WebKit/public/platform/WebRTCStatsResponse.h"
22 class WebRTCDataChannelHandler
;
23 class WebRTCOfferOptions
;
28 class PeerConnectionDependencyFactory
;
29 class PeerConnectionTracker
;
30 class RemoteMediaStreamImpl
;
31 class RTCMediaConstraints
;
32 class WebRtcMediaStreamAdapter
;
34 // Mockable wrapper for blink::WebRTCStatsResponse
35 class CONTENT_EXPORT LocalRTCStatsResponse
36 : public NON_EXPORTED_BASE(rtc::RefCountInterface
) {
38 explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse
& impl
)
42 virtual blink::WebRTCStatsResponse
webKitStatsResponse() const;
43 virtual size_t addReport(blink::WebString type
, blink::WebString id
,
45 virtual void addStatistic(size_t report
,
46 blink::WebString name
, blink::WebString value
);
49 virtual ~LocalRTCStatsResponse() {}
50 // Constructor for creating mocks.
51 LocalRTCStatsResponse() {}
54 blink::WebRTCStatsResponse impl_
;
57 // Mockable wrapper for blink::WebRTCStatsRequest
58 class CONTENT_EXPORT LocalRTCStatsRequest
59 : public NON_EXPORTED_BASE(rtc::RefCountInterface
) {
61 explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl
);
62 // Constructor for testing.
63 LocalRTCStatsRequest();
65 virtual bool hasSelector() const;
66 virtual blink::WebMediaStreamTrack
component() const;
67 virtual void requestSucceeded(const LocalRTCStatsResponse
* response
);
68 virtual scoped_refptr
<LocalRTCStatsResponse
> createResponse();
71 virtual ~LocalRTCStatsRequest();
74 blink::WebRTCStatsRequest impl_
;
75 rtc::scoped_refptr
<LocalRTCStatsResponse
> response_
;
78 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API
79 // messages going between WebKit and native PeerConnection in libjingle. It's
81 // WebKit calls all of these methods on the main render thread.
82 // Callbacks to the webrtc::PeerConnectionObserver implementation also occur on
83 // the main render thread.
84 class CONTENT_EXPORT RTCPeerConnectionHandler
85 : NON_EXPORTED_BASE(public blink::WebRTCPeerConnectionHandler
),
86 NON_EXPORTED_BASE(public webrtc::PeerConnectionObserver
) {
88 RTCPeerConnectionHandler(
89 blink::WebRTCPeerConnectionHandlerClient
* client
,
90 PeerConnectionDependencyFactory
* dependency_factory
);
91 virtual ~RTCPeerConnectionHandler();
93 // Destroy all existing RTCPeerConnectionHandler objects.
94 static void DestructAllHandlers();
96 static void ConvertOfferOptionsToConstraints(
97 const blink::WebRTCOfferOptions
& options
,
98 RTCMediaConstraints
* output
);
100 void associateWithFrame(blink::WebFrame
* frame
);
102 // Initialize method only used for unit test.
103 bool InitializeForTest(
104 const blink::WebRTCConfiguration
& server_configuration
,
105 const blink::WebMediaConstraints
& options
,
106 PeerConnectionTracker
* peer_connection_tracker
);
108 // blink::WebRTCPeerConnectionHandler implementation
109 virtual bool initialize(
110 const blink::WebRTCConfiguration
& server_configuration
,
111 const blink::WebMediaConstraints
& options
) OVERRIDE
;
113 virtual void createOffer(
114 const blink::WebRTCSessionDescriptionRequest
& request
,
115 const blink::WebMediaConstraints
& options
) OVERRIDE
;
116 virtual void createOffer(
117 const blink::WebRTCSessionDescriptionRequest
& request
,
118 const blink::WebRTCOfferOptions
& options
) OVERRIDE
;
120 virtual void createAnswer(
121 const blink::WebRTCSessionDescriptionRequest
& request
,
122 const blink::WebMediaConstraints
& options
) OVERRIDE
;
124 virtual void setLocalDescription(
125 const blink::WebRTCVoidRequest
& request
,
126 const blink::WebRTCSessionDescription
& description
) OVERRIDE
;
127 virtual void setRemoteDescription(
128 const blink::WebRTCVoidRequest
& request
,
129 const blink::WebRTCSessionDescription
& description
) OVERRIDE
;
131 virtual blink::WebRTCSessionDescription
localDescription()
133 virtual blink::WebRTCSessionDescription
remoteDescription()
136 virtual bool updateICE(
137 const blink::WebRTCConfiguration
& server_configuration
,
138 const blink::WebMediaConstraints
& options
) OVERRIDE
;
139 virtual bool addICECandidate(
140 const blink::WebRTCICECandidate
& candidate
) OVERRIDE
;
141 virtual bool addICECandidate(
142 const blink::WebRTCVoidRequest
& request
,
143 const blink::WebRTCICECandidate
& candidate
) OVERRIDE
;
144 virtual void OnaddICECandidateResult(const blink::WebRTCVoidRequest
& request
,
147 virtual bool addStream(
148 const blink::WebMediaStream
& stream
,
149 const blink::WebMediaConstraints
& options
) OVERRIDE
;
150 virtual void removeStream(
151 const blink::WebMediaStream
& stream
) OVERRIDE
;
152 virtual void getStats(
153 const blink::WebRTCStatsRequest
& request
) OVERRIDE
;
154 virtual blink::WebRTCDataChannelHandler
* createDataChannel(
155 const blink::WebString
& label
,
156 const blink::WebRTCDataChannelInit
& init
) OVERRIDE
;
157 virtual blink::WebRTCDTMFSenderHandler
* createDTMFSender(
158 const blink::WebMediaStreamTrack
& track
) OVERRIDE
;
159 virtual void stop() OVERRIDE
;
161 // webrtc::PeerConnectionObserver implementation
162 virtual void OnError() OVERRIDE
;
163 // Triggered when the SignalingState changed.
164 virtual void OnSignalingChange(
165 webrtc::PeerConnectionInterface::SignalingState new_state
) OVERRIDE
;
166 virtual void OnAddStream(webrtc::MediaStreamInterface
* stream
) OVERRIDE
;
167 virtual void OnRemoveStream(webrtc::MediaStreamInterface
* stream
) OVERRIDE
;
168 virtual void OnIceCandidate(
169 const webrtc::IceCandidateInterface
* candidate
) OVERRIDE
;
170 virtual void OnIceConnectionChange(
171 webrtc::PeerConnectionInterface::IceConnectionState new_state
) OVERRIDE
;
172 virtual void OnIceGatheringChange(
173 webrtc::PeerConnectionInterface::IceGatheringState new_state
) OVERRIDE
;
175 virtual void OnDataChannel(
176 webrtc::DataChannelInterface
* data_channel
) OVERRIDE
;
177 virtual void OnRenegotiationNeeded() OVERRIDE
;
179 // Delegate functions to allow for mocking of WebKit interfaces.
180 // getStats takes ownership of request parameter.
181 virtual void getStats(LocalRTCStatsRequest
* request
);
183 // Calls GetStats on |native_peer_connection_|.
184 void GetStats(webrtc::StatsObserver
* observer
,
185 webrtc::MediaStreamTrackInterface
* track
,
186 webrtc::PeerConnectionInterface::StatsOutputLevel level
);
188 PeerConnectionTracker
* peer_connection_tracker();
191 webrtc::PeerConnectionInterface
* native_peer_connection() {
192 return native_peer_connection_
.get();
196 webrtc::SessionDescriptionInterface
* CreateNativeSessionDescription(
197 const blink::WebRTCSessionDescription
& description
,
198 webrtc::SdpParseError
* error
);
200 // |client_| is a weak pointer, and is valid until stop() has returned.
201 blink::WebRTCPeerConnectionHandlerClient
* client_
;
203 // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
205 PeerConnectionDependencyFactory
* dependency_factory_
;
207 blink::WebFrame
* frame_
;
209 ScopedVector
<WebRtcMediaStreamAdapter
> local_streams_
;
211 PeerConnectionTracker
* peer_connection_tracker_
;
213 MediaStreamTrackMetrics track_metrics_
;
215 // Counter for a UMA stat reported at destruction time.
216 int num_data_channels_created_
;
218 // |native_peer_connection_| is the libjingle native PeerConnection object.
219 scoped_refptr
<webrtc::PeerConnectionInterface
> native_peer_connection_
;
221 typedef std::map
<webrtc::MediaStreamInterface
*,
222 content::RemoteMediaStreamImpl
*> RemoteStreamMap
;
223 RemoteStreamMap remote_streams_
;
224 scoped_refptr
<webrtc::UMAObserver
> uma_observer_
;
225 base::TimeTicks ice_connection_checking_start_
;
227 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler
);
230 } // namespace content
232 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_