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 "media/audio/win/core_audio_util_win.h"
12 using base::win::ScopedComPtr
;
16 // Volume value which should be considered as muted in range [0, 100].
17 const int kMuteThresholdPercent
= 1;
19 // Lowest volume which is considered to be audible in the range [0, 100].
20 const int kDefaultUnmuteVolumePercent
= 4;
27 void TrayAudioDelegateWin::AdjustOutputVolumeToAudibleLevel() {
28 if (GetOutputVolumeLevel() <= kMuteThresholdPercent
)
29 SetOutputVolumeLevel(kDefaultUnmuteVolumePercent
);
32 int TrayAudioDelegateWin::GetOutputDefaultVolumeMuteLevel() {
33 return kMuteThresholdPercent
;
36 int TrayAudioDelegateWin::GetOutputVolumeLevel() {
37 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
38 CreateDefaultVolumeControl();
39 if (!volume_control
.get())
43 if (FAILED(volume_control
->GetMasterVolume(&level
)))
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
49 return static_cast<int>(level
+ 0.5);
52 int TrayAudioDelegateWin::GetActiveOutputDeviceIconId() {
53 return kNoAudioDeviceIcon
;
56 bool TrayAudioDelegateWin::HasAlternativeSources() {
60 bool TrayAudioDelegateWin::IsOutputAudioMuted() {
61 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
62 CreateDefaultVolumeControl();
64 if (!volume_control
.get())
68 if (FAILED(volume_control
->GetMute(&mute
)))
74 void TrayAudioDelegateWin::SetOutputAudioIsMuted(bool is_muted
) {
75 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
76 CreateDefaultVolumeControl();
78 if (!volume_control
.get())
81 volume_control
->SetMute(is_muted
, NULL
);
84 void TrayAudioDelegateWin::SetOutputVolumeLevel(int level
) {
85 ScopedComPtr
<ISimpleAudioVolume
> volume_control
=
86 CreateDefaultVolumeControl();
88 if (!volume_control
.get())
91 float volume_level
= static_cast<float>(level
) / 100.0f
;
92 volume_control
->SetMasterVolume(volume_level
, NULL
);
95 void TrayAudioDelegateWin::SetInternalSpeakerChannelMode(
96 AudioChannelMode mode
) {
99 void TrayAudioDelegateWin::SetActiveHDMIOutoutRediscoveringIfNecessary(
100 bool force_rediscovering
) {
103 ScopedComPtr
<ISimpleAudioVolume
>
104 TrayAudioDelegateWin::CreateDefaultVolumeControl() {
105 ScopedComPtr
<ISimpleAudioVolume
> volume_control
;
106 ScopedComPtr
<IAudioSessionManager
> session_manager
;
108 ScopedComPtr
<IMMDevice
> device
=
109 media::CoreAudioUtil::CreateDefaultDevice(eRender
, eConsole
);
111 FAILED(device
->Activate(__uuidof(IAudioSessionManager
), CLSCTX_ALL
, NULL
,
112 session_manager
.ReceiveVoid()))) {
113 return volume_control
;
116 session_manager
->GetSimpleAudioVolume(NULL
, FALSE
,
117 volume_control
.Receive());
119 return volume_control
;
122 } // namespace system