OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/system/win/audio/tray_audio_delegate_win.h" |
| 6 |
| 7 #include <audiopolicy.h> |
| 8 #include <cmath> |
| 9 |
| 10 #include "grit/ash_resources.h" |
| 11 #include "grit/ash_strings.h" |
| 12 #include "media/audio/win/core_audio_util_win.h" |
| 13 |
| 14 using base::win::ScopedComPtr; |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Volume value which should be considered as muted in range [0, 100]. |
| 19 const int kMuteThresholdPercent = 1; |
| 20 |
| 21 // Lowest volume which is considered to be audible in the range [0, 100]. |
| 22 const int kDefaultUnmuteVolumePercent = 4; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 namespace ash { |
| 27 namespace system { |
| 28 |
| 29 void TrayAudioDelegateWin::AdjustOutputVolumeToAudibleLevel() { |
| 30 if (GetOutputVolumeLevel() <= kMuteThresholdPercent) |
| 31 SetOutputVolumeLevel(kDefaultUnmuteVolumePercent); |
| 32 } |
| 33 |
| 34 int TrayAudioDelegateWin::GetOutputDefaultVolumeMuteLevel() { |
| 35 return kMuteThresholdPercent; |
| 36 } |
| 37 |
| 38 int TrayAudioDelegateWin::GetOutputVolumeLevel() { |
| 39 ScopedComPtr<ISimpleAudioVolume> volume_control = |
| 40 CreateDefaultVolumeControl(); |
| 41 |
| 42 float level = 0.0f; |
| 43 if (FAILED(volume_control->GetMasterVolume(&level))) |
| 44 return 0; |
| 45 |
| 46 // MSVC prior to 2013 doesn't have a round function. The below code is not |
| 47 // conformant to C99 round(), but since we know that 0 <= level <= 100, it |
| 48 // should be ok. |
| 49 return static_cast<int>(level + 0.5); |
| 50 } |
| 51 |
| 52 int TrayAudioDelegateWin::GetActiveOutputDeviceIconId() { |
| 53 return kNoAudioDeviceIcon; |
| 54 } |
| 55 |
| 56 bool TrayAudioDelegateWin::HasAlternativeSources() { |
| 57 return false; |
| 58 } |
| 59 |
| 60 bool TrayAudioDelegateWin::IsOutputAudioMuted() { |
| 61 ScopedComPtr<ISimpleAudioVolume> volume_control = |
| 62 CreateDefaultVolumeControl(); |
| 63 |
| 64 BOOL mute = FALSE; |
| 65 if (FAILED(volume_control->GetMute(&mute))) |
| 66 return false; |
| 67 |
| 68 return !!mute; |
| 69 } |
| 70 |
| 71 void TrayAudioDelegateWin::SetOutputAudioIsMuted(bool is_muted) { |
| 72 ScopedComPtr<ISimpleAudioVolume> volume_control = |
| 73 CreateDefaultVolumeControl(); |
| 74 |
| 75 volume_control->SetMute(is_muted, NULL); |
| 76 } |
| 77 |
| 78 void TrayAudioDelegateWin::SetOutputVolumeLevel(int level) { |
| 79 ScopedComPtr<ISimpleAudioVolume> volume_control = |
| 80 CreateDefaultVolumeControl(); |
| 81 |
| 82 float volume_level = static_cast<float>(level) / 100.0f; |
| 83 volume_control->SetMasterVolume(volume_level, NULL); |
| 84 } |
| 85 |
| 86 ScopedComPtr<ISimpleAudioVolume> |
| 87 TrayAudioDelegateWin::CreateDefaultVolumeControl() { |
| 88 ScopedComPtr<ISimpleAudioVolume> volume_control; |
| 89 ScopedComPtr<IAudioSessionManager> session_manager; |
| 90 |
| 91 ScopedComPtr<IMMDevice> device = |
| 92 media::CoreAudioUtil::CreateDefaultDevice(eRender, eConsole); |
| 93 if (FAILED(device->Activate(__uuidof(IAudioSessionManager), CLSCTX_ALL, NULL, |
| 94 session_manager.ReceiveVoid()))) { |
| 95 return volume_control; |
| 96 } |
| 97 |
| 98 session_manager->GetSimpleAudioVolume(NULL, FALSE, |
| 99 volume_control.Receive()); |
| 100 |
| 101 return volume_control; |
| 102 } |
| 103 |
| 104 } // namespace system |
| 105 } // namespace ash |
OLD | NEW |