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/media_stream_audio_track.h" | 5 #include "content/renderer/media/media_stream_audio_track.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "content/public/renderer/media_stream_audio_sink.h" |
9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 12 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
10 #include "third_party/webrtc/api/mediastreaminterface.h" | |
11 | 13 |
12 namespace content { | 14 namespace content { |
13 | 15 |
14 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) | 16 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) |
15 : MediaStreamTrack(is_local_track) { | 17 : MediaStreamTrack(is_local_track), is_enabled_(true), weak_factory_(this) { |
16 DVLOG(1) << "MediaStreamAudioTrack::MediaStreamAudioTrack(is a " | 18 DVLOG(1) << "MediaStreamAudioTrack::MediaStreamAudioTrack(is a " |
17 << (is_local_track ? "local" : "remote") << " track)"; | 19 << (is_local_track ? "local" : "remote") << " track)"; |
18 } | 20 } |
19 | 21 |
20 MediaStreamAudioTrack::~MediaStreamAudioTrack() { | 22 MediaStreamAudioTrack::~MediaStreamAudioTrack() { |
21 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 23 DCHECK(thread_checker_.CalledOnValidThread()); |
22 DVLOG(1) << "MediaStreamAudioTrack::~MediaStreamAudioTrack()"; | 24 DVLOG(1) << "MediaStreamAudioTrack::~MediaStreamAudioTrack()"; |
23 DCHECK(stop_callback_.is_null()) | 25 DCHECK(stop_callback_.is_null()) |
24 << "BUG: Subclass must ensure Stop() is called."; | 26 << "BUG: Subclass must ensure Stop() is called."; |
25 } | 27 } |
26 | 28 |
27 // static | 29 // static |
28 MediaStreamAudioTrack* MediaStreamAudioTrack::From( | 30 MediaStreamAudioTrack* MediaStreamAudioTrack::From( |
29 const blink::WebMediaStreamTrack& track) { | 31 const blink::WebMediaStreamTrack& track) { |
30 if (track.isNull() || | 32 if (track.isNull() || |
31 track.source().getType() != blink::WebMediaStreamSource::TypeAudio) { | 33 track.source().getType() != blink::WebMediaStreamSource::TypeAudio) { |
32 return nullptr; | 34 return nullptr; |
33 } | 35 } |
34 return static_cast<MediaStreamAudioTrack*>(track.getExtraData()); | 36 return static_cast<MediaStreamAudioTrack*>(track.getExtraData()); |
35 } | 37 } |
36 | 38 |
| 39 void MediaStreamAudioTrack::AddSink(MediaStreamAudioSink* sink) { |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 41 DCHECK(sink); |
| 42 DVLOG(1) << "MediaStreamAudioTrack::AddSink(" << sink << ')'; |
| 43 { |
| 44 base::AutoLock auto_lock(lock_); |
| 45 DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()); |
| 46 DCHECK(std::find(pending_sinks_.begin(), pending_sinks_.end(), sink) == |
| 47 pending_sinks_.end()); |
| 48 pending_sinks_.push_back(sink); |
| 49 } |
| 50 } |
| 51 |
| 52 void MediaStreamAudioTrack::RemoveSink(MediaStreamAudioSink* sink) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 { |
| 55 base::AutoLock auto_lock(lock_); |
| 56 auto it = std::find(pending_sinks_.begin(), pending_sinks_.end(), sink); |
| 57 if (it != pending_sinks_.end()) { |
| 58 DVLOG(1) << "MediaStreamAudioTrack::RemoveSink(" << sink |
| 59 << ") from pending sinks list."; |
| 60 pending_sinks_.erase(it); |
| 61 } else { |
| 62 it = std::find(sinks_.begin(), sinks_.end(), sink); |
| 63 if (it != sinks_.end()) { |
| 64 DVLOG(1) << "MediaStreamAudioTrack::RemoveSink(" << sink |
| 65 << ") from active sinks list."; |
| 66 sinks_.erase(it); |
| 67 } |
| 68 } |
| 69 } |
| 70 } |
| 71 |
| 72 media::AudioParameters MediaStreamAudioTrack::GetOutputFormat() const { |
| 73 base::AutoLock auto_lock(lock_); |
| 74 return params_; |
| 75 } |
| 76 |
| 77 void MediaStreamAudioTrack::SetEnabled(bool enabled) { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 base::AutoLock auto_lock(lock_); |
| 80 DVLOG(1) << "MediaStreamAudioTrack::SetEnabled(" << (enabled ? 'Y' : 'N') |
| 81 << "), was previously set to " << (is_enabled_ ? 'Y' : 'N') << '.'; |
| 82 is_enabled_ = enabled; |
| 83 } |
| 84 |
| 85 void* MediaStreamAudioTrack::GetClassIdentifier() const { |
| 86 return nullptr; |
| 87 } |
| 88 |
37 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { | 89 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { |
38 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 90 DCHECK(thread_checker_.CalledOnValidThread()); |
39 DCHECK(!stop_callback.is_null()); | 91 DCHECK(!stop_callback.is_null()); |
40 DCHECK(stop_callback_.is_null()); | 92 DCHECK(stop_callback_.is_null()); |
41 DVLOG(1) << "MediaStreamAudioTrack::Start()"; | 93 DVLOG(1) << "MediaStreamAudioTrack::Start()"; |
42 stop_callback_ = stop_callback; | 94 stop_callback_ = stop_callback; |
43 } | 95 } |
44 | 96 |
45 void MediaStreamAudioTrack::Stop() { | 97 void MediaStreamAudioTrack::Stop() { |
46 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 98 DCHECK(thread_checker_.CalledOnValidThread()); |
47 DVLOG(1) << "MediaStreamAudioTrack::Stop()"; | 99 DVLOG(1) << "MediaStreamAudioTrack::Stop()"; |
| 100 |
48 if (!stop_callback_.is_null()) | 101 if (!stop_callback_.is_null()) |
49 base::ResetAndReturn(&stop_callback_).Run(); | 102 base::ResetAndReturn(&stop_callback_).Run(); |
| 103 |
50 OnStop(); | 104 OnStop(); |
| 105 |
| 106 std::vector<MediaStreamAudioSink*> sinks_to_notify; |
| 107 { |
| 108 base::AutoLock auto_lock(lock_); |
| 109 sinks_to_notify.swap(sinks_); |
| 110 sinks_to_notify.insert(sinks_to_notify.end(), pending_sinks_.begin(), |
| 111 pending_sinks_.end()); |
| 112 pending_sinks_.clear(); |
| 113 } |
| 114 for (MediaStreamAudioSink* sink : sinks_to_notify) |
| 115 sink->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded); |
| 116 |
| 117 weak_factory_.InvalidateWeakPtrs(); |
51 } | 118 } |
52 | 119 |
53 void MediaStreamAudioTrack::OnStop() {} | 120 void MediaStreamAudioTrack::OnStop() {} |
54 | 121 |
55 webrtc::AudioTrackInterface* MediaStreamAudioTrack::GetAudioAdapter() { | 122 void MediaStreamAudioTrack::SetFormat(const media::AudioParameters& params) { |
56 NOTREACHED(); | 123 // Note: May be called on any thread. |
57 return nullptr; | 124 base::AutoLock auto_lock(lock_); |
| 125 if (params_.Equals(params)) |
| 126 return; |
| 127 params_ = params; |
| 128 pending_sinks_.insert(pending_sinks_.end(), sinks_.begin(), sinks_.end()); |
| 129 sinks_.clear(); |
| 130 } |
| 131 |
| 132 void MediaStreamAudioTrack::DeliverDataToSinks(const media::AudioBus& audio_bus, |
| 133 base::TimeTicks reference_time) { |
| 134 // Note: May be called on any thread. |
| 135 base::AutoLock auto_lock(lock_); |
| 136 |
| 137 // If audio delivery is currently disabled, take no actions. |
| 138 if (!is_enabled_) |
| 139 return; |
| 140 |
| 141 // Call OnSetFormat() for all pending sinks and move them to the |
| 142 // active-delivery list. |
| 143 DCHECK(params_.IsValid()); |
| 144 if (!pending_sinks_.empty()) { |
| 145 for (MediaStreamAudioSink* sink : pending_sinks_) |
| 146 sink->OnSetFormat(params_); |
| 147 sinks_.insert(sinks_.end(), pending_sinks_.begin(), pending_sinks_.end()); |
| 148 pending_sinks_.clear(); |
| 149 } |
| 150 |
| 151 // Deliver the audio data to each sink. |
| 152 for (MediaStreamAudioSink* sink : sinks_) |
| 153 sink->OnData(audio_bus, reference_time); |
58 } | 154 } |
59 | 155 |
60 } // namespace content | 156 } // namespace content |
OLD | NEW |