Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/media_internals.h" | 5 #include "content/browser/media/media_internals.h" |
| 6 | 6 |
| 7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 | 110 |
| 111 dict.SetString(kAudioLogStatusKey, "created"); | 111 dict.SetString(kAudioLogStatusKey, "created"); |
| 112 dict.SetString("device_id", device_id); | 112 dict.SetString("device_id", device_id); |
| 113 dict.SetInteger("frames_per_buffer", params.frames_per_buffer()); | 113 dict.SetInteger("frames_per_buffer", params.frames_per_buffer()); |
| 114 dict.SetInteger("sample_rate", params.sample_rate()); | 114 dict.SetInteger("sample_rate", params.sample_rate()); |
| 115 dict.SetInteger("channels", params.channels()); | 115 dict.SetInteger("channels", params.channels()); |
| 116 dict.SetString("channel_layout", | 116 dict.SetString("channel_layout", |
| 117 ChannelLayoutToString(params.channel_layout())); | 117 ChannelLayoutToString(params.channel_layout())); |
| 118 dict.SetString("effects", EffectsToString(params.effects())); | 118 dict.SetString("effects", EffectsToString(params.effects())); |
| 119 | 119 |
| 120 media_internals_->SendUpdateAndCache( | 120 media_internals_->SendUpdateAndCacheAudioStreamKey( |
| 121 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); | 121 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void AudioLogImpl::OnStarted(int component_id) { | 124 void AudioLogImpl::OnStarted(int component_id) { |
| 125 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started"); | 125 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started"); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void AudioLogImpl::OnStopped(int component_id) { | 128 void AudioLogImpl::OnStopped(int component_id) { |
| 129 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped"); | 129 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped"); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void AudioLogImpl::OnClosed(int component_id) { | 132 void AudioLogImpl::OnClosed(int component_id) { |
| 133 base::DictionaryValue dict; | 133 base::DictionaryValue dict; |
| 134 StoreComponentMetadata(component_id, &dict); | 134 StoreComponentMetadata(component_id, &dict); |
| 135 dict.SetString(kAudioLogStatusKey, "closed"); | 135 dict.SetString(kAudioLogStatusKey, "closed"); |
| 136 media_internals_->SendUpdateAndPurgeCache( | 136 media_internals_->SendUpdateAndPurgeAudioStreamCache( |
| 137 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); | 137 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void AudioLogImpl::OnError(int component_id) { | 140 void AudioLogImpl::OnError(int component_id) { |
| 141 SendSingleStringUpdate(component_id, "error_occurred", "true"); | 141 SendSingleStringUpdate(component_id, "error_occurred", "true"); |
| 142 } | 142 } |
| 143 | 143 |
| 144 void AudioLogImpl::OnSetVolume(int component_id, double volume) { | 144 void AudioLogImpl::OnSetVolume(int component_id, double volume) { |
| 145 base::DictionaryValue dict; | 145 base::DictionaryValue dict; |
| 146 StoreComponentMetadata(component_id, &dict); | 146 StoreComponentMetadata(component_id, &dict); |
| 147 dict.SetDouble("volume", volume); | 147 dict.SetDouble("volume", volume); |
| 148 media_internals_->SendUpdateAndCache( | 148 media_internals_->SendUpdateAndCacheAudioStreamKey( |
| 149 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); | 149 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 150 } | 150 } |
| 151 | 151 |
| 152 std::string AudioLogImpl::FormatCacheKey(int component_id) { | 152 std::string AudioLogImpl::FormatCacheKey(int component_id) { |
| 153 return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id); | 153 return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id); |
| 154 } | 154 } |
| 155 | 155 |
| 156 void AudioLogImpl::SendSingleStringUpdate(int component_id, | 156 void AudioLogImpl::SendSingleStringUpdate(int component_id, |
| 157 const std::string& key, | 157 const std::string& key, |
| 158 const std::string& value) { | 158 const std::string& value) { |
| 159 base::DictionaryValue dict; | 159 base::DictionaryValue dict; |
| 160 StoreComponentMetadata(component_id, &dict); | 160 StoreComponentMetadata(component_id, &dict); |
| 161 dict.SetString(key, value); | 161 dict.SetString(key, value); |
| 162 media_internals_->SendUpdateAndCache( | 162 media_internals_->SendUpdateAndCacheAudioStreamKey( |
| 163 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); | 163 FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void AudioLogImpl::StoreComponentMetadata(int component_id, | 166 void AudioLogImpl::StoreComponentMetadata(int component_id, |
| 167 base::DictionaryValue* dict) { | 167 base::DictionaryValue* dict) { |
| 168 dict->SetInteger("owner_id", owner_id_); | 168 dict->SetInteger("owner_id", owner_id_); |
| 169 dict->SetInteger("component_id", component_id); | 169 dict->SetInteger("component_id", component_id); |
| 170 dict->SetInteger("component_type", component_); | 170 dict->SetInteger("component_type", component_); |
| 171 } | 171 } |
| 172 | 172 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 208 for (size_t i = 0; i < update_callbacks_.size(); ++i) { | 208 for (size_t i = 0; i < update_callbacks_.size(); ++i) { |
| 209 if (update_callbacks_[i].Equals(callback)) { | 209 if (update_callbacks_[i].Equals(callback)) { |
| 210 update_callbacks_.erase(update_callbacks_.begin() + i); | 210 update_callbacks_.erase(update_callbacks_.begin() + i); |
| 211 return; | 211 return; |
| 212 } | 212 } |
| 213 } | 213 } |
| 214 NOTREACHED(); | 214 NOTREACHED(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 void MediaInternals::SendEverything() { | 217 void MediaInternals::SendAudioStreamData() { |
| 218 base::string16 everything_update; | 218 base::string16 audio_stream_update; |
| 219 { | 219 { |
| 220 base::AutoLock auto_lock(lock_); | 220 base::AutoLock auto_lock(lock_); |
| 221 everything_update = SerializeUpdate( | 221 audio_stream_update = SerializeUpdate( |
| 222 "media.onReceiveEverything", &cached_data_); | 222 "media.onReceiveEverything", &audio_streams_cached_data_); |
|
wolenetz
2014/10/09 20:51:43
s/onReceiveEverything/onReceiveAudioStreamData/ he
mcasas
2014/10/10 11:30:41
Done.
| |
| 223 } | 223 } |
| 224 SendUpdate(everything_update); | 224 SendUpdate(audio_stream_update); |
| 225 } | |
| 226 | |
| 227 void MediaInternals::UpdateVideoCaptureDeviceCapabilities( | |
| 228 const media::VideoCaptureDeviceInfos& name_and_formats) { | |
| 229 base::DictionaryValue video_devices_info_dictionary; | |
| 230 | |
| 231 for (const auto &name_and_format : name_and_formats) { | |
|
wolenetz
2014/10/09 20:51:43
nit: here and below, s/auto &/auto& /
mcasas
2014/10/10 11:30:41
Done.
| |
| 232 base::DictionaryValue* formats_dict = new base::DictionaryValue(); | |
| 233 formats_dict->SetString("Unique ID", name_and_format.name.id()); | |
| 234 #if defined(OS_WIN) || defined(OS_MACOSX) | |
| 235 formats_dict->SetInteger("Capture API: #", | |
| 236 name_and_format.name.capture_api_type()); | |
| 237 #endif | |
| 238 int count = 0; | |
| 239 for (const auto &format : name_and_format.supported_formats) { | |
|
wolenetz
2014/10/09 20:51:43
nit: ditto.
mcasas
2014/10/10 11:30:41
Done.
| |
| 240 formats_dict->SetString(base::StringPrintf("[%3d]", count++), | |
| 241 format.ToString()); | |
| 242 } | |
| 243 video_devices_info_dictionary.Set(name_and_format.name.GetNameAndModel(), | |
| 244 formats_dict); | |
| 245 } | |
| 246 // TODO(mcasas): Remove the following printout when sending the capabilities | |
| 247 // to JS is implemented in a similar way to how SendAudioStreamData() does. | |
|
wolenetz
2014/10/09 20:55:05
One other nit: you might want to note here that |l
mcasas
2014/10/10 11:30:41
Done. We don't need to use the same |lock_| though
| |
| 248 DVLOG(1) << "Received: " << video_devices_info_dictionary; | |
| 249 } | |
| 250 | |
| 251 scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog( | |
| 252 AudioComponent component) { | |
| 253 base::AutoLock auto_lock(lock_); | |
| 254 return scoped_ptr<media::AudioLog>(new AudioLogImpl( | |
| 255 owner_ids_[component]++, component, this)); | |
| 225 } | 256 } |
| 226 | 257 |
| 227 void MediaInternals::SendUpdate(const base::string16& update) { | 258 void MediaInternals::SendUpdate(const base::string16& update) { |
| 228 // SendUpdate() may be called from any thread, but must run on the IO thread. | 259 // SendUpdate() may be called from any thread, but must run on the IO thread. |
| 229 // TODO(dalecurtis): This is pretty silly since the update callbacks simply | 260 // TODO(dalecurtis): This is pretty silly since the update callbacks simply |
| 230 // forward the calls to the UI thread. We should avoid the extra hop. | 261 // forward the calls to the UI thread. We should avoid the extra hop. |
| 231 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 262 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 232 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | 263 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 233 &MediaInternals::SendUpdate, base::Unretained(this), update)); | 264 &MediaInternals::SendUpdate, base::Unretained(this), update)); |
| 234 return; | 265 return; |
| 235 } | 266 } |
| 236 | 267 |
| 237 for (size_t i = 0; i < update_callbacks_.size(); i++) | 268 for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 238 update_callbacks_[i].Run(update); | 269 update_callbacks_[i].Run(update); |
| 239 } | 270 } |
| 240 | 271 |
| 241 scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog( | 272 void MediaInternals::SendUpdateAndCacheAudioStreamKey( |
| 242 AudioComponent component) { | 273 const std::string& cache_key, |
| 243 base::AutoLock auto_lock(lock_); | 274 const std::string& function, |
| 244 return scoped_ptr<media::AudioLog>(new AudioLogImpl( | 275 const base::DictionaryValue* value) { |
| 245 owner_ids_[component]++, component, this)); | |
| 246 } | |
| 247 | |
| 248 void MediaInternals::SendUpdateAndCache(const std::string& cache_key, | |
| 249 const std::string& function, | |
| 250 const base::DictionaryValue* value) { | |
| 251 SendUpdate(SerializeUpdate(function, value)); | 276 SendUpdate(SerializeUpdate(function, value)); |
| 252 | 277 |
| 253 base::AutoLock auto_lock(lock_); | 278 base::AutoLock auto_lock(lock_); |
| 254 if (!cached_data_.HasKey(cache_key)) { | 279 if (!audio_streams_cached_data_.HasKey(cache_key)) { |
| 255 cached_data_.Set(cache_key, value->DeepCopy()); | 280 audio_streams_cached_data_.Set(cache_key, value->DeepCopy()); |
| 256 return; | 281 return; |
| 257 } | 282 } |
| 258 | 283 |
| 259 base::DictionaryValue* existing_dict = NULL; | 284 base::DictionaryValue* existing_dict = NULL; |
| 260 CHECK(cached_data_.GetDictionary(cache_key, &existing_dict)); | 285 CHECK(audio_streams_cached_data_.GetDictionary(cache_key, &existing_dict)); |
| 261 existing_dict->MergeDictionary(value); | 286 existing_dict->MergeDictionary(value); |
| 262 } | 287 } |
| 263 | 288 |
| 264 void MediaInternals::SendUpdateAndPurgeCache( | 289 void MediaInternals::SendUpdateAndPurgeAudioStreamCache( |
| 265 const std::string& cache_key, | 290 const std::string& cache_key, |
| 266 const std::string& function, | 291 const std::string& function, |
| 267 const base::DictionaryValue* value) { | 292 const base::DictionaryValue* value) { |
| 268 SendUpdate(SerializeUpdate(function, value)); | 293 SendUpdate(SerializeUpdate(function, value)); |
| 269 | 294 |
| 270 base::AutoLock auto_lock(lock_); | 295 base::AutoLock auto_lock(lock_); |
| 271 scoped_ptr<base::Value> out_value; | 296 scoped_ptr<base::Value> out_value; |
| 272 CHECK(cached_data_.Remove(cache_key, &out_value)); | 297 CHECK(audio_streams_cached_data_.Remove(cache_key, &out_value)); |
| 273 } | 298 } |
| 274 | 299 |
| 275 } // namespace content | 300 } // namespace content |
| OLD | NEW |