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 "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "content/common/content_export.h"
11 #include "content/renderer/media/peer_connection_handler_base.h"
12 #include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandler.h"
13 #include "third_party/WebKit/public/platform/WebRTCStatsRequest.h"
14 #include "third_party/WebKit/public/platform/WebRTCStatsResponse.h"
18 class WebRTCDataChannelHandler;
23 class PeerConnectionTracker;
25 // Mockable wrapper for WebKit::WebRTCStatsResponse
26 class CONTENT_EXPORT LocalRTCStatsResponse
27 : public NON_EXPORTED_BASE(talk_base::RefCountInterface) {
29 explicit LocalRTCStatsResponse(const WebKit::WebRTCStatsResponse& impl)
33 virtual WebKit::WebRTCStatsResponse webKitStatsResponse() const;
34 virtual size_t addReport(WebKit::WebString type, WebKit::WebString id,
36 virtual void addStatistic(size_t report,
37 WebKit::WebString name, WebKit::WebString value);
40 virtual ~LocalRTCStatsResponse() {}
41 // Constructor for creating mocks.
42 LocalRTCStatsResponse() {}
45 WebKit::WebRTCStatsResponse impl_;
48 // Mockable wrapper for WebKit::WebRTCStatsRequest
49 class CONTENT_EXPORT LocalRTCStatsRequest
50 : public NON_EXPORTED_BASE(talk_base::RefCountInterface) {
52 explicit LocalRTCStatsRequest(WebKit::WebRTCStatsRequest impl);
53 // Constructor for testing.
54 LocalRTCStatsRequest();
56 virtual bool hasSelector() const;
57 virtual WebKit::WebMediaStreamTrack component() const;
58 virtual void requestSucceeded(const LocalRTCStatsResponse* response);
59 virtual scoped_refptr<LocalRTCStatsResponse> createResponse();
62 virtual ~LocalRTCStatsRequest();
65 WebKit::WebRTCStatsRequest impl_;
66 talk_base::scoped_refptr<LocalRTCStatsResponse> response_;
69 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API
70 // messages going between WebKit and native PeerConnection in libjingle. It's
72 // WebKit calls all of these methods on the main render thread.
73 // Callbacks to the webrtc::PeerConnectionObserver implementation also occur on
74 // the main render thread.
75 class CONTENT_EXPORT RTCPeerConnectionHandler
76 : public PeerConnectionHandlerBase,
77 NON_EXPORTED_BASE(public WebKit::WebRTCPeerConnectionHandler) {
79 RTCPeerConnectionHandler(
80 WebKit::WebRTCPeerConnectionHandlerClient* client,
81 MediaStreamDependencyFactory* dependency_factory);
82 virtual ~RTCPeerConnectionHandler();
84 void associateWithFrame(WebKit::WebFrame* frame);
86 // Initialize method only used for unit test.
87 bool InitializeForTest(
88 const WebKit::WebRTCConfiguration& server_configuration,
89 const WebKit::WebMediaConstraints& options,
90 PeerConnectionTracker* peer_connection_tracker);
92 // WebKit::WebRTCPeerConnectionHandler implementation
93 virtual bool initialize(
94 const WebKit::WebRTCConfiguration& server_configuration,
95 const WebKit::WebMediaConstraints& options) OVERRIDE;
97 virtual void createOffer(
98 const WebKit::WebRTCSessionDescriptionRequest& request,
99 const WebKit::WebMediaConstraints& options) OVERRIDE;
100 virtual void createAnswer(
101 const WebKit::WebRTCSessionDescriptionRequest& request,
102 const WebKit::WebMediaConstraints& options) OVERRIDE;
104 virtual void setLocalDescription(
105 const WebKit::WebRTCVoidRequest& request,
106 const WebKit::WebRTCSessionDescription& description) OVERRIDE;
107 virtual void setRemoteDescription(
108 const WebKit::WebRTCVoidRequest& request,
109 const WebKit::WebRTCSessionDescription& description) OVERRIDE;
111 virtual WebKit::WebRTCSessionDescription localDescription()
113 virtual WebKit::WebRTCSessionDescription remoteDescription()
115 virtual bool updateICE(
116 const WebKit::WebRTCConfiguration& server_configuration,
117 const WebKit::WebMediaConstraints& options) OVERRIDE;
118 virtual bool addICECandidate(
119 const WebKit::WebRTCICECandidate& candidate) OVERRIDE;
121 virtual bool addStream(
122 const WebKit::WebMediaStream& stream,
123 const WebKit::WebMediaConstraints& options) OVERRIDE;
124 virtual void removeStream(
125 const WebKit::WebMediaStream& stream) OVERRIDE;
126 virtual void getStats(
127 const WebKit::WebRTCStatsRequest& request) OVERRIDE;
128 virtual WebKit::WebRTCDataChannelHandler* createDataChannel(
129 const WebKit::WebString& label,
130 const WebKit::WebRTCDataChannelInit& init) OVERRIDE;
131 virtual WebKit::WebRTCDTMFSenderHandler* createDTMFSender(
132 const WebKit::WebMediaStreamTrack& track) OVERRIDE;
133 virtual void stop() OVERRIDE;
135 // webrtc::PeerConnectionObserver implementation
136 virtual void OnError() OVERRIDE;
137 // Triggered when the SignalingState changed.
138 virtual void OnSignalingChange(
139 webrtc::PeerConnectionInterface::SignalingState new_state) OVERRIDE;
140 virtual void OnAddStream(webrtc::MediaStreamInterface* stream) OVERRIDE;
141 virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream) OVERRIDE;
142 virtual void OnIceCandidate(
143 const webrtc::IceCandidateInterface* candidate) OVERRIDE;
144 virtual void OnIceConnectionChange(
145 webrtc::PeerConnectionInterface::IceConnectionState new_state) OVERRIDE;
146 virtual void OnIceGatheringChange(
147 webrtc::PeerConnectionInterface::IceGatheringState new_state) OVERRIDE;
149 virtual void OnDataChannel(
150 webrtc::DataChannelInterface* data_channel) OVERRIDE;
151 virtual void OnRenegotiationNeeded() OVERRIDE;
153 // Delegate functions to allow for mocking of WebKit interfaces.
154 // getStats takes ownership of request parameter.
155 virtual void getStats(LocalRTCStatsRequest* request);
157 // Calls GetStats on |native_peer_connection_|.
158 void GetStats(webrtc::StatsObserver* observer,
159 webrtc::MediaStreamTrackInterface* track);
161 PeerConnectionTracker* peer_connection_tracker();
164 webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
165 const WebKit::WebRTCSessionDescription& description,
166 webrtc::SdpParseError* error);
168 // |client_| is a weak pointer, and is valid until stop() has returned.
169 WebKit::WebRTCPeerConnectionHandlerClient* client_;
171 WebKit::WebFrame* frame_;
173 PeerConnectionTracker* peer_connection_tracker_;
175 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
178 } // namespace content
180 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_