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 blink::WebRTCStatsResponse
26 class CONTENT_EXPORT LocalRTCStatsResponse
27 : public NON_EXPORTED_BASE(talk_base::RefCountInterface) {
29 explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse& impl)
33 virtual blink::WebRTCStatsResponse webKitStatsResponse() const;
34 virtual size_t addReport(blink::WebString type, blink::WebString id,
36 virtual void addStatistic(size_t report,
37 blink::WebString name, blink::WebString value);
40 virtual ~LocalRTCStatsResponse() {}
41 // Constructor for creating mocks.
42 LocalRTCStatsResponse() {}
45 blink::WebRTCStatsResponse impl_;
48 // Mockable wrapper for blink::WebRTCStatsRequest
49 class CONTENT_EXPORT LocalRTCStatsRequest
50 : public NON_EXPORTED_BASE(talk_base::RefCountInterface) {
52 explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl);
53 // Constructor for testing.
54 LocalRTCStatsRequest();
56 virtual bool hasSelector() const;
57 virtual blink::WebMediaStreamTrack component() const;
58 virtual void requestSucceeded(const LocalRTCStatsResponse* response);
59 virtual scoped_refptr<LocalRTCStatsResponse> createResponse();
62 virtual ~LocalRTCStatsRequest();
65 blink::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 blink::WebRTCPeerConnectionHandler) {
79 RTCPeerConnectionHandler(
80 blink::WebRTCPeerConnectionHandlerClient* client,
81 MediaStreamDependencyFactory* dependency_factory);
82 virtual ~RTCPeerConnectionHandler();
84 void associateWithFrame(blink::WebFrame* frame);
86 // Initialize method only used for unit test.
87 bool InitializeForTest(
88 const blink::WebRTCConfiguration& server_configuration,
89 const blink::WebMediaConstraints& options,
90 PeerConnectionTracker* peer_connection_tracker);
92 // blink::WebRTCPeerConnectionHandler implementation
93 virtual bool initialize(
94 const blink::WebRTCConfiguration& server_configuration,
95 const blink::WebMediaConstraints& options) OVERRIDE;
97 virtual void createOffer(
98 const blink::WebRTCSessionDescriptionRequest& request,
99 const blink::WebMediaConstraints& options) OVERRIDE;
100 virtual void createAnswer(
101 const blink::WebRTCSessionDescriptionRequest& request,
102 const blink::WebMediaConstraints& options) OVERRIDE;
104 virtual void setLocalDescription(
105 const blink::WebRTCVoidRequest& request,
106 const blink::WebRTCSessionDescription& description) OVERRIDE;
107 virtual void setRemoteDescription(
108 const blink::WebRTCVoidRequest& request,
109 const blink::WebRTCSessionDescription& description) OVERRIDE;
111 virtual blink::WebRTCSessionDescription localDescription()
113 virtual blink::WebRTCSessionDescription remoteDescription()
116 virtual bool updateICE(
117 const blink::WebRTCConfiguration& server_configuration,
118 const blink::WebMediaConstraints& options) OVERRIDE;
119 virtual bool addICECandidate(
120 const blink::WebRTCICECandidate& candidate) OVERRIDE;
121 virtual bool addICECandidate(
122 const blink::WebRTCVoidRequest& request,
123 const blink::WebRTCICECandidate& candidate) OVERRIDE;
124 virtual void OnaddICECandidateResult(const blink::WebRTCVoidRequest& request,
127 virtual bool addStream(
128 const blink::WebMediaStream& stream,
129 const blink::WebMediaConstraints& options) OVERRIDE;
130 virtual void removeStream(
131 const blink::WebMediaStream& stream) OVERRIDE;
132 virtual void getStats(
133 const blink::WebRTCStatsRequest& request) OVERRIDE;
134 virtual blink::WebRTCDataChannelHandler* createDataChannel(
135 const blink::WebString& label,
136 const blink::WebRTCDataChannelInit& init) OVERRIDE;
137 virtual blink::WebRTCDTMFSenderHandler* createDTMFSender(
138 const blink::WebMediaStreamTrack& track) OVERRIDE;
139 virtual void stop() OVERRIDE;
141 // webrtc::PeerConnectionObserver implementation
142 virtual void OnError() OVERRIDE;
143 // Triggered when the SignalingState changed.
144 virtual void OnSignalingChange(
145 webrtc::PeerConnectionInterface::SignalingState new_state) OVERRIDE;
146 virtual void OnAddStream(webrtc::MediaStreamInterface* stream) OVERRIDE;
147 virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream) OVERRIDE;
148 virtual void OnIceCandidate(
149 const webrtc::IceCandidateInterface* candidate) OVERRIDE;
150 virtual void OnIceConnectionChange(
151 webrtc::PeerConnectionInterface::IceConnectionState new_state) OVERRIDE;
152 virtual void OnIceGatheringChange(
153 webrtc::PeerConnectionInterface::IceGatheringState new_state) OVERRIDE;
155 virtual void OnDataChannel(
156 webrtc::DataChannelInterface* data_channel) OVERRIDE;
157 virtual void OnRenegotiationNeeded() OVERRIDE;
159 // Delegate functions to allow for mocking of WebKit interfaces.
160 // getStats takes ownership of request parameter.
161 virtual void getStats(LocalRTCStatsRequest* request);
163 // Calls GetStats on |native_peer_connection_|.
164 void GetStats(webrtc::StatsObserver* observer,
165 webrtc::MediaStreamTrackInterface* track);
167 PeerConnectionTracker* peer_connection_tracker();
170 webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
171 const blink::WebRTCSessionDescription& description,
172 webrtc::SdpParseError* error);
174 // |client_| is a weak pointer, and is valid until stop() has returned.
175 blink::WebRTCPeerConnectionHandlerClient* client_;
177 blink::WebFrame* frame_;
179 PeerConnectionTracker* peer_connection_tracker_;
181 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
184 } // namespace content
186 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_