OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/audio/audio_mixer_cras.h" |
| 6 |
| 7 #include <cmath> |
| 8 #include <cras_client.h> |
| 9 #include <unistd.h> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/message_loop.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "base/threading/thread_restrictions.h" |
| 17 #include "chrome/browser/extensions/extension_tts_api_chromeos.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 |
| 20 using content::BrowserThread; |
| 21 |
| 22 namespace chromeos { |
| 23 |
| 24 namespace { |
| 25 |
| 26 // Default volume as a percentage in the range [0.0, 100.0]. |
| 27 const double kDefaultVolumePercent = 75.0; |
| 28 |
| 29 // Number of seconds that we'll sleep between each connection attempt. |
| 30 const int kConnectionRetrySleepSec = 1; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 AudioMixerCras::AudioMixerCras() |
| 35 : client_(NULL), |
| 36 client_connected_(false), |
| 37 volume_percent_(kDefaultVolumePercent), |
| 38 is_muted_(false), |
| 39 apply_is_pending_(true) { |
| 40 } |
| 41 |
| 42 AudioMixerCras::~AudioMixerCras() { |
| 43 if (!thread_.get()) |
| 44 return; |
| 45 DCHECK(MessageLoop::current() != thread_->message_loop()); |
| 46 |
| 47 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; |
| 48 thread_->Stop(); |
| 49 thread_.reset(); |
| 50 |
| 51 cras_client_destroy(client_); |
| 52 } |
| 53 |
| 54 void AudioMixerCras::Init() { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 56 DCHECK(!thread_.get()) << "Init() called twice"; |
| 57 |
| 58 thread_.reset(new base::Thread("AudioMixerCras")); |
| 59 CHECK(thread_->Start()); |
| 60 thread_->message_loop()->PostTask( |
| 61 FROM_HERE, base::Bind(&AudioMixerCras::Connect, base::Unretained(this))); |
| 62 } |
| 63 |
| 64 double AudioMixerCras::GetVolumePercent() { |
| 65 base::AutoLock lock(lock_); |
| 66 return volume_percent_; |
| 67 } |
| 68 |
| 69 void AudioMixerCras::SetVolumePercent(double percent) { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 |
| 72 if (isnan(percent)) |
| 73 percent = 0.0; |
| 74 percent = std::max(std::min(percent, 100.0), 0.0); |
| 75 |
| 76 base::AutoLock lock(lock_); |
| 77 volume_percent_ = percent; |
| 78 if (client_connected_ && !apply_is_pending_) |
| 79 thread_->message_loop()->PostTask(FROM_HERE, |
| 80 base::Bind(&AudioMixerCras::ApplyState, base::Unretained(this))); |
| 81 } |
| 82 |
| 83 bool AudioMixerCras::IsMuted() { |
| 84 base::AutoLock lock(lock_); |
| 85 return is_muted_; |
| 86 } |
| 87 |
| 88 void AudioMixerCras::SetMuted(bool muted) { |
| 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 90 base::AutoLock lock(lock_); |
| 91 is_muted_ = muted; |
| 92 if (client_connected_ && !apply_is_pending_) |
| 93 thread_->message_loop()->PostTask(FROM_HERE, |
| 94 base::Bind(&AudioMixerCras::ApplyState, base::Unretained(this))); |
| 95 } |
| 96 |
| 97 void AudioMixerCras::Connect() { |
| 98 DCHECK(MessageLoop::current() == thread_->message_loop()); |
| 99 |
| 100 // Create the client structure. |
| 101 if (client_ == NULL && cras_client_create(&client_) < 0) { |
| 102 LOG(DFATAL) << "cras_client_create() failed"; |
| 103 return; // TODO(dgreid) change interface so this can return an error. |
| 104 } |
| 105 |
| 106 if (cras_client_connect(client_) != 0) { |
| 107 thread_->message_loop()->PostDelayedTask(FROM_HERE, |
| 108 base::Bind(&AudioMixerCras::Connect, base::Unretained(this)), |
| 109 kConnectionRetrySleepSec * 1000); |
| 110 return; |
| 111 } |
| 112 client_connected_ = true; |
| 113 |
| 114 // The speech synthesis service shouldn't be initialized until after |
| 115 // we get to this point, so we call this function to tell it that it's |
| 116 // safe to do TTS now. NotificationService would be cleaner, |
| 117 // but it's not available at this point. |
| 118 EnableChromeOsTts(); |
| 119 |
| 120 ApplyState(); |
| 121 } |
| 122 |
| 123 void AudioMixerCras::ApplyState() { |
| 124 DCHECK(MessageLoop::current() == thread_->message_loop()); |
| 125 if (!client_connected_) |
| 126 return; |
| 127 |
| 128 bool should_mute = false; |
| 129 size_t new_volume = 0; |
| 130 { |
| 131 base::AutoLock lock(lock_); |
| 132 should_mute = is_muted_; |
| 133 new_volume = floor(volume_percent_ + 0.5); |
| 134 apply_is_pending_ = false; |
| 135 } |
| 136 |
| 137 // If muting mute before setting volume, if un-muting set volume first. |
| 138 if (should_mute) { |
| 139 cras_client_set_system_mute(client_, should_mute); |
| 140 cras_client_set_system_volume(client_, new_volume); |
| 141 } else { |
| 142 cras_client_set_system_volume(client_, new_volume); |
| 143 cras_client_set_system_mute(client_, should_mute); |
| 144 } |
| 145 } |
| 146 |
| 147 } // namespace chromeos |
OLD | NEW |