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

Side by Side Diff: content/renderer/media/speech_recognition_audio_source_provider.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: Refactoring unit test and source provider, moved to media Created 6 years, 3 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_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, const base::SharedMemoryHandle memory,
18 base::SyncSocket* socket, OnStoppedCB on_stopped_cb)
19 : track_(track),
20 shared_memory_(memory, false),
21 socket_(socket),
22 output_params_(params),
23 track_stopped_(false),
24 buffer_index_(0),
25 on_stopped_cb_(on_stopped_cb) {
26 DCHECK(main_render_thread_checker_.CalledOnValidThread());
27 DCHECK(params.IsValid());
28 DCHECK(IsSupportedTrack(track));
29 const size_t memory_length = media::AudioBus::CalculateMemorySize(params) +
30 sizeof(media::AudioInputBufferParameters);
31 CHECK(shared_memory_.Map(memory_length));
32
33 // Buffer index for sync with client is |params.size| on the shared memory.
34 uint8* ptr = static_cast<uint8*>(shared_memory_.memory());
35 media::AudioInputBuffer* buffer =
36 reinterpret_cast<media::AudioInputBuffer*>(ptr);
37 peer_buffer_index_ = &(buffer->params.size);
38
39 // Client must manage his own counter and reset it.
40 DCHECK_EQ(0U, *peer_buffer_index_);
41 output_bus_ = media::AudioBus::WrapMemory(params, buffer->audio);
42
43 // Connect the source provider to the track as a sink.
44 MediaStreamAudioSink::AddToAudioTrack(this, track_);
45 }
46
47 SpeechRecognitionAudioSourceProvider::~SpeechRecognitionAudioSourceProvider() {
48 DCHECK(main_render_thread_checker_.CalledOnValidThread());
49 if (audio_converter_.get()) audio_converter_->RemoveInput(this);
no longer working on chromium 2014/09/17 15:55:19 nit, new line
burnik 2014/09/18 09:19:35 Done.
50
51 // Notify the track before this sink goes away.
52 if (!track_stopped_)
53 MediaStreamAudioSink::RemoveFromAudioTrack(this, track_);
54 }
55
56 // static
57 bool SpeechRecognitionAudioSourceProvider::IsSupportedTrack(
58 const blink::WebMediaStreamTrack& track) {
59 if (track.source().type() != blink::WebMediaStreamSource::TypeAudio)
60 return false;
61
62 MediaStreamAudioSource* native_source =
63 static_cast<MediaStreamAudioSource*>(track.source().extraData());
64 if (!native_source)
65 return false;
66
67 const StreamDeviceInfo& device_info = native_source->device_info();
68 // Purposely only support tracks from an audio device. Dissallow WebAudio.
69 return (device_info.device.type == content::MEDIA_DEVICE_AUDIO_CAPTURE);
70 }
71
72 void SpeechRecognitionAudioSourceProvider::OnSetFormat(
73 const media::AudioParameters& input_params) {
74 // We need detach the thread here because it will be a new capture thread
75 // calling OnSetFormat() and OnData() if the source is restarted.
76 capture_thread_checker_.DetachFromThread();
77 DCHECK(capture_thread_checker_.CalledOnValidThread());
78 DCHECK(input_params.IsValid());
79
80 input_params_ = input_params;
81 fifo_buffer_size_ = output_params_.frames_per_buffer() *
82 input_params_.sample_rate() /
83 output_params_.sample_rate();
84 DCHECK_GE(fifo_buffer_size_, input_params_.frames_per_buffer());
85 DCHECK_GE(fifo_buffer_size_, output_params_.frames_per_buffer());
86
87 // Allows for some delays on the endpoint client.
88 static const int kNumberOfBuffersInFifo = 2;
89 int frames_in_fifo = kNumberOfBuffersInFifo * fifo_buffer_size_;
90 fifo_.reset(new media::AudioFifo(input_params.channels(), frames_in_fifo));
91 input_bus_ = media::AudioBus::Create(input_params.channels(),
92 input_params.frames_per_buffer());
93
94 // Create the audio converter with |disable_fifo| as false so that the
95 // converter will request input_params.frames_per_buffer() each time.
96 // This will not increase the complexity as there is only one client to
97 // the converter.
98 audio_converter_.reset(
99 new media::AudioConverter(input_params, output_params_, false));
100 audio_converter_->AddInput(this);
101 }
102
103 void SpeechRecognitionAudioSourceProvider::OnReadyStateChanged(
104 blink::WebMediaStreamSource::ReadyState state) {
105 DCHECK(main_render_thread_checker_.CalledOnValidThread());
106 DCHECK(!track_stopped_);
107
108 if (state == blink::WebMediaStreamSource::ReadyStateEnded) {
109 track_stopped_ = true;
110 if (!on_stopped_cb_.is_null())
111 on_stopped_cb_.Run();
112 }
113 }
114
115 void SpeechRecognitionAudioSourceProvider::OnData(const int16* audio_data,
116 int sample_rate,
117 int number_of_channels,
118 int number_of_frames) {
119 DCHECK(capture_thread_checker_.CalledOnValidThread());
120 DCHECK(peer_buffer_index_);
121 DCHECK_EQ(input_bus_->frames(), number_of_frames);
122 DCHECK_EQ(input_bus_->channels(), number_of_channels);
123 if (fifo_->frames() + number_of_frames > fifo_->max_frames()) {
124 NOTREACHED() << "Audio FIFO overflow";
125 return;
126 }
127 // TODO(xians): A better way to handle the interleaved and deinterleaved
128 // format switching, see issue/317710.
129 input_bus_->FromInterleaved(audio_data, number_of_frames,
130 sizeof(audio_data[0]));
131
132 fifo_->Push(input_bus_.get());
133 // Wait for FIFO to have at least |fifo_buffer_size_| frames ready.
134 if (fifo_->frames() < fifo_buffer_size_)
135 return;
136
137 // Make sure the previous output buffer was consumed by client before we send
138 // the next buffer. |peer_buffer_index_| is pointing to shared memory.
139 // The client must write to it (incrementing by 1) once the the buffer was
140 // consumed. This is intentional not to block this audio capturing thread.
141 if (buffer_index_ != (*peer_buffer_index_)) {
142 DLOG(WARNING) << "Buffer synchronization lag";
no longer working on chromium 2014/09/17 15:55:19 hmm, thinking about these code a bit, it seems ris
burnik 2014/09/18 09:19:35 If indices are out of sync, that would indicate th
143 return;
144 }
145
146 audio_converter_->Convert(output_bus_.get());
147
148 // Notify client to consume buffer |buffer_index_| on |output_bus_|.
149 const size_t bytes_sent =
150 socket_->Send(&buffer_index_, sizeof(buffer_index_));
151 if (bytes_sent != sizeof(buffer_index_)) {
152 // The send usually fails if the user changes his input audio device.
153 DVLOG(1) << "Failed sending buffer index to peer";
154 // We have discarded this buffer, but could still recover on the next one.
155 return;
156 }
157
158 // Count the sent buffer. We expect the client to do the same on his end.
159 ++buffer_index_;
160 }
161
162 double SpeechRecognitionAudioSourceProvider::ProvideInput(
163 media::AudioBus* audio_bus, base::TimeDelta buffer_delay) {
164 DCHECK(capture_thread_checker_.CalledOnValidThread());
165 if (fifo_->frames() >= audio_bus->frames())
166 fifo_->Consume(audio_bus, 0, audio_bus->frames());
167 else
168 audio_bus->Zero();
169
170 return 1.0;
171 }
172
173 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698