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

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

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

Powered by Google App Engine
This is Rietveld 408576698