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::WebString& username) { | 34 const WebKit::WebString& username) { |
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 |
92 void PeerConnectionHandler::stop() { | 76 void PeerConnectionHandler::stop() { |
93 // TODO(ronghuawu): There's an issue with signaling messages being sent during | 77 // TODO(ronghuawu): There's an issue with signaling messages being sent during |
94 // close. We need to investigate further. Not calling Close() on native | 78 // close. We need to investigate further. Not calling Close() on native |
95 // PeerConnection is OK for now. | 79 // PeerConnection is OK for now. |
96 native_peer_connection_ = NULL; | 80 native_peer_connection_ = NULL; |
97 media_stream_impl_->ClosePeerConnection(); | 81 media_stream_impl_->ClosePeerConnection(this); |
98 } | 82 } |
99 | 83 |
100 void PeerConnectionHandler::OnError() { | 84 void PeerConnectionHandler::OnError() { |
101 // TODO(grunell): Implement. | 85 // TODO(grunell): Implement. |
102 NOTIMPLEMENTED(); | 86 NOTIMPLEMENTED(); |
103 } | 87 } |
104 | 88 |
105 void PeerConnectionHandler::OnMessage(const std::string& msg) { | 89 void PeerConnectionHandler::OnMessage(const std::string& msg) { |
106 // TODO(grunell): Implement. | 90 // TODO(grunell): Implement. |
107 NOTIMPLEMENTED(); | 91 NOTIMPLEMENTED(); |
(...skipping 39 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 talk_base::scoped_refptr<webrtc::StreamCollectionInterface> native_streams = | |
195 native_peer_connection_->local_streams(); | |
196 // TODO(perkj): Change libJingle PeerConnection::RemoveStream API to take a | |
197 // label as input instead of stream and return bool. | |
198 for (size_t i = 0; i < streams.size() && native_streams != NULL; ++i) { | |
199 webrtc::LocalMediaStreamInterface* stream = | |
200 static_cast<webrtc::LocalMediaStreamInterface*>(native_streams->find( | |
201 UTF16ToUTF8(streams[i].label()))); | |
202 DCHECK(stream); | |
203 native_peer_connection_->RemoveStream(stream); | |
204 } | |
205 } | 148 } |
206 | 149 |
207 void PeerConnectionHandler::OnAddStreamCallback( | 150 void PeerConnectionHandler::OnAddStreamCallback( |
208 webrtc::MediaStreamInterface* stream) { | 151 webrtc::MediaStreamInterface* stream) { |
209 DCHECK(remote_streams_.find(stream) == remote_streams_.end()); | 152 DCHECK(remote_streams_.find(stream) == remote_streams_.end()); |
210 WebKit::WebMediaStreamDescriptor descriptor = | 153 WebKit::WebMediaStreamDescriptor descriptor = |
211 CreateWebKitStreamDescriptor(stream); | 154 CreateWebKitStreamDescriptor(stream); |
212 remote_streams_.insert( | 155 remote_streams_.insert( |
213 std::pair<webrtc::MediaStreamInterface*, | 156 std::pair<webrtc::MediaStreamInterface*, |
214 WebKit::WebMediaStreamDescriptor>(stream, descriptor)); | 157 WebKit::WebMediaStreamDescriptor>(stream, descriptor)); |
215 client_->didAddRemoteStream(descriptor); | 158 client_->didAddRemoteStream(descriptor); |
216 } | 159 } |
217 | 160 |
218 void PeerConnectionHandler::OnRemoveStreamCallback( | 161 void PeerConnectionHandler::OnRemoveStreamCallback( |
219 webrtc::MediaStreamInterface* stream) { | 162 webrtc::MediaStreamInterface* stream) { |
220 RemoteStreamMap::iterator it = remote_streams_.find(stream); | 163 RemoteStreamMap::iterator it = remote_streams_.find(stream); |
221 if (it == remote_streams_.end()) { | 164 if (it == remote_streams_.end()) { |
222 NOTREACHED() << "Stream not found"; | 165 NOTREACHED() << "Stream not found"; |
223 return; | 166 return; |
224 } | 167 } |
225 WebKit::WebMediaStreamDescriptor descriptor = it->second; | 168 WebKit::WebMediaStreamDescriptor descriptor = it->second; |
226 DCHECK(!descriptor.isNull()); | 169 DCHECK(!descriptor.isNull()); |
227 remote_streams_.erase(it); | 170 remote_streams_.erase(it); |
228 client_->didRemoveRemoteStream(descriptor); | 171 client_->didRemoveRemoteStream(descriptor); |
229 } | 172 } |
230 | |
231 WebKit::WebMediaStreamDescriptor | |
232 PeerConnectionHandler::CreateWebKitStreamDescriptor( | |
233 webrtc::MediaStreamInterface* stream) { | |
234 webrtc::AudioTracks* audio_tracks = stream->audio_tracks(); | |
235 webrtc::VideoTracks* video_tracks = stream->video_tracks(); | |
236 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( | |
237 audio_tracks->count() + video_tracks->count()); | |
238 | |
239 // Add audio tracks. | |
240 size_t i = 0; | |
241 for (; i < audio_tracks->count(); ++i) { | |
242 webrtc::AudioTrackInterface* audio_track = audio_tracks->at(i); | |
243 DCHECK(audio_track); | |
244 source_vector[i].initialize( | |
245 // TODO(grunell): Set id to something unique. | |
246 UTF8ToUTF16(audio_track->label()), | |
247 WebKit::WebMediaStreamSource::TypeAudio, | |
248 UTF8ToUTF16(audio_track->label())); | |
249 } | |
250 | |
251 // Add video tracks. | |
252 for (i = 0; i < video_tracks->count(); ++i) { | |
253 webrtc::VideoTrackInterface* video_track = video_tracks->at(i); | |
254 DCHECK(video_track); | |
255 source_vector[audio_tracks->count() + i].initialize( | |
256 // TODO(grunell): Set id to something unique. | |
257 UTF8ToUTF16(video_track->label()), | |
258 WebKit::WebMediaStreamSource::TypeVideo, | |
259 UTF8ToUTF16(video_track->label())); | |
260 } | |
261 | |
262 WebKit::WebMediaStreamDescriptor descriptor; | |
263 descriptor.initialize(UTF8ToUTF16(stream->label()), source_vector); | |
264 | |
265 return descriptor; | |
266 } | |
OLD | NEW |