OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/webrtc/webrtc_media_stream_adapter.h" | 5 #include "content/renderer/media/webrtc/webrtc_media_stream_adapter.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/renderer/media/media_stream_audio_source.h" | |
9 #include "content/renderer/media/media_stream_audio_track.h" | 8 #include "content/renderer/media/media_stream_audio_track.h" |
10 #include "content/renderer/media/media_stream_track.h" | 9 #include "content/renderer/media/media_stream_track.h" |
11 #include "content/renderer/media/webrtc/media_stream_video_webrtc_sink.h" | 10 #include "content/renderer/media/webrtc/media_stream_video_webrtc_sink.h" |
12 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" | 11 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h" |
| 12 #include "content/renderer/media/webrtc/processed_local_audio_source.h" |
| 13 #include "content/renderer/media/webrtc/webrtc_audio_sink.h" |
13 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
15 #include "third_party/WebKit/public/platform/WebString.h" | 16 #include "third_party/WebKit/public/platform/WebString.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 WebRtcMediaStreamAdapter::WebRtcMediaStreamAdapter( | 20 WebRtcMediaStreamAdapter::WebRtcMediaStreamAdapter( |
20 const blink::WebMediaStream& web_stream, | 21 const blink::WebMediaStream& web_stream, |
21 PeerConnectionDependencyFactory* factory) | 22 PeerConnectionDependencyFactory* factory) |
22 : web_stream_(web_stream), | 23 : web_stream_(web_stream), |
23 factory_(factory) { | 24 factory_(factory) { |
24 webrtc_media_stream_ = | 25 webrtc_media_stream_ = |
25 factory_->CreateLocalMediaStream(web_stream.id().utf8()); | 26 factory_->CreateLocalMediaStream(web_stream.id().utf8()); |
26 | 27 |
27 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks; | 28 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks; |
28 web_stream_.audioTracks(audio_tracks); | 29 web_stream_.audioTracks(audio_tracks); |
29 for (blink::WebMediaStreamTrack& audio_track : audio_tracks) | 30 for (blink::WebMediaStreamTrack& audio_track : audio_tracks) |
30 CreateAudioTrack(audio_track); | 31 AddAudioSinkToTrack(audio_track); |
31 | 32 |
32 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; | 33 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; |
33 web_stream_.videoTracks(video_tracks); | 34 web_stream_.videoTracks(video_tracks); |
34 for (blink::WebMediaStreamTrack& video_track : video_tracks) | 35 for (blink::WebMediaStreamTrack& video_track : video_tracks) |
35 CreateVideoTrack(video_track); | 36 AddVideoSinkToTrack(video_track); |
36 | 37 |
37 MediaStream* const native_stream = MediaStream::GetMediaStream(web_stream_); | 38 MediaStream* const native_stream = MediaStream::GetMediaStream(web_stream_); |
38 native_stream->AddObserver(this); | 39 native_stream->AddObserver(this); |
39 } | 40 } |
40 | 41 |
41 WebRtcMediaStreamAdapter::~WebRtcMediaStreamAdapter() { | 42 WebRtcMediaStreamAdapter::~WebRtcMediaStreamAdapter() { |
42 MediaStream* const native_stream = MediaStream::GetMediaStream(web_stream_); | 43 MediaStream* const native_stream = MediaStream::GetMediaStream(web_stream_); |
43 native_stream->RemoveObserver(this); | 44 native_stream->RemoveObserver(this); |
| 45 |
| 46 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks; |
| 47 web_stream_.audioTracks(audio_tracks); |
| 48 for (blink::WebMediaStreamTrack& audio_track : audio_tracks) |
| 49 TrackRemoved(audio_track); |
| 50 DCHECK(audio_sinks_.empty()); |
| 51 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; |
| 52 web_stream_.videoTracks(video_tracks); |
| 53 for (blink::WebMediaStreamTrack& video_track : video_tracks) |
| 54 TrackRemoved(video_track); |
| 55 DCHECK(video_sinks_.empty()); |
44 } | 56 } |
45 | 57 |
46 void WebRtcMediaStreamAdapter::TrackAdded( | 58 void WebRtcMediaStreamAdapter::TrackAdded( |
47 const blink::WebMediaStreamTrack& track) { | 59 const blink::WebMediaStreamTrack& track) { |
48 if (track.source().getType() == blink::WebMediaStreamSource::TypeAudio) | 60 if (track.source().getType() == blink::WebMediaStreamSource::TypeAudio) |
49 CreateAudioTrack(track); | 61 AddAudioSinkToTrack(track); |
50 else | 62 else |
51 CreateVideoTrack(track); | 63 AddVideoSinkToTrack(track); |
52 } | 64 } |
53 | 65 |
54 void WebRtcMediaStreamAdapter::TrackRemoved( | 66 void WebRtcMediaStreamAdapter::TrackRemoved( |
55 const blink::WebMediaStreamTrack& track) { | 67 const blink::WebMediaStreamTrack& track) { |
56 const std::string track_id = track.id().utf8(); | 68 const std::string track_id = track.id().utf8(); |
57 if (track.source().getType() == blink::WebMediaStreamSource::TypeAudio) { | 69 if (track.source().getType() == blink::WebMediaStreamSource::TypeAudio) { |
58 webrtc_media_stream_->RemoveTrack( | 70 scoped_refptr<webrtc::AudioTrackInterface> webrtc_track = |
59 webrtc_media_stream_->FindAudioTrack(track_id)); | 71 make_scoped_refptr( |
| 72 webrtc_media_stream_->FindAudioTrack(track_id).get()); |
| 73 if (!webrtc_track) |
| 74 return; |
| 75 webrtc_media_stream_->RemoveTrack(webrtc_track.get()); |
| 76 |
| 77 for (auto it = audio_sinks_.begin(); it != audio_sinks_.end(); ++it) { |
| 78 if ((*it)->webrtc_audio_track() == webrtc_track.get()) { |
| 79 if (auto* media_stream_track = MediaStreamAudioTrack::From(track)) |
| 80 media_stream_track->RemoveSink(it->get()); |
| 81 audio_sinks_.erase(it); |
| 82 break; |
| 83 } |
| 84 } |
60 } else { | 85 } else { |
61 DCHECK_EQ(track.source().getType(), blink::WebMediaStreamSource::TypeVideo); | 86 DCHECK_EQ(track.source().getType(), blink::WebMediaStreamSource::TypeVideo); |
62 scoped_refptr<webrtc::VideoTrackInterface> webrtc_track = | 87 scoped_refptr<webrtc::VideoTrackInterface> webrtc_track = |
63 webrtc_media_stream_->FindVideoTrack(track_id).get(); | 88 make_scoped_refptr( |
| 89 webrtc_media_stream_->FindVideoTrack(track_id).get()); |
| 90 if (!webrtc_track) |
| 91 return; |
64 webrtc_media_stream_->RemoveTrack(webrtc_track.get()); | 92 webrtc_media_stream_->RemoveTrack(webrtc_track.get()); |
65 | 93 |
66 for (ScopedVector<MediaStreamVideoWebRtcSink>::iterator it = | 94 for (auto it = video_sinks_.begin(); it != video_sinks_.end(); ++it) { |
67 video_adapters_.begin(); it != video_adapters_.end(); ++it) { | |
68 if ((*it)->webrtc_video_track() == webrtc_track.get()) { | 95 if ((*it)->webrtc_video_track() == webrtc_track.get()) { |
69 video_adapters_.erase(it); | 96 video_sinks_.erase(it); |
70 break; | 97 break; |
71 } | 98 } |
72 } | 99 } |
73 } | 100 } |
74 } | 101 } |
75 | 102 |
76 void WebRtcMediaStreamAdapter::CreateAudioTrack( | 103 void WebRtcMediaStreamAdapter::AddAudioSinkToTrack( |
77 const blink::WebMediaStreamTrack& track) { | 104 const blink::WebMediaStreamTrack& track) { |
78 DCHECK_EQ(track.source().getType(), blink::WebMediaStreamSource::TypeAudio); | |
79 // A media stream is connected to a peer connection, enable the | |
80 // peer connection mode for the sources. | |
81 MediaStreamAudioTrack* native_track = MediaStreamAudioTrack::From(track); | 105 MediaStreamAudioTrack* native_track = MediaStreamAudioTrack::From(track); |
82 if (!native_track) { | 106 if (!native_track) { |
83 DLOG(ERROR) << "No native track for blink audio track."; | 107 DLOG(ERROR) << "No native track for blink audio track."; |
84 return; | 108 return; |
85 } | 109 } |
86 | 110 |
87 webrtc::AudioTrackInterface* audio_track = native_track->GetAudioAdapter(); | 111 WebRtcAudioSink* audio_sink; |
88 if (!audio_track) { | 112 if (auto* media_stream_source = ProcessedLocalAudioSource::From( |
89 DLOG(ERROR) << "Audio track doesn't support webrtc."; | 113 MediaStreamAudioSource::From(track.source()))) { |
90 return; | 114 audio_sink = new WebRtcAudioSink( |
| 115 track.id().utf8(), media_stream_source->rtc_source(), |
| 116 factory_->GetWebRtcSignalingThread()); |
| 117 audio_sink->SetLevel(media_stream_source->audio_level()); |
| 118 // The sink only grabs stats from the audio processor. Stats are only |
| 119 // available if audio processing is turned on. Therefore, only provide the |
| 120 // sink a reference to the processor if audio processing is turned on. |
| 121 if (auto processor = media_stream_source->audio_processor()) { |
| 122 if (processor && processor->has_audio_processing()) |
| 123 audio_sink->SetAudioProcessor(processor); |
| 124 } |
| 125 } else { |
| 126 // Remote sources and other non-WebRtc local sources do not provide an |
| 127 // instance of the webrtc::AudioSourceInterface, and also do not need |
| 128 // references to the audio level calculator or audio processor passed to the |
| 129 // sink. |
| 130 webrtc::AudioSourceInterface* source_interface = nullptr; |
| 131 audio_sink = new WebRtcAudioSink( |
| 132 track.id().utf8(), source_interface, |
| 133 factory_->GetWebRtcSignalingThread()); |
91 } | 134 } |
92 | 135 |
93 if (native_track->is_local_track()) { | 136 audio_sinks_.push_back(std::unique_ptr<WebRtcAudioSink>(audio_sink)); |
94 const blink::WebMediaStreamSource& source = track.source(); | 137 native_track->AddSink(audio_sink); |
95 MediaStreamAudioSource* audio_source = MediaStreamAudioSource::From(source); | 138 webrtc_media_stream_->AddTrack(audio_sink->webrtc_audio_track()); |
96 if (audio_source && audio_source->audio_capturer()) | |
97 audio_source->audio_capturer()->EnablePeerConnectionMode(); | |
98 } | |
99 | |
100 webrtc_media_stream_->AddTrack(audio_track); | |
101 } | 139 } |
102 | 140 |
103 void WebRtcMediaStreamAdapter::CreateVideoTrack( | 141 void WebRtcMediaStreamAdapter::AddVideoSinkToTrack( |
104 const blink::WebMediaStreamTrack& track) { | 142 const blink::WebMediaStreamTrack& track) { |
105 DCHECK_EQ(track.source().getType(), blink::WebMediaStreamSource::TypeVideo); | 143 DCHECK_EQ(track.source().getType(), blink::WebMediaStreamSource::TypeVideo); |
106 MediaStreamVideoWebRtcSink* adapter = | 144 MediaStreamVideoWebRtcSink* video_sink = |
107 new MediaStreamVideoWebRtcSink(track, factory_); | 145 new MediaStreamVideoWebRtcSink(track, factory_); |
108 video_adapters_.push_back(adapter); | 146 video_sinks_.push_back( |
109 webrtc_media_stream_->AddTrack(adapter->webrtc_video_track()); | 147 std::unique_ptr<MediaStreamVideoWebRtcSink>(video_sink)); |
| 148 webrtc_media_stream_->AddTrack(video_sink->webrtc_video_track()); |
110 } | 149 } |
111 | 150 |
112 } // namespace content | 151 } // namespace content |
OLD | NEW |