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
;
27 class PeerConnectionDependencyFactory
;
28 class PeerConnectionTracker
;
29 class RemoteMediaStreamImpl
;
30 class WebRtcMediaStreamAdapter
;
32 // Mockable wrapper for blink::WebRTCStatsResponse
33 class CONTENT_EXPORT LocalRTCStatsResponse
34 : public NON_EXPORTED_BASE(talk_base::RefCountInterface
) {
36 explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse
& impl
)
40 virtual blink::WebRTCStatsResponse
webKitStatsResponse() const;
41 virtual size_t addReport(blink::WebString type
, blink::WebString id
,
43 virtual void addStatistic(size_t report
,
44 blink::WebString name
, blink::WebString value
);
47 virtual ~LocalRTCStatsResponse() {}
48 // Constructor for creating mocks.
49 LocalRTCStatsResponse() {}
52 blink::WebRTCStatsResponse impl_
;
55 // Mockable wrapper for blink::WebRTCStatsRequest
56 class CONTENT_EXPORT LocalRTCStatsRequest
57 : public NON_EXPORTED_BASE(talk_base::RefCountInterface
) {
59 explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl
);
60 // Constructor for testing.
61 LocalRTCStatsRequest();
63 virtual bool hasSelector() const;
64 virtual blink::WebMediaStreamTrack
component() const;
65 virtual void requestSucceeded(const LocalRTCStatsResponse
* response
);
66 virtual scoped_refptr
<LocalRTCStatsResponse
> createResponse();
69 virtual ~LocalRTCStatsRequest();
72 blink::WebRTCStatsRequest impl_
;
73 talk_base::scoped_refptr
<LocalRTCStatsResponse
> response_
;
76 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API
77 // messages going between WebKit and native PeerConnection in libjingle. It's
79 // WebKit calls all of these methods on the main render thread.
80 // Callbacks to the webrtc::PeerConnectionObserver implementation also occur on
81 // the main render thread.
82 class CONTENT_EXPORT RTCPeerConnectionHandler
83 : NON_EXPORTED_BASE(public blink::WebRTCPeerConnectionHandler
),
84 NON_EXPORTED_BASE(public webrtc::PeerConnectionObserver
) {
86 RTCPeerConnectionHandler(
87 blink::WebRTCPeerConnectionHandlerClient
* client
,
88 PeerConnectionDependencyFactory
* dependency_factory
);
89 virtual ~RTCPeerConnectionHandler();
91 // Destroy all existing RTCPeerConnectionHandler objects.
92 static void DestructAllHandlers();
94 void associateWithFrame(blink::WebFrame
* frame
);
96 // Initialize method only used for unit test.
97 bool InitializeForTest(
98 const blink::WebRTCConfiguration
& server_configuration
,
99 const blink::WebMediaConstraints
& options
,
100 PeerConnectionTracker
* peer_connection_tracker
);
102 // blink::WebRTCPeerConnectionHandler implementation
103 virtual bool initialize(
104 const blink::WebRTCConfiguration
& server_configuration
,
105 const blink::WebMediaConstraints
& options
) OVERRIDE
;
107 virtual void createOffer(
108 const blink::WebRTCSessionDescriptionRequest
& request
,
109 const blink::WebMediaConstraints
& options
) OVERRIDE
;
110 virtual void createAnswer(
111 const blink::WebRTCSessionDescriptionRequest
& request
,
112 const blink::WebMediaConstraints
& options
) OVERRIDE
;
114 virtual void setLocalDescription(
115 const blink::WebRTCVoidRequest
& request
,
116 const blink::WebRTCSessionDescription
& description
) OVERRIDE
;
117 virtual void setRemoteDescription(
118 const blink::WebRTCVoidRequest
& request
,
119 const blink::WebRTCSessionDescription
& description
) OVERRIDE
;
121 virtual blink::WebRTCSessionDescription
localDescription()
123 virtual blink::WebRTCSessionDescription
remoteDescription()
126 virtual bool updateICE(
127 const blink::WebRTCConfiguration
& server_configuration
,
128 const blink::WebMediaConstraints
& options
) OVERRIDE
;
129 virtual bool addICECandidate(
130 const blink::WebRTCICECandidate
& candidate
) OVERRIDE
;
131 virtual bool addICECandidate(
132 const blink::WebRTCVoidRequest
& request
,
133 const blink::WebRTCICECandidate
& candidate
) OVERRIDE
;
134 virtual void OnaddICECandidateResult(const blink::WebRTCVoidRequest
& request
,
137 virtual bool addStream(
138 const blink::WebMediaStream
& stream
,
139 const blink::WebMediaConstraints
& options
) OVERRIDE
;
140 virtual void removeStream(
141 const blink::WebMediaStream
& stream
) OVERRIDE
;
142 virtual void getStats(
143 const blink::WebRTCStatsRequest
& request
) OVERRIDE
;
144 virtual blink::WebRTCDataChannelHandler
* createDataChannel(
145 const blink::WebString
& label
,
146 const blink::WebRTCDataChannelInit
& init
) OVERRIDE
;
147 virtual blink::WebRTCDTMFSenderHandler
* createDTMFSender(
148 const blink::WebMediaStreamTrack
& track
) OVERRIDE
;
149 virtual void stop() OVERRIDE
;
151 // webrtc::PeerConnectionObserver implementation
152 virtual void OnError() OVERRIDE
;
153 // Triggered when the SignalingState changed.
154 virtual void OnSignalingChange(
155 webrtc::PeerConnectionInterface::SignalingState new_state
) OVERRIDE
;
156 virtual void OnAddStream(webrtc::MediaStreamInterface
* stream
) OVERRIDE
;
157 virtual void OnRemoveStream(webrtc::MediaStreamInterface
* stream
) OVERRIDE
;
158 virtual void OnIceCandidate(
159 const webrtc::IceCandidateInterface
* candidate
) OVERRIDE
;
160 virtual void OnIceConnectionChange(
161 webrtc::PeerConnectionInterface::IceConnectionState new_state
) OVERRIDE
;
162 virtual void OnIceGatheringChange(
163 webrtc::PeerConnectionInterface::IceGatheringState new_state
) OVERRIDE
;
165 virtual void OnDataChannel(
166 webrtc::DataChannelInterface
* data_channel
) OVERRIDE
;
167 virtual void OnRenegotiationNeeded() OVERRIDE
;
169 // Delegate functions to allow for mocking of WebKit interfaces.
170 // getStats takes ownership of request parameter.
171 virtual void getStats(LocalRTCStatsRequest
* request
);
173 // Calls GetStats on |native_peer_connection_|.
174 void GetStats(webrtc::StatsObserver
* observer
,
175 webrtc::MediaStreamTrackInterface
* track
,
176 webrtc::PeerConnectionInterface::StatsOutputLevel level
);
178 PeerConnectionTracker
* peer_connection_tracker();
181 webrtc::PeerConnectionInterface
* native_peer_connection() {
182 return native_peer_connection_
.get();
186 webrtc::SessionDescriptionInterface
* CreateNativeSessionDescription(
187 const blink::WebRTCSessionDescription
& description
,
188 webrtc::SdpParseError
* error
);
190 // |client_| is a weak pointer, and is valid until stop() has returned.
191 blink::WebRTCPeerConnectionHandlerClient
* client_
;
193 // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
195 PeerConnectionDependencyFactory
* dependency_factory_
;
197 blink::WebFrame
* frame_
;
199 ScopedVector
<WebRtcMediaStreamAdapter
> local_streams_
;
201 PeerConnectionTracker
* peer_connection_tracker_
;
203 MediaStreamTrackMetrics track_metrics_
;
205 // Counter for a UMA stat reported at destruction time.
206 int num_data_channels_created_
;
208 // |native_peer_connection_| is the libjingle native PeerConnection object.
209 scoped_refptr
<webrtc::PeerConnectionInterface
> native_peer_connection_
;
211 typedef std::map
<webrtc::MediaStreamInterface
*,
212 content::RemoteMediaStreamImpl
*> RemoteStreamMap
;
213 RemoteStreamMap remote_streams_
;
214 scoped_refptr
<webrtc::UMAObserver
> uma_observer_
;
216 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler
);
219 } // namespace content
221 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_