Upstream version 11.40.277.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 "base/memory/weak_ptr.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/threading/thread.h"
17 #include "base/threading/thread_checker.h"
18 #include "content/common/content_export.h"
19 #include "content/renderer/media/webrtc/media_stream_track_metrics.h"
20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
21 #include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandler.h"
22 #include "third_party/WebKit/public/platform/WebRTCStatsRequest.h"
23 #include "third_party/WebKit/public/platform/WebRTCStatsResponse.h"
24
25 namespace blink {
26 class WebFrame;
27 class WebRTCDataChannelHandler;
28 class WebRTCOfferOptions;
29 }
30
31 namespace content {
32
33 class PeerConnectionDependencyFactory;
34 class PeerConnectionTracker;
35 class RemoteMediaStreamImpl;
36 class RtcDataChannelHandler;
37 class RTCMediaConstraints;
38 class WebRtcMediaStreamAdapter;
39
40 // Mockable wrapper for blink::WebRTCStatsResponse
41 class CONTENT_EXPORT LocalRTCStatsResponse
42     : public NON_EXPORTED_BASE(rtc::RefCountInterface) {
43  public:
44   explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse& impl)
45       : impl_(impl) {
46   }
47
48   virtual blink::WebRTCStatsResponse webKitStatsResponse() const;
49   virtual size_t addReport(blink::WebString type, blink::WebString id,
50                            double timestamp);
51   virtual void addStatistic(size_t report,
52                             blink::WebString name, blink::WebString value);
53
54  protected:
55   ~LocalRTCStatsResponse() override {}
56   // Constructor for creating mocks.
57   LocalRTCStatsResponse() {}
58
59  private:
60   blink::WebRTCStatsResponse impl_;
61 };
62
63 // Mockable wrapper for blink::WebRTCStatsRequest
64 class CONTENT_EXPORT LocalRTCStatsRequest
65     : public NON_EXPORTED_BASE(rtc::RefCountInterface) {
66  public:
67   explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl);
68   // Constructor for testing.
69   LocalRTCStatsRequest();
70
71   virtual bool hasSelector() const;
72   virtual blink::WebMediaStreamTrack component() const;
73   virtual void requestSucceeded(const LocalRTCStatsResponse* response);
74   virtual scoped_refptr<LocalRTCStatsResponse> createResponse();
75
76  protected:
77   ~LocalRTCStatsRequest() override;
78
79  private:
80   blink::WebRTCStatsRequest impl_;
81 };
82
83 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API
84 // messages going between WebKit and native PeerConnection in libjingle. It's
85 // owned by WebKit.
86 // WebKit calls all of these methods on the main render thread.
87 // Callbacks to the webrtc::PeerConnectionObserver implementation also occur on
88 // the main render thread.
89 class CONTENT_EXPORT RTCPeerConnectionHandler
90     : NON_EXPORTED_BASE(public blink::WebRTCPeerConnectionHandler) {
91  public:
92   RTCPeerConnectionHandler(
93       blink::WebRTCPeerConnectionHandlerClient* client,
94       PeerConnectionDependencyFactory* dependency_factory);
95   virtual ~RTCPeerConnectionHandler();
96
97   // Destroy all existing RTCPeerConnectionHandler objects.
98   static void DestructAllHandlers();
99
100   static void ConvertOfferOptionsToConstraints(
101       const blink::WebRTCOfferOptions& options,
102       RTCMediaConstraints* output);
103
104   void associateWithFrame(blink::WebFrame* frame);
105
106   // Initialize method only used for unit test.
107   bool InitializeForTest(
108       const blink::WebRTCConfiguration& server_configuration,
109       const blink::WebMediaConstraints& options,
110       const base::WeakPtr<PeerConnectionTracker>& peer_connection_tracker);
111
112   // blink::WebRTCPeerConnectionHandler implementation
113   virtual bool initialize(
114       const blink::WebRTCConfiguration& server_configuration,
115       const blink::WebMediaConstraints& options) override;
116
117   virtual void createOffer(
118       const blink::WebRTCSessionDescriptionRequest& request,
119       const blink::WebMediaConstraints& options) override;
120   virtual void createOffer(
121       const blink::WebRTCSessionDescriptionRequest& request,
122       const blink::WebRTCOfferOptions& options) override;
123
124   virtual void createAnswer(
125       const blink::WebRTCSessionDescriptionRequest& request,
126       const blink::WebMediaConstraints& options) override;
127
128   virtual void setLocalDescription(
129       const blink::WebRTCVoidRequest& request,
130       const blink::WebRTCSessionDescription& description) override;
131   virtual void setRemoteDescription(
132         const blink::WebRTCVoidRequest& request,
133         const blink::WebRTCSessionDescription& description) override;
134
135   virtual blink::WebRTCSessionDescription localDescription()
136       override;
137   virtual blink::WebRTCSessionDescription remoteDescription()
138       override;
139
140   virtual bool updateICE(
141       const blink::WebRTCConfiguration& server_configuration,
142       const blink::WebMediaConstraints& options) override;
143   virtual bool addICECandidate(
144       const blink::WebRTCICECandidate& candidate) override;
145   virtual bool addICECandidate(
146       const blink::WebRTCVoidRequest& request,
147       const blink::WebRTCICECandidate& candidate) override;
148   virtual void OnaddICECandidateResult(const blink::WebRTCVoidRequest& request,
149                                        bool result);
150
151   virtual bool addStream(
152       const blink::WebMediaStream& stream,
153       const blink::WebMediaConstraints& options) override;
154   virtual void removeStream(
155       const blink::WebMediaStream& stream) override;
156   virtual void getStats(
157       const blink::WebRTCStatsRequest& request) override;
158   virtual blink::WebRTCDataChannelHandler* createDataChannel(
159       const blink::WebString& label,
160       const blink::WebRTCDataChannelInit& init) override;
161   virtual blink::WebRTCDTMFSenderHandler* createDTMFSender(
162       const blink::WebMediaStreamTrack& track) override;
163   virtual void stop() override;
164
165   // Delegate functions to allow for mocking of WebKit interfaces.
166   // getStats takes ownership of request parameter.
167   virtual void getStats(const scoped_refptr<LocalRTCStatsRequest>& request);
168
169   // Asynchronously calls native_peer_connection_->getStats on the signaling
170   // thread.  If the |track_id| is empty, the |track_type| parameter is ignored.
171   void GetStats(webrtc::StatsObserver* observer,
172                 webrtc::PeerConnectionInterface::StatsOutputLevel level,
173                 const std::string& track_id,
174                 blink::WebMediaStreamSource::Type track_type);
175
176   // Tells the |client_| to close RTCPeerConnection.
177   void CloseClientPeerConnection();
178
179  protected:
180   webrtc::PeerConnectionInterface* native_peer_connection() {
181     return native_peer_connection_.get();
182   }
183
184   class Observer;
185   friend class Observer;
186
187   void OnSignalingChange(
188       webrtc::PeerConnectionInterface::SignalingState new_state);
189   void OnIceConnectionChange(
190       webrtc::PeerConnectionInterface::IceConnectionState new_state);
191   void OnIceGatheringChange(
192       webrtc::PeerConnectionInterface::IceGatheringState new_state);
193   void OnRenegotiationNeeded();
194   void OnAddStream(scoped_ptr<RemoteMediaStreamImpl> stream);
195   void OnRemoveStream(
196       const scoped_refptr<webrtc::MediaStreamInterface>& stream);
197   void OnDataChannel(scoped_ptr<RtcDataChannelHandler> handler);
198   void OnIceCandidate(const std::string& sdp, const std::string& sdp_mid,
199       int sdp_mline_index, int component, int address_family);
200
201  private:
202   webrtc::SessionDescriptionInterface* CreateNativeSessionDescription(
203       const std::string& sdp, const std::string& type,
204       webrtc::SdpParseError* error);
205
206   // Virtual to allow mocks to override.
207   virtual scoped_refptr<base::SingleThreadTaskRunner> signaling_thread() const;
208
209   void RunSynchronousClosureOnSignalingThread(const base::Closure& closure,
210                                               const char* trace_event_name);
211
212   base::ThreadChecker thread_checker_;
213
214   // |client_| is a weak pointer, and is valid until stop() has returned.
215   blink::WebRTCPeerConnectionHandlerClient* client_;
216
217   // |dependency_factory_| is a raw pointer, and is valid for the lifetime of
218   // RenderThreadImpl.
219   PeerConnectionDependencyFactory* const dependency_factory_;
220
221   blink::WebFrame* frame_;
222
223   ScopedVector<WebRtcMediaStreamAdapter> local_streams_;
224
225   base::WeakPtr<PeerConnectionTracker> peer_connection_tracker_;
226
227   MediaStreamTrackMetrics track_metrics_;
228
229   // Counter for a UMA stat reported at destruction time.
230   int num_data_channels_created_;
231
232   // Counter for number of IPv4 and IPv6 local candidates.
233   int num_local_candidates_ipv4_;
234   int num_local_candidates_ipv6_;
235
236   // To make sure the observer is released after the native_peer_connection_,
237   // it has to come first.
238   scoped_refptr<Observer> peer_connection_observer_;
239
240   // |native_peer_connection_| is the libjingle native PeerConnection object.
241   scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection_;
242
243   typedef std::map<webrtc::MediaStreamInterface*,
244       content::RemoteMediaStreamImpl*> RemoteStreamMap;
245   RemoteStreamMap remote_streams_;
246   scoped_refptr<webrtc::UMAObserver> uma_observer_;
247   base::TimeTicks ice_connection_checking_start_;
248   base::WeakPtrFactory<RTCPeerConnectionHandler> weak_factory_;
249
250   DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler);
251 };
252
253 }  // namespace content
254
255 #endif  // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_