Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(846)

Side by Side Diff: content/renderer/media/peer_connection_handler.cc

Issue 8060055: Adding support for MediaStream and PeerConnection functionality (Closed) Base URL: http://git.chromium.org/chromium/chromium.git@trunk
Patch Set: Code review fixes. Created 8 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #include "content/renderer/media/peer_connection_handler.h"
6
7 #include <stdlib.h>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/string_number_conversions.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread.h"
15 #include "base/utf_string_conversions.h"
16 #include "content/renderer/media/media_stream_dependency_factory.h"
17 #include "content/renderer/media/media_stream_impl.h"
18 #include "content/renderer/p2p/ipc_network_manager.h"
19 #include "content/renderer/p2p/ipc_socket_factory.h"
20 #include "third_party/libjingle/source/talk/base/thread.h"
21 #include "third_party/libjingle/source/talk/p2p/base/portallocator.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amDescriptor.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amSource.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne ctionHandlerClient.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
26
27 PeerConnectionHandler::PeerConnectionHandler(
28 WebKit::WebPeerConnectionHandlerClient* client,
29 MediaStreamImpl* msi,
30 MediaStreamDependencyFactory* dependency_factory,
31 talk_base::Thread* signaling_thread,
32 content::P2PSocketDispatcher* socket_dispatcher,
33 content::IpcNetworkManager* network_manager,
34 content::IpcPacketSocketFactory* socket_factory)
35 : client_(client),
36 media_stream_impl_(msi),
37 dependency_factory_(dependency_factory),
38 message_loop_proxy_(base::MessageLoopProxy::current()),
39 signaling_thread_(signaling_thread),
40 socket_dispatcher_(socket_dispatcher),
41 network_manager_(network_manager),
42 socket_factory_(socket_factory),
43 call_state_(NOT_STARTED) {
44 }
45
46 PeerConnectionHandler::~PeerConnectionHandler() {
47 if (native_peer_connection_.get()) {
48 native_peer_connection_->RegisterObserver(NULL);
49 native_peer_connection_->Close();
50 }
51 }
52
53 bool PeerConnectionHandler::SetVideoRenderer(
54 const std::string& stream_label,
55 cricket::VideoRenderer* renderer) {
56 return native_peer_connection_->SetVideoRenderer(stream_label, renderer);
57 }
58
59 void PeerConnectionHandler::initialize(
60 const WebKit::WebString& server_configuration,
61 const WebKit::WebSecurityOrigin& security_origin) {
62 // We support the following server configuration format:
63 // "STUN <address>:<port>". We only support STUN at the moment.
64 // TODO(grunell): Support TURN.
65
66 // Strip "STUN ".
67 std::string strip_string = "STUN ";
68 std::string config = UTF16ToUTF8(server_configuration);
69 size_t pos = config.find(strip_string);
70 if (pos != 0) {
71 DVLOG(1) << "Invalid configuration string.";
72 return;
73 }
74 config = config.substr(strip_string.length());
75 // Parse out port.
76 pos = config.find(':');
77 if (pos == std::string::npos) {
78 DVLOG(1) << "Invalid configuration string.";
79 return;
80 }
81 int port = 0;
82 bool success = base::StringToInt(config.substr(pos+1), &port);
83 if (!success || (port == 0)) {
84 DVLOG(1) << "Invalid configuration string.";
85 return;
86 }
87 // Get address.
88 std::string address = config.substr(0, pos);
89
90 webkit_glue::P2PTransport::Config p2p_config;
91 p2p_config.stun_server = address;
92 p2p_config.stun_server_port = port;
93
94 port_allocator_.reset(dependency_factory_->CreatePortAllocator(
95 socket_dispatcher_,
96 network_manager_,
97 socket_factory_,
98 p2p_config));
99
100 native_peer_connection_.reset(dependency_factory_->CreatePeerConnection(
101 port_allocator_.get(),
102 signaling_thread_));
103 native_peer_connection_->RegisterObserver(this);
104 }
105
106 void PeerConnectionHandler::produceInitialOffer(
107 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>&
108 pending_add_streams) {
109 // We currently don't support creating an initial offer without a stream.
110 // Native PeerConnection will anyway create the initial offer when the first
111 // (and only) stream is added, so it will be done when processPendingStreams
112 // is called if not here.
113 if (pending_add_streams.isEmpty()) {
114 DVLOG(1) << "Can't produce initial offer with no stream.";
115 return;
116 }
117 // TODO(grunell): Support several streams.
118 DCHECK_EQ(pending_add_streams.size(), 1u);
119 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector;
120 pending_add_streams[0].sources(source_vector);
121 DCHECK_GT(source_vector.size(), 0u);
122 std::string label = UTF16ToUTF8(source_vector[0].id());
123 AddStream(label);
124 }
125
126 void PeerConnectionHandler::handleInitialOffer(const WebKit::WebString& sdp) {
127 native_peer_connection_->SignalingMessage(UTF16ToUTF8(sdp));
128 }
129
130 void PeerConnectionHandler::processSDP(const WebKit::WebString& sdp) {
131 native_peer_connection_->SignalingMessage(UTF16ToUTF8(sdp));
132 }
133
134 void PeerConnectionHandler::processPendingStreams(
135 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>&
136 pending_add_streams,
137 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>&
138 pending_remove_streams) {
139 // TODO(grunell): Support several streams.
140 if (!pending_add_streams.isEmpty()) {
141 DCHECK_EQ(pending_add_streams.size(), 1u);
142 AddStream(UTF16ToUTF8(pending_add_streams[0].label()));
143 }
144 // Currently we ignore remove stream, no support in native PeerConnection.
145 }
146
147 void PeerConnectionHandler::sendDataStreamMessage(
148 const char* data,
149 size_t length) {
150 // TODO(grunell): Implement. Not supported in native PeerConnection.
151 NOTIMPLEMENTED();
152 }
153
154 void PeerConnectionHandler::stop() {
155 if (native_peer_connection_.get())
156 native_peer_connection_->RegisterObserver(NULL);
157 native_peer_connection_.reset();
158 // The close function will delete us.
159 media_stream_impl_->ClosePeerConnection();
160 }
161
162 void PeerConnectionHandler::OnSignalingMessage(const std::string& msg) {
163 if (!message_loop_proxy_->BelongsToCurrentThread()) {
164 message_loop_proxy_->PostTask(FROM_HERE, base::Bind(
165 &PeerConnectionHandler::OnSignalingMessage,
166 base::Unretained(this),
167 msg));
168 return;
169 }
170 client_->didGenerateSDP(UTF8ToUTF16(msg));
171 }
172
173 void PeerConnectionHandler::OnAddStream(const std::string& stream_id,
174 bool video) {
175 if (!video)
176 return;
177
178 if (!message_loop_proxy_->BelongsToCurrentThread()) {
179 message_loop_proxy_->PostTask(FROM_HERE, base::Bind(
180 &PeerConnectionHandler::OnAddStreamCallback,
181 base::Unretained(this),
182 stream_id));
183 } else {
184 OnAddStreamCallback(stream_id);
185 }
186 }
187
188 void PeerConnectionHandler::OnRemoveStream(
189 const std::string& stream_id,
190 bool video) {
191 if (!video)
192 return;
193
194 if (!message_loop_proxy_->BelongsToCurrentThread()) {
195 message_loop_proxy_->PostTask(FROM_HERE, base::Bind(
196 &PeerConnectionHandler::OnRemoveStreamCallback,
197 base::Unretained(this),
198 remote_label_));
199 } else {
200 OnRemoveStreamCallback(remote_label_);
201 }
202 }
203
204 void PeerConnectionHandler::AddStream(const std::string label) {
205 // TODO(grunell): Fix code in this function after a new native PeerConnection
206 // version has been rolled out.
207 if (call_state_ == NOT_STARTED) {
208 // TODO(grunell): Add audio and/or video depending on what's enabled
209 // in the stream.
210 std::string audio_label = label;
211 audio_label.append("-audio");
212 native_peer_connection_->AddStream(audio_label, false); // Audio
213 native_peer_connection_->AddStream(label, true); // Video
214 call_state_ = INITIATING;
215 }
216 if (call_state_ == INITIATING || call_state_ == RECEIVING) {
217 local_label_ = label;
218 if (media_stream_impl_->SetVideoCaptureModule(label))
219 native_peer_connection_->SetVideoCapture("");
220 if (call_state_ == INITIATING)
221 native_peer_connection_->Connect();
222 else if (call_state_ == RECEIVING)
223 call_state_ = SENDING_AND_RECEIVING;
224 } else {
225 DLOG(ERROR) << "Multiple streams not supported";
226 }
227 }
228
229 void PeerConnectionHandler::OnAddStreamCallback(
230 const std::string& stream_label) {
231 // TODO(grunell): Fix code in this function after a new native PeerConnection
232 // version has been rolled out.
233 if (call_state_ == NOT_STARTED) {
234 remote_label_ = stream_label;
235 call_state_ = RECEIVING;
236 } else if (call_state_ == INITIATING) {
237 remote_label_ = local_label_;
238 remote_label_ += "-remote";
239 call_state_ = SENDING_AND_RECEIVING;
240 }
241
242 // TODO(grunell): Support several tracks.
243 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector(
244 static_cast<size_t>(2));
245 source_vector[0].initialize(WebKit::WebString::fromUTF8(remote_label_),
246 WebKit::WebMediaStreamSource::TypeVideo,
247 WebKit::WebString::fromUTF8("RemoteVideo"));
248 source_vector[1].initialize(WebKit::WebString::fromUTF8(remote_label_),
249 WebKit::WebMediaStreamSource::TypeAudio,
250 WebKit::WebString::fromUTF8("RemoteAudio"));
251 WebKit::WebMediaStreamDescriptor descriptor;
252 descriptor.initialize(UTF8ToUTF16(remote_label_), source_vector);
253 client_->didAddRemoteStream(descriptor);
254 }
255
256 void PeerConnectionHandler::OnRemoveStreamCallback(
257 const std::string& stream_label) {
258 // TODO(grunell): Support several tracks.
259 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector(
260 static_cast<size_t>(2));
261 source_vector[0].initialize(WebKit::WebString::fromUTF8(stream_label),
262 WebKit::WebMediaStreamSource::TypeVideo,
263 WebKit::WebString::fromUTF8("RemoteVideo"));
264 source_vector[1].initialize(WebKit::WebString::fromUTF8(stream_label),
265 WebKit::WebMediaStreamSource::TypeAudio,
266 WebKit::WebString::fromUTF8("RemoteAudio"));
267 WebKit::WebMediaStreamDescriptor descriptor;
268 descriptor.initialize(UTF8ToUTF16(stream_label), source_vector);
269 client_->didRemoveRemoteStream(descriptor);
270 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698