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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// 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 "content/browser/media/media_internals.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_ui.h"
#include "media/audio/audio_parameters.h"
#include "media/base/media_log.h"
#include "media/base/media_log_event.h"
namespace {
static base::LazyInstance<content::MediaInternals>::Leaky g_media_internals =
LAZY_INSTANCE_INITIALIZER;
base::string16 SerializeUpdate(const std::string& function,
const base::Value* value) {
return content::WebUI::GetJavascriptCall(
function, std::vector<const base::Value*>(1, value));
}
std::string EffectsToString(int effects) {
if (effects == media::AudioParameters::NO_EFFECTS)
return "NO_EFFECTS";
struct {
int flag;
const char* name;
} flags[] = {
{ media::AudioParameters::ECHO_CANCELLER, "ECHO_CANCELLER" },
{ media::AudioParameters::DUCKING, "DUCKING" },
{ media::AudioParameters::KEYBOARD_MIC, "KEYBOARD_MIC" },
};
std::string ret;
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(flags); ++i) {
if (effects & flags[i].flag) {
if (!ret.empty())
ret += " | ";
ret += flags[i].name;
effects &= ~flags[i].flag;
}
}
if (effects) {
if (!ret.empty())
ret += " | ";
ret += base::IntToString(effects);
}
return ret;
}
const char kAudioLogStatusKey[] = "status";
const char kAudioLogUpdateFunction[] = "media.updateAudioComponent";
} // namespace
namespace content {
class AudioLogImpl : public media::AudioLog {
public:
AudioLogImpl(int owner_id,
media::AudioLogFactory::AudioComponent component,
content::MediaInternals* media_internals);
virtual ~AudioLogImpl();
virtual void OnCreated(int component_id,
const media::AudioParameters& params,
const std::string& device_id) OVERRIDE;
virtual void OnStarted(int component_id) OVERRIDE;
virtual void OnStopped(int component_id) OVERRIDE;
virtual void OnClosed(int component_id) OVERRIDE;
virtual void OnError(int component_id) OVERRIDE;
virtual void OnSetVolume(int component_id, double volume) OVERRIDE;
private:
void SendSingleStringUpdate(int component_id,
const std::string& key,
const std::string& value);
void StoreComponentMetadata(int component_id, base::DictionaryValue* dict);
std::string FormatCacheKey(int component_id);
const int owner_id_;
const media::AudioLogFactory::AudioComponent component_;
content::MediaInternals* const media_internals_;
DISALLOW_COPY_AND_ASSIGN(AudioLogImpl);
};
AudioLogImpl::AudioLogImpl(int owner_id,
media::AudioLogFactory::AudioComponent component,
content::MediaInternals* media_internals)
: owner_id_(owner_id),
component_(component),
media_internals_(media_internals) {}
AudioLogImpl::~AudioLogImpl() {}
void AudioLogImpl::OnCreated(int component_id,
const media::AudioParameters& params,
const std::string& device_id) {
base::DictionaryValue dict;
StoreComponentMetadata(component_id, &dict);
dict.SetString(kAudioLogStatusKey, "created");
dict.SetString("device_id", device_id);
dict.SetInteger("input_channels", params.input_channels());
dict.SetInteger("frames_per_buffer", params.frames_per_buffer());
dict.SetInteger("sample_rate", params.sample_rate());
dict.SetInteger("channels", params.channels());
dict.SetString("channel_layout",
ChannelLayoutToString(params.channel_layout()));
dict.SetString("effects", EffectsToString(params.effects()));
media_internals_->SendUpdateAndCache(
FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
}
void AudioLogImpl::OnStarted(int component_id) {
SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started");
}
void AudioLogImpl::OnStopped(int component_id) {
SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped");
}
void AudioLogImpl::OnClosed(int component_id) {
base::DictionaryValue dict;
StoreComponentMetadata(component_id, &dict);
dict.SetString(kAudioLogStatusKey, "closed");
media_internals_->SendUpdateAndPurgeCache(
FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
}
void AudioLogImpl::OnError(int component_id) {
SendSingleStringUpdate(component_id, "error_occurred", "true");
}
void AudioLogImpl::OnSetVolume(int component_id, double volume) {
base::DictionaryValue dict;
StoreComponentMetadata(component_id, &dict);
dict.SetDouble("volume", volume);
media_internals_->SendUpdateAndCache(
FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
}
std::string AudioLogImpl::FormatCacheKey(int component_id) {
return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id);
}
void AudioLogImpl::SendSingleStringUpdate(int component_id,
const std::string& key,
const std::string& value) {
base::DictionaryValue dict;
StoreComponentMetadata(component_id, &dict);
dict.SetString(key, value);
media_internals_->SendUpdateAndCache(
FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
}
void AudioLogImpl::StoreComponentMetadata(int component_id,
base::DictionaryValue* dict) {
dict->SetInteger("owner_id", owner_id_);
dict->SetInteger("component_id", component_id);
dict->SetInteger("component_type", component_);
}
MediaInternals* MediaInternals::GetInstance() {
return g_media_internals.Pointer();
}
MediaInternals::MediaInternals() : owner_ids_() {}
MediaInternals::~MediaInternals() {}
void MediaInternals::OnMediaEvents(
int render_process_id, const std::vector<media::MediaLogEvent>& events) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Notify observers that |event| has occurred.
for (std::vector<media::MediaLogEvent>::const_iterator event = events.begin();
event != events.end(); ++event) {
base::DictionaryValue dict;
dict.SetInteger("renderer", render_process_id);
dict.SetInteger("player", event->id);
dict.SetString("type", media::MediaLog::EventTypeToString(event->type));
// TODO(dalecurtis): This is technically not correct. TimeTicks "can't" be
// converted to to a human readable time format. See base/time/time.h.
const double ticks = event->time.ToInternalValue();
const double ticks_millis = ticks / base::Time::kMicrosecondsPerMillisecond;
dict.SetDouble("ticksMillis", ticks_millis);
dict.Set("params", event->params.DeepCopy());
SendUpdate(SerializeUpdate("media.onMediaEvent", &dict));
}
}
void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
update_callbacks_.push_back(callback);
}
void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
for (size_t i = 0; i < update_callbacks_.size(); ++i) {
if (update_callbacks_[i].Equals(callback)) {
update_callbacks_.erase(update_callbacks_.begin() + i);
return;
}
}
NOTREACHED();
}
void MediaInternals::SendEverything() {
base::string16 everything_update;
{
base::AutoLock auto_lock(lock_);
everything_update = SerializeUpdate(
"media.onReceiveEverything", &cached_data_);
}
SendUpdate(everything_update);
}
void MediaInternals::SendUpdate(const base::string16& update) {
// SendUpdate() may be called from any thread, but must run on the IO thread.
// TODO(dalecurtis): This is pretty silly since the update callbacks simply
// forward the calls to the UI thread. We should avoid the extra hop.
if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
&MediaInternals::SendUpdate, base::Unretained(this), update));
return;
}
for (size_t i = 0; i < update_callbacks_.size(); i++)
update_callbacks_[i].Run(update);
}
scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog(
AudioComponent component) {
base::AutoLock auto_lock(lock_);
return scoped_ptr<media::AudioLog>(new AudioLogImpl(
owner_ids_[component]++, component, this));
}
void MediaInternals::SendUpdateAndCache(const std::string& cache_key,
const std::string& function,
const base::DictionaryValue* value) {
SendUpdate(SerializeUpdate(function, value));
base::AutoLock auto_lock(lock_);
if (!cached_data_.HasKey(cache_key)) {
cached_data_.Set(cache_key, value->DeepCopy());
return;
}
base::DictionaryValue* existing_dict = NULL;
CHECK(cached_data_.GetDictionary(cache_key, &existing_dict));
existing_dict->MergeDictionary(value);
}
void MediaInternals::SendUpdateAndPurgeCache(
const std::string& cache_key,
const std::string& function,
const base::DictionaryValue* value) {
SendUpdate(SerializeUpdate(function, value));
base::AutoLock auto_lock(lock_);
scoped_ptr<base::Value> out_value;
CHECK(cached_data_.Remove(cache_key, &out_value));
}
} // namespace content
|