| 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.onReceiveAudioStreamData", &audio_streams_cached_data_); |
| 223 } | 223 } |
| 224 SendUpdate(everything_update); | 224 SendUpdate(audio_stream_update); |
| 225 } |
| 226 |
| 227 void MediaInternals::UpdateVideoCaptureDeviceCapabilities( |
| 228 const media::VideoCaptureDeviceInfos& video_capture_device_infos) { |
| 229 base::DictionaryValue video_devices_info_dictionary; |
| 230 |
| 231 for (const auto& video_capture_device_info : video_capture_device_infos) { |
| 232 base::DictionaryValue* formats_dict = new base::DictionaryValue(); |
| 233 formats_dict->SetString("Unique ID", video_capture_device_info.name.id()); |
| 234 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 235 formats_dict->SetInteger("Capture API: #", |
| 236 video_capture_device_info.name.capture_api_type()); |
| 237 #endif |
| 238 int count = 0; |
| 239 for (const auto& format : video_capture_device_info.supported_formats) { |
| 240 formats_dict->SetString(base::StringPrintf("[%3d]", count++), |
| 241 format.ToString()); |
| 242 } |
| 243 video_devices_info_dictionary.Set( |
| 244 video_capture_device_info.name.GetNameAndModel(), 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. |
| 248 // A lock might be needed if these capabilities are cached at this point. |
| 249 DVLOG(1) << "Received: " << video_devices_info_dictionary; |
| 250 } |
| 251 |
| 252 scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog( |
| 253 AudioComponent component) { |
| 254 base::AutoLock auto_lock(lock_); |
| 255 return scoped_ptr<media::AudioLog>(new AudioLogImpl( |
| 256 owner_ids_[component]++, component, this)); |
| 225 } | 257 } |
| 226 | 258 |
| 227 void MediaInternals::SendUpdate(const base::string16& update) { | 259 void MediaInternals::SendUpdate(const base::string16& update) { |
| 228 // SendUpdate() may be called from any thread, but must run on the IO thread. | 260 // 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 | 261 // 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. | 262 // forward the calls to the UI thread. We should avoid the extra hop. |
| 231 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 263 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 232 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | 264 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 233 &MediaInternals::SendUpdate, base::Unretained(this), update)); | 265 &MediaInternals::SendUpdate, base::Unretained(this), update)); |
| 234 return; | 266 return; |
| 235 } | 267 } |
| 236 | 268 |
| 237 for (size_t i = 0; i < update_callbacks_.size(); i++) | 269 for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 238 update_callbacks_[i].Run(update); | 270 update_callbacks_[i].Run(update); |
| 239 } | 271 } |
| 240 | 272 |
| 241 scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog( | 273 void MediaInternals::SendUpdateAndCacheAudioStreamKey( |
| 242 AudioComponent component) { | 274 const std::string& cache_key, |
| 243 base::AutoLock auto_lock(lock_); | 275 const std::string& function, |
| 244 return scoped_ptr<media::AudioLog>(new AudioLogImpl( | 276 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)); | 277 SendUpdate(SerializeUpdate(function, value)); |
| 252 | 278 |
| 253 base::AutoLock auto_lock(lock_); | 279 base::AutoLock auto_lock(lock_); |
| 254 if (!cached_data_.HasKey(cache_key)) { | 280 if (!audio_streams_cached_data_.HasKey(cache_key)) { |
| 255 cached_data_.Set(cache_key, value->DeepCopy()); | 281 audio_streams_cached_data_.Set(cache_key, value->DeepCopy()); |
| 256 return; | 282 return; |
| 257 } | 283 } |
| 258 | 284 |
| 259 base::DictionaryValue* existing_dict = NULL; | 285 base::DictionaryValue* existing_dict = NULL; |
| 260 CHECK(cached_data_.GetDictionary(cache_key, &existing_dict)); | 286 CHECK(audio_streams_cached_data_.GetDictionary(cache_key, &existing_dict)); |
| 261 existing_dict->MergeDictionary(value); | 287 existing_dict->MergeDictionary(value); |
| 262 } | 288 } |
| 263 | 289 |
| 264 void MediaInternals::SendUpdateAndPurgeCache( | 290 void MediaInternals::SendUpdateAndPurgeAudioStreamCache( |
| 265 const std::string& cache_key, | 291 const std::string& cache_key, |
| 266 const std::string& function, | 292 const std::string& function, |
| 267 const base::DictionaryValue* value) { | 293 const base::DictionaryValue* value) { |
| 268 SendUpdate(SerializeUpdate(function, value)); | 294 SendUpdate(SerializeUpdate(function, value)); |
| 269 | 295 |
| 270 base::AutoLock auto_lock(lock_); | 296 base::AutoLock auto_lock(lock_); |
| 271 scoped_ptr<base::Value> out_value; | 297 scoped_ptr<base::Value> out_value; |
| 272 CHECK(cached_data_.Remove(cache_key, &out_value)); | 298 CHECK(audio_streams_cached_data_.Remove(cache_key, &out_value)); |
| 273 } | 299 } |
| 274 | 300 |
| 275 } // namespace content | 301 } // namespace content |
| OLD | NEW |