Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / rtc_peer_connection_handler.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_
7
8 #include <map>
9 #include <string>
10
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"
19
20 namespace blink {
21 class WebFrame;
22 class WebRTCDataChannelHandler;
23 }
24
25 namespace content {
26
27 class MediaStreamDependencyFactory;
28 class PeerConnectionTracker;
29 class RemoteMediaStreamImpl;
30 class WebRtcMediaStreamAdapter;
31
32 // Mockable wrapper for blink::WebRTCStatsResponse
33 class CONTENT_EXPORT LocalRTCStatsResponse
34     : public NON_EXPORTED_BASE(talk_base::RefCountInterface) {
35  public:
36   explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse& impl)
37       : impl_(impl) {
38   }
39
40   virtual blink::WebRTCStatsResponse webKitStatsResponse() const;
41   virtual size_t addReport(blink::WebString type, blink::WebString id,
42                            double timestamp);
43   virtual void addStatistic(size_t report,
44                             blink::WebString name, blink::WebString value);
45
46  protected:
47   virtual ~LocalRTCStatsResponse() {}
48   // Constructor for creating mocks.
49   LocalRTCStatsResponse() {}
50
51  private:
52   blink::WebRTCStatsResponse impl_;
53 };
54
55 // Mockable wrapper for blink::WebRTCStatsRequest
56 class CONTENT_EXPORT LocalRTCStatsRequest
57     : public NON_EXPORTED_BASE(talk_base::RefCountInterface) {
58  public:
59   explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl);
60   // Constructor for testing.
61   LocalRTCStatsRequest();
62
63   virtual bool hasSelector() const;
64   virtual blink::WebMediaStreamTrack component() const;
65   virtual void requestSucceeded(const LocalRTCStatsResponse* response);
66   virtual scoped_refptr<LocalRTCStatsResponse> createResponse();
67
68  protected:
69   virtual ~LocalRTCStatsRequest();
70
71  private:
72   blink::WebRTCStatsRequest impl_;
73   talk_base::scoped_refptr<LocalRTCStatsResponse> response_;
74 };
75
76 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API
77 // messages going between WebKit and native PeerConnection in libjingle. It's
78 // owned by WebKit.
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) {
85  public:
86   RTCPeerConnectionHandler(
87       blink::WebRTCPeerConnectionHandlerClient* client,
88       MediaStreamDependencyFactory* dependency_factory);
89   virtual ~RTCPeerConnectionHandler();
90
91   void associateWithFrame(blink::WebFrame* frame);
92
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);
98
99   // blink::WebRTCPeerConnectionHandler implementation
100   virtual bool initialize(
101       const blink::WebRTCConfiguration& server_configuration,
102       const blink::WebMediaConstraints& options) OVERRIDE;
103
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;
110
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;
117
118   virtual blink::WebRTCSessionDescription localDescription()
119       OVERRIDE;
120   virtual blink::WebRTCSessionDescription remoteDescription()
121       OVERRIDE;
122
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,
132                                        bool result);
133
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;
147
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;
161
162   virtual void OnDataChannel(
163       webrtc::DataChannelInterface* data_channel) OVERRIDE;
164   virtual void OnRenegotiationNeeded() OVERRIDE;
165
166   // Delegate functions to allow for mocking of WebKit interfaces.
167   // getStats takes ownership of request parameter.
168   virtual void getStats(LocalRTCStatsRequest* request);
169
170   // Calls GetStats on |native_peer_connection_|.
171   void GetStats(webrtc::StatsObserver* observer,
172                 webrtc::MediaStreamTrackInterface* track,
173                 webrtc::PeerConnectionInterface::StatsOutputLevel level);
174
175   PeerConnectionTracker* peer_connection_tracker();
176
177  protected:
178   webrtc::PeerConnectionInterface* native_peer_connection() {
179     return native_peer_connection_.get();
180   }
181
182  private:
183   webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
184       const blink::WebRTCSessionDescription& description,
185       webrtc::SdpParseError* error);
186
187   // |client_| is a weak pointer, and is valid until stop() has returned.
188   blink::WebRTCPeerConnectionHandlerClient* client_;
189
190   // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
191   // RenderThreadImpl.
192   MediaStreamDependencyFactory* dependency_factory_;
193
194   blink::WebFrame* frame_;
195
196   ScopedVector<WebRtcMediaStreamAdapter> local_streams_;
197
198   PeerConnectionTracker* peer_connection_tracker_;
199
200   MediaStreamTrackMetrics track_metrics_;
201
202   // Counter for a UMA stat reported at destruction time.
203   int num_data_channels_created_;
204
205   // |native_peer_connection_| is the libjingle native PeerConnection object.
206   scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection_;
207
208   typedef std::map<webrtc::MediaStreamInterface*,
209       content::RemoteMediaStreamImpl*> RemoteStreamMap;
210   RemoteStreamMap remote_streams_;
211
212   DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
213 };
214
215 }  // namespace content
216
217 #endif  // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_