Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ash / system / win / audio / tray_audio_delegate_win.cc
blob18e46af3bdd968f68c6f17d76367beef154eb7d8
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 ScopedComPtr<ISimpleAudioVolume>
100 TrayAudioDelegateWin::CreateDefaultVolumeControl() {
101 ScopedComPtr<ISimpleAudioVolume> volume_control;
102 ScopedComPtr<IAudioSessionManager> session_manager;
104 ScopedComPtr<IMMDevice> device =
105 media::CoreAudioUtil::CreateDefaultDevice(eRender, eConsole);
106 if (!device.get() ||
107 FAILED(device->Activate(__uuidof(IAudioSessionManager), CLSCTX_ALL, NULL,
108 session_manager.ReceiveVoid()))) {
109 return volume_control;
112 session_manager->GetSimpleAudioVolume(NULL, FALSE,
113 volume_control.Receive());
115 return volume_control;
118 } // namespace system
119 } // namespace ash