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

Side by Side Diff: remoting/host/audio_capturer_win.cc

Issue 10823440: Silence detection test for the Windows audio capturer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 8 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « remoting/host/audio_capturer_win.h ('k') | remoting/host/audio_capturer_win_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "remoting/host/audio_capturer_win.h"
6
5 #include <windows.h> 7 #include <windows.h>
6 #include <audioclient.h>
7 #include <avrt.h> 8 #include <avrt.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 <algorithm> 12 #include <algorithm>
13 #include <stdlib.h> 13 #include <stdlib.h>
14 14
15 #include "base/basictypes.h"
16 #include "base/logging.h" 15 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop.h" 16 #include "base/message_loop.h"
Sergey Ulanov 2012/08/21 22:05:46 This is not used anywhere.
kxing 2012/08/21 22:17:08 Done.
19 #include "base/timer.h"
20 #include "base/win/scoped_co_mem.h"
21 #include "base/win/scoped_com_initializer.h"
22 #include "base/win/scoped_comptr.h"
23 #include "remoting/host/audio_capturer.h"
24 #include "remoting/proto/audio.pb.h"
25 17
26 namespace { 18 namespace {
27 const int kChannels = 2; 19 const int kChannels = 2;
28 const int kBitsPerSample = 16; 20 const int kBitsPerSample = 16;
29 const int kBitsPerByte = 8; 21 const int kBitsPerByte = 8;
30 // Conversion factor from 100ns to 1ms. 22 // Conversion factor from 100ns to 1ms.
31 const int k100nsPerMillisecond = 10000; 23 const int k100nsPerMillisecond = 10000;
32 24
33 // Tolerance for catching packets of silence. If all samples have absolute 25 // Tolerance for catching packets of silence. If all samples have absolute
34 // value less than this threshold, the packet will be counted as a packet of 26 // value less than this threshold, the packet will be counted as a packet of
35 // silence. A value of 2 was chosen, because Windows can give samples of 1 and 27 // silence. A value of 2 was chosen, because Windows can give samples of 1 and
36 // -1, even when no audio is playing. 28 // -1, even when no audio is playing.
37 const int kSilenceThreshold = 2; 29 const int kSilenceThreshold = 2;
38 30
39 // Lower bound for timer intervals, in milliseconds. 31 // Lower bound for timer intervals, in milliseconds.
40 const int kMinTimerInterval = 30; 32 const int kMinTimerInterval = 30;
41 33
42 // Upper bound for the timer precision error, in milliseconds. 34 // Upper bound for the timer precision error, in milliseconds.
43 // Timers are supposed to be accurate to 20ms, so we use 30ms to be safe. 35 // Timers are supposed to be accurate to 20ms, so we use 30ms to be safe.
44 const int kMaxExpectedTimerLag = 30; 36 const int kMaxExpectedTimerLag = 30;
45 } // namespace 37 } // namespace
46 38
47 namespace remoting { 39 namespace remoting {
48 40
49 class AudioCapturerWin : public AudioCapturer {
50 public:
51 AudioCapturerWin();
52 virtual ~AudioCapturerWin();
53
54 // AudioCapturer interface.
55 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE;
56 virtual void Stop() OVERRIDE;
57 virtual bool IsRunning() OVERRIDE;
58
59 private:
60 // Receives all packets from the audio capture endpoint buffer and pushes them
61 // to the network.
62 void DoCapture();
63
64 static bool IsPacketOfSilence(const int16* samples, int number_of_samples);
65
66 PacketCapturedCallback callback_;
67
68 AudioPacket::SamplingRate sampling_rate_;
69
70 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_;
71 base::TimeDelta audio_device_period_;
72
73 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_;
74 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_;
75 base::win::ScopedComPtr<IAudioClient> audio_client_;
76 base::win::ScopedComPtr<IMMDevice> mm_device_;
77 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_;
78
79 base::ThreadChecker thread_checker_;
80
81 DISALLOW_COPY_AND_ASSIGN(AudioCapturerWin);
82 };
83
84 AudioCapturerWin::AudioCapturerWin() 41 AudioCapturerWin::AudioCapturerWin()
85 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID) { 42 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID) {
86 thread_checker_.DetachFromThread(); 43 thread_checker_.DetachFromThread();
87 } 44 }
88 45
89 AudioCapturerWin::~AudioCapturerWin() { 46 AudioCapturerWin::~AudioCapturerWin() {
90 } 47 }
91 48
92 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { 49 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) {
93 DCHECK(!audio_capture_client_.get()); 50 DCHECK(!audio_capture_client_.get());
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 if (FAILED(hr)) { 263 if (FAILED(hr)) {
307 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; 264 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr;
308 return; 265 return;
309 } 266 }
310 } 267 }
311 } 268 }
312 269
313 // Detects whether there is audio playing in a packet of samples. 270 // Detects whether there is audio playing in a packet of samples.
314 // Windows can give nonzero samples, even when there is no audio playing, so 271 // Windows can give nonzero samples, even when there is no audio playing, so
315 // extremely low amplitude samples are counted as silence. 272 // extremely low amplitude samples are counted as silence.
273 // static
316 bool AudioCapturerWin::IsPacketOfSilence( 274 bool AudioCapturerWin::IsPacketOfSilence(
317 const int16* samples, int number_of_samples) { 275 const int16* samples, int number_of_samples) {
318 for (int i = 0; i < number_of_samples; i++) { 276 for (int i = 0; i < number_of_samples; i++) {
319 if (abs(samples[i]) > kSilenceThreshold) 277 if (abs(samples[i]) > kSilenceThreshold)
320 return false; 278 return false;
321 } 279 }
322 return true; 280 return true;
323 } 281 }
324 282
325 scoped_ptr<AudioCapturer> AudioCapturer::Create() { 283 scoped_ptr<AudioCapturer> AudioCapturer::Create() {
326 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); 284 return scoped_ptr<AudioCapturer>(new AudioCapturerWin());
327 } 285 }
328 286
329 } // namespace remoting 287 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/audio_capturer_win.h ('k') | remoting/host/audio_capturer_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698