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 : PeerConnectionHandlerBase(msi, dependency_factory), | 25 : client_(client), |
26 client_(client) { | 26 media_stream_impl_(msi), |
| 27 dependency_factory_(dependency_factory), |
| 28 message_loop_proxy_(base::MessageLoopProxy::current()) { |
27 } | 29 } |
28 | 30 |
29 PeerConnectionHandler::~PeerConnectionHandler() { | 31 PeerConnectionHandler::~PeerConnectionHandler() { |
30 } | 32 } |
31 | 33 |
| 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 |
32 void PeerConnectionHandler::initialize( | 51 void PeerConnectionHandler::initialize( |
33 const WebKit::WebString& server_configuration, | 52 const WebKit::WebString& server_configuration, |
34 const WebKit::WebString& username) { | 53 const WebKit::WebString& username) { |
35 native_peer_connection_ = dependency_factory_->CreateRoapPeerConnection( | 54 native_peer_connection_ = dependency_factory_->CreateRoapPeerConnection( |
36 UTF16ToUTF8(server_configuration), | 55 UTF16ToUTF8(server_configuration), |
37 this); | 56 this); |
38 CHECK(native_peer_connection_); | 57 CHECK(native_peer_connection_); |
39 } | 58 } |
40 | 59 |
41 void PeerConnectionHandler::produceInitialOffer( | 60 void PeerConnectionHandler::produceInitialOffer( |
42 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 61 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
43 pending_add_streams) { | 62 pending_add_streams) { |
44 for (size_t i = 0; i < pending_add_streams.size(); ++i) | 63 AddStreams(pending_add_streams); |
45 AddStream(pending_add_streams[i]); | |
46 native_peer_connection_->CommitStreamChanges(); | 64 native_peer_connection_->CommitStreamChanges(); |
47 } | 65 } |
48 | 66 |
49 void PeerConnectionHandler::handleInitialOffer(const WebKit::WebString& sdp) { | 67 void PeerConnectionHandler::handleInitialOffer(const WebKit::WebString& sdp) { |
50 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); | 68 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); |
51 } | 69 } |
52 | 70 |
53 void PeerConnectionHandler::processSDP(const WebKit::WebString& sdp) { | 71 void PeerConnectionHandler::processSDP(const WebKit::WebString& sdp) { |
54 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); | 72 native_peer_connection_->ProcessSignalingMessage(UTF16ToUTF8(sdp)); |
55 } | 73 } |
56 | 74 |
57 void PeerConnectionHandler::processPendingStreams( | 75 void PeerConnectionHandler::processPendingStreams( |
58 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 76 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
59 pending_add_streams, | 77 pending_add_streams, |
60 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | 78 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
61 pending_remove_streams) { | 79 pending_remove_streams) { |
62 for (size_t i = 0; i < pending_add_streams.size(); ++i) | 80 AddStreams(pending_add_streams); |
63 AddStream(pending_add_streams[i]); | 81 RemoveStreams(pending_remove_streams); |
64 for (size_t i = 0; i < pending_remove_streams.size(); ++i) | |
65 RemoveStream(pending_remove_streams[i]); | |
66 native_peer_connection_->CommitStreamChanges(); | 82 native_peer_connection_->CommitStreamChanges(); |
67 } | 83 } |
68 | 84 |
69 void PeerConnectionHandler::sendDataStreamMessage( | 85 void PeerConnectionHandler::sendDataStreamMessage( |
70 const char* data, | 86 const char* data, |
71 size_t length) { | 87 size_t length) { |
72 // TODO(grunell): Implement. | 88 // TODO(grunell): Implement. |
73 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
74 } | 90 } |
75 | 91 |
76 void PeerConnectionHandler::stop() { | 92 void PeerConnectionHandler::stop() { |
77 // TODO(ronghuawu): There's an issue with signaling messages being sent during | 93 // TODO(ronghuawu): There's an issue with signaling messages being sent during |
78 // close. We need to investigate further. Not calling Close() on native | 94 // close. We need to investigate further. Not calling Close() on native |
79 // PeerConnection is OK for now. | 95 // PeerConnection is OK for now. |
80 native_peer_connection_ = NULL; | 96 native_peer_connection_ = NULL; |
81 media_stream_impl_->ClosePeerConnection(this); | 97 media_stream_impl_->ClosePeerConnection(); |
82 } | 98 } |
83 | 99 |
84 void PeerConnectionHandler::OnError() { | 100 void PeerConnectionHandler::OnError() { |
85 // TODO(grunell): Implement. | 101 // TODO(grunell): Implement. |
86 NOTIMPLEMENTED(); | 102 NOTIMPLEMENTED(); |
87 } | 103 } |
88 | 104 |
89 void PeerConnectionHandler::OnMessage(const std::string& msg) { | 105 void PeerConnectionHandler::OnMessage(const std::string& msg) { |
90 // TODO(grunell): Implement. | 106 // TODO(grunell): Implement. |
91 NOTIMPLEMENTED(); | 107 NOTIMPLEMENTED(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 &PeerConnectionHandler::OnRemoveStreamCallback, | 147 &PeerConnectionHandler::OnRemoveStreamCallback, |
132 base::Unretained(this), | 148 base::Unretained(this), |
133 stream)); | 149 stream)); |
134 } else { | 150 } else { |
135 OnRemoveStreamCallback(stream); | 151 OnRemoveStreamCallback(stream); |
136 } | 152 } |
137 } | 153 } |
138 | 154 |
139 void PeerConnectionHandler::OnIceCandidate( | 155 void PeerConnectionHandler::OnIceCandidate( |
140 const webrtc::IceCandidateInterface* candidate) { | 156 const webrtc::IceCandidateInterface* candidate) { |
141 // Not used by ROAP PeerConnection. | 157 // TODO(grunell): Implement. |
142 NOTREACHED(); | 158 NOTIMPLEMENTED(); |
143 } | 159 } |
144 | 160 |
145 void PeerConnectionHandler::OnIceComplete() { | 161 void PeerConnectionHandler::OnIceComplete() { |
146 // Not used by ROAP PeerConnection. | 162 // TODO(grunell): Implement. |
147 NOTREACHED(); | 163 NOTIMPLEMENTED(); |
| 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 } |
148 } | 205 } |
149 | 206 |
150 void PeerConnectionHandler::OnAddStreamCallback( | 207 void PeerConnectionHandler::OnAddStreamCallback( |
151 webrtc::MediaStreamInterface* stream) { | 208 webrtc::MediaStreamInterface* stream) { |
152 DCHECK(remote_streams_.find(stream) == remote_streams_.end()); | 209 DCHECK(remote_streams_.find(stream) == remote_streams_.end()); |
153 WebKit::WebMediaStreamDescriptor descriptor = | 210 WebKit::WebMediaStreamDescriptor descriptor = |
154 CreateWebKitStreamDescriptor(stream); | 211 CreateWebKitStreamDescriptor(stream); |
155 remote_streams_.insert( | 212 remote_streams_.insert( |
156 std::pair<webrtc::MediaStreamInterface*, | 213 std::pair<webrtc::MediaStreamInterface*, |
157 WebKit::WebMediaStreamDescriptor>(stream, descriptor)); | 214 WebKit::WebMediaStreamDescriptor>(stream, descriptor)); |
158 client_->didAddRemoteStream(descriptor); | 215 client_->didAddRemoteStream(descriptor); |
159 } | 216 } |
160 | 217 |
161 void PeerConnectionHandler::OnRemoveStreamCallback( | 218 void PeerConnectionHandler::OnRemoveStreamCallback( |
162 webrtc::MediaStreamInterface* stream) { | 219 webrtc::MediaStreamInterface* stream) { |
163 RemoteStreamMap::iterator it = remote_streams_.find(stream); | 220 RemoteStreamMap::iterator it = remote_streams_.find(stream); |
164 if (it == remote_streams_.end()) { | 221 if (it == remote_streams_.end()) { |
165 NOTREACHED() << "Stream not found"; | 222 NOTREACHED() << "Stream not found"; |
166 return; | 223 return; |
167 } | 224 } |
168 WebKit::WebMediaStreamDescriptor descriptor = it->second; | 225 WebKit::WebMediaStreamDescriptor descriptor = it->second; |
169 DCHECK(!descriptor.isNull()); | 226 DCHECK(!descriptor.isNull()); |
170 remote_streams_.erase(it); | 227 remote_streams_.erase(it); |
171 client_->didRemoveRemoteStream(descriptor); | 228 client_->didRemoveRemoteStream(descriptor); |
172 } | 229 } |
| 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 |