blob: 9f420e8abaeb51b09fb2108d1b345ac3525dbde8 [file] [log] [blame]
[email protected]11158e2d2013-02-01 02:31:561// 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 "content/browser/media/media_internals.h"
6
[email protected]348fbaac2013-06-11 06:31:517#include "base/strings/string16.h"
8#include "base/strings/stringprintf.h"
[email protected]11158e2d2013-02-01 02:31:569#include "content/public/browser/browser_thread.h"
10#include "content/public/browser/web_ui.h"
[email protected]e2fd1f72013-08-16 00:34:2511#include "media/audio/audio_parameters.h"
[email protected]11158e2d2013-02-01 02:31:5612#include "media/base/media_log.h"
13#include "media/base/media_log_event.h"
14
[email protected]69946cf2013-11-27 00:11:4215namespace {
16
17static base::LazyInstance<content::MediaInternals>::Leaky g_media_internals =
18 LAZY_INSTANCE_INITIALIZER;
19
20string16 SerializeUpdate(const std::string& function,
21 const base::Value* value) {
22 return content::WebUI::GetJavascriptCall(
23 function, std::vector<const base::Value*>(1, value));
24}
25
26const char kAudioLogStatusKey[] = "status";
27const char kAudioLogUpdateFunction[] = "media.updateAudioComponent";
28
29} // namespace
30
[email protected]11158e2d2013-02-01 02:31:5631namespace content {
32
[email protected]69946cf2013-11-27 00:11:4233class AudioLogImpl : public media::AudioLog {
34 public:
35 AudioLogImpl(int owner_id,
36 media::AudioLogFactory::AudioComponent component,
37 content::MediaInternals* media_internals);
38 virtual ~AudioLogImpl();
39
40 virtual void OnCreated(int component_id,
41 const media::AudioParameters& params,
42 const std::string& input_device_id,
43 const std::string& output_device_id) OVERRIDE;
44 virtual void OnStarted(int component_id) OVERRIDE;
45 virtual void OnStopped(int component_id) OVERRIDE;
46 virtual void OnClosed(int component_id) OVERRIDE;
47 virtual void OnError(int component_id) OVERRIDE;
48 virtual void OnSetVolume(int component_id, double volume) OVERRIDE;
49
50 private:
51 void SendSingleStringUpdate(int component_id,
52 const std::string& key,
53 const std::string& value);
54 void StoreComponentMetadata(int component_id, base::DictionaryValue* dict);
55 std::string FormatCacheKey(int component_id);
56
57 const int owner_id_;
58 const media::AudioLogFactory::AudioComponent component_;
59 content::MediaInternals* const media_internals_;
60
61 DISALLOW_COPY_AND_ASSIGN(AudioLogImpl);
62};
63
64AudioLogImpl::AudioLogImpl(int owner_id,
65 media::AudioLogFactory::AudioComponent component,
66 content::MediaInternals* media_internals)
67 : owner_id_(owner_id),
68 component_(component),
69 media_internals_(media_internals) {}
70
71AudioLogImpl::~AudioLogImpl() {}
72
73void AudioLogImpl::OnCreated(int component_id,
74 const media::AudioParameters& params,
75 const std::string& input_device_id,
76 const std::string& output_device_id) {
77 base::DictionaryValue dict;
78 StoreComponentMetadata(component_id, &dict);
79
80 dict.SetString(kAudioLogStatusKey, "created");
81 dict.SetString("input_device_id", input_device_id);
82 dict.SetInteger("input_channels", params.input_channels());
83 dict.SetInteger("frames_per_buffer", params.frames_per_buffer());
84 dict.SetInteger("sample_rate", params.sample_rate());
85 dict.SetString("output_device_id", output_device_id);
86 dict.SetInteger("output_channels", params.channels());
87 dict.SetString("output_channel_layout",
88 ChannelLayoutToString(params.channel_layout()));
89
90 media_internals_->SendUpdateAndCache(
91 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
92}
93
94void AudioLogImpl::OnStarted(int component_id) {
95 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started");
96}
97
98void AudioLogImpl::OnStopped(int component_id) {
99 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped");
100}
101
102void AudioLogImpl::OnClosed(int component_id) {
103 base::DictionaryValue dict;
104 StoreComponentMetadata(component_id, &dict);
105 dict.SetString(kAudioLogStatusKey, "closed");
106 media_internals_->SendUpdateAndPurgeCache(
107 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
108}
109
110void AudioLogImpl::OnError(int component_id) {
111 SendSingleStringUpdate(component_id, "error_occurred", "true");
112}
113
114void AudioLogImpl::OnSetVolume(int component_id, double volume) {
115 base::DictionaryValue dict;
116 StoreComponentMetadata(component_id, &dict);
117 dict.SetDouble("volume", volume);
118 media_internals_->SendUpdateAndCache(
119 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
120}
121
122std::string AudioLogImpl::FormatCacheKey(int component_id) {
123 return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id);
124}
125
126void AudioLogImpl::SendSingleStringUpdate(int component_id,
127 const std::string& key,
128 const std::string& value) {
129 base::DictionaryValue dict;
130 StoreComponentMetadata(component_id, &dict);
131 dict.SetString(key, value);
132 media_internals_->SendUpdateAndCache(
133 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict);
134}
135
136void AudioLogImpl::StoreComponentMetadata(int component_id,
137 base::DictionaryValue* dict) {
138 dict->SetInteger("owner_id", owner_id_);
139 dict->SetInteger("component_id", component_id);
140 dict->SetInteger("component_type", component_);
141}
142
[email protected]11158e2d2013-02-01 02:31:56143MediaInternals* MediaInternals::GetInstance() {
[email protected]69946cf2013-11-27 00:11:42144 return g_media_internals.Pointer();
[email protected]11158e2d2013-02-01 02:31:56145}
146
[email protected]69946cf2013-11-27 00:11:42147MediaInternals::MediaInternals() : owner_ids_() {}
[email protected]11158e2d2013-02-01 02:31:56148MediaInternals::~MediaInternals() {}
149
[email protected]0e7ee582013-05-04 14:06:59150void MediaInternals::OnMediaEvents(
151 int render_process_id, const std::vector<media::MediaLogEvent>& events) {
[email protected]11158e2d2013-02-01 02:31:56152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]69946cf2013-11-27 00:11:42153 // Notify observers that |event| has occurred.
[email protected]0e7ee582013-05-04 14:06:59154 for (std::vector<media::MediaLogEvent>::const_iterator event = events.begin();
[email protected]69946cf2013-11-27 00:11:42155 event != events.end(); ++event) {
[email protected]0e7ee582013-05-04 14:06:59156 base::DictionaryValue dict;
157 dict.SetInteger("renderer", render_process_id);
158 dict.SetInteger("player", event->id);
159 dict.SetString("type", media::MediaLog::EventTypeToString(event->type));
[email protected]fbc46bd2013-06-26 04:21:41160
[email protected]69946cf2013-11-27 00:11:42161 // TODO(dalecurtis): This is technically not correct. TimeTicks "can't" be
162 // converted to to a human readable time format. See base/time/time.h.
163 const double ticks = event->time.ToInternalValue();
164 const double ticks_millis = ticks / base::Time::kMicrosecondsPerMillisecond;
[email protected]fbc46bd2013-06-26 04:21:41165 dict.SetDouble("ticksMillis", ticks_millis);
[email protected]0e7ee582013-05-04 14:06:59166 dict.Set("params", event->params.DeepCopy());
[email protected]69946cf2013-11-27 00:11:42167 SendUpdate(SerializeUpdate("media.onMediaEvent", &dict));
[email protected]0e7ee582013-05-04 14:06:59168 }
[email protected]11158e2d2013-02-01 02:31:56169}
170
171void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) {
[email protected]69946cf2013-11-27 00:11:42172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]11158e2d2013-02-01 02:31:56173 update_callbacks_.push_back(callback);
174}
175
176void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) {
[email protected]69946cf2013-11-27 00:11:42177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]11158e2d2013-02-01 02:31:56178 for (size_t i = 0; i < update_callbacks_.size(); ++i) {
179 if (update_callbacks_[i].Equals(callback)) {
180 update_callbacks_.erase(update_callbacks_.begin() + i);
181 return;
182 }
183 }
184 NOTREACHED();
185}
186
187void MediaInternals::SendEverything() {
[email protected]69946cf2013-11-27 00:11:42188 string16 everything_update;
189 {
190 base::AutoLock auto_lock(lock_);
191 everything_update = SerializeUpdate(
192 "media.onReceiveEverything", &cached_data_);
[email protected]11158e2d2013-02-01 02:31:56193 }
[email protected]69946cf2013-11-27 00:11:42194 SendUpdate(everything_update);
[email protected]e2fd1f72013-08-16 00:34:25195}
196
[email protected]69946cf2013-11-27 00:11:42197void MediaInternals::SendUpdate(const string16& update) {
198 // SendUpdate() may be called from any thread, but must run on the IO thread.
199 // TODO(dalecurtis): This is pretty silly since the update callbacks simply
200 // forward the calls to the UI thread. We should avoid the extra hop.
201 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
202 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
203 &MediaInternals::SendUpdate, base::Unretained(this), update));
[email protected]11158e2d2013-02-01 02:31:56204 return;
[email protected]69946cf2013-11-27 00:11:42205 }
[email protected]11158e2d2013-02-01 02:31:56206
[email protected]11158e2d2013-02-01 02:31:56207 for (size_t i = 0; i < update_callbacks_.size(); i++)
208 update_callbacks_[i].Run(update);
209}
210
[email protected]69946cf2013-11-27 00:11:42211scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog(
212 AudioComponent component) {
213 base::AutoLock auto_lock(lock_);
214 return scoped_ptr<media::AudioLog>(new AudioLogImpl(
215 owner_ids_[component]++, component, this));
216}
217
218void MediaInternals::SendUpdateAndCache(const std::string& cache_key,
219 const std::string& function,
220 const base::DictionaryValue* value) {
221 SendUpdate(SerializeUpdate(function, value));
222
223 base::AutoLock auto_lock(lock_);
224 if (!cached_data_.HasKey(cache_key)) {
225 cached_data_.Set(cache_key, value->DeepCopy());
226 return;
227 }
228
229 base::DictionaryValue* existing_dict = NULL;
230 CHECK(cached_data_.GetDictionary(cache_key, &existing_dict));
231 existing_dict->MergeDictionary(value);
232}
233
234void MediaInternals::SendUpdateAndPurgeCache(
235 const std::string& cache_key,
236 const std::string& function,
237 const base::DictionaryValue* value) {
238 SendUpdate(SerializeUpdate(function, value));
239
240 base::AutoLock auto_lock(lock_);
241 scoped_ptr<base::Value> out_value;
242 CHECK(cached_data_.Remove(cache_key, &out_value));
243}
244
[email protected]11158e2d2013-02-01 02:31:56245} // namespace content