1 // Copyright (c) 2012 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 "chrome/browser/ui/ash/volume_controller_chromeos.h"
7 #include "ash/ash_switches.h"
8 #include "ash/audio/sounds.h"
9 #include "base/command_line.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/api/system_private/system_private_api.h"
12 #include "chromeos/audio/chromeos_sounds.h"
13 #include "chromeos/chromeos_switches.h"
14 #include "content/public/browser/user_metrics.h"
15 #include "grit/browser_resources.h"
16 #include "media/audio/sounds/sounds_manager.h"
17 #include "ui/base/resource/resource_bundle.h"
19 using chromeos::CrasAudioHandler
;
23 // Percent by which the volume should be changed when a volume key is pressed.
24 const double kStepPercentage
= 4.0;
26 bool VolumeAdjustSoundEnabled() {
27 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
28 chromeos::switches::kDisableVolumeAdjustSound
);
31 void PlayVolumeAdjustSound() {
32 if (VolumeAdjustSoundEnabled())
33 ash::PlaySystemSoundIfSpokenFeedback(chromeos::SOUND_VOLUME_ADJUST
);
38 VolumeController::VolumeController() {
39 CrasAudioHandler::Get()->AddAudioObserver(this);
40 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
41 if (VolumeAdjustSoundEnabled()) {
42 media::SoundsManager::Get()->Initialize(
43 chromeos::SOUND_VOLUME_ADJUST
,
44 bundle
.GetRawDataResource(IDR_SOUND_VOLUME_ADJUST_WAV
));
48 VolumeController::~VolumeController() {
49 if (CrasAudioHandler::IsInitialized())
50 CrasAudioHandler::Get()->RemoveAudioObserver(this);
53 void VolumeController::HandleVolumeMute(const ui::Accelerator
& accelerator
) {
54 if (accelerator
.key_code() == ui::VKEY_VOLUME_MUTE
)
55 content::RecordAction(base::UserMetricsAction("Accel_VolumeMute_F8"));
57 CrasAudioHandler::Get()->SetOutputMute(true);
60 void VolumeController::HandleVolumeDown(const ui::Accelerator
& accelerator
) {
61 if (accelerator
.key_code() == ui::VKEY_VOLUME_DOWN
)
62 content::RecordAction(base::UserMetricsAction("Accel_VolumeDown_F9"));
64 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
65 if (audio_handler
->IsOutputMuted()) {
66 audio_handler
->SetOutputVolumePercent(0);
68 audio_handler
->AdjustOutputVolumeByPercent(-kStepPercentage
);
69 if (audio_handler
->IsOutputVolumeBelowDefaultMuteLevel())
70 audio_handler
->SetOutputMute(true);
72 PlayVolumeAdjustSound();
76 void VolumeController::HandleVolumeUp(const ui::Accelerator
& accelerator
) {
77 if (accelerator
.key_code() == ui::VKEY_VOLUME_UP
)
78 content::RecordAction(base::UserMetricsAction("Accel_VolumeUp_F10"));
80 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
81 bool play_sound
= false;
82 if (audio_handler
->IsOutputMuted()) {
83 audio_handler
->SetOutputMute(false);
84 audio_handler
->AdjustOutputVolumeToAudibleLevel();
87 play_sound
= audio_handler
->GetOutputVolumePercent() != 100;
88 audio_handler
->AdjustOutputVolumeByPercent(kStepPercentage
);
92 PlayVolumeAdjustSound();
95 void VolumeController::OnOutputNodeVolumeChanged(uint64_t node_id
, int volume
) {
96 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
97 extensions::DispatchVolumeChangedEvent(
98 audio_handler
->GetOutputVolumePercent(),
99 audio_handler
->IsOutputMuted());
102 void VolumeController::OnOutputMuteChanged(bool /* mute_on */,
103 bool /* system_adjust */) {
104 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
105 extensions::DispatchVolumeChangedEvent(
106 audio_handler
->GetOutputVolumePercent(),
107 audio_handler
->IsOutputMuted());