[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" |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 8 | #include "base/strings/string_number_conversions.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 10 | #include "content/public/browser/browser_thread.h" |
| 11 | #include "content/public/browser/web_ui.h" |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 12 | #include "media/audio/audio_parameters.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 13 | #include "media/base/media_log.h" |
| 14 | #include "media/base/media_log_event.h" |
| 15 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | static base::LazyInstance<content::MediaInternals>::Leaky g_media_internals = |
| 19 | LAZY_INSTANCE_INITIALIZER; |
| 20 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 21 | base::string16 SerializeUpdate(const std::string& function, |
| 22 | const base::Value* value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 23 | return content::WebUI::GetJavascriptCall( |
| 24 | function, std::vector<const base::Value*>(1, value)); |
| 25 | } |
| 26 | |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 27 | std::string EffectsToString(int effects) { |
| 28 | if (effects == media::AudioParameters::NO_EFFECTS) |
| 29 | return "NO_EFFECTS"; |
| 30 | |
| 31 | struct { |
| 32 | int flag; |
| 33 | const char* name; |
| 34 | } flags[] = { |
| 35 | { media::AudioParameters::ECHO_CANCELLER, "ECHO_CANCELLER" }, |
| 36 | { media::AudioParameters::DUCKING, "DUCKING" }, |
[email protected] | 81495eb | 2014-03-19 06:08:18 | [diff] [blame] | 37 | { media::AudioParameters::KEYBOARD_MIC, "KEYBOARD_MIC" }, |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | std::string ret; |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 41 | for (size_t i = 0; i < arraysize(flags); ++i) { |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 42 | if (effects & flags[i].flag) { |
| 43 | if (!ret.empty()) |
| 44 | ret += " | "; |
| 45 | ret += flags[i].name; |
| 46 | effects &= ~flags[i].flag; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (effects) { |
| 51 | if (!ret.empty()) |
| 52 | ret += " | "; |
| 53 | ret += base::IntToString(effects); |
| 54 | } |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 59 | const char kAudioLogStatusKey[] = "status"; |
| 60 | const char kAudioLogUpdateFunction[] = "media.updateAudioComponent"; |
| 61 | |
| 62 | } // namespace |
| 63 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 64 | namespace content { |
| 65 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 66 | class AudioLogImpl : public media::AudioLog { |
| 67 | public: |
| 68 | AudioLogImpl(int owner_id, |
| 69 | media::AudioLogFactory::AudioComponent component, |
| 70 | content::MediaInternals* media_internals); |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame^] | 71 | ~AudioLogImpl() override; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 72 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame^] | 73 | void OnCreated(int component_id, |
| 74 | const media::AudioParameters& params, |
| 75 | const std::string& device_id) override; |
| 76 | void OnStarted(int component_id) override; |
| 77 | void OnStopped(int component_id) override; |
| 78 | void OnClosed(int component_id) override; |
| 79 | void OnError(int component_id) override; |
| 80 | void OnSetVolume(int component_id, double volume) override; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | void SendSingleStringUpdate(int component_id, |
| 84 | const std::string& key, |
| 85 | const std::string& value); |
| 86 | void StoreComponentMetadata(int component_id, base::DictionaryValue* dict); |
| 87 | std::string FormatCacheKey(int component_id); |
| 88 | |
| 89 | const int owner_id_; |
| 90 | const media::AudioLogFactory::AudioComponent component_; |
| 91 | content::MediaInternals* const media_internals_; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(AudioLogImpl); |
| 94 | }; |
| 95 | |
| 96 | AudioLogImpl::AudioLogImpl(int owner_id, |
| 97 | media::AudioLogFactory::AudioComponent component, |
| 98 | content::MediaInternals* media_internals) |
| 99 | : owner_id_(owner_id), |
| 100 | component_(component), |
| 101 | media_internals_(media_internals) {} |
| 102 | |
| 103 | AudioLogImpl::~AudioLogImpl() {} |
| 104 | |
| 105 | void AudioLogImpl::OnCreated(int component_id, |
| 106 | const media::AudioParameters& params, |
[email protected] | 25d7f89 | 2014-02-13 15:22:45 | [diff] [blame] | 107 | const std::string& device_id) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 108 | base::DictionaryValue dict; |
| 109 | StoreComponentMetadata(component_id, &dict); |
| 110 | |
| 111 | dict.SetString(kAudioLogStatusKey, "created"); |
[email protected] | 25d7f89 | 2014-02-13 15:22:45 | [diff] [blame] | 112 | dict.SetString("device_id", device_id); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 113 | dict.SetInteger("frames_per_buffer", params.frames_per_buffer()); |
| 114 | dict.SetInteger("sample_rate", params.sample_rate()); |
[email protected] | ad82f41 | 2013-11-27 04:20:41 | [diff] [blame] | 115 | dict.SetInteger("channels", params.channels()); |
| 116 | dict.SetString("channel_layout", |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 117 | ChannelLayoutToString(params.channel_layout())); |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 118 | dict.SetString("effects", EffectsToString(params.effects())); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 119 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 120 | media_internals_->SendUpdateAndCacheAudioStreamKey( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 121 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 122 | } |
| 123 | |
| 124 | void AudioLogImpl::OnStarted(int component_id) { |
| 125 | SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started"); |
| 126 | } |
| 127 | |
| 128 | void AudioLogImpl::OnStopped(int component_id) { |
| 129 | SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped"); |
| 130 | } |
| 131 | |
| 132 | void AudioLogImpl::OnClosed(int component_id) { |
| 133 | base::DictionaryValue dict; |
| 134 | StoreComponentMetadata(component_id, &dict); |
| 135 | dict.SetString(kAudioLogStatusKey, "closed"); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 136 | media_internals_->SendUpdateAndPurgeAudioStreamCache( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 137 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 138 | } |
| 139 | |
| 140 | void AudioLogImpl::OnError(int component_id) { |
| 141 | SendSingleStringUpdate(component_id, "error_occurred", "true"); |
| 142 | } |
| 143 | |
| 144 | void AudioLogImpl::OnSetVolume(int component_id, double volume) { |
| 145 | base::DictionaryValue dict; |
| 146 | StoreComponentMetadata(component_id, &dict); |
| 147 | dict.SetDouble("volume", volume); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 148 | media_internals_->SendUpdateAndCacheAudioStreamKey( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 149 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 150 | } |
| 151 | |
| 152 | std::string AudioLogImpl::FormatCacheKey(int component_id) { |
| 153 | return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id); |
| 154 | } |
| 155 | |
| 156 | void AudioLogImpl::SendSingleStringUpdate(int component_id, |
| 157 | const std::string& key, |
| 158 | const std::string& value) { |
| 159 | base::DictionaryValue dict; |
| 160 | StoreComponentMetadata(component_id, &dict); |
| 161 | dict.SetString(key, value); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 162 | media_internals_->SendUpdateAndCacheAudioStreamKey( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 163 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 164 | } |
| 165 | |
| 166 | void AudioLogImpl::StoreComponentMetadata(int component_id, |
| 167 | base::DictionaryValue* dict) { |
| 168 | dict->SetInteger("owner_id", owner_id_); |
| 169 | dict->SetInteger("component_id", component_id); |
| 170 | dict->SetInteger("component_type", component_); |
| 171 | } |
| 172 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 173 | MediaInternals* MediaInternals::GetInstance() { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 174 | return g_media_internals.Pointer(); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 177 | MediaInternals::MediaInternals() : owner_ids_() {} |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 178 | MediaInternals::~MediaInternals() {} |
| 179 | |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 180 | void MediaInternals::OnMediaEvents( |
| 181 | int render_process_id, const std::vector<media::MediaLogEvent>& events) { |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 182 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 183 | // Notify observers that |event| has occurred. |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 184 | for (std::vector<media::MediaLogEvent>::const_iterator event = events.begin(); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 185 | event != events.end(); ++event) { |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 186 | base::DictionaryValue dict; |
| 187 | dict.SetInteger("renderer", render_process_id); |
| 188 | dict.SetInteger("player", event->id); |
| 189 | dict.SetString("type", media::MediaLog::EventTypeToString(event->type)); |
[email protected] | fbc46bd | 2013-06-26 04:21:41 | [diff] [blame] | 190 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 191 | // TODO(dalecurtis): This is technically not correct. TimeTicks "can't" be |
| 192 | // converted to to a human readable time format. See base/time/time.h. |
| 193 | const double ticks = event->time.ToInternalValue(); |
| 194 | const double ticks_millis = ticks / base::Time::kMicrosecondsPerMillisecond; |
[email protected] | fbc46bd | 2013-06-26 04:21:41 | [diff] [blame] | 195 | dict.SetDouble("ticksMillis", ticks_millis); |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 196 | dict.Set("params", event->params.DeepCopy()); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 197 | SendUpdate(SerializeUpdate("media.onMediaEvent", &dict)); |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 198 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 202 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 203 | update_callbacks_.push_back(callback); |
| 204 | } |
| 205 | |
| 206 | void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 207 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 208 | for (size_t i = 0; i < update_callbacks_.size(); ++i) { |
| 209 | if (update_callbacks_[i].Equals(callback)) { |
| 210 | update_callbacks_.erase(update_callbacks_.begin() + i); |
| 211 | return; |
| 212 | } |
| 213 | } |
| 214 | NOTREACHED(); |
| 215 | } |
| 216 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 217 | void MediaInternals::SendAudioStreamData() { |
| 218 | base::string16 audio_stream_update; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 219 | { |
| 220 | base::AutoLock auto_lock(lock_); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 221 | audio_stream_update = SerializeUpdate( |
| 222 | "media.onReceiveAudioStreamData", &audio_streams_cached_data_); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 223 | } |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 224 | SendUpdate(audio_stream_update); |
| 225 | } |
| 226 | |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 227 | void MediaInternals::SendVideoCaptureDeviceCapabilities() { |
| 228 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 229 | SendUpdate(SerializeUpdate("media.onReceiveVideoCaptureCapabilities", |
| 230 | &video_capture_capabilities_cached_data_)); |
| 231 | } |
| 232 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 233 | void MediaInternals::UpdateVideoCaptureDeviceCapabilities( |
| 234 | const media::VideoCaptureDeviceInfos& video_capture_device_infos) { |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 235 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 236 | video_capture_capabilities_cached_data_.Clear(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 237 | |
| 238 | for (const auto& video_capture_device_info : video_capture_device_infos) { |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 239 | base::ListValue* format_list = new base::ListValue(); |
| 240 | for (const auto& format : video_capture_device_info.supported_formats) |
| 241 | format_list->AppendString(format.ToString()); |
| 242 | |
| 243 | base::DictionaryValue* device_dict = new base::DictionaryValue(); |
| 244 | device_dict->SetString("id", video_capture_device_info.name.id()); |
| 245 | device_dict->SetString( |
| 246 | "name", video_capture_device_info.name.GetNameAndModel()); |
| 247 | device_dict->Set("formats", format_list); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 248 | #if defined(OS_WIN) || defined(OS_MACOSX) |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 249 | device_dict->SetInteger( |
| 250 | "captureApi", |
| 251 | video_capture_device_info.name.capture_api_type()); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 252 | #endif |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 253 | video_capture_capabilities_cached_data_.Append(device_dict); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 254 | } |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 255 | |
| 256 | if (update_callbacks_.size() > 0) |
| 257 | SendVideoCaptureDeviceCapabilities(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog( |
| 261 | AudioComponent component) { |
| 262 | base::AutoLock auto_lock(lock_); |
| 263 | return scoped_ptr<media::AudioLog>(new AudioLogImpl( |
| 264 | owner_ids_[component]++, component, this)); |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 267 | void MediaInternals::SendUpdate(const base::string16& update) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 268 | // SendUpdate() may be called from any thread, but must run on the IO thread. |
| 269 | // TODO(dalecurtis): This is pretty silly since the update callbacks simply |
| 270 | // forward the calls to the UI thread. We should avoid the extra hop. |
| 271 | if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 272 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 273 | &MediaInternals::SendUpdate, base::Unretained(this), update)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 274 | return; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 275 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 276 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 277 | for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 278 | update_callbacks_[i].Run(update); |
| 279 | } |
| 280 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 281 | void MediaInternals::SendUpdateAndCacheAudioStreamKey( |
| 282 | const std::string& cache_key, |
| 283 | const std::string& function, |
| 284 | const base::DictionaryValue* value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 285 | SendUpdate(SerializeUpdate(function, value)); |
| 286 | |
| 287 | base::AutoLock auto_lock(lock_); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 288 | if (!audio_streams_cached_data_.HasKey(cache_key)) { |
| 289 | audio_streams_cached_data_.Set(cache_key, value->DeepCopy()); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
| 293 | base::DictionaryValue* existing_dict = NULL; |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 294 | CHECK(audio_streams_cached_data_.GetDictionary(cache_key, &existing_dict)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 295 | existing_dict->MergeDictionary(value); |
| 296 | } |
| 297 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 298 | void MediaInternals::SendUpdateAndPurgeAudioStreamCache( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 299 | const std::string& cache_key, |
| 300 | const std::string& function, |
| 301 | const base::DictionaryValue* value) { |
| 302 | SendUpdate(SerializeUpdate(function, value)); |
| 303 | |
| 304 | base::AutoLock auto_lock(lock_); |
| 305 | scoped_ptr<base::Value> out_value; |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 306 | CHECK(audio_streams_cached_data_.Remove(cache_key, &out_value)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 309 | } // namespace content |