[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 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 "content/browser/media/media_internals.h" |
| 6 | |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 7 | #include "base/strings/string16.h" |
| 8 | #include "base/strings/stringprintf.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 9 | #include "content/public/browser/browser_thread.h" |
| 10 | #include "content/public/browser/web_ui.h" |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 11 | #include "media/audio/audio_parameters.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 12 | #include "media/base/media_log.h" |
| 13 | #include "media/base/media_log_event.h" |
| 14 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 15 | namespace { |
| 16 | |
| 17 | static base::LazyInstance<content::MediaInternals>::Leaky g_media_internals = |
| 18 | LAZY_INSTANCE_INITIALIZER; |
| 19 | |
| 20 | string16 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 | |
| 26 | const char kAudioLogStatusKey[] = "status"; |
| 27 | const char kAudioLogUpdateFunction[] = "media.updateAudioComponent"; |
| 28 | |
| 29 | } // namespace |
| 30 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 31 | namespace content { |
| 32 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 33 | class 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 | |
| 64 | AudioLogImpl::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 | |
| 71 | AudioLogImpl::~AudioLogImpl() {} |
| 72 | |
| 73 | void 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 | |
| 94 | void AudioLogImpl::OnStarted(int component_id) { |
| 95 | SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started"); |
| 96 | } |
| 97 | |
| 98 | void AudioLogImpl::OnStopped(int component_id) { |
| 99 | SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped"); |
| 100 | } |
| 101 | |
| 102 | void 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 | |
| 110 | void AudioLogImpl::OnError(int component_id) { |
| 111 | SendSingleStringUpdate(component_id, "error_occurred", "true"); |
| 112 | } |
| 113 | |
| 114 | void 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 | |
| 122 | std::string AudioLogImpl::FormatCacheKey(int component_id) { |
| 123 | return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id); |
| 124 | } |
| 125 | |
| 126 | void 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 | |
| 136 | void 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] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 143 | MediaInternals* MediaInternals::GetInstance() { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 144 | return g_media_internals.Pointer(); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 147 | MediaInternals::MediaInternals() : owner_ids_() {} |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 148 | MediaInternals::~MediaInternals() {} |
| 149 | |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 150 | void MediaInternals::OnMediaEvents( |
| 151 | int render_process_id, const std::vector<media::MediaLogEvent>& events) { |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 152 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 153 | // Notify observers that |event| has occurred. |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 154 | for (std::vector<media::MediaLogEvent>::const_iterator event = events.begin(); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 155 | event != events.end(); ++event) { |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 156 | 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] | fbc46bd | 2013-06-26 04:21:41 | [diff] [blame] | 160 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 161 | // 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] | fbc46bd | 2013-06-26 04:21:41 | [diff] [blame] | 165 | dict.SetDouble("ticksMillis", ticks_millis); |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 166 | dict.Set("params", event->params.DeepCopy()); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 167 | SendUpdate(SerializeUpdate("media.onMediaEvent", &dict)); |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 168 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 172 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 173 | update_callbacks_.push_back(callback); |
| 174 | } |
| 175 | |
| 176 | void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 177 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 178 | 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 | |
| 187 | void MediaInternals::SendEverything() { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 188 | string16 everything_update; |
| 189 | { |
| 190 | base::AutoLock auto_lock(lock_); |
| 191 | everything_update = SerializeUpdate( |
| 192 | "media.onReceiveEverything", &cached_data_); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 193 | } |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 194 | SendUpdate(everything_update); |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 197 | void 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] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 204 | return; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 205 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 206 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 207 | for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 208 | update_callbacks_[i].Run(update); |
| 209 | } |
| 210 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame^] | 211 | scoped_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 | |
| 218 | void 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 | |
| 234 | void 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] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 245 | } // namespace content |