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_base.h" | 5 #include "content/renderer/media/peer_connection_handler_base.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "content/renderer/media/media_stream_dependency_factory.h" | 9 #include "content/renderer/media/media_stream_dependency_factory.h" |
10 #include "content/renderer/media/media_stream_impl.h" | 10 #include "content/renderer/media/media_stream_extra_data.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.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" | 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" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
14 | 14 |
| 15 static webrtc::LocalMediaStreamInterface* GetLocalNativeMediaStream( |
| 16 const WebKit::WebMediaStreamDescriptor& stream) { |
| 17 MediaStreamExtraData* extra_data = |
| 18 static_cast<MediaStreamExtraData*>(stream.extraData()); |
| 19 if (extra_data) |
| 20 return extra_data->local_stream(); |
| 21 return NULL; |
| 22 } |
| 23 |
15 PeerConnectionHandlerBase::PeerConnectionHandlerBase( | 24 PeerConnectionHandlerBase::PeerConnectionHandlerBase( |
16 MediaStreamImpl* msi, | |
17 MediaStreamDependencyFactory* dependency_factory) | 25 MediaStreamDependencyFactory* dependency_factory) |
18 : media_stream_impl_(msi), | 26 : dependency_factory_(dependency_factory), |
19 dependency_factory_(dependency_factory), | |
20 message_loop_proxy_(base::MessageLoopProxy::current()) { | 27 message_loop_proxy_(base::MessageLoopProxy::current()) { |
21 } | 28 } |
22 | 29 |
23 PeerConnectionHandlerBase::~PeerConnectionHandlerBase() { | 30 PeerConnectionHandlerBase::~PeerConnectionHandlerBase() { |
24 } | 31 } |
25 | 32 |
26 webrtc::MediaStreamInterface* PeerConnectionHandlerBase::GetRemoteMediaStream( | |
27 const WebKit::WebMediaStreamDescriptor& stream) { | |
28 for (RemoteStreamMap::const_iterator it = remote_streams_.begin(); | |
29 it != remote_streams_.end(); ++it) { | |
30 if (it->second.label() == stream.label()) | |
31 return it->first; | |
32 } | |
33 return NULL; | |
34 } | |
35 | |
36 void PeerConnectionHandlerBase::SetRemoteVideoRenderer( | |
37 const std::string& source_id, | |
38 webrtc::VideoRendererWrapperInterface* renderer) { | |
39 webrtc::VideoTrackInterface* remote_track = FindRemoteVideoTrack(source_id); | |
40 if (remote_track) { | |
41 remote_track->SetRenderer(renderer); | |
42 // TODO(perkj): Remove video_renderers_ from this class when it's not longer | |
43 // needed. See http://code.google.com/p/webrtc/issues/detail?id=447 | |
44 video_renderers_.erase(source_id); // Remove old renderer if exists. | |
45 video_renderers_.insert(std::make_pair(source_id, renderer)); | |
46 } else { | |
47 NOTREACHED(); | |
48 } | |
49 } | |
50 | |
51 void PeerConnectionHandlerBase::AddStream( | 33 void PeerConnectionHandlerBase::AddStream( |
52 const WebKit::WebMediaStreamDescriptor& stream) { | 34 const WebKit::WebMediaStreamDescriptor& stream) { |
53 webrtc::LocalMediaStreamInterface* native_stream = | 35 webrtc::LocalMediaStreamInterface* native_stream = |
54 media_stream_impl_->GetLocalMediaStream(stream); | 36 GetLocalNativeMediaStream(stream); |
55 if (native_stream) | 37 if (native_stream) |
56 native_peer_connection_->AddStream(native_stream); | 38 native_peer_connection_->AddStream(native_stream); |
57 DCHECK(native_stream); | 39 DCHECK(native_stream); |
58 } | 40 } |
59 | 41 |
60 void PeerConnectionHandlerBase::RemoveStream( | 42 void PeerConnectionHandlerBase::RemoveStream( |
61 const WebKit::WebMediaStreamDescriptor& stream) { | 43 const WebKit::WebMediaStreamDescriptor& stream) { |
62 webrtc::LocalMediaStreamInterface* native_stream = | 44 webrtc::LocalMediaStreamInterface* native_stream = |
63 media_stream_impl_->GetLocalMediaStream(stream); | 45 GetLocalNativeMediaStream(stream); |
64 if (native_stream) | 46 if (native_stream) |
65 native_peer_connection_->RemoveStream(native_stream); | 47 native_peer_connection_->RemoveStream(native_stream); |
66 DCHECK(native_stream); | 48 DCHECK(native_stream); |
67 } | 49 } |
68 | 50 |
69 WebKit::WebMediaStreamDescriptor | 51 WebKit::WebMediaStreamDescriptor |
70 PeerConnectionHandlerBase::CreateWebKitStreamDescriptor( | 52 PeerConnectionHandlerBase::CreateWebKitStreamDescriptor( |
71 webrtc::MediaStreamInterface* stream) { | 53 webrtc::MediaStreamInterface* stream) { |
72 webrtc::AudioTracks* audio_tracks = stream->audio_tracks(); | 54 webrtc::AudioTracks* audio_tracks = stream->audio_tracks(); |
73 webrtc::VideoTracks* video_tracks = stream->video_tracks(); | 55 webrtc::VideoTracks* video_tracks = stream->video_tracks(); |
74 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( | 56 WebKit::WebVector<WebKit::WebMediaStreamSource> audio_source_vector( |
75 audio_tracks->count() + video_tracks->count()); | 57 audio_tracks->count()); |
| 58 WebKit::WebVector<WebKit::WebMediaStreamSource> video_source_vector( |
| 59 video_tracks->count()); |
76 | 60 |
77 // Add audio tracks. | 61 // Add audio tracks. |
78 size_t i = 0; | 62 size_t i = 0; |
79 for (; i < audio_tracks->count(); ++i) { | 63 for (; i < audio_tracks->count(); ++i) { |
80 webrtc::AudioTrackInterface* audio_track = audio_tracks->at(i); | 64 webrtc::AudioTrackInterface* audio_track = audio_tracks->at(i); |
81 DCHECK(audio_track); | 65 DCHECK(audio_track); |
82 source_vector[i].initialize( | 66 audio_source_vector[i].initialize( |
83 // TODO(grunell): Set id to something unique. | |
84 UTF8ToUTF16(audio_track->label()), | 67 UTF8ToUTF16(audio_track->label()), |
85 WebKit::WebMediaStreamSource::TypeAudio, | 68 WebKit::WebMediaStreamSource::TypeAudio, |
86 UTF8ToUTF16(audio_track->label())); | 69 UTF8ToUTF16(audio_track->label())); |
87 } | 70 } |
88 | 71 |
89 // Add video tracks. | 72 // Add video tracks. |
90 for (i = 0; i < video_tracks->count(); ++i) { | 73 for (i = 0; i < video_tracks->count(); ++i) { |
91 webrtc::VideoTrackInterface* video_track = video_tracks->at(i); | 74 webrtc::VideoTrackInterface* video_track = video_tracks->at(i); |
92 DCHECK(video_track); | 75 DCHECK(video_track); |
93 source_vector[audio_tracks->count() + i].initialize( | 76 video_source_vector[i].initialize( |
94 // TODO(grunell): Set id to something unique. | |
95 UTF8ToUTF16(video_track->label()), | 77 UTF8ToUTF16(video_track->label()), |
96 WebKit::WebMediaStreamSource::TypeVideo, | 78 WebKit::WebMediaStreamSource::TypeVideo, |
97 UTF8ToUTF16(video_track->label())); | 79 UTF8ToUTF16(video_track->label())); |
98 } | 80 } |
99 | |
100 WebKit::WebMediaStreamDescriptor descriptor; | 81 WebKit::WebMediaStreamDescriptor descriptor; |
101 descriptor.initialize(UTF8ToUTF16(stream->label()), source_vector); | 82 descriptor.initialize(UTF8ToUTF16(stream->label()), |
102 | 83 audio_source_vector, video_source_vector); |
| 84 descriptor.setExtraData(new MediaStreamExtraData(stream)); |
103 return descriptor; | 85 return descriptor; |
104 } | 86 } |
105 | |
106 webrtc::VideoTrackInterface* PeerConnectionHandlerBase::FindRemoteVideoTrack( | |
107 const std::string& source_id) { | |
108 talk_base::scoped_refptr<webrtc::StreamCollectionInterface> streams = | |
109 native_peer_connection_->remote_streams(); | |
110 for (size_t i = 0; i < streams->count(); ++i) { | |
111 webrtc::VideoTracks* track_list =streams->at(i)->video_tracks(); | |
112 for (size_t j =0; j < track_list->count(); ++j) { | |
113 if (track_list->at(j)->label() == source_id) { | |
114 return track_list->at(j); | |
115 } | |
116 } | |
117 } | |
118 return NULL; | |
119 } | |
120 | |
121 | |
122 | |
OLD | NEW |