| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <windows.h> | 5 #include <windows.h> |
| 6 #include <audioclient.h> | 6 #include <audioclient.h> |
| 7 #include <avrt.h> | 7 #include <avrt.h> |
| 8 #include <mmdeviceapi.h> | 8 #include <mmdeviceapi.h> |
| 9 #include <mmreg.h> | 9 #include <mmreg.h> |
| 10 #include <mmsystem.h> | 10 #include <mmsystem.h> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // AudioCapturer interface. | 46 // AudioCapturer interface. |
| 47 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE; | 47 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE; |
| 48 virtual void Stop() OVERRIDE; | 48 virtual void Stop() OVERRIDE; |
| 49 virtual bool IsRunning() OVERRIDE; | 49 virtual bool IsRunning() OVERRIDE; |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 // Receives all packets from the audio capture endpoint buffer and pushes them | 52 // Receives all packets from the audio capture endpoint buffer and pushes them |
| 53 // to the network. | 53 // to the network. |
| 54 void DoCapture(); | 54 void DoCapture(); |
| 55 | 55 |
| 56 static bool IsPacketOfSilence(const AudioPacket* packet); | 56 static bool IsPacketOfSilence(const int16* samples, int number_of_samples); |
| 57 | 57 |
| 58 PacketCapturedCallback callback_; | 58 PacketCapturedCallback callback_; |
| 59 | 59 |
| 60 AudioPacket::SamplingRate sampling_rate_; | 60 AudioPacket::SamplingRate sampling_rate_; |
| 61 | 61 |
| 62 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_; | 62 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_; |
| 63 base::TimeDelta audio_device_period_; | 63 base::TimeDelta audio_device_period_; |
| 64 | 64 |
| 65 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_; | 65 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_; |
| 66 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; | 66 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 BYTE* data; | 267 BYTE* data; |
| 268 UINT32 frames; | 268 UINT32 frames; |
| 269 DWORD flags; | 269 DWORD flags; |
| 270 hr = audio_capture_client_->GetBuffer( | 270 hr = audio_capture_client_->GetBuffer( |
| 271 &data, &frames, &flags, NULL, NULL); | 271 &data, &frames, &flags, NULL, NULL); |
| 272 if (FAILED(hr)) { | 272 if (FAILED(hr)) { |
| 273 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; | 273 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; |
| 274 return; | 274 return; |
| 275 } | 275 } |
| 276 | 276 |
| 277 scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); | 277 if (!IsPacketOfSilence( |
| 278 packet->set_data(data, frames * wave_format_ex_->nBlockAlign); | 278 reinterpret_cast<const int16*>(data), |
| 279 packet->set_sampling_rate(sampling_rate_); | 279 frames * kChannels)) { |
| 280 packet->set_bytes_per_sample( | 280 scoped_ptr<AudioPacket> packet = |
| 281 static_cast<AudioPacket::BytesPerSample>(sizeof(int16))); | 281 scoped_ptr<AudioPacket>(new AudioPacket()); |
| 282 packet->set_encoding(AudioPacket::ENCODING_RAW); | 282 packet->add_data(data, frames * wave_format_ex_->nBlockAlign); |
| 283 packet->set_sampling_rate(sampling_rate_); |
| 284 packet->set_bytes_per_sample( |
| 285 static_cast<AudioPacket::BytesPerSample>(sizeof(int16))); |
| 286 packet->set_encoding(AudioPacket::ENCODING_RAW); |
| 283 | 287 |
| 284 if (!IsPacketOfSilence(packet.get())) | |
| 285 callback_.Run(packet.Pass()); | 288 callback_.Run(packet.Pass()); |
| 289 } |
| 286 | 290 |
| 287 hr = audio_capture_client_->ReleaseBuffer(frames); | 291 hr = audio_capture_client_->ReleaseBuffer(frames); |
| 288 if (FAILED(hr)) { | 292 if (FAILED(hr)) { |
| 289 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; | 293 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; |
| 290 return; | 294 return; |
| 291 } | 295 } |
| 292 } | 296 } |
| 293 } | 297 } |
| 294 | 298 |
| 295 // Detects whether there is audio playing in a packet of samples. | 299 // Detects whether there is audio playing in a packet of samples. |
| 296 // Windows can give nonzero samples, even when there is no audio playing, so | 300 // Windows can give nonzero samples, even when there is no audio playing, so |
| 297 // extremely low amplitude samples are counted as silence. | 301 // extremely low amplitude samples are counted as silence. |
| 298 bool AudioCapturerWin::IsPacketOfSilence(const AudioPacket* packet) { | 302 bool AudioCapturerWin::IsPacketOfSilence( |
| 299 DCHECK_EQ(static_cast<AudioPacket::BytesPerSample>(sizeof(int16)), | 303 const int16* samples, int number_of_samples) { |
| 300 packet->bytes_per_sample()); | |
| 301 const int16* data = reinterpret_cast<const int16*>(packet->data().data()); | |
| 302 int number_of_samples = packet->data().size() * kBitsPerByte / kBitsPerSample; | |
| 303 | |
| 304 for (int i = 0; i < number_of_samples; i++) { | 304 for (int i = 0; i < number_of_samples; i++) { |
| 305 if (abs(data[i]) > kSilenceThreshold) | 305 if (abs(samples[i]) > kSilenceThreshold) |
| 306 return false; | 306 return false; |
| 307 } | 307 } |
| 308 return true; | 308 return true; |
| 309 } | 309 } |
| 310 | 310 |
| 311 scoped_ptr<AudioCapturer> AudioCapturer::Create() { | 311 scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
| 312 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); | 312 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); |
| 313 } | 313 } |
| 314 | 314 |
| 315 } // namespace remoting | 315 } // namespace remoting |
| OLD | NEW |