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 MediaStreamDependencyFactory;
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 MediaStreamDependencyFactory* dependency_factory);
89 virtual ~RTCPeerConnectionHandler();
91 void associateWithFrame(blink::WebFrame* frame);
93 // Initialize method only used for unit test.
94 bool InitializeForTest(
95 const blink::WebRTCConfiguration& server_configuration,
96 const blink::WebMediaConstraints& options,
97 PeerConnectionTracker* peer_connection_tracker);
99 // blink::WebRTCPeerConnectionHandler implementation
100 virtual bool initialize(
101 const blink::WebRTCConfiguration& server_configuration,
102 const blink::WebMediaConstraints& options) OVERRIDE;
104 virtual void createOffer(
105 const blink::WebRTCSessionDescriptionRequest& request,
106 const blink::WebMediaConstraints& options) OVERRIDE;
107 virtual void createAnswer(
108 const blink::WebRTCSessionDescriptionRequest& request,
109 const blink::WebMediaConstraints& options) OVERRIDE;
111 virtual void setLocalDescription(
112 const blink::WebRTCVoidRequest& request,
113 const blink::WebRTCSessionDescription& description) OVERRIDE;
114 virtual void setRemoteDescription(
115 const blink::WebRTCVoidRequest& request,
116 const blink::WebRTCSessionDescription& description) OVERRIDE;
118 virtual blink::WebRTCSessionDescription localDescription()
120 virtual blink::WebRTCSessionDescription remoteDescription()
123 virtual bool updateICE(
124 const blink::WebRTCConfiguration& server_configuration,
125 const blink::WebMediaConstraints& options) OVERRIDE;
126 virtual bool addICECandidate(
127 const blink::WebRTCICECandidate& candidate) OVERRIDE;
128 virtual bool addICECandidate(
129 const blink::WebRTCVoidRequest& request,
130 const blink::WebRTCICECandidate& candidate) OVERRIDE;
131 virtual void OnaddICECandidateResult(const blink::WebRTCVoidRequest& request,
134 virtual bool addStream(
135 const blink::WebMediaStream& stream,
136 const blink::WebMediaConstraints& options) OVERRIDE;
137 virtual void removeStream(
138 const blink::WebMediaStream& stream) OVERRIDE;
139 virtual void getStats(
140 const blink::WebRTCStatsRequest& request) OVERRIDE;
141 virtual blink::WebRTCDataChannelHandler* createDataChannel(
142 const blink::WebString& label,
143 const blink::WebRTCDataChannelInit& init) OVERRIDE;
144 virtual blink::WebRTCDTMFSenderHandler* createDTMFSender(
145 const blink::WebMediaStreamTrack& track) OVERRIDE;
146 virtual void stop() OVERRIDE;
148 // webrtc::PeerConnectionObserver implementation
149 virtual void OnError() OVERRIDE;
150 // Triggered when the SignalingState changed.
151 virtual void OnSignalingChange(
152 webrtc::PeerConnectionInterface::SignalingState new_state) OVERRIDE;
153 virtual void OnAddStream(webrtc::MediaStreamInterface* stream) OVERRIDE;
154 virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream) OVERRIDE;
155 virtual void OnIceCandidate(
156 const webrtc::IceCandidateInterface* candidate) OVERRIDE;
157 virtual void OnIceConnectionChange(
158 webrtc::PeerConnectionInterface::IceConnectionState new_state) OVERRIDE;
159 virtual void OnIceGatheringChange(
160 webrtc::PeerConnectionInterface::IceGatheringState new_state) OVERRIDE;
162 virtual void OnDataChannel(
163 webrtc::DataChannelInterface* data_channel) OVERRIDE;
164 virtual void OnRenegotiationNeeded() OVERRIDE;
166 // Delegate functions to allow for mocking of WebKit interfaces.
167 // getStats takes ownership of request parameter.
168 virtual void getStats(LocalRTCStatsRequest* request);
170 // Calls GetStats on |native_peer_connection_|.
171 void GetStats(webrtc::StatsObserver* observer,
172 webrtc::MediaStreamTrackInterface* track,
173 webrtc::PeerConnectionInterface::StatsOutputLevel level);
175 PeerConnectionTracker* peer_connection_tracker();
178 webrtc::PeerConnectionInterface* native_peer_connection() {
179 return native_peer_connection_.get();
183 webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
184 const blink::WebRTCSessionDescription& description,
185 webrtc::SdpParseError* error);
187 // |client_| is a weak pointer, and is valid until stop() has returned.
188 blink::WebRTCPeerConnectionHandlerClient* client_;
190 // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
192 MediaStreamDependencyFactory* dependency_factory_;
194 blink::WebFrame* frame_;
196 ScopedVector<WebRtcMediaStreamAdapter> local_streams_;
198 PeerConnectionTracker* peer_connection_tracker_;
200 MediaStreamTrackMetrics track_metrics_;
202 // Counter for a UMA stat reported at destruction time.
203 int num_data_channels_created_;
205 // |native_peer_connection_| is the libjingle native PeerConnection object.
206 scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection_;
208 typedef std::map<webrtc::MediaStreamInterface*,
209 content::RemoteMediaStreamImpl*> RemoteStreamMap;
210 RemoteStreamMap remote_streams_;
212 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
215 } // namespace content
217 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_