Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / win / audio / tray_audio_delegate_win.cc
blob2b6095a1fae6bef1574d78433a30f3ee74729f89
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>
8 #include <cmath>
10 #include "media/audio/win/core_audio_util_win.h"
12 using base::win::ScopedComPtr;
14 namespace {
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;
22 } // namespace
24 namespace ash {
25 namespace system {
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())
40 return 0;
42 float level = 0.0f;
43 if (FAILED(volume_control->GetMasterVolume(&level)))
44 return 0;
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);
52 int TrayAudioDelegateWin::GetActiveOutputDeviceIconId() {
53 return kNoAudioDeviceIcon;
56 bool TrayAudioDelegateWin::HasAlternativeSources() {
57 return false;
60 bool TrayAudioDelegateWin::IsOutputAudioMuted() {
61 ScopedComPtr<ISimpleAudioVolume> volume_control =
62 CreateDefaultVolumeControl();
64 if (!volume_control.get())
65 return false;
67 BOOL mute = FALSE;
68 if (FAILED(volume_control->GetMute(&mute)))
69 return false;
71 return !!mute;
74 void TrayAudioDelegateWin::SetOutputAudioIsMuted(bool is_muted) {
75 ScopedComPtr<ISimpleAudioVolume> volume_control =
76 CreateDefaultVolumeControl();
78 if (!volume_control.get())
79 return;
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())
89 return;
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);
110 if (!device.get() ||
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
123 } // namespace ash