Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: content/renderer/media/speech_recognition_audio_sink.cc

Issue 499233003: Binding media stream audio track to speech recognition [renderer] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove peer_buffer_index_ Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/media/speech_recognition_audio_sink.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 SpeechRecognitionAudioSink::SpeechRecognitionAudioSink(
16 const blink::WebMediaStreamTrack& track,
17 const media::AudioParameters& params,
18 const base::SharedMemoryHandle memory,
19 scoped_ptr<base::SyncSocket> socket,
20 const OnStoppedCB& on_stopped_cb)
21 : track_(track),
22 shared_memory_(memory, false),
23 socket_(socket.Pass()),
24 output_params_(params),
25 track_stopped_(false),
26 buffer_index_(0),
27 on_stopped_cb_(on_stopped_cb) {
28 DCHECK(socket_.get());
29 DCHECK(main_render_thread_checker_.CalledOnValidThread());
30 DCHECK(params.IsValid());
31 DCHECK(IsSupportedTrack(track));
32 const size_t memory_length = media::AudioBus::CalculateMemorySize(params) +
33 sizeof(media::AudioInputBufferParameters);
34 CHECK(shared_memory_.Map(memory_length));
35
36 media::AudioInputBuffer* buffer =
37 static_cast<media::AudioInputBuffer*>(shared_memory_.memory());
38
39 // The peer must manage their own counter and reset it to 0.
40 DCHECK_EQ(0U, buffer->params.size);
41 output_bus_ = media::AudioBus::WrapMemory(params, buffer->audio);
42
43 // Connect this audio sink to the track
44 MediaStreamAudioSink::AddToAudioTrack(this, track_);
45 }
46
47 SpeechRecognitionAudioSink::~SpeechRecognitionAudioSink() {
48 DCHECK(main_render_thread_checker_.CalledOnValidThread());
49 if (audio_converter_.get())
50 audio_converter_->RemoveInput(this);
51
52 // Notify the track before this sink goes away.
53 if (!track_stopped_)
54 MediaStreamAudioSink::RemoveFromAudioTrack(this, track_);
55 }
56
57 // static
58 bool SpeechRecognitionAudioSink::IsSupportedTrack(
59 const blink::WebMediaStreamTrack& track) {
60 if (track.source().type() != blink::WebMediaStreamSource::TypeAudio)
61 return false;
62
63 MediaStreamAudioSource* native_source =
64 static_cast<MediaStreamAudioSource*>(track.source().extraData());
65 if (!native_source)
66 return false;
67
68 const StreamDeviceInfo& device_info = native_source->device_info();
69 // Purposely only support tracks from an audio device. Dissallow WebAudio.
70 return (device_info.device.type == content::MEDIA_DEVICE_AUDIO_CAPTURE);
71 }
72
73 void SpeechRecognitionAudioSink::OnSetFormat(
74 const media::AudioParameters& input_params) {
75 DCHECK(input_params.IsValid());
76 DCHECK_LE(
77 input_params.frames_per_buffer() * 1000 / input_params.sample_rate(),
78 output_params_.frames_per_buffer() * 1000 / output_params_.sample_rate());
79
80 // Detach the thread here because it will be a new capture thread
81 // calling OnSetFormat() and OnData() if the source is restarted.
82 capture_thread_checker_.DetachFromThread();
83
84 input_params_ = input_params;
85 fifo_buffer_size_ =
86 std::ceil(output_params_.frames_per_buffer() *
87 static_cast<double>(input_params_.sample_rate()) /
88 output_params_.sample_rate());
89 DCHECK_GE(fifo_buffer_size_, input_params_.frames_per_buffer());
90
91 // Allows for some delays on the peer.
92 static const int kNumberOfBuffersInFifo = 2;
93 int frames_in_fifo = kNumberOfBuffersInFifo * fifo_buffer_size_;
94 fifo_.reset(new media::AudioFifo(input_params.channels(), frames_in_fifo));
95 input_bus_ = media::AudioBus::Create(input_params.channels(),
96 input_params.frames_per_buffer());
97
98 // Create the audio converter with |disable_fifo| as false so that the
99 // converter will request input_params.frames_per_buffer() each time.
100 // This will not increase the complexity as there is only one client to
101 // the converter.
102 audio_converter_.reset(
103 new media::AudioConverter(input_params, output_params_, false));
104 audio_converter_->AddInput(this);
105 }
106
107 void SpeechRecognitionAudioSink::OnReadyStateChanged(
108 blink::WebMediaStreamSource::ReadyState state) {
109 DCHECK(main_render_thread_checker_.CalledOnValidThread());
110 DCHECK(!track_stopped_);
111
112 if (state == blink::WebMediaStreamSource::ReadyStateEnded) {
113 track_stopped_ = true;
114
115 if (!on_stopped_cb_.is_null())
116 on_stopped_cb_.Run();
117 }
118 }
119
120 void SpeechRecognitionAudioSink::OnData(const int16* audio_data,
121 int sample_rate,
122 int number_of_channels,
123 int number_of_frames) {
124 DCHECK(capture_thread_checker_.CalledOnValidThread());
125 DCHECK_EQ(input_bus_->frames(), number_of_frames);
126 DCHECK_EQ(input_bus_->channels(), number_of_channels);
127 if (fifo_->frames() + number_of_frames > fifo_->max_frames()) {
128 // This would indicate a serious issue with the browser process or the
129 // SyncSocket and/or SharedMemory. We stop delivering any data to the peer.
130 NOTREACHED() << "Audio FIFO overflow";
131 return;
132 }
133 // TODO(xians): A better way to handle the interleaved and deinterleaved
134 // format switching, see issue/317710.
135 input_bus_->FromInterleaved(audio_data, number_of_frames,
136 sizeof(audio_data[0]));
137
138 fifo_->Push(input_bus_.get());
139 // Wait for FIFO to have at least |fifo_buffer_size_| frames ready.
140 if (fifo_->frames() < fifo_buffer_size_)
141 return;
142
143 // Make sure the previous output buffer was consumed by the peer before we
144 // send the next buffer.
145 // The peer must write to it (incrementing by 1) once the the buffer was
146 // consumed. This is intentional not to block this audio capturing thread.
147 if (buffer_index_ != audio_input_buffer()->params.size) {
148 DLOG(WARNING) << "Buffer synchronization lag";
149 return;
150 }
151
152 audio_converter_->Convert(output_bus_.get());
153
154 // Notify peer to consume buffer |buffer_index_| on |output_bus_|.
155 const size_t bytes_sent =
156 socket_->Send(&buffer_index_, sizeof(buffer_index_));
157 if (bytes_sent != sizeof(buffer_index_)) {
158 // The send ocasionally fails if the user changes their input audio device.
159 DVLOG(1) << "Failed sending buffer index to peer";
160 // We have discarded this buffer, but could still recover on the next one.
161 return;
162 }
163
164 // Count the sent buffer. We expect the peer to do the same on their end.
165 ++buffer_index_;
166 }
167
168 double SpeechRecognitionAudioSink::ProvideInput(
169 media::AudioBus* audio_bus, base::TimeDelta buffer_delay) {
170 DCHECK(capture_thread_checker_.CalledOnValidThread());
171 if (fifo_->frames() >= audio_bus->frames())
172 fifo_->Consume(audio_bus, 0, audio_bus->frames());
173 else
174 audio_bus->Zero();
175
176 // Return volume greater than zero to indicate we have more data.
177 return 1.0;
178 }
179
180 media::AudioInputBuffer*
181 SpeechRecognitionAudioSink::audio_input_buffer() const {
182 DCHECK(capture_thread_checker_.CalledOnValidThread());
183 DCHECK(shared_memory_.memory());
184 return static_cast<media::AudioInputBuffer*>(shared_memory_.memory());
185 }
186
187 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698