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 "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/public/renderer/media_stream_audio_sink.h" |
| 10 #include "media/base/audio_bus.h" |
9 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 11 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
10 #include "third_party/webrtc/api/mediastreaminterface.h" | |
11 | 12 |
12 namespace content { | 13 namespace content { |
13 | 14 |
14 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) | 15 MediaStreamAudioTrack::MediaStreamAudioTrack(bool is_local_track) |
15 : MediaStreamTrack(is_local_track) { | 16 : MediaStreamTrack(is_local_track), is_enabled_(true), weak_factory_(this) { |
16 DVLOG(1) << "MediaStreamAudioTrack::MediaStreamAudioTrack(is a " | 17 DVLOG(1) << "MediaStreamAudioTrack@" << this << "::MediaStreamAudioTrack(" |
17 << (is_local_track ? "local" : "remote") << " track)"; | 18 << (is_local_track ? "local" : "remote") << " track)"; |
18 } | 19 } |
19 | 20 |
20 MediaStreamAudioTrack::~MediaStreamAudioTrack() { | 21 MediaStreamAudioTrack::~MediaStreamAudioTrack() { |
21 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 22 DCHECK(thread_checker_.CalledOnValidThread()); |
22 DVLOG(1) << "MediaStreamAudioTrack::~MediaStreamAudioTrack()"; | 23 DVLOG(1) << "MediaStreamAudioTrack@" << this << " is being destroyed."; |
23 DCHECK(stop_callback_.is_null()) | 24 DCHECK(stop_callback_.is_null()) |
24 << "BUG: Subclass must ensure Stop() is called."; | 25 << "BUG: Subclass must ensure Stop() is called."; |
25 } | 26 } |
26 | 27 |
27 // static | 28 // static |
28 MediaStreamAudioTrack* MediaStreamAudioTrack::From( | 29 MediaStreamAudioTrack* MediaStreamAudioTrack::From( |
29 const blink::WebMediaStreamTrack& track) { | 30 const blink::WebMediaStreamTrack& track) { |
30 if (track.isNull() || | 31 if (track.isNull() || |
31 track.source().getType() != blink::WebMediaStreamSource::TypeAudio) { | 32 track.source().getType() != blink::WebMediaStreamSource::TypeAudio) { |
32 return nullptr; | 33 return nullptr; |
33 } | 34 } |
34 return static_cast<MediaStreamAudioTrack*>(track.getExtraData()); | 35 return static_cast<MediaStreamAudioTrack*>(track.getExtraData()); |
35 } | 36 } |
36 | 37 |
| 38 void MediaStreamAudioTrack::AddSink(MediaStreamAudioSink* sink) { |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 40 DVLOG(1) << "Adding MediaStreamAudioSink@" << sink |
| 41 << " to MediaStreamAudioTrack@" << this << '.'; |
| 42 deliverer_.AddConsumer(sink); |
| 43 } |
| 44 |
| 45 void MediaStreamAudioTrack::RemoveSink(MediaStreamAudioSink* sink) { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 deliverer_.RemoveConsumer(sink); |
| 48 DVLOG(1) << "Removed MediaStreamAudioSink@" << sink |
| 49 << " from MediaStreamAudioTrack@" << this << '.'; |
| 50 } |
| 51 |
| 52 media::AudioParameters MediaStreamAudioTrack::GetOutputFormat() const { |
| 53 return deliverer_.GetAudioParameters(); |
| 54 } |
| 55 |
| 56 void MediaStreamAudioTrack::SetEnabled(bool enabled) { |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 DVLOG(1) << "MediaStreamAudioTrack@" << this << "::SetEnabled(" |
| 59 << (enabled ? 'Y' : 'N') << ')'; |
| 60 { |
| 61 base::AutoLock auto_lock(enabled_lock_); |
| 62 if (is_enabled_ == enabled) |
| 63 return; |
| 64 is_enabled_ = enabled; |
| 65 } |
| 66 |
| 67 std::vector<MediaStreamAudioSink*> sinks_to_notify; |
| 68 deliverer_.GetConsumerList(&sinks_to_notify); |
| 69 for (MediaStreamAudioSink* sink : sinks_to_notify) |
| 70 sink->OnEnabledChanged(enabled); |
| 71 } |
| 72 |
| 73 void* MediaStreamAudioTrack::GetClassIdentifier() const { |
| 74 return nullptr; |
| 75 } |
| 76 |
37 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { | 77 void MediaStreamAudioTrack::Start(const base::Closure& stop_callback) { |
38 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 78 DCHECK(thread_checker_.CalledOnValidThread()); |
39 DCHECK(!stop_callback.is_null()); | 79 DCHECK(!stop_callback.is_null()); |
40 DCHECK(stop_callback_.is_null()); | 80 DCHECK(stop_callback_.is_null()); |
41 DVLOG(1) << "MediaStreamAudioTrack::Start()"; | 81 DVLOG(1) << "Starting MediaStreamAudioTrack@" << this << '.'; |
42 stop_callback_ = stop_callback; | 82 stop_callback_ = stop_callback; |
43 } | 83 } |
44 | 84 |
45 void MediaStreamAudioTrack::Stop() { | 85 void MediaStreamAudioTrack::Stop() { |
46 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
47 DVLOG(1) << "MediaStreamAudioTrack::Stop()"; | 87 DVLOG(1) << "Stopping MediaStreamAudioTrack@" << this << '.'; |
| 88 |
48 if (!stop_callback_.is_null()) | 89 if (!stop_callback_.is_null()) |
49 base::ResetAndReturn(&stop_callback_).Run(); | 90 base::ResetAndReturn(&stop_callback_).Run(); |
| 91 |
50 OnStop(); | 92 OnStop(); |
| 93 |
| 94 std::vector<MediaStreamAudioSink*> sinks_to_end; |
| 95 deliverer_.GetConsumerList(&sinks_to_end); |
| 96 for (MediaStreamAudioSink* sink : sinks_to_end) { |
| 97 deliverer_.RemoveConsumer(sink); |
| 98 sink->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded); |
| 99 } |
| 100 |
| 101 weak_factory_.InvalidateWeakPtrs(); |
51 } | 102 } |
52 | 103 |
53 void MediaStreamAudioTrack::OnStop() {} | 104 void MediaStreamAudioTrack::OnStop() {} |
54 | 105 |
55 webrtc::AudioTrackInterface* MediaStreamAudioTrack::GetAudioAdapter() { | 106 void MediaStreamAudioTrack::OnSetFormat(const media::AudioParameters& params) { |
56 NOTREACHED(); | 107 deliverer_.OnSetFormat(params); |
57 return nullptr; | 108 } |
| 109 |
| 110 void MediaStreamAudioTrack::OnData(const media::AudioBus& audio_bus, |
| 111 base::TimeTicks reference_time) { |
| 112 bool enabled; |
| 113 { |
| 114 base::AutoLock auto_lock(enabled_lock_); |
| 115 enabled = is_enabled_; |
| 116 } |
| 117 |
| 118 if (enabled) { |
| 119 deliverer_.OnData(audio_bus, reference_time); |
| 120 } else { |
| 121 // The W3C spec requires silent audio to flow while a track is disabled. |
| 122 if (!silent_bus_ || silent_bus_->channels() != audio_bus.channels() || |
| 123 silent_bus_->frames() != audio_bus.frames()) { |
| 124 silent_bus_ = media::AudioBus::Create(audio_bus.channels(), |
| 125 audio_bus.frames()); |
| 126 silent_bus_->Zero(); |
| 127 } |
| 128 deliverer_.OnData(*silent_bus_, reference_time); |
| 129 } |
58 } | 130 } |
59 | 131 |
60 } // namespace content | 132 } // namespace content |
OLD | NEW |