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