| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_RENDERER_MEDIA_PEER_CONNECTION_HANDLER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_PEER_CONNECTION_HANDLER_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_PEER_CONNECTION_HANDLER_H_ | 6 #define CONTENT_RENDERER_MEDIA_PEER_CONNECTION_HANDLER_H_ |
| 7 | 7 |
| 8 #include <map> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 12 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
| 15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 16 #include "third_party/libjingle/source/talk/app/webrtcv1/peerconnection.h" | 17 #include "third_party/libjingle/source/talk/app/webrtc/mediastream.h" |
| 17 #include "third_party/libjingle/source/talk/base/socketaddress.h" | 18 #include "third_party/libjingle/source/talk/app/webrtc/peerconnection.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne
ctionHandler.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne
ctionHandler.h" |
| 19 | 20 |
| 20 namespace base { | |
| 21 class Thread; | |
| 22 class WaitableEvent; | |
| 23 } | |
| 24 | |
| 25 namespace content { | |
| 26 class IpcNetworkManager; | |
| 27 class IpcPacketSocketFactory; | |
| 28 class P2PSocketDispatcher; | |
| 29 } | |
| 30 | |
| 31 namespace cricket { | |
| 32 class PortAllocator; | |
| 33 } | |
| 34 | |
| 35 namespace talk_base { | |
| 36 class Thread; | |
| 37 } | |
| 38 | |
| 39 class MediaStreamDependencyFactory; | 21 class MediaStreamDependencyFactory; |
| 40 class MediaStreamImpl; | 22 class MediaStreamImpl; |
| 41 | 23 |
| 42 // PeerConnectionHandler is a delegate for the PeerConnection API messages going | 24 // PeerConnectionHandler is a delegate for the PeerConnection API messages going |
| 43 // between WebKit and native PeerConnection in libjingle. It's owned by | 25 // between WebKit and native PeerConnection in libjingle. It's owned by |
| 44 // MediaStreamImpl. | 26 // WebKit. |
| 45 class CONTENT_EXPORT PeerConnectionHandler | 27 class CONTENT_EXPORT PeerConnectionHandler |
| 46 : NON_EXPORTED_BASE(public WebKit::WebPeerConnectionHandler), | 28 : NON_EXPORTED_BASE(public WebKit::WebPeerConnectionHandler), |
| 47 NON_EXPORTED_BASE(public webrtc::PeerConnectionObserver) { | 29 NON_EXPORTED_BASE(public webrtc::PeerConnectionObserver) { |
| 48 public: | 30 public: |
| 49 PeerConnectionHandler( | 31 PeerConnectionHandler( |
| 50 WebKit::WebPeerConnectionHandlerClient* client, | 32 WebKit::WebPeerConnectionHandlerClient* client, |
| 51 MediaStreamImpl* msi, | 33 MediaStreamImpl* msi, |
| 52 MediaStreamDependencyFactory* dependency_factory, | 34 MediaStreamDependencyFactory* dependency_factory); |
| 53 talk_base::Thread* signaling_thread, | |
| 54 content::P2PSocketDispatcher* socket_dispatcher, | |
| 55 content::IpcNetworkManager* network_manager, | |
| 56 content::IpcPacketSocketFactory* socket_factory); | |
| 57 virtual ~PeerConnectionHandler(); | 35 virtual ~PeerConnectionHandler(); |
| 58 | 36 |
| 59 // Set the video renderer for the specified stream. | 37 // Set the video renderer for the specified stream. |
| 60 virtual bool SetVideoRenderer(const std::string& stream_label, | 38 virtual void SetVideoRenderer( |
| 61 cricket::VideoRenderer* renderer); | 39 const std::string& stream_label, |
| 40 webrtc::VideoRendererWrapperInterface* renderer); |
| 62 | 41 |
| 63 // WebKit::WebPeerConnectionHandler implementation | 42 // WebKit::WebPeerConnectionHandler implementation |
| 64 virtual void initialize( | 43 virtual void initialize( |
| 65 const WebKit::WebString& server_configuration, | 44 const WebKit::WebString& server_configuration, |
| 66 const WebKit::WebSecurityOrigin& security_origin) OVERRIDE; | 45 const WebKit::WebSecurityOrigin& security_origin) OVERRIDE; |
| 67 virtual void produceInitialOffer( | 46 virtual void produceInitialOffer( |
| 68 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 47 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
| 69 pending_add_streams) OVERRIDE; | 48 pending_add_streams) OVERRIDE; |
| 70 virtual void handleInitialOffer(const WebKit::WebString& sdp) OVERRIDE; | 49 virtual void handleInitialOffer(const WebKit::WebString& sdp) OVERRIDE; |
| 71 virtual void processSDP(const WebKit::WebString& sdp) OVERRIDE; | 50 virtual void processSDP(const WebKit::WebString& sdp) OVERRIDE; |
| 72 virtual void processPendingStreams( | 51 virtual void processPendingStreams( |
| 73 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 52 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
| 74 pending_add_streams, | 53 pending_add_streams, |
| 75 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 54 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
| 76 pending_remove_streams) OVERRIDE; | 55 pending_remove_streams) OVERRIDE; |
| 77 virtual void sendDataStreamMessage(const char* data, size_t length) OVERRIDE; | 56 virtual void sendDataStreamMessage(const char* data, size_t length) OVERRIDE; |
| 57 // We will be deleted by WebKit after stop has been returned. |
| 78 virtual void stop() OVERRIDE; | 58 virtual void stop() OVERRIDE; |
| 79 | 59 |
| 80 // webrtc::PeerConnectionObserver implementation | 60 // webrtc::PeerConnectionObserver implementation |
| 61 virtual void OnError() OVERRIDE; |
| 62 virtual void OnMessage(const std::string& msg) OVERRIDE; |
| 81 virtual void OnSignalingMessage(const std::string& msg) OVERRIDE; | 63 virtual void OnSignalingMessage(const std::string& msg) OVERRIDE; |
| 82 virtual void OnAddStream(const std::string& stream_id, bool video) OVERRIDE; | 64 virtual void OnStateChange(StateType state_changed) OVERRIDE; |
| 83 virtual void OnRemoveStream( | 65 virtual void OnAddStream(webrtc::MediaStreamInterface* stream) OVERRIDE; |
| 84 const std::string& stream_id, | 66 virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream) OVERRIDE; |
| 85 bool video) OVERRIDE; | |
| 86 | 67 |
| 87 private: | 68 private: |
| 88 FRIEND_TEST_ALL_PREFIXES(PeerConnectionHandlerTest, Basic); | 69 FRIEND_TEST_ALL_PREFIXES(PeerConnectionHandlerTest, Basic); |
| 89 | 70 |
| 90 void AddStream(const std::string label); | 71 void AddStreams( |
| 91 void OnAddStreamCallback(const std::string& stream_label); | 72 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& streams); |
| 92 void OnRemoveStreamCallback(const std::string& stream_label); | 73 void RemoveStreams( |
| 74 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& streams); |
| 75 void OnAddStreamCallback(webrtc::MediaStreamInterface* stream); |
| 76 void OnRemoveStreamCallback(webrtc::MediaStreamInterface* stream); |
| 77 WebKit::WebMediaStreamDescriptor CreateWebKitStreamDescriptor( |
| 78 webrtc::MediaStreamInterface* stream); |
| 93 | 79 |
| 94 // client_ is a weak pointer, and is valid until stop() has returned. | 80 // client_ is a weak pointer, and is valid until stop() has returned. |
| 95 WebKit::WebPeerConnectionHandlerClient* client_; | 81 WebKit::WebPeerConnectionHandlerClient* client_; |
| 96 | 82 |
| 97 // media_stream_impl_ is a weak pointer, and is valid for the lifetime of this | 83 // media_stream_impl_ is a weak pointer, and is valid for the lifetime of this |
| 98 // class. Calls to it must be done on the render thread. | 84 // class. Calls to it must be done on the render thread. |
| 99 MediaStreamImpl* media_stream_impl_; | 85 MediaStreamImpl* media_stream_impl_; |
| 100 | 86 |
| 101 // dependency_factory_ is a weak pointer, and is valid for the lifetime of | 87 // dependency_factory_ is a weak pointer, and is valid for the lifetime of |
| 102 // MediaStreamImpl. | 88 // MediaStreamImpl. |
| 103 MediaStreamDependencyFactory* dependency_factory_; | 89 MediaStreamDependencyFactory* dependency_factory_; |
| 104 | 90 |
| 105 // native_peer_connection_ is the native PeerConnection object, | 91 // native_peer_connection_ is the native PeerConnection object, |
| 106 // it handles the ICE processing and media engine. | 92 // it handles the ICE processing and media engine. |
| 107 scoped_ptr<webrtc::PeerConnection> native_peer_connection_; | 93 talk_base::scoped_refptr<webrtc::PeerConnectionInterface> |
| 94 native_peer_connection_; |
| 95 |
| 96 typedef std::map<webrtc::MediaStreamInterface*, |
| 97 WebKit::WebMediaStreamDescriptor> RemoteStreamMap; |
| 98 RemoteStreamMap remote_streams_; |
| 108 | 99 |
| 109 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 100 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 110 | 101 |
| 111 talk_base::Thread* signaling_thread_; | |
| 112 | |
| 113 // socket_dispatcher_ is a weak reference, owned by RenderView. It's valid | |
| 114 // for the lifetime of RenderView. | |
| 115 content::P2PSocketDispatcher* socket_dispatcher_; | |
| 116 | |
| 117 // network_manager_ and socket_factory_ are a weak references, owned by | |
| 118 // MediaStreamImpl. | |
| 119 content::IpcNetworkManager* network_manager_; | |
| 120 content::IpcPacketSocketFactory* socket_factory_; | |
| 121 | |
| 122 scoped_ptr<cricket::PortAllocator> port_allocator_; | |
| 123 | |
| 124 // Currently, a stream in WebKit has audio and/or video and has one label. | |
| 125 // Local and remote streams have different labels. | |
| 126 // In native PeerConnection, a stream has audio or video (not both), and they | |
| 127 // have separate labels. A remote stream has the same label as the | |
| 128 // corresponding local stream. Hence the workarounds in the implementation to | |
| 129 // handle this. It could look like this: | |
| 130 // WebKit: Local, audio and video: label "foo". | |
| 131 // Remote, audio and video: label "foo-remote". | |
| 132 // Native: Local and remote, audio: label "foo-audio". | |
| 133 // Local and remote, video: label "foo". | |
| 134 // TODO(grunell): This shall be removed or changed when native PeerConnection | |
| 135 // has been updated to closer follow the specification. | |
| 136 std::string local_label_; // Label used in WebKit | |
| 137 std::string remote_label_; // Label used in WebKit | |
| 138 | |
| 139 // Call states. Possible transitions: | |
| 140 // NOT_STARTED -> INITIATING -> SENDING_AND_RECEIVING | |
| 141 // NOT_STARTED -> RECEIVING | |
| 142 // RECEIVING -> NOT_STARTED | |
| 143 // RECEIVING -> SENDING_AND_RECEIVING | |
| 144 // SENDING_AND_RECEIVING -> NOT_STARTED | |
| 145 // Note that when in state SENDING_AND_RECEIVING, the other side may or may | |
| 146 // not send media. Thus, this state does not necessarily mean full duplex. | |
| 147 // TODO(grunell): This shall be removed or changed when native PeerConnection | |
| 148 // has been updated to closer follow the specification. | |
| 149 enum CallState { | |
| 150 NOT_STARTED, | |
| 151 INITIATING, | |
| 152 RECEIVING, | |
| 153 SENDING_AND_RECEIVING | |
| 154 }; | |
| 155 CallState call_state_; | |
| 156 | |
| 157 DISALLOW_COPY_AND_ASSIGN(PeerConnectionHandler); | 102 DISALLOW_COPY_AND_ASSIGN(PeerConnectionHandler); |
| 158 }; | 103 }; |
| 159 | 104 |
| 160 #endif // CONTENT_RENDERER_MEDIA_PEER_CONNECTION_HANDLER_H_ | 105 #endif // CONTENT_RENDERER_MEDIA_PEER_CONNECTION_HANDLER_H_ |
| OLD | NEW |