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 "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" 15 16 namespace blink { 17 class WebFrame; 18 class WebRTCDataChannelHandler; 19 } 20 21 namespace content { 22 23 class PeerConnectionTracker; 24 25 // Mockable wrapper for blink::WebRTCStatsResponse 26 class CONTENT_EXPORT LocalRTCStatsResponse 27 : public NON_EXPORTED_BASE(talk_base::RefCountInterface) { 28 public: 29 explicit LocalRTCStatsResponse(const blink::WebRTCStatsResponse& impl) 30 : impl_(impl) { 31 } 32 33 virtual blink::WebRTCStatsResponse webKitStatsResponse() const; 34 virtual size_t addReport(blink::WebString type, blink::WebString id, 35 double timestamp); 36 virtual void addStatistic(size_t report, 37 blink::WebString name, blink::WebString value); 38 39 protected: 40 virtual ~LocalRTCStatsResponse() {} 41 // Constructor for creating mocks. 42 LocalRTCStatsResponse() {} 43 44 private: 45 blink::WebRTCStatsResponse impl_; 46 }; 47 48 // Mockable wrapper for blink::WebRTCStatsRequest 49 class CONTENT_EXPORT LocalRTCStatsRequest 50 : public NON_EXPORTED_BASE(talk_base::RefCountInterface) { 51 public: 52 explicit LocalRTCStatsRequest(blink::WebRTCStatsRequest impl); 53 // Constructor for testing. 54 LocalRTCStatsRequest(); 55 56 virtual bool hasSelector() const; 57 virtual blink::WebMediaStreamTrack component() const; 58 virtual void requestSucceeded(const LocalRTCStatsResponse* response); 59 virtual scoped_refptr<LocalRTCStatsResponse> createResponse(); 60 61 protected: 62 virtual ~LocalRTCStatsRequest(); 63 64 private: 65 blink::WebRTCStatsRequest impl_; 66 talk_base::scoped_refptr<LocalRTCStatsResponse> response_; 67 }; 68 69 // RTCPeerConnectionHandler is a delegate for the RTC PeerConnection API 70 // messages going between WebKit and native PeerConnection in libjingle. It's 71 // owned by WebKit. 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) { 78 public: 79 RTCPeerConnectionHandler( 80 blink::WebRTCPeerConnectionHandlerClient* client, 81 MediaStreamDependencyFactory* dependency_factory); 82 virtual ~RTCPeerConnectionHandler(); 83 84 void associateWithFrame(blink::WebFrame* frame); 85 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); 91 92 // blink::WebRTCPeerConnectionHandler implementation 93 virtual bool initialize( 94 const blink::WebRTCConfiguration& server_configuration, 95 const blink::WebMediaConstraints& options) OVERRIDE; 96 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; 103 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; 110 111 virtual blink::WebRTCSessionDescription localDescription() 112 OVERRIDE; 113 virtual blink::WebRTCSessionDescription remoteDescription() 114 OVERRIDE; 115 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, 125 bool result); 126 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; 140 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; 154 155 virtual void OnDataChannel( 156 webrtc::DataChannelInterface* data_channel) OVERRIDE; 157 virtual void OnRenegotiationNeeded() OVERRIDE; 158 159 // Delegate functions to allow for mocking of WebKit interfaces. 160 // getStats takes ownership of request parameter. 161 virtual void getStats(LocalRTCStatsRequest* request); 162 163 // Calls GetStats on |native_peer_connection_|. 164 void GetStats(webrtc::StatsObserver* observer, 165 webrtc::MediaStreamTrackInterface* track); 166 167 PeerConnectionTracker* peer_connection_tracker(); 168 169 private: 170 webrtc::SessionDescriptionInterface* CreateNativeSessionDescription( 171 const blink::WebRTCSessionDescription& description, 172 webrtc::SdpParseError* error); 173 174 // |client_| is a weak pointer, and is valid until stop() has returned. 175 blink::WebRTCPeerConnectionHandlerClient* client_; 176 177 blink::WebFrame* frame_; 178 179 PeerConnectionTracker* peer_connection_tracker_; 180 181 DISALLOW_COPY_AND_ASSIGN(RTCPeerConnectionHandler); 182 }; 183 184 } // namespace content 185 186 #endif // CONTENT_RENDERER_MEDIA_RTC_PEER_CONNECTION_HANDLER_H_ 187