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 #include "content/renderer/media/peer_connection_handler.h" | 5 #include "content/renderer/media/peer_connection_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "content/renderer/media/media_stream_dependency_factory.h" | 14 #include "content/renderer/media/media_stream_dependency_factory.h" |
15 #include "content/renderer/media/media_stream_impl.h" | 15 #include "content/renderer/media/media_stream_impl.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSource.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSource.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne
ctionHandlerClient.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne
ctionHandlerClient.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
20 | 20 |
21 PeerConnectionHandler::PeerConnectionHandler( | 21 PeerConnectionHandler::PeerConnectionHandler( |
22 WebKit::WebPeerConnectionHandlerClient* client, | 22 WebKit::WebPeerConnectionHandlerClient* client, |
23 MediaStreamImpl* msi, | 23 MediaStreamImpl* msi, |
24 MediaStreamDependencyFactory* dependency_factory) | 24 MediaStreamDependencyFactory* dependency_factory) |
25 : client_(client), | 25 : PeerConnectionHandlerBase(msi, dependency_factory), |
26 media_stream_impl_(msi), | 26 client_(client) { |
27 dependency_factory_(dependency_factory), | |
28 message_loop_proxy_(base::MessageLoopProxy::current()) { | |
29 } | 27 } |
30 | 28 |
31 PeerConnectionHandler::~PeerConnectionHandler() { | 29 PeerConnectionHandler::~PeerConnectionHandler() { |
32 } | 30 } |
33 | 31 |
34 void PeerConnectionHandler::SetVideoRenderer( | |
35 const std::string& stream_label, | |
36 webrtc::VideoRendererWrapperInterface* renderer) { | |
37 webrtc::MediaStreamInterface* stream = | |
38 native_peer_connection_->remote_streams()->find(stream_label); | |
39 webrtc::VideoTracks* video_tracks = stream->video_tracks(); | |
40 // We assume there is only one enabled video track. | |
41 for(size_t i = 0; i < video_tracks->count(); ++i) { | |
42 webrtc::VideoTrackInterface* video_track = video_tracks->at(i); | |
43 if (video_track->enabled()) { | |
44 video_track->SetRenderer(renderer); | |
45 return; | |
46 } | |
47 } | |
48 DVLOG(1) << "No enabled video track."; | |
49 } | |
50 | |
51 void PeerConnectionHandler::initialize( | 32 void PeerConnectionHandler::initialize( |
52 const WebKit::WebString& server_configuration, | 33 const WebKit::WebString& server_configuration, |
53 const WebKit::WebSecurityOrigin& security_origin) { | 34 const WebKit::WebSecurityOrigin& security_origin) { |
54 native_peer_connection_ = dependency_factory_->CreateRoapPeerConnection( | 35 native_peer_connection_ = dependency_factory_->CreateRoapPeerConnection( |
55 UTF16ToUTF8(server_configuration), | 36 UTF16ToUTF8(server_configuration), |
56 this); | 37 this); |
57 CHECK(native_peer_connection_); | 38 CHECK(native_peer_connection_); |
58 } | 39 } |
59 | 40 |
60 void PeerConnectionHandler::produceInitialOffer( | 41 void PeerConnectionHandler::produceInitialOffer( |
61 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 42 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
62 pending_add_streams) { | 43 pending_add_streams) { |
63 AddStreams(pending_add_streams); | 44 for (size_t i = 0; i < pending_add_streams.size(); ++i) |
| 45 AddStream(pending_add_streams[i]); |
64 native_peer_connection_->CommitStreamChanges(); | 46 native_peer_connection_->CommitStreamChanges(); |
65 } | 47 } |
66 | 48 |
67 void PeerConnectionHandler::handleInitialOffer(const WebKit::WebString& sdp) { | 49 void PeerConnectionHandler::handleInitialOffer(const WebKit::WebString& sdp) { |
68 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); | 50 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); |
69 } | 51 } |
70 | 52 |
71 void PeerConnectionHandler::processSDP(const WebKit::WebString& sdp) { | 53 void PeerConnectionHandler::processSDP(const WebKit::WebString& sdp) { |
72 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); | 54 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); |
73 } | 55 } |
74 | 56 |
75 void PeerConnectionHandler::processPendingStreams( | 57 void PeerConnectionHandler::processPendingStreams( |
76 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 58 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
77 pending_add_streams, | 59 pending_add_streams, |
78 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 60 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
79 pending_remove_streams) { | 61 pending_remove_streams) { |
80 AddStreams(pending_add_streams); | 62 for (size_t i = 0; i < pending_add_streams.size(); ++i) |
81 RemoveStreams(pending_remove_streams); | 63 AddStream(pending_add_streams[i]); |
| 64 for (size_t i = 0; i < pending_remove_streams.size(); ++i) |
| 65 RemoveStream(pending_remove_streams[i]); |
82 native_peer_connection_->CommitStreamChanges(); | 66 native_peer_connection_->CommitStreamChanges(); |
83 } | 67 } |
84 | 68 |
85 void PeerConnectionHandler::sendDataStreamMessage( | 69 void PeerConnectionHandler::sendDataStreamMessage( |
86 const char* data, | 70 const char* data, |
87 size_t length) { | 71 size_t length) { |
88 // TODO(grunell): Implement. | 72 // TODO(grunell): Implement. |
89 NOTIMPLEMENTED(); | 73 NOTIMPLEMENTED(); |
90 } | 74 } |
91 | 75 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 &PeerConnectionHandler::OnRemoveStreamCallback, | 131 &PeerConnectionHandler::OnRemoveStreamCallback, |
148 base::Unretained(this), | 132 base::Unretained(this), |
149 stream)); | 133 stream)); |
150 } else { | 134 } else { |
151 OnRemoveStreamCallback(stream); | 135 OnRemoveStreamCallback(stream); |
152 } | 136 } |
153 } | 137 } |
154 | 138 |
155 void PeerConnectionHandler::OnIceCandidate( | 139 void PeerConnectionHandler::OnIceCandidate( |
156 const webrtc::IceCandidateInterface* candidate) { | 140 const webrtc::IceCandidateInterface* candidate) { |
157 // TODO(grunell): Implement. | 141 // Not used by ROAP PeerConnection. |
158 NOTIMPLEMENTED(); | 142 NOTREACHED(); |
159 } | 143 } |
160 | 144 |
161 void PeerConnectionHandler::OnIceComplete() { | 145 void PeerConnectionHandler::OnIceComplete() { |
162 // TODO(grunell): Implement. | 146 // Not used by ROAP PeerConnection. |
163 NOTIMPLEMENTED(); | 147 NOTREACHED(); |
164 } | |
165 | |
166 void PeerConnectionHandler::AddStreams( | |
167 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& streams) { | |
168 for (size_t i = 0; i < streams.size(); ++i) { | |
169 talk_base::scoped_refptr<webrtc::LocalMediaStreamInterface> stream = | |
170 dependency_factory_->CreateLocalMediaStream( | |
171 UTF16ToUTF8(streams[i].label())); | |
172 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector; | |
173 streams[i].sources(source_vector); | |
174 | |
175 // Get and add all tracks. | |
176 for (size_t j = 0; j < source_vector.size(); ++j) { | |
177 webrtc::MediaStreamTrackInterface* track = | |
178 media_stream_impl_->GetLocalMediaStreamTrack( | |
179 UTF16ToUTF8(source_vector[j].id())); | |
180 DCHECK(track); | |
181 if (source_vector[j].type() == WebKit::WebMediaStreamSource::TypeVideo) { | |
182 stream->AddTrack(static_cast<webrtc::VideoTrackInterface*>(track)); | |
183 } else { | |
184 stream->AddTrack(static_cast<webrtc::AudioTrackInterface*>(track)); | |
185 } | |
186 } | |
187 | |
188 native_peer_connection_->AddStream(stream); | |
189 } | |
190 } | |
191 | |
192 void PeerConnectionHandler::RemoveStreams( | |
193 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& streams) { | |
194 for (size_t i = 0; i < streams.size(); ++i) { | |
195 webrtc::MediaStreamInterface* stream = | |
196 native_peer_connection_->remote_streams()->find( | |
197 UTF16ToUTF8(streams[i].label())); | |
198 native_peer_connection_->RemoveStream( | |
199 static_cast<webrtc::LocalMediaStreamInterface*>(stream)); | |
200 } | |
201 } | 148 } |
202 | 149 |
203 void PeerConnectionHandler::OnAddStreamCallback( | 150 void PeerConnectionHandler::OnAddStreamCallback( |
204 webrtc::MediaStreamInterface* stream) { | 151 webrtc::MediaStreamInterface* stream) { |
205 DCHECK(remote_streams_.find(stream) == remote_streams_.end()); | 152 DCHECK(remote_streams_.find(stream) == remote_streams_.end()); |
206 WebKit::WebMediaStreamDescriptor descriptor = | 153 WebKit::WebMediaStreamDescriptor descriptor = |
207 CreateWebKitStreamDescriptor(stream); | 154 CreateWebKitStreamDescriptor(stream); |
208 remote_streams_.insert( | 155 remote_streams_.insert( |
209 std::pair<webrtc::MediaStreamInterface*, | 156 std::pair<webrtc::MediaStreamInterface*, |
210 WebKit::WebMediaStreamDescriptor>(stream, descriptor)); | 157 WebKit::WebMediaStreamDescriptor>(stream, descriptor)); |
211 client_->didAddRemoteStream(descriptor); | 158 client_->didAddRemoteStream(descriptor); |
212 } | 159 } |
213 | 160 |
214 void PeerConnectionHandler::OnRemoveStreamCallback( | 161 void PeerConnectionHandler::OnRemoveStreamCallback( |
215 webrtc::MediaStreamInterface* stream) { | 162 webrtc::MediaStreamInterface* stream) { |
216 RemoteStreamMap::iterator it = remote_streams_.find(stream); | 163 RemoteStreamMap::iterator it = remote_streams_.find(stream); |
217 if (it == remote_streams_.end()) { | 164 if (it == remote_streams_.end()) { |
218 NOTREACHED() << "Stream not found"; | 165 NOTREACHED() << "Stream not found"; |
219 return; | 166 return; |
220 } | 167 } |
221 WebKit::WebMediaStreamDescriptor descriptor = it->second; | 168 WebKit::WebMediaStreamDescriptor descriptor = it->second; |
222 DCHECK(!descriptor.isNull()); | 169 DCHECK(!descriptor.isNull()); |
223 remote_streams_.erase(it); | 170 remote_streams_.erase(it); |
224 client_->didRemoveRemoteStream(descriptor); | 171 client_->didRemoveRemoteStream(descriptor); |
225 } | 172 } |
226 | |
227 WebKit::WebMediaStreamDescriptor | |
228 PeerConnectionHandler::CreateWebKitStreamDescriptor( | |
229 webrtc::MediaStreamInterface* stream) { | |
230 webrtc::AudioTracks* audio_tracks = stream->audio_tracks(); | |
231 webrtc::VideoTracks* video_tracks = stream->video_tracks(); | |
232 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( | |
233 audio_tracks->count() + video_tracks->count()); | |
234 | |
235 // Add audio tracks. | |
236 size_t i = 0; | |
237 for (; i < audio_tracks->count(); ++i) { | |
238 webrtc::AudioTrackInterface* audio_track = audio_tracks->at(i); | |
239 DCHECK(audio_track); | |
240 source_vector[i].initialize( | |
241 // TODO(grunell): Set id to something unique. | |
242 UTF8ToUTF16(audio_track->label()), | |
243 WebKit::WebMediaStreamSource::TypeAudio, | |
244 UTF8ToUTF16(audio_track->label())); | |
245 } | |
246 | |
247 // Add video tracks. | |
248 for (i = 0; i < video_tracks->count(); ++i) { | |
249 webrtc::VideoTrackInterface* video_track = video_tracks->at(i); | |
250 DCHECK(video_track); | |
251 source_vector[audio_tracks->count() + i].initialize( | |
252 // TODO(grunell): Set id to something unique. | |
253 UTF8ToUTF16(video_track->label()), | |
254 WebKit::WebMediaStreamSource::TypeVideo, | |
255 UTF8ToUTF16(video_track->label())); | |
256 } | |
257 | |
258 WebKit::WebMediaStreamDescriptor descriptor; | |
259 descriptor.initialize(UTF8ToUTF16(stream->label()), source_vector); | |
260 | |
261 return descriptor; | |
262 } | |
OLD | NEW |