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

Unified Diff: remoting/host/audio_capturer_win.cc

Issue 1753663002: Add volume control for windows host (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Resolve code review comments Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
« remoting/host/audio_capturer_win.h ('K') | « remoting/host/audio_capturer_win.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/host/audio_capturer_win.cc
diff --git a/remoting/host/audio_capturer_win.cc b/remoting/host/audio_capturer_win.cc
index 8c901137945dd58a475a9faca8dfcf36b92ed237..a52cd8f48aae0654a33a9f01ece65d173403bb72 100644
--- a/remoting/host/audio_capturer_win.cc
+++ b/remoting/host/audio_capturer_win.cc
@@ -54,6 +54,7 @@ bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) {
DCHECK(!audio_capture_client_.get());
DCHECK(!audio_client_.get());
DCHECK(!mm_device_.get());
+ DCHECK(!audio_volume_.get());
DCHECK(static_cast<PWAVEFORMATEX>(wave_format_ex_) == nullptr);
DCHECK(thread_checker_.CalledOnValidThread());
@@ -193,6 +194,15 @@ bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) {
return false;
}
+ // Initialize ISimpleAudioVolume.
+ // TODO(zijiehe): Do we need to control per process volume?
+ hr = audio_client_->GetService(__uuidof(ISimpleAudioVolume),
+ audio_volume_.ReceiveVoid());
+ if (FAILED(hr)) {
+ LOG(ERROR) << "Failed to get an ISimpleAudioVolume. Error " << hr;
+ return false;
+ }
+
silence_detector_.Reset(sampling_rate_, kChannels);
// Start capturing.
@@ -203,6 +213,63 @@ bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) {
return true;
}
+float AudioCapturerWin::GetAudioLevel() {
+ BOOL mute;
+ HRESULT hr = audio_volume_->GetMute(&mute);
+ if (FAILED(hr)) {
+ return 1;
joedow 2016/03/04 16:46:10 Is there an advantage to returning an int here ins
Sergey Ulanov 2016/03/04 21:30:03 FWIW IMO it makes no difference. Compiler will gen
Hzj_jie 2016/03/09 19:47:57 Done.
+ } else if (mute) {
Sergey Ulanov 2016/03/04 21:30:03 don't need this else. See https://www.chromium.org
Hzj_jie 2016/03/09 19:47:57 Done.
+ return 0;
+ }
joedow 2016/03/04 16:46:10 nit: add a newline here to break up the mute and v
Hzj_jie 2016/03/09 19:47:57 Done.
+ float level;
+ hr = audio_volume_->GetMasterVolume(&level);
+ if (FAILED(hr) || level > 1) {
+ return 1;
+ } else if (level < 0) {
+ return 0;
+ } else {
Sergey Ulanov 2016/03/04 21:30:03 don't need this else
Hzj_jie 2016/03/09 19:47:57 Done.
+ return level;
+ }
+}
+
+void AudioCapturerWin::ProcessSamples(BYTE* data, UINT32 frames, DWORD flags) {
+ if (frames == 0) {
+ return;
+ }
+
+ if ((flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0) {
+ return;
+ }
+
+ float level = GetAudioLevel();
+ if (level == 0) {
joedow 2016/03/04 16:46:10 Are there problems here if the level is 0.01 or 0.
Sergey Ulanov 2016/03/04 21:30:03 Compiler is required to convert ints to floats for
Hzj_jie 2016/03/09 19:47:57 I believe what Joe mentioned is the string display
+ return;
+ }
+
+ int16_t* samples = reinterpret_cast<int16_t*>(data);
+ static_assert(sizeof(samples[0]) == kBytesPerSample,
+ "expect 16 bits per sample");
+ size_t sample_count = frames * kChannels;
+ if (level < 1) {
+ // Windows API does not provide volume adjusted audio sample as Linux does.
+ // So we need to manually append volume signal to the samples.
+ int32_t level_int = static_cast<int32_t>(level * 65536);
+ for (size_t i = 0; i < sample_count; i++) {
+ samples[i] = (static_cast<int32_t>(samples[i]) * level_int) >> 16;
+ }
+ }
+ if (!silence_detector_.IsSilence(samples, sample_count)) {
Sergey Ulanov 2016/03/04 21:30:03 this can be reformatted as follows if (silence_de
Hzj_jie 2016/03/09 19:47:57 Done.
+ scoped_ptr<AudioPacket> packet(new AudioPacket());
+ packet->add_data(data, frames * wave_format_ex_->nBlockAlign);
+ packet->set_encoding(AudioPacket::ENCODING_RAW);
+ packet->set_sampling_rate(sampling_rate_);
+ packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);
+ packet->set_channels(AudioPacket::CHANNELS_STEREO);
+
+ callback_.Run(std::move(packet));
+ }
+}
+
void AudioCapturerWin::DoCapture() {
DCHECK(AudioCapturer::IsValidSampleRate(sampling_rate_));
DCHECK(thread_checker_.CalledOnValidThread());
@@ -227,18 +294,7 @@ void AudioCapturerWin::DoCapture() {
if (FAILED(hr))
break;
- if ((flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0 &&
- !silence_detector_.IsSilence(reinterpret_cast<const int16_t*>(data),
- frames * kChannels)) {
- scoped_ptr<AudioPacket> packet(new AudioPacket());
- packet->add_data(data, frames * wave_format_ex_->nBlockAlign);
- packet->set_encoding(AudioPacket::ENCODING_RAW);
- packet->set_sampling_rate(sampling_rate_);
- packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);
- packet->set_channels(AudioPacket::CHANNELS_STEREO);
-
- callback_.Run(std::move(packet));
- }
+ ProcessSamples(data, frames, flags);
hr = audio_capture_client_->ReleaseBuffer(frames);
if (FAILED(hr))
« remoting/host/audio_capturer_win.h ('K') | « remoting/host/audio_capturer_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698