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