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> |
11 | 11 |
12 #include <stdlib.h> | |
13 | |
12 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
16 #include "base/timer.h" | 18 #include "base/timer.h" |
17 #include "base/win/scoped_co_mem.h" | 19 #include "base/win/scoped_co_mem.h" |
18 #include "base/win/scoped_com_initializer.h" | 20 #include "base/win/scoped_com_initializer.h" |
19 #include "base/win/scoped_comptr.h" | 21 #include "base/win/scoped_comptr.h" |
20 #include "remoting/host/audio_capturer.h" | 22 #include "remoting/host/audio_capturer.h" |
21 #include "remoting/proto/audio.pb.h" | 23 #include "remoting/proto/audio.pb.h" |
22 | 24 |
23 namespace { | 25 namespace { |
24 const int kChannels = 2; | 26 const int kChannels = 2; |
25 const int kBitsPerSample = 16; | 27 const int kBitsPerSample = 16; |
26 const int kBitsPerByte = 8; | 28 const int kBitsPerByte = 8; |
27 // Conversion factor from 100ns to 1ms. | 29 // Conversion factor from 100ns to 1ms. |
28 const int kHnsToMs = 10000; | 30 const int kHnsToMs = 10000; |
31 | |
32 // Tolerance for catching packets of silence. If all samples have absolute | |
33 // value less than this threshold, the packet will be counted as a packet of | |
34 // silence. A value of 2 was chosen, because Windows can give samples of 1 and | |
35 // -1, even when no audio is playing. | |
Wez
2012/08/07 18:01:37
This doesn't sound right; a packet of frames all o
| |
36 const int kSilenceThreshold = 2; | |
29 } // namespace | 37 } // namespace |
30 | 38 |
31 namespace remoting { | 39 namespace remoting { |
32 | 40 |
33 class AudioCapturerWin : public AudioCapturer { | 41 class AudioCapturerWin : public AudioCapturer { |
34 public: | 42 public: |
35 AudioCapturerWin(); | 43 AudioCapturerWin(); |
36 virtual ~AudioCapturerWin(); | 44 virtual ~AudioCapturerWin(); |
37 | 45 |
38 // AudioCapturer interface. | 46 // AudioCapturer interface. |
39 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE; | 47 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE; |
40 virtual void Stop() OVERRIDE; | 48 virtual void Stop() OVERRIDE; |
41 virtual bool IsRunning() OVERRIDE; | 49 virtual bool IsRunning() OVERRIDE; |
42 | 50 |
43 private: | 51 private: |
44 // 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 |
45 // to the network. | 53 // to the network. |
46 void DoCapture(); | 54 void DoCapture(); |
47 | 55 |
56 static bool IsPacketOfSilence(const AudioPacket* packet); | |
57 | |
48 PacketCapturedCallback callback_; | 58 PacketCapturedCallback callback_; |
49 | 59 |
50 AudioPacket::SamplingRate sampling_rate_; | 60 AudioPacket::SamplingRate sampling_rate_; |
51 | 61 |
52 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_; | 62 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_; |
53 base::TimeDelta audio_device_period_; | 63 base::TimeDelta audio_device_period_; |
54 | 64 |
55 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_; | 65 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_; |
56 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; | 66 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; |
57 base::win::ScopedComPtr<IAudioClient> audio_client_; | 67 base::win::ScopedComPtr<IAudioClient> audio_client_; |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 hr = audio_capture_client_->GetBuffer( | 270 hr = audio_capture_client_->GetBuffer( |
261 &data, &frames, &flags, NULL, NULL); | 271 &data, &frames, &flags, NULL, NULL); |
262 if (FAILED(hr)) { | 272 if (FAILED(hr)) { |
263 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; | 273 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; |
264 return; | 274 return; |
265 } | 275 } |
266 | 276 |
267 scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); | 277 scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); |
268 packet->set_data(data, frames * wave_format_ex_->nBlockAlign); | 278 packet->set_data(data, frames * wave_format_ex_->nBlockAlign); |
269 packet->set_sampling_rate(sampling_rate_); | 279 packet->set_sampling_rate(sampling_rate_); |
280 packet->set_bytes_per_sample( | |
281 static_cast<AudioPacket::BytesPerSample>(sizeof(int16))); | |
270 | 282 |
271 callback_.Run(packet.Pass()); | 283 if (!IsPacketOfSilence(packet.get())) |
284 callback_.Run(packet.Pass()); | |
272 | 285 |
273 hr = audio_capture_client_->ReleaseBuffer(frames); | 286 hr = audio_capture_client_->ReleaseBuffer(frames); |
274 if (FAILED(hr)) { | 287 if (FAILED(hr)) { |
275 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; | 288 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; |
276 return; | 289 return; |
277 } | 290 } |
278 } | 291 } |
279 } | 292 } |
280 | 293 |
294 // Detects whether there is audio playing in a packet of samples. | |
295 // Windows can give nonzero samples, even when there is no audio playing, so | |
296 // extremely low amplitude samples are counted as silence. | |
297 bool AudioCapturerWin::IsPacketOfSilence(const AudioPacket* packet) { | |
298 DCHECK_EQ(static_cast<AudioPacket::BytesPerSample>(sizeof(int16)), | |
299 packet->bytes_per_sample()); | |
300 const int16* data = reinterpret_cast<const int16*>(packet->data().data()); | |
301 int number_of_samples = packet->data().size() * kBitsPerByte / kBitsPerSample; | |
302 | |
303 for (int i = 0; i < number_of_samples; i++) { | |
304 if (abs(data[i]) > kSilenceThreshold) | |
305 return false; | |
306 } | |
307 return true; | |
308 } | |
309 | |
281 scoped_ptr<AudioCapturer> AudioCapturer::Create() { | 310 scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
282 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); | 311 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); |
283 } | 312 } |
284 | 313 |
285 } // namespace remoting | 314 } // namespace remoting |
OLD | NEW |