558ecbf631d6a00cea292ac2b3e6e1f3d2233c8d
[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 PeerConnectionDependencyFactory;
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       PeerConnectionDependencyFactory* dependency_factory);
89   virtual ~RTCPeerConnectionHandler();
90
91   // Destroy all existing RTCPeerConnectionHandler objects.
92   static void DestructAllHandlers();
93
94   void associateWithFrame(blink::WebFrame* frame);
95
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);
101
102   // blink::WebRTCPeerConnectionHandler implementation
103   virtual bool initialize(
104       const blink::WebRTCConfiguration& server_configuration,
105       const blink::WebMediaConstraints& options) OVERRIDE;
106
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;
113
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;
120
121   virtual blink::WebRTCSessionDescription localDescription()
122       OVERRIDE;
123   virtual blink::WebRTCSessionDescription remoteDescription()
124       OVERRIDE;
125
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,
135                                        bool result);
136
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;
150
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;
164
165   virtual void OnDataChannel(
166       webrtc::DataChannelInterface* data_channel) OVERRIDE;
167   virtual void OnRenegotiationNeeded() OVERRIDE;
168
169   // Delegate functions to allow for mocking of WebKit interfaces.
170   // getStats takes ownership of request parameter.
171   virtual void getStats(LocalRTCStatsRequest* request);
172
173   // Calls GetStats on |native_peer_connection_|.
174   void GetStats(webrtc::StatsObserver* observer,
175                 webrtc::MediaStreamTrackInterface* track,
176                 webrtc::PeerConnectionInterface::StatsOutputLevel level);
177
178   PeerConnectionTracker* peer_connection_tracker();
179
180  protected:
181   webrtc::PeerConnectionInterface* native_peer_connection() {
182     return native_peer_connection_.get();
183   }
184
185  private:
186   webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
187       const blink::WebRTCSessionDescription& description,
188       webrtc::SdpParseError* error);
189
190   // |client_| is a weak pointer, and is valid until stop() has returned.
191   blink::WebRTCPeerConnectionHandlerClient* client_;
192
193   // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
194   // RenderThreadImpl.
195   PeerConnectionDependencyFactory* dependency_factory_;
196
197   blink::WebFrame* frame_;
198
199   ScopedVector<WebRtcMediaStreamAdapter> local_streams_;
200
201   PeerConnectionTracker* peer_connection_tracker_;
202
203   MediaStreamTrackMetrics track_metrics_;
204
205   // Counter for a UMA stat reported at destruction time.
206   int num_data_channels_created_;
207
208   // |native_peer_connection_| is the libjingle native PeerConnection object.
209   scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection_;
210
211   typedef std::map<webrtc::MediaStreamInterface*,
212       content::RemoteMediaStreamImpl*> RemoteStreamMap;
213   RemoteStreamMap remote_streams_;
214   scoped_refptr<webrtc::UMAObserver> uma_observer_;
215
216   DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
217 };
218
219 }  // namespace content
220
221 #endif  // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_