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