OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012, Google Inc. All rights reserved. | 2 * Copyright (C) 2012, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "modules/webaudio/AudioNodeOutput.h" | 29 #include "modules/webaudio/AudioNodeOutput.h" |
30 #include "modules/webaudio/BaseAudioContext.h" | 30 #include "modules/webaudio/BaseAudioContext.h" |
31 #include "modules/webaudio/MediaStreamAudioSourceOptions.h" | 31 #include "modules/webaudio/MediaStreamAudioSourceOptions.h" |
32 #include "wtf/Locker.h" | 32 #include "wtf/Locker.h" |
33 #include <memory> | 33 #include <memory> |
34 | 34 |
35 namespace blink { | 35 namespace blink { |
36 | 36 |
37 MediaStreamAudioSourceHandler::MediaStreamAudioSourceHandler( | 37 MediaStreamAudioSourceHandler::MediaStreamAudioSourceHandler( |
38 AudioNode& node, | 38 AudioNode& node, |
39 MediaStream& mediaStream, | |
40 MediaStreamTrack* audioTrack, | |
41 std::unique_ptr<AudioSourceProvider> audioSourceProvider) | 39 std::unique_ptr<AudioSourceProvider> audioSourceProvider) |
42 : AudioHandler(NodeTypeMediaStreamAudioSource, | 40 : AudioHandler(NodeTypeMediaStreamAudioSource, |
43 node, | 41 node, |
44 node.context()->sampleRate()), | 42 node.context()->sampleRate()), |
45 m_mediaStream(mediaStream), | |
46 m_audioTrack(audioTrack), | |
47 m_audioSourceProvider(std::move(audioSourceProvider)), | 43 m_audioSourceProvider(std::move(audioSourceProvider)), |
48 m_sourceNumberOfChannels(0) { | 44 m_sourceNumberOfChannels(0) { |
49 // Default to stereo. This could change depending on the format of the | 45 // Default to stereo. This could change depending on the format of the |
50 // MediaStream's audio track. | 46 // MediaStream's audio track. |
51 addOutput(2); | 47 addOutput(2); |
52 | 48 |
53 initialize(); | 49 initialize(); |
54 } | 50 } |
55 | 51 |
56 PassRefPtr<MediaStreamAudioSourceHandler> MediaStreamAudioSourceHandler::create( | 52 PassRefPtr<MediaStreamAudioSourceHandler> MediaStreamAudioSourceHandler::create( |
57 AudioNode& node, | 53 AudioNode& node, |
58 MediaStream& mediaStream, | |
59 MediaStreamTrack* audioTrack, | |
60 std::unique_ptr<AudioSourceProvider> audioSourceProvider) { | 54 std::unique_ptr<AudioSourceProvider> audioSourceProvider) { |
61 return adoptRef(new MediaStreamAudioSourceHandler( | 55 return adoptRef( |
62 node, mediaStream, audioTrack, std::move(audioSourceProvider))); | 56 new MediaStreamAudioSourceHandler(node, std::move(audioSourceProvider))); |
63 } | 57 } |
64 | 58 |
65 MediaStreamAudioSourceHandler::~MediaStreamAudioSourceHandler() { | 59 MediaStreamAudioSourceHandler::~MediaStreamAudioSourceHandler() { |
66 uninitialize(); | 60 uninitialize(); |
67 } | 61 } |
68 | 62 |
69 void MediaStreamAudioSourceHandler::setFormat(size_t numberOfChannels, | 63 void MediaStreamAudioSourceHandler::setFormat(size_t numberOfChannels, |
70 float sourceSampleRate) { | 64 float sourceSampleRate) { |
71 if (numberOfChannels != m_sourceNumberOfChannels || | 65 if (numberOfChannels != m_sourceNumberOfChannels || |
72 sourceSampleRate != context()->sampleRate()) { | 66 sourceSampleRate != context()->sampleRate()) { |
(...skipping 24 matching lines...) Expand all Loading... |
97 } | 91 } |
98 | 92 |
99 void MediaStreamAudioSourceHandler::process(size_t numberOfFrames) { | 93 void MediaStreamAudioSourceHandler::process(size_t numberOfFrames) { |
100 AudioBus* outputBus = output(0).bus(); | 94 AudioBus* outputBus = output(0).bus(); |
101 | 95 |
102 if (!getAudioSourceProvider()) { | 96 if (!getAudioSourceProvider()) { |
103 outputBus->zero(); | 97 outputBus->zero(); |
104 return; | 98 return; |
105 } | 99 } |
106 | 100 |
107 if (!getMediaStream() || | 101 if (m_sourceNumberOfChannels != outputBus->numberOfChannels()) { |
108 m_sourceNumberOfChannels != outputBus->numberOfChannels()) { | |
109 outputBus->zero(); | 102 outputBus->zero(); |
110 return; | 103 return; |
111 } | 104 } |
112 | 105 |
113 // Use a tryLock() to avoid contention in the real-time audio thread. | 106 // Use a tryLock() to avoid contention in the real-time audio thread. |
114 // If we fail to acquire the lock then the MediaStream must be in the middle | 107 // If we fail to acquire the lock then the MediaStream must be in the middle |
115 // of a format change, so we output silence in this case. | 108 // of a format change, so we output silence in this case. |
116 MutexTryLocker tryLocker(m_processLock); | 109 MutexTryLocker tryLocker(m_processLock); |
117 if (tryLocker.locked()) { | 110 if (tryLocker.locked()) { |
118 getAudioSourceProvider()->provideInput(outputBus, numberOfFrames); | 111 getAudioSourceProvider()->provideInput(outputBus, numberOfFrames); |
119 } else { | 112 } else { |
120 // We failed to acquire the lock. | 113 // We failed to acquire the lock. |
121 outputBus->zero(); | 114 outputBus->zero(); |
122 } | 115 } |
123 } | 116 } |
124 | 117 |
125 // ---------------------------------------------------------------- | 118 // ---------------------------------------------------------------- |
126 | 119 |
127 MediaStreamAudioSourceNode::MediaStreamAudioSourceNode( | 120 MediaStreamAudioSourceNode::MediaStreamAudioSourceNode( |
128 BaseAudioContext& context, | 121 BaseAudioContext& context, |
129 MediaStream& mediaStream, | 122 MediaStream& mediaStream, |
130 MediaStreamTrack* audioTrack, | 123 MediaStreamTrack* audioTrack, |
131 std::unique_ptr<AudioSourceProvider> audioSourceProvider) | 124 std::unique_ptr<AudioSourceProvider> audioSourceProvider) |
132 : AudioNode(context) { | 125 : AudioNode(context), m_audioTrack(audioTrack), m_mediaStream(mediaStream) { |
133 setHandler(MediaStreamAudioSourceHandler::create( | 126 setHandler(MediaStreamAudioSourceHandler::create( |
134 *this, mediaStream, audioTrack, std::move(audioSourceProvider))); | 127 *this, std::move(audioSourceProvider))); |
135 } | 128 } |
136 | 129 |
137 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( | 130 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( |
138 BaseAudioContext& context, | 131 BaseAudioContext& context, |
139 MediaStream& mediaStream, | 132 MediaStream& mediaStream, |
140 ExceptionState& exceptionState) { | 133 ExceptionState& exceptionState) { |
141 DCHECK(isMainThread()); | 134 DCHECK(isMainThread()); |
142 | 135 |
143 if (context.isContextClosed()) { | 136 if (context.isContextClosed()) { |
144 context.throwExceptionForClosedState(exceptionState); | 137 context.throwExceptionForClosedState(exceptionState); |
(...skipping 28 matching lines...) Expand all Loading... |
173 } | 166 } |
174 | 167 |
175 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( | 168 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( |
176 BaseAudioContext* context, | 169 BaseAudioContext* context, |
177 const MediaStreamAudioSourceOptions& options, | 170 const MediaStreamAudioSourceOptions& options, |
178 ExceptionState& exceptionState) { | 171 ExceptionState& exceptionState) { |
179 return create(*context, *options.mediaStream(), exceptionState); | 172 return create(*context, *options.mediaStream(), exceptionState); |
180 } | 173 } |
181 | 174 |
182 DEFINE_TRACE(MediaStreamAudioSourceNode) { | 175 DEFINE_TRACE(MediaStreamAudioSourceNode) { |
| 176 visitor->trace(m_audioTrack); |
| 177 visitor->trace(m_mediaStream); |
183 AudioSourceProviderClient::trace(visitor); | 178 AudioSourceProviderClient::trace(visitor); |
184 AudioNode::trace(visitor); | 179 AudioNode::trace(visitor); |
185 } | 180 } |
186 | 181 |
187 MediaStreamAudioSourceHandler& | 182 MediaStreamAudioSourceHandler& |
188 MediaStreamAudioSourceNode::mediaStreamAudioSourceHandler() const { | 183 MediaStreamAudioSourceNode::mediaStreamAudioSourceHandler() const { |
189 return static_cast<MediaStreamAudioSourceHandler&>(handler()); | 184 return static_cast<MediaStreamAudioSourceHandler&>(handler()); |
190 } | 185 } |
191 | 186 |
192 MediaStream* MediaStreamAudioSourceNode::getMediaStream() const { | 187 MediaStream* MediaStreamAudioSourceNode::getMediaStream() const { |
193 return mediaStreamAudioSourceHandler().getMediaStream(); | 188 return m_mediaStream; |
194 } | 189 } |
195 | 190 |
196 void MediaStreamAudioSourceNode::setFormat(size_t numberOfChannels, | 191 void MediaStreamAudioSourceNode::setFormat(size_t numberOfChannels, |
197 float sourceSampleRate) { | 192 float sourceSampleRate) { |
198 mediaStreamAudioSourceHandler().setFormat(numberOfChannels, sourceSampleRate); | 193 mediaStreamAudioSourceHandler().setFormat(numberOfChannels, sourceSampleRate); |
199 } | 194 } |
200 | 195 |
201 } // namespace blink | 196 } // namespace blink |
OLD | NEW |