| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/speech_recognition_audio_sink.h" | 5 #include "content/renderer/media/speech_recognition_audio_sink.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "content/renderer/media/media_stream_audio_source.h" | 10 #include "content/renderer/media/media_stream_audio_source.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 shared_memory_(memory, false), | 23 shared_memory_(memory, false), |
| 24 socket_(socket.Pass()), | 24 socket_(socket.Pass()), |
| 25 output_params_(params), | 25 output_params_(params), |
| 26 track_stopped_(false), | 26 track_stopped_(false), |
| 27 buffer_index_(0), | 27 buffer_index_(0), |
| 28 on_stopped_cb_(on_stopped_cb) { | 28 on_stopped_cb_(on_stopped_cb) { |
| 29 DCHECK(socket_.get()); | 29 DCHECK(socket_.get()); |
| 30 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 30 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 31 DCHECK(params.IsValid()); | 31 DCHECK(params.IsValid()); |
| 32 DCHECK(IsSupportedTrack(track)); | 32 DCHECK(IsSupportedTrack(track)); |
| 33 const size_t memory_length = media::AudioBus::CalculateMemorySize(params) + | 33 const size_t kSharedMemorySize = sizeof(media::AudioInputBufferParameters) + |
| 34 sizeof(media::AudioInputBufferParameters); | 34 media::AudioBus::CalculateMemorySize(params); |
| 35 CHECK(shared_memory_.Map(memory_length)); | 35 CHECK(shared_memory_.Map(kSharedMemorySize)); |
| 36 | 36 |
| 37 media::AudioInputBuffer* buffer = | 37 media::AudioInputBuffer* buffer = |
| 38 static_cast<media::AudioInputBuffer*>(shared_memory_.memory()); | 38 static_cast<media::AudioInputBuffer*>(shared_memory_.memory()); |
| 39 | 39 |
| 40 // The peer must manage their own counter and reset it to 0. | 40 // The peer must manage their own counter and reset it to 0. |
| 41 DCHECK_EQ(0U, buffer->params.size); | 41 DCHECK_EQ(0U, buffer->params.size); |
| 42 output_bus_ = media::AudioBus::WrapMemory(params, buffer->audio); | 42 output_bus_ = media::AudioBus::WrapMemory(params, buffer->audio); |
| 43 | 43 |
| 44 // Connect this audio sink to the track | 44 // Connect this audio sink to the track |
| 45 MediaStreamAudioSink::AddToAudioTrack(this, track_); | 45 MediaStreamAudioSink::AddToAudioTrack(this, track_); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 120 |
| 121 void SpeechRecognitionAudioSink::OnData(const int16* audio_data, | 121 void SpeechRecognitionAudioSink::OnData(const int16* audio_data, |
| 122 int sample_rate, | 122 int sample_rate, |
| 123 int number_of_channels, | 123 int number_of_channels, |
| 124 int number_of_frames) { | 124 int number_of_frames) { |
| 125 DCHECK(capture_thread_checker_.CalledOnValidThread()); | 125 DCHECK(capture_thread_checker_.CalledOnValidThread()); |
| 126 DCHECK_EQ(input_bus_->frames(), number_of_frames); | 126 DCHECK_EQ(input_bus_->frames(), number_of_frames); |
| 127 DCHECK_EQ(input_bus_->channels(), number_of_channels); | 127 DCHECK_EQ(input_bus_->channels(), number_of_channels); |
| 128 if (fifo_->frames() + number_of_frames > fifo_->max_frames()) { | 128 if (fifo_->frames() + number_of_frames > fifo_->max_frames()) { |
| 129 // This would indicate a serious issue with the browser process or the | 129 // This would indicate a serious issue with the browser process or the |
| 130 // SyncSocket and/or SharedMemory. We stop delivering any data to the peer. | 130 // SyncSocket and/or SharedMemory. We drop any previous buffers and try to |
| 131 NOTREACHED() << "Audio FIFO overflow"; | 131 // recover by resuming where the peer left of. |
| 132 return; | 132 DLOG(ERROR) << "Audio FIFO overflow"; |
| 133 fifo_->Clear(); |
| 134 buffer_index_ = GetAudioInputBuffer()->params.size; |
| 133 } | 135 } |
| 134 // TODO(xians): A better way to handle the interleaved and deinterleaved | 136 // TODO(xians): A better way to handle the interleaved and deinterleaved |
| 135 // format switching, see issue/317710. | 137 // format switching, see issue/317710. |
| 136 input_bus_->FromInterleaved(audio_data, number_of_frames, | 138 input_bus_->FromInterleaved(audio_data, number_of_frames, |
| 137 sizeof(audio_data[0])); | 139 sizeof(audio_data[0])); |
| 138 | 140 |
| 139 fifo_->Push(input_bus_.get()); | 141 fifo_->Push(input_bus_.get()); |
| 140 // Wait for FIFO to have at least |fifo_buffer_size_| frames ready. | 142 // Wait for FIFO to have at least |fifo_buffer_size_| frames ready. |
| 141 if (fifo_->frames() < fifo_buffer_size_) | 143 if (fifo_->frames() < fifo_buffer_size_) |
| 142 return; | 144 return; |
| 143 | 145 |
| 144 // Make sure the previous output buffer was consumed by the peer before we | 146 // Make sure the previous output buffer was consumed by the peer before we |
| 145 // send the next buffer. | 147 // send the next buffer. |
| 146 // The peer must write to it (incrementing by 1) once the the buffer was | 148 // The peer must write to it (incrementing by 1) once the the buffer was |
| 147 // consumed. This is intentional not to block this audio capturing thread. | 149 // consumed. This is intentional not to block this audio capturing thread. |
| 148 if (buffer_index_ != GetAudioInputBuffer()->params.size) { | 150 if (buffer_index_ != GetAudioInputBuffer()->params.size) { |
| 149 DLOG(WARNING) << "Buffer synchronization lag"; | 151 DVLOG(1) << "Buffer synchronization lag"; |
| 150 return; | 152 return; |
| 151 } | 153 } |
| 152 | 154 |
| 153 audio_converter_->Convert(output_bus_.get()); | 155 audio_converter_->Convert(output_bus_.get()); |
| 154 | 156 |
| 155 // Notify peer to consume buffer |buffer_index_| on |output_bus_|. | 157 // Notify peer to consume buffer |buffer_index_| on |output_bus_|. |
| 156 const size_t bytes_sent = | 158 const size_t bytes_sent = |
| 157 socket_->Send(&buffer_index_, sizeof(buffer_index_)); | 159 socket_->Send(&buffer_index_, sizeof(buffer_index_)); |
| 158 if (bytes_sent != sizeof(buffer_index_)) { | 160 if (bytes_sent != sizeof(buffer_index_)) { |
| 159 // The send ocasionally fails if the user changes their input audio device. | 161 // The send ocasionally fails if the user changes their input audio device. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 179 } | 181 } |
| 180 | 182 |
| 181 media::AudioInputBuffer* | 183 media::AudioInputBuffer* |
| 182 SpeechRecognitionAudioSink::GetAudioInputBuffer() const { | 184 SpeechRecognitionAudioSink::GetAudioInputBuffer() const { |
| 183 DCHECK(capture_thread_checker_.CalledOnValidThread()); | 185 DCHECK(capture_thread_checker_.CalledOnValidThread()); |
| 184 DCHECK(shared_memory_.memory()); | 186 DCHECK(shared_memory_.memory()); |
| 185 return static_cast<media::AudioInputBuffer*>(shared_memory_.memory()); | 187 return static_cast<media::AudioInputBuffer*>(shared_memory_.memory()); |
| 186 } | 188 } |
| 187 | 189 |
| 188 } // namespace content | 190 } // namespace content |
| OLD | NEW |