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 #include "content/renderer/media/peer_connection_handler_base.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "content/renderer/media/media_stream_dependency_factory.h" |
| 10 #include "content/renderer/media/media_stream_impl.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSource.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 14 |
| 15 PeerConnectionHandlerBase::PeerConnectionHandlerBase( |
| 16 MediaStreamImpl* msi, |
| 17 MediaStreamDependencyFactory* dependency_factory) |
| 18 : media_stream_impl_(msi), |
| 19 dependency_factory_(dependency_factory), |
| 20 message_loop_proxy_(base::MessageLoopProxy::current()) { |
| 21 } |
| 22 |
| 23 PeerConnectionHandlerBase::~PeerConnectionHandlerBase() { |
| 24 } |
| 25 |
| 26 void PeerConnectionHandlerBase::SetVideoRenderer( |
| 27 const std::string& stream_label, |
| 28 webrtc::VideoRendererWrapperInterface* renderer) { |
| 29 webrtc::MediaStreamInterface* stream = |
| 30 native_peer_connection_->remote_streams()->find(stream_label); |
| 31 webrtc::VideoTracks* video_tracks = stream->video_tracks(); |
| 32 // We assume there is only one enabled video track. |
| 33 for (size_t i = 0; i < video_tracks->count(); ++i) { |
| 34 webrtc::VideoTrackInterface* video_track = video_tracks->at(i); |
| 35 if (video_track->enabled()) { |
| 36 video_track->SetRenderer(renderer); |
| 37 return; |
| 38 } |
| 39 } |
| 40 DVLOG(1) << "No enabled video track."; |
| 41 } |
| 42 |
| 43 void PeerConnectionHandlerBase::AddStream( |
| 44 const WebKit::WebMediaStreamDescriptor& stream) { |
| 45 talk_base::scoped_refptr<webrtc::LocalMediaStreamInterface> native_stream = |
| 46 dependency_factory_->CreateLocalMediaStream(UTF16ToUTF8(stream.label())); |
| 47 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector; |
| 48 stream.sources(source_vector); |
| 49 |
| 50 // Get and add all tracks. |
| 51 for (size_t i = 0; i < source_vector.size(); ++i) { |
| 52 webrtc::MediaStreamTrackInterface* track = media_stream_impl_ |
| 53 ->GetLocalMediaStreamTrack(UTF16ToUTF8(source_vector[i].id())); |
| 54 DCHECK(track); |
| 55 if (source_vector[i].type() == WebKit::WebMediaStreamSource::TypeVideo) { |
| 56 native_stream->AddTrack(static_cast<webrtc::VideoTrackInterface*>(track)); |
| 57 } else { |
| 58 native_stream->AddTrack(static_cast<webrtc::AudioTrackInterface*>(track)); |
| 59 } |
| 60 } |
| 61 |
| 62 native_peer_connection_->AddStream(native_stream); |
| 63 } |
| 64 |
| 65 void PeerConnectionHandlerBase::RemoveStream( |
| 66 const WebKit::WebMediaStreamDescriptor& stream) { |
| 67 webrtc::MediaStreamInterface* native_stream = |
| 68 native_peer_connection_->remote_streams()->find( |
| 69 UTF16ToUTF8(stream.label())); |
| 70 native_peer_connection_->RemoveStream( |
| 71 static_cast<webrtc::LocalMediaStreamInterface*>(native_stream)); |
| 72 } |
| 73 |
| 74 WebKit::WebMediaStreamDescriptor |
| 75 PeerConnectionHandlerBase::CreateWebKitStreamDescriptor( |
| 76 webrtc::MediaStreamInterface* stream) { |
| 77 webrtc::AudioTracks* audio_tracks = stream->audio_tracks(); |
| 78 webrtc::VideoTracks* video_tracks = stream->video_tracks(); |
| 79 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( |
| 80 audio_tracks->count() + video_tracks->count()); |
| 81 |
| 82 // Add audio tracks. |
| 83 size_t i = 0; |
| 84 for (; i < audio_tracks->count(); ++i) { |
| 85 webrtc::AudioTrackInterface* audio_track = audio_tracks->at(i); |
| 86 DCHECK(audio_track); |
| 87 source_vector[i].initialize( |
| 88 // TODO(grunell): Set id to something unique. |
| 89 UTF8ToUTF16(audio_track->label()), |
| 90 WebKit::WebMediaStreamSource::TypeAudio, |
| 91 UTF8ToUTF16(audio_track->label())); |
| 92 } |
| 93 |
| 94 // Add video tracks. |
| 95 for (i = 0; i < video_tracks->count(); ++i) { |
| 96 webrtc::VideoTrackInterface* video_track = video_tracks->at(i); |
| 97 DCHECK(video_track); |
| 98 source_vector[audio_tracks->count() + i].initialize( |
| 99 // TODO(grunell): Set id to something unique. |
| 100 UTF8ToUTF16(video_track->label()), |
| 101 WebKit::WebMediaStreamSource::TypeVideo, |
| 102 UTF8ToUTF16(video_track->label())); |
| 103 } |
| 104 |
| 105 WebKit::WebMediaStreamDescriptor descriptor; |
| 106 descriptor.initialize(UTF8ToUTF16(stream->label()), source_vector); |
| 107 |
| 108 return descriptor; |
| 109 } |
OLD | NEW |