Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 | 49 |
| 50 AudioRendererSinkCacheImpl::~AudioRendererSinkCacheImpl() { | 50 AudioRendererSinkCacheImpl::~AudioRendererSinkCacheImpl() { |
| 51 // We just release all the cached sinks here. | 51 // We just release all the cached sinks here. |
| 52 } | 52 } |
| 53 | 53 |
| 54 media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo( | 54 media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo( |
| 55 int source_render_frame_id, | 55 int source_render_frame_id, |
| 56 int session_id, | 56 int session_id, |
| 57 const std::string& device_id, | 57 const std::string& device_id, |
| 58 const url::Origin& security_origin) { | 58 const url::Origin& security_origin) { |
| 59 CacheEntry cache_entry = {source_render_frame_id, | 59 CacheEntry cache_entry = {source_render_frame_id, |
|
Guido Urdaneta
2016/07/05 15:10:39
Should this have a thread check to make sure there
Guido Urdaneta
2016/07/05 15:16:20
Forget about this. I didn't see the lock.
o1ka
2016/07/05 15:16:38
It's all protected by |cache_lock_| :)
| |
| 60 std::string() /* device_id */, security_origin, | 60 std::string() /* device_id */, security_origin, |
| 61 nullptr /* sink */, false /* not used */}; | 61 nullptr /* sink */, false /* not used */}; |
| 62 | 62 |
| 63 if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id, | 63 if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id, |
| 64 device_id)) { | 64 device_id)) { |
| 65 // We are provided with session id instead of device id. Session id is | 65 // We are provided with session id instead of device id. Session id is |
| 66 // unique, so we can't find any matching sink. Creating a new one. | 66 // unique, so we can't find any matching sink. Creating a new one. |
| 67 cache_entry.sink = create_sink_cb_.Run(source_render_frame_id, session_id, | 67 cache_entry.sink = create_sink_cb_.Run(source_render_frame_id, session_id, |
| 68 device_id, security_origin); | 68 device_id, security_origin); |
| 69 cache_entry.device_id = cache_entry.sink->GetOutputDeviceInfo().device_id(); | 69 cache_entry.device_id = cache_entry.sink->GetOutputDeviceInfo().device_id(); |
| 70 | 70 |
| 71 DVLOG(1) << "GetSinkInfo: address: " << cache_entry.sink.get() | 71 DVLOG(1) << "GetSinkInfo: address: " << cache_entry.sink.get() |
| 72 << " - used session to create new sink."; | 72 << " - used session to create new sink."; |
| 73 | 73 |
| 74 // Cache a newly-created sink. | 74 // Cache a newly-created sink. |
| 75 base::AutoLock auto_lock(cache_lock_); | 75 base::AutoLock auto_lock(cache_lock_); |
| 76 cache_.push_back(cache_entry); | 76 cache_.push_back(cache_entry); |
| 77 | 77 |
| 78 } else { | 78 } else { |
| 79 // Ignore session id. | 79 // Ignore session id. |
| 80 base::AutoLock auto_lock(cache_lock_); | 80 base::AutoLock auto_lock(cache_lock_); |
| 81 | 81 |
| 82 auto cache_iter = | 82 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) { |
| 83 FindCacheEntry_Locked(source_render_frame_id, device_id, | 83 if (default_device_info_) |
| 84 security_origin, false /* unused_only */); | 84 return default_device_info_.value(); |
| 85 | 85 |
| 86 if (cache_iter != cache_.end()) { | 86 // Otherwise, there is no default device cached. |
| 87 // A matching cached sink is found. | 87 DCHECK(FindCacheEntry_Locked(source_render_frame_id, device_id, |
| 88 DVLOG(1) << "GetSinkInfo: address: " << cache_iter->sink.get() | 88 security_origin, |
| 89 << " - reused a cached sink."; | 89 false /* unused_only */) == cache_.end()); |
|
Guido Urdaneta
2016/07/05 15:10:39
the /* unused_only */ comment does not add any inf
| |
| 90 } else { | |
| 91 // Looking up a cached non-default device. | |
| 92 auto cache_iter = | |
| 93 FindCacheEntry_Locked(source_render_frame_id, device_id, | |
| 94 security_origin, false /* unused_only */); | |
| 90 | 95 |
| 91 return cache_iter->sink->GetOutputDeviceInfo(); | 96 if (cache_iter != cache_.end()) { |
| 97 // A matching cached sink is found. | |
| 98 DVLOG(1) << "GetSinkInfo: address: " << cache_iter->sink.get() | |
| 99 << " - reused a cached sink."; | |
| 100 | |
| 101 return cache_iter->sink->GetOutputDeviceInfo(); | |
| 102 } | |
| 92 } | 103 } |
| 93 | 104 |
| 94 // No matching sink found, create a new one. | 105 // No matching sink found, create a new one. |
| 95 cache_entry.device_id = device_id; | 106 cache_entry.device_id = device_id; |
| 96 cache_entry.sink = create_sink_cb_.Run( | 107 cache_entry.sink = create_sink_cb_.Run( |
| 97 source_render_frame_id, 0 /* session_id */, device_id, security_origin); | 108 source_render_frame_id, 0 /* session_id */, device_id, security_origin); |
| 98 | 109 |
| 99 DVLOG(1) << "GetSinkInfo: address: " << cache_entry.sink.get() | 110 DVLOG(1) << "GetSinkInfo: address: " << cache_entry.sink.get() |
| 100 << " - no matching cached sink found, created a new one."; | 111 << " - no matching cached sink found, created a new one."; |
| 101 | 112 |
| 102 // Cache a newly-created sink. | 113 // Cache a newly-created sink. |
| 103 cache_.push_back(cache_entry); | 114 cache_.push_back(cache_entry); |
| 115 | |
| 116 // Cache information for default output device. | |
| 117 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) | |
| 118 default_device_info_ = cache_entry.sink->GetOutputDeviceInfo(); | |
| 104 } | 119 } |
| 105 | 120 |
| 106 // Schedule it for deletion. | 121 // Schedule it for deletion. |
| 107 DeleteLaterIfUnused(cache_entry.sink.get()); | 122 DeleteLaterIfUnused(cache_entry.sink.get()); |
| 108 | 123 |
| 109 DVLOG(1) << "GetSinkInfo: address: " << cache_entry.sink.get() | 124 DVLOG(1) << "GetSinkInfo: address: " << cache_entry.sink.get() |
| 110 << " created. source_render_frame_id: " << source_render_frame_id | 125 << " created. source_render_frame_id: " << source_render_frame_id |
| 111 << " session_id: " << session_id << " device_id: " << device_id | 126 << " session_id: " << session_id << " device_id: " << device_id |
| 112 << " security_origin: " << security_origin; | 127 << " security_origin: " << security_origin; |
| 113 | 128 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 135 | 150 |
| 136 // No unused sink is found, create one, mark it used, cache it and return. | 151 // No unused sink is found, create one, mark it used, cache it and return. |
| 137 CacheEntry cache_entry = { | 152 CacheEntry cache_entry = { |
| 138 source_render_frame_id, device_id, security_origin, | 153 source_render_frame_id, device_id, security_origin, |
| 139 create_sink_cb_.Run(source_render_frame_id, 0 /* session_id */, device_id, | 154 create_sink_cb_.Run(source_render_frame_id, 0 /* session_id */, device_id, |
| 140 security_origin), | 155 security_origin), |
| 141 true /* used */}; | 156 true /* used */}; |
| 142 | 157 |
| 143 cache_.push_back(cache_entry); | 158 cache_.push_back(cache_entry); |
| 144 | 159 |
| 160 // Update cached information for default output device. | |
| 161 if (media::AudioDeviceDescription::IsDefaultDevice(device_id)) | |
| 162 default_device_info_ = cache_entry.sink->GetOutputDeviceInfo(); | |
| 163 | |
| 145 DVLOG(1) << "GetSink: address: " << cache_entry.sink.get() | 164 DVLOG(1) << "GetSink: address: " << cache_entry.sink.get() |
| 146 << " - no unused cached sink found, created a new one." | 165 << " - no unused cached sink found, created a new one." |
| 147 << " source_render_frame_id: " << source_render_frame_id | 166 << " source_render_frame_id: " << source_render_frame_id |
| 148 << " device_id: " << device_id | 167 << " device_id: " << device_id |
| 149 << " security_origin: " << security_origin; | 168 << " security_origin: " << security_origin; |
| 150 return cache_entry.sink; | 169 return cache_entry.sink; |
| 151 } | 170 } |
| 152 | 171 |
| 153 void AudioRendererSinkCacheImpl::ReleaseSink( | 172 void AudioRendererSinkCacheImpl::ReleaseSink( |
| 154 const media::AudioRendererSink* sink_ptr) { | 173 const media::AudioRendererSink* sink_ptr) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 return val.device_id == device_id && | 263 return val.device_id == device_id && |
| 245 val.security_origin == security_origin; | 264 val.security_origin == security_origin; |
| 246 }); | 265 }); |
| 247 }; | 266 }; |
| 248 | 267 |
| 249 int AudioRendererSinkCacheImpl::GetCacheSizeForTesting() { | 268 int AudioRendererSinkCacheImpl::GetCacheSizeForTesting() { |
| 250 return cache_.size(); | 269 return cache_.size(); |
| 251 } | 270 } |
| 252 | 271 |
| 253 } // namespace content | 272 } // namespace content |
| OLD | NEW |