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

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: Unit test nits + ctor comments 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 // Peer's buffer index is accessed via |params.size| in shared memory.
37 uint8* ptr = static_cast<uint8*>(shared_memory_.memory());
38 media::AudioInputBuffer* buffer =
39 reinterpret_cast<media::AudioInputBuffer*>(ptr);
40 peer_buffer_index_ = &(buffer->params.size);
tommi (sloooow) - chröme 2014/10/06 20:30:33 instead of peer_buffer_index_ I'd rather want to u
burnik 2014/10/07 09:01:09 What about: http://crrev.com/526113002 struct Au
no longer working on chromium 2014/10/07 09:46:18 I don't think you should make this CL more complic
burnik 2014/10/07 15:05:48 Done.
41
42 // The peer must manage their own counter and reset it.
43 DCHECK_EQ(0U, *peer_buffer_index_);
44 output_bus_ = media::AudioBus::WrapMemory(params, buffer->audio);
45
46 // Connect this audio sink to the track
47 MediaStreamAudioSink::AddToAudioTrack(this, track_);
48 }
49
50 SpeechRecognitionAudioSink::~SpeechRecognitionAudioSink() {
51 DCHECK(main_render_thread_checker_.CalledOnValidThread());
52 if (audio_converter_.get())
53 audio_converter_->RemoveInput(this);
54
55 // Notify the track before this sink goes away.
56 if (!track_stopped_)
57 MediaStreamAudioSink::RemoveFromAudioTrack(this, track_);
58 }
59
60 // static
61 bool SpeechRecognitionAudioSink::IsSupportedTrack(
62 const blink::WebMediaStreamTrack& track) {
63 if (track.source().type() != blink::WebMediaStreamSource::TypeAudio)
64 return false;
65
66 MediaStreamAudioSource* native_source =
67 static_cast<MediaStreamAudioSource*>(track.source().extraData());
68 if (!native_source)
69 return false;
70
71 const StreamDeviceInfo& device_info = native_source->device_info();
72 // Purposely only support tracks from an audio device. Dissallow WebAudio.
73 return (device_info.device.type == content::MEDIA_DEVICE_AUDIO_CAPTURE);
74 }
75
76 void SpeechRecognitionAudioSink::OnSetFormat(
77 const media::AudioParameters& input_params) {
78 DCHECK(input_params.IsValid());
79 DCHECK_LE(
80 input_params.frames_per_buffer() * 1000 / input_params.sample_rate(),
81 output_params_.frames_per_buffer() * 1000 / output_params_.sample_rate());
82
83 // Detach the thread here because it will be a new capture thread
84 // calling OnSetFormat() and OnData() if the source is restarted.
85 capture_thread_checker_.DetachFromThread();
86
87 input_params_ = input_params;
88 fifo_buffer_size_ =
89 std::ceil(output_params_.frames_per_buffer() *
90 static_cast<double>(input_params_.sample_rate()) /
91 output_params_.sample_rate());
92 DCHECK_GE(fifo_buffer_size_, input_params_.frames_per_buffer());
93
94 // Allows for some delays on the peer.
95 static const int kNumberOfBuffersInFifo = 2;
96 int frames_in_fifo = kNumberOfBuffersInFifo * fifo_buffer_size_;
97 fifo_.reset(new media::AudioFifo(input_params.channels(), frames_in_fifo));
98 input_bus_ = media::AudioBus::Create(input_params.channels(),
99 input_params.frames_per_buffer());
100
101 // Create the audio converter with |disable_fifo| as false so that the
102 // converter will request input_params.frames_per_buffer() each time.
103 // This will not increase the complexity as there is only one client to
104 // the converter.
105 audio_converter_.reset(
106 new media::AudioConverter(input_params, output_params_, false));
107 audio_converter_->AddInput(this);
108 }
109
110 void SpeechRecognitionAudioSink::OnReadyStateChanged(
111 blink::WebMediaStreamSource::ReadyState state) {
112 DCHECK(main_render_thread_checker_.CalledOnValidThread());
113 DCHECK(!track_stopped_);
114
115 if (state == blink::WebMediaStreamSource::ReadyStateEnded) {
116 track_stopped_ = true;
117
118 if (!on_stopped_cb_.is_null())
119 on_stopped_cb_.Run();
120 }
121 }
122
123 void SpeechRecognitionAudioSink::OnData(const int16* audio_data,
124 int sample_rate,
125 int number_of_channels,
126 int number_of_frames) {
127 DCHECK(capture_thread_checker_.CalledOnValidThread());
128 DCHECK(peer_buffer_index_);
129 DCHECK_EQ(input_bus_->frames(), number_of_frames);
130 DCHECK_EQ(input_bus_->channels(), number_of_channels);
131 if (fifo_->frames() + number_of_frames > fifo_->max_frames()) {
132 // This would indicate a serious issue with the browser process or the
133 // SyncSocket and/or SharedMemory. We stop delivering any data to the peer.
134 NOTREACHED() << "Audio FIFO overflow";
135 return;
136 }
137 // TODO(xians): A better way to handle the interleaved and deinterleaved
138 // format switching, see issue/317710.
139 input_bus_->FromInterleaved(audio_data, number_of_frames,
140 sizeof(audio_data[0]));
141
142 fifo_->Push(input_bus_.get());
143 // Wait for FIFO to have at least |fifo_buffer_size_| frames ready.
144 if (fifo_->frames() < fifo_buffer_size_)
145 return;
146
147 // Make sure the previous output buffer was consumed by the peer before we
148 // send the next buffer. |peer_buffer_index_| is pointing to shared memory.
149 // The peer must write to it (incrementing by 1) once the the buffer was
150 // consumed. This is intentional not to block this audio capturing thread.
151 if (buffer_index_ != (*peer_buffer_index_)) {
152 DLOG(WARNING) << "Buffer synchronization lag";
153 return;
154 }
155
156 audio_converter_->Convert(output_bus_.get());
157
158 // Notify peer to consume buffer |buffer_index_| on |output_bus_|.
159 const size_t bytes_sent =
160 socket_->Send(&buffer_index_, sizeof(buffer_index_));
161 if (bytes_sent != sizeof(buffer_index_)) {
162 // The send ocasionally fails if the user changes their input audio device.
163 DVLOG(1) << "Failed sending buffer index to peer";
164 // We have discarded this buffer, but could still recover on the next one.
165 return;
166 }
167
168 // Count the sent buffer. We expect the peer to do the same on their end.
169 ++buffer_index_;
170 }
171
172 double SpeechRecognitionAudioSink::ProvideInput(
173 media::AudioBus* audio_bus, base::TimeDelta buffer_delay) {
174 DCHECK(capture_thread_checker_.CalledOnValidThread());
175 if (fifo_->frames() >= audio_bus->frames())
176 fifo_->Consume(audio_bus, 0, audio_bus->frames());
177 else
178 audio_bus->Zero();
179
180 // Return volume greater than zero to indicate we have more data.
181 return 1.0;
182 }
183
184 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698