| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/audio/audio_mixer_cras.h" | 5 #include "chrome/browser/chromeos/audio/audio_mixer_cras.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <cras_client.h> | 8 #include <cras_client.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 15 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 16 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
| 17 #include "chrome/browser/speech/extension_api/tts_extension_api_chromeos.h" | |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 | 19 |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 namespace chromeos { | 22 namespace chromeos { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Default volume as a percentage in the range [0.0, 100.0]. | 26 // Default volume as a percentage in the range [0.0, 100.0]. |
| 27 const double kDefaultVolumePercent = 75.0; | 27 const double kDefaultVolumePercent = 75.0; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 47 return; | 47 return; |
| 48 DCHECK(MessageLoop::current() != thread_->message_loop()); | 48 DCHECK(MessageLoop::current() != thread_->message_loop()); |
| 49 | 49 |
| 50 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; | 50 base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; |
| 51 thread_->Stop(); | 51 thread_->Stop(); |
| 52 thread_.reset(); | 52 thread_.reset(); |
| 53 | 53 |
| 54 cras_client_destroy(client_); | 54 cras_client_destroy(client_); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void AudioMixerCras::Init() { | 57 void AudioMixerCras::Init(const base::Closure& on_connected) { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 59 DCHECK(!thread_.get()) << "Init() called twice"; | 59 DCHECK(!thread_.get()) << "Init() called twice"; |
| 60 | 60 |
| 61 thread_.reset(new base::Thread("AudioMixerCras")); | 61 thread_.reset(new base::Thread("AudioMixerCras")); |
| 62 CHECK(thread_->Start()); | 62 CHECK(thread_->Start()); |
| 63 thread_->message_loop()->PostTask( | 63 thread_->message_loop()->PostTask( |
| 64 FROM_HERE, base::Bind(&AudioMixerCras::Connect, base::Unretained(this))); | 64 FROM_HERE, base::Bind(&AudioMixerCras::Connect, |
| 65 base::Unretained(this), |
| 66 on_connected)); |
| 65 } | 67 } |
| 66 | 68 |
| 67 double AudioMixerCras::GetVolumePercent() { | 69 double AudioMixerCras::GetVolumePercent() { |
| 68 base::AutoLock lock(lock_); | 70 base::AutoLock lock(lock_); |
| 69 return volume_percent_; | 71 return volume_percent_; |
| 70 } | 72 } |
| 71 | 73 |
| 72 void AudioMixerCras::SetVolumePercent(double percent) { | 74 void AudioMixerCras::SetVolumePercent(double percent) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 74 | 76 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 return is_capture_mute_locked_; | 134 return is_capture_mute_locked_; |
| 133 } | 135 } |
| 134 | 136 |
| 135 void AudioMixerCras::SetCaptureMuteLocked(bool locked) { | 137 void AudioMixerCras::SetCaptureMuteLocked(bool locked) { |
| 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 137 base::AutoLock lock(lock_); | 139 base::AutoLock lock(lock_); |
| 138 is_capture_mute_locked_ = locked; | 140 is_capture_mute_locked_ = locked; |
| 139 ApplyStateIfNeeded(); | 141 ApplyStateIfNeeded(); |
| 140 } | 142 } |
| 141 | 143 |
| 142 void AudioMixerCras::Connect() { | 144 void AudioMixerCras::Connect(const base::Closure& on_connected) { |
| 143 DCHECK(MessageLoop::current() == thread_->message_loop()); | 145 DCHECK(MessageLoop::current() == thread_->message_loop()); |
| 144 | 146 |
| 145 // Create the client structure. | 147 // Create the client structure. |
| 146 if (client_ == NULL && cras_client_create(&client_) < 0) { | 148 if (client_ == NULL && cras_client_create(&client_) < 0) { |
| 147 LOG(DFATAL) << "cras_client_create() failed"; | 149 LOG(DFATAL) << "cras_client_create() failed"; |
| 148 return; // TODO(dgreid) change interface so this can return an error. | 150 return; // TODO(dgreid) change interface so this can return an error. |
| 149 } | 151 } |
| 150 | 152 |
| 151 if (cras_client_connect(client_) != 0) { | 153 if (cras_client_connect(client_) != 0) { |
| 152 thread_->message_loop()->PostDelayedTask(FROM_HERE, | 154 thread_->message_loop()->PostDelayedTask( |
| 153 base::Bind(&AudioMixerCras::Connect, base::Unretained(this)), | 155 FROM_HERE, |
| 156 base::Bind(&AudioMixerCras::Connect, |
| 157 base::Unretained(this), |
| 158 on_connected), |
| 154 base::TimeDelta::FromSeconds(kConnectionRetrySleepSec)); | 159 base::TimeDelta::FromSeconds(kConnectionRetrySleepSec)); |
| 155 return; | 160 return; |
| 156 } | 161 } |
| 157 client_connected_ = true; | 162 client_connected_ = true; |
| 158 | 163 |
| 159 // The speech synthesis service shouldn't be initialized until after | 164 on_connected.Run(); |
| 160 // we get to this point, so we call this function to tell it that it's | |
| 161 // safe to do TTS now. NotificationService would be cleaner, | |
| 162 // but it's not available at this point. | |
| 163 EnableChromeOsTts(); | |
| 164 | 165 |
| 165 ApplyState(); | 166 ApplyState(); |
| 166 } | 167 } |
| 167 | 168 |
| 168 void AudioMixerCras::ApplyState() { | 169 void AudioMixerCras::ApplyState() { |
| 169 DCHECK(MessageLoop::current() == thread_->message_loop()); | 170 DCHECK(MessageLoop::current() == thread_->message_loop()); |
| 170 if (!client_connected_) | 171 if (!client_connected_) |
| 171 return; | 172 return; |
| 172 | 173 |
| 173 bool should_mute = false; | 174 bool should_mute = false; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 201 void AudioMixerCras::ApplyStateIfNeeded() { | 202 void AudioMixerCras::ApplyStateIfNeeded() { |
| 202 lock_.AssertAcquired(); | 203 lock_.AssertAcquired(); |
| 203 if (client_connected_ && !apply_is_pending_) { | 204 if (client_connected_ && !apply_is_pending_) { |
| 204 thread_->message_loop()->PostTask( | 205 thread_->message_loop()->PostTask( |
| 205 FROM_HERE, | 206 FROM_HERE, |
| 206 base::Bind(&AudioMixerCras::ApplyState, base::Unretained(this))); | 207 base::Bind(&AudioMixerCras::ApplyState, base::Unretained(this))); |
| 207 } | 208 } |
| 208 } | 209 } |
| 209 | 210 |
| 210 } // namespace chromeos | 211 } // namespace chromeos |
| OLD | NEW |