OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/speech_recognition_audio_source_provider.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/shared_memory.h" | |
9 #include "base/time/time.h" | |
10 #include "media/audio/audio_parameters.h" | |
11 #include "media/base/audio_fifo.h" | |
12 | |
13 namespace content { | |
14 | |
15 SpeechRecognitionAudioSourceProvider::SpeechRecognitionAudioSourceProvider( | |
16 const blink::WebMediaStreamTrack& track, | |
17 const media::AudioParameters& params, | |
18 base::SharedMemoryHandle memory, | |
19 int memory_length, | |
20 const OnDataCB& on_data_callback, | |
21 const OnErrorCB& on_error_callback | |
22 ) | |
tommi (sloooow) - chröme
2014/08/25 14:38:40
put on previous line?
burnik
2014/08/29 09:18:15
Done.
| |
23 : track_(track), | |
24 shared_memory_(memory, false), | |
25 output_params_(params), | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
should output_params_ be const?
burnik
2014/08/29 09:18:16
I think so. AudioConverter marks them const. I'm o
tommi (sloooow) - chröme
2014/08/29 11:25:30
OK, but I'm not seeing that it has been changed to
| |
26 on_data_callback_(on_data_callback), | |
27 on_error_callback_(on_error_callback), | |
28 attached_converter_(false), | |
29 track_stopped_(false), | |
30 unconsumed_audio_buffers_(0) { | |
31 DCHECK(shared_memory_.Map(memory_length)); | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
I think this is a bug... DCHECK()ed code isn't inc
burnik
2014/08/29 09:18:15
Done.
| |
32 DCHECK_EQ(memory_length, media::AudioBus::CalculateMemorySize(params)); | |
33 DCHECK(params.IsValid()); | |
34 DCHECK(!on_data_callback_.is_null()); | |
35 | |
36 output_bus_ = media::AudioBus::WrapMemory(params, shared_memory_.memory()); | |
37 | |
38 // Connect the source provider to the track as a sink. | |
39 MediaStreamAudioSink::AddToAudioTrack(this, track_); | |
40 } | |
41 | |
42 SpeechRecognitionAudioSourceProvider::~SpeechRecognitionAudioSourceProvider() { | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
missing thread check for dtor
burnik
2014/08/29 09:18:16
Done.
| |
43 if (audio_converter_.get() && attached_converter_) | |
44 audio_converter_->RemoveInput(this); | |
45 // If the track is still active, it is necessary to notify the track before | |
46 // the sink goes away. | |
47 if (!track_stopped_) | |
48 MediaStreamAudioSink::RemoveFromAudioTrack(this, track_); | |
49 } | |
50 | |
51 void SpeechRecognitionAudioSourceProvider::OnSetFormat( | |
52 const media::AudioParameters& input_params) { | |
53 // We need detach the thread here because it will be a new capture thread | |
54 // calling OnSetFormat() and OnData() if the source is restarted. | |
55 capture_thread_checker_.DetachFromThread(); | |
56 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
this dcheck will never hit because of the Detach o
tommi (sloooow) - chröme
2014/08/29 11:25:30
Anything needed to be done here?
| |
57 DCHECK(input_params.IsValid()); | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
would it make sense to assert that input_params_ (
burnik
2014/08/29 09:18:15
I believe previous state of input_params_ does not
| |
58 | |
59 input_params_ = input_params; | |
60 // TODO(burnik): Check if this is necessary: | |
61 // Create the audio converter with |disable_fifo| as false so that the | |
62 // converter will request input_params.frames_per_buffer() each time. | |
63 // This will not increase the complexity as there is only one client to | |
64 // the converter. | |
65 audio_converter_.reset( | |
66 new media::AudioConverter(input_params, output_params_, false)); | |
67 | |
68 DCHECK_EQ(0,output_params_.frames_per_buffer() * input_params_.sample_rate() % | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
space after ,
burnik
2014/08/29 09:18:16
Done.
| |
69 output_params_.sample_rate()); | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
4 space indent
| |
70 fifo_buffer_size_ = output_params_.frames_per_buffer() * | |
71 input_params_.sample_rate() / output_params_.sample_rate(); | |
tommi (sloooow) - chröme
2014/08/25 14:38:44
4 spaces
burnik
2014/08/29 09:18:15
Done.
| |
72 | |
73 int frames_in_fifo = kNumberOfBuffersInFifo * fifo_buffer_size_; | |
74 | |
75 fifo_.reset(new media::AudioFifo(input_params.channels(), frames_in_fifo)); | |
76 input_bus_ = media::AudioBus::Create(input_params.channels(), | |
77 input_params.frames_per_buffer()); | |
78 } | |
79 | |
80 void SpeechRecognitionAudioSourceProvider::OnReadyStateChanged( | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
thread check here?
burnik
2014/08/29 09:18:15
Done.
| |
81 blink::WebMediaStreamSource::ReadyState state) { | |
82 if (state == blink::WebMediaStreamSource::ReadyStateEnded) | |
83 track_stopped_ = true; | |
tommi (sloooow) - chröme
2014/08/25 14:38:40
would it make sense to add
else
DCHECK(!track_s
burnik
2014/08/29 09:18:15
Done.
| |
84 } | |
85 | |
86 void SpeechRecognitionAudioSourceProvider::OnData( | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
on which thread does this function run?
burnik
2014/08/29 09:18:16
capturer thread. Done.
On 2014/08/25 14:38:45, tom
| |
87 const int16* audio_data, | |
88 int sample_rate, | |
89 int number_of_channels, | |
90 int number_of_frames) { | |
91 | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
remove empty line
burnik
2014/08/29 09:18:15
Done.
| |
92 // TODO(burnik): Remove this if sync socket proves a good alternative to IPCs | |
93 // Notify client the FIFO is overflowing and stop | |
94 if (fifo_->frames() + number_of_frames > fifo_->max_frames()) { | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
it's not clear to me if fifo_ needs protection etc
burnik
2014/08/29 09:18:16
Done.
| |
95 if (attached_converter_) { | |
96 audio_converter_->RemoveInput(this); | |
97 attached_converter_ = false; | |
98 on_error_callback_.Run(); | |
99 } | |
100 return; | |
101 } | |
102 | |
103 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
tommi (sloooow) - chröme
2014/08/25 14:38:40
ah... this should be at the top of the function.
burnik
2014/08/29 09:18:15
Done.
| |
104 DCHECK(input_bus_->frames() == number_of_frames); | |
105 DCHECK(input_bus_->channels() == number_of_channels); | |
106 DCHECK_LE(fifo_->frames() + number_of_frames, fifo_->max_frames()); | |
107 | |
108 // TODO(xians): A better way to handle the interleaved and deinterleaved | |
109 // format switching, see issue/317710. | |
110 input_bus_->FromInterleaved(audio_data, number_of_frames, | |
111 sizeof(audio_data[0])); | |
112 | |
113 fifo_->Push(input_bus_.get()); | |
114 | |
115 // wait for FIFO to have at least |fifo_buffer_size_| frames ready | |
116 if (fifo_->frames() < fifo_buffer_size_) | |
117 return; | |
118 // Attach converter when we first reach |fifo_buffer_size_| frames in the FIFO | |
119 if (!attached_converter_) { | |
120 audio_converter_->AddInput(this); | |
121 attached_converter_ = true; | |
122 // we need one more buffer of |number_of_frames| before we start converting | |
123 return; | |
124 } | |
125 | |
126 // make sure the previous output buffer was consumed by the client | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
why is it safe to touch the above member variables
burnik
2014/08/29 09:18:16
This was only intended for protecting unconsumed_a
| |
127 base::AutoLock auto_lock(lock_); | |
128 if (unconsumed_audio_buffers_ > 0) { | |
129 DLOG(WARNING) << "Client still consuming buffers: " | |
130 << unconsumed_audio_buffers_; | |
131 return; | |
132 } | |
133 // first call to |Convert| must have |fifo_buffer_size_| + |number_of_frames| | |
134 // waiting on the FIFO since it will trigger one extra |ProvideInput| call. | |
135 // This way it is also guaranteed to have a non-empty first output buffer | |
136 audio_converter_->Convert(output_bus_.get()); | |
137 | |
138 DCHECK_EQ(unconsumed_audio_buffers_, 0); | |
139 ++unconsumed_audio_buffers_; | |
140 // Notify client to consume buffer on |output_bus_| | |
141 on_data_callback_.Run(); | |
tommi (sloooow) - chröme
2014/08/25 14:38:45
if there's a way to avoid holding the lock when fi
burnik
2014/08/29 09:18:16
Locks removed from the design.
On 2014/08/25 14:38
| |
142 } | |
143 | |
144 double SpeechRecognitionAudioSourceProvider::ProvideInput( | |
145 media::AudioBus* audio_bus, base::TimeDelta buffer_delay) { | |
146 DCHECK(capture_thread_checker_.CalledOnValidThread()); | |
147 DCHECK_GE(fifo_->frames(), audio_bus->frames()); | |
148 // Consume queued input frames by passing them to |audio_converter_| | |
149 fifo_->Consume(audio_bus, 0, audio_bus->frames()); | |
150 return 1.0; | |
151 } | |
152 | |
153 void SpeechRecognitionAudioSourceProvider::NotifyAudioBusConsumed() { | |
154 // client notifies us previous output buffer was consumed | |
155 base::AutoLock auto_lock(lock_); | |
156 --unconsumed_audio_buffers_; | |
157 } | |
158 | |
159 } // namespace content | |
OLD | NEW |