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.
5 #include "ash/system/win/audio/tray_audio_delegate_win.h"
7 #include <audiopolicy.h>
10 #include "grit/ash_resources.h"
11 #include "grit/ash_strings.h"
12 #include "media/audio/win/core_audio_util_win.h"
14 using base::win::ScopedComPtr
;
18 // Volume value which should be considered as muted in range [0, 100].
19 const int kMuteThresholdPercent
= 1;
21 // Lowest volume which is considered to be audible in the range [0, 100].
22 const int kDefaultUnmuteVolumePercent
= 4;
29 void TrayAudioDelegateWin::AdjustOutputVolumeToAudibleLevel() {
30 if (GetOutputVolumeLevel() <= kMuteThresholdPercent
)
31 SetOutputVolumeLevel(kDefaultUnmuteVolumePercent
);
34 int TrayAudioDelegateWin::GetOutputDefaultVolumeMuteLevel() {
35 return kMuteThresholdPercent
;
38 int TrayAudioDelegateWin::GetOutputVolumeLevel() {
39 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
40 CreateDefaultVolumeControl();
45 if (FAILED(volume_control
->GetMasterVolume(&level
)))
48 // MSVC prior to 2013 doesn't have a round function. The below code is not
49 // conformant to C99 round(), but since we know that 0 <= level <= 100, it
51 return static_cast<int>(level
+ 0.5);
54 int TrayAudioDelegateWin::GetActiveOutputDeviceIconId() {
55 return kNoAudioDeviceIcon
;
58 bool TrayAudioDelegateWin::HasAlternativeSources() {
62 bool TrayAudioDelegateWin::IsOutputAudioMuted() {
63 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
64 CreateDefaultVolumeControl();
70 if (FAILED(volume_control
->GetMute(&mute
)))
76 void TrayAudioDelegateWin::SetOutputAudioIsMuted(bool is_muted
) {
77 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
78 CreateDefaultVolumeControl();
83 volume_control
->SetMute(is_muted
, NULL
);
86 void TrayAudioDelegateWin::SetOutputVolumeLevel(int level
) {
87 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
88 CreateDefaultVolumeControl();
93 float volume_level
= static_cast<float>(level
) / 100.0f
;
94 volume_control
->SetMasterVolume(volume_level
, NULL
);
97 ScopedComPtr
<ISimpleAudioVolume
>
98 TrayAudioDelegateWin::CreateDefaultVolumeControl() {
99 ScopedComPtr
<ISimpleAudioVolume
> volume_control
;
100 ScopedComPtr
<IAudioSessionManager
> session_manager
;
102 ScopedComPtr
<IMMDevice
> device
=
103 media::CoreAudioUtil::CreateDefaultDevice(eRender
, eConsole
);
105 FAILED(device
->Activate(__uuidof(IAudioSessionManager
), CLSCTX_ALL
, NULL
,
106 session_manager
.ReceiveVoid()))) {
107 return volume_control
;
110 session_manager
->GetSimpleAudioVolume(NULL
, FALSE
,
111 volume_control
.Receive());
113 return volume_control
;
116 } // namespace system