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/renderer_host/media/video_capture_controller.h" | 5 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 static const int kInfiniteRatio = 99999; | 42 static const int kInfiniteRatio = 99999; |
| 43 | 43 |
| 44 #define UMA_HISTOGRAM_ASPECT_RATIO(name, width, height) \ | 44 #define UMA_HISTOGRAM_ASPECT_RATIO(name, width, height) \ |
| 45 UMA_HISTOGRAM_SPARSE_SLOWLY( \ | 45 UMA_HISTOGRAM_SPARSE_SLOWLY( \ |
| 46 name, \ | 46 name, \ |
| 47 (height) ? ((width) * 100) / (height) : kInfiniteRatio); | 47 (height) ? ((width) * 100) / (height) : kInfiniteRatio); |
| 48 | 48 |
| 49 class SyncTokenClientImpl : public VideoFrame::SyncTokenClient { | |
| 50 public: | |
| 51 explicit SyncTokenClientImpl(display_compositor::GLHelper* gl_helper) | |
| 52 : gl_helper_(gl_helper) {} | |
| 53 ~SyncTokenClientImpl() override {} | |
| 54 void GenerateSyncToken(gpu::SyncToken* sync_token) override { | |
| 55 gl_helper_->GenerateSyncToken(sync_token); | |
| 56 } | |
| 57 void WaitSyncToken(const gpu::SyncToken& sync_token) override { | |
| 58 gl_helper_->WaitSyncToken(sync_token); | |
| 59 } | |
| 60 | |
| 61 private: | |
| 62 display_compositor::GLHelper* gl_helper_; | |
| 63 }; | |
| 64 | |
| 65 void ReturnVideoFrame(const scoped_refptr<VideoFrame>& video_frame, | |
| 66 const gpu::SyncToken& sync_token) { | |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 68 #if defined(OS_ANDROID) | |
| 69 NOTREACHED(); | |
| 70 #else | |
| 71 display_compositor::GLHelper* gl_helper = | |
| 72 ImageTransportFactory::GetInstance()->GetGLHelper(); | |
| 73 // UpdateReleaseSyncToken() creates a new sync_token using |gl_helper|, so | |
| 74 // wait the given |sync_token| using |gl_helper|. | |
| 75 if (gl_helper) { | |
| 76 gl_helper->WaitSyncToken(sync_token); | |
| 77 SyncTokenClientImpl client(gl_helper); | |
| 78 video_frame->UpdateReleaseSyncToken(&client); | |
| 79 } | |
| 80 #endif | |
| 81 } | |
| 82 | |
| 83 std::unique_ptr<media::VideoCaptureJpegDecoder> CreateGpuJpegDecoder( | 49 std::unique_ptr<media::VideoCaptureJpegDecoder> CreateGpuJpegDecoder( |
| 84 const media::VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) { | 50 const media::VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) { |
| 85 return base::MakeUnique<VideoCaptureGpuJpegDecoder>(decode_done_cb); | 51 return base::MakeUnique<VideoCaptureGpuJpegDecoder>(decode_done_cb); |
| 86 } | 52 } |
| 87 | 53 |
| 88 // Decorator for media::VideoFrameReceiver that forwards all incoming calls | 54 // Decorator for media::VideoFrameReceiver that forwards all incoming calls |
| 89 // to the Browser IO thread. | 55 // to the Browser IO thread. |
| 90 class VideoFrameReceiverOnIOThread : public media::VideoFrameReceiver { | 56 class VideoFrameReceiverOnIOThread : public media::VideoFrameReceiver { |
| 91 public: | 57 public: |
| 92 explicit VideoFrameReceiverOnIOThread( | 58 explicit VideoFrameReceiverOnIOThread( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 // ID used for identifying this object. | 109 // ID used for identifying this object. |
| 144 const VideoCaptureControllerID controller_id; | 110 const VideoCaptureControllerID controller_id; |
| 145 VideoCaptureControllerEventHandler* const event_handler; | 111 VideoCaptureControllerEventHandler* const event_handler; |
| 146 | 112 |
| 147 const media::VideoCaptureSessionId session_id; | 113 const media::VideoCaptureSessionId session_id; |
| 148 const media::VideoCaptureParams parameters; | 114 const media::VideoCaptureParams parameters; |
| 149 | 115 |
| 150 // Buffers that are currently known to this client. | 116 // Buffers that are currently known to this client. |
| 151 std::set<int> known_buffers; | 117 std::set<int> known_buffers; |
| 152 | 118 |
| 153 // Buffers currently held by this client, and sync token callback to call when | 119 // Buffers currently held by this client. |
| 154 // they are returned from the client. | 120 std::set<int> buffers_in_use; |
| 155 typedef std::map<int, scoped_refptr<VideoFrame>> ActiveBufferMap; | |
| 156 ActiveBufferMap active_buffers; | |
| 157 | 121 |
| 158 // State of capture session, controlled by VideoCaptureManager directly. This | 122 // State of capture session, controlled by VideoCaptureManager directly. This |
| 159 // transitions to true as soon as StopSession() occurs, at which point the | 123 // transitions to true as soon as StopSession() occurs, at which point the |
| 160 // client is sent an OnEnded() event. However, because the client retains a | 124 // client is sent an OnEnded() event. However, because the client retains a |
| 161 // VideoCaptureController* pointer, its ControllerClient entry lives on until | 125 // VideoCaptureController* pointer, its ControllerClient entry lives on until |
| 162 // it unregisters itself via RemoveClient(), which may happen asynchronously. | 126 // it unregisters itself via RemoveClient(), which may happen asynchronously. |
| 163 // | 127 // |
| 164 // TODO(nick): If we changed the semantics of VideoCaptureHost so that | 128 // TODO(nick): If we changed the semantics of VideoCaptureHost so that |
| 165 // OnEnded() events were processed synchronously (with the RemoveClient() done | 129 // OnEnded() events were processed synchronously (with the RemoveClient() done |
| 166 // implicitly), we could avoid tracking this state here in the Controller, and | 130 // implicitly), we could avoid tracking this state here in the Controller, and |
| 167 // simplify the code in both places. | 131 // simplify the code in both places. |
| 168 bool session_closed; | 132 bool session_closed; |
| 169 | 133 |
| 170 // Indicates whether the client is paused, if true, VideoCaptureController | 134 // Indicates whether the client is paused, if true, VideoCaptureController |
| 171 // stops updating its buffer. | 135 // stops updating its buffer. |
| 172 bool paused; | 136 bool paused; |
| 173 }; | 137 }; |
| 174 | 138 |
| 139 VideoCaptureController::BufferState::BufferState() | |
| 140 : buffer_id_(0), | |
| 141 frame_receiver_observer_(nullptr), | |
| 142 max_consumer_utilization_(0), | |
| 143 consumer_hold_count_(0) {} | |
| 144 | |
| 145 VideoCaptureController::BufferState::BufferState( | |
| 146 int buffer_id, | |
| 147 media::FrameReceiverObserver* frame_receiver_observer, | |
| 148 const scoped_refptr<media::VideoCaptureBufferPool>& buffer_pool, | |
| 149 const scoped_refptr<media::VideoFrame>& frame) | |
| 150 : buffer_id_(buffer_id), | |
| 151 frame_receiver_observer_(frame_receiver_observer), | |
| 152 buffer_pool_(buffer_pool), | |
| 153 frame_(frame), | |
| 154 max_consumer_utilization_( | |
| 155 media::FrameReceiverObserver::no_utilization_recorded()), | |
| 156 consumer_hold_count_(0) {} | |
| 157 | |
| 158 VideoCaptureController::BufferState::BufferState( | |
| 159 VideoCaptureController::BufferState&& other) = default; | |
| 160 | |
| 161 VideoCaptureController::BufferState::~BufferState() = default; | |
| 162 | |
| 163 VideoCaptureController::BufferState& VideoCaptureController::BufferState:: | |
| 164 operator=(VideoCaptureController::BufferState&& other) = default; | |
| 165 | |
| 166 void VideoCaptureController::BufferState::RecordConsumerUtilization( | |
| 167 double utilization) { | |
| 168 if (std::isfinite(utilization) && utilization >= 0.0) { | |
| 169 max_consumer_utilization_ = | |
| 170 std::max(max_consumer_utilization_, utilization); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 void VideoCaptureController::BufferState::IncreaseConsumerCount() { | |
| 175 if (consumer_hold_count_ == 0) | |
| 176 buffer_pool_->HoldForConsumers(buffer_id_, 1); | |
|
miu
2016/12/01 05:25:17
Looks like the "hold count" tracking isn't needed
chfremer
2016/12/02 01:28:28
Excellent observation. I put it on the list of fut
| |
| 177 consumer_hold_count_++; | |
| 178 } | |
| 179 | |
| 180 void VideoCaptureController::BufferState::DecreaseConsumerCount() { | |
| 181 consumer_hold_count_--; | |
| 182 if (consumer_hold_count_ == 0) { | |
| 183 if (frame_receiver_observer_ != nullptr && | |
| 184 max_consumer_utilization_ != | |
| 185 media::FrameReceiverObserver::no_utilization_recorded()) { | |
| 186 frame_receiver_observer_->OnReceiverReportingUtilization( | |
| 187 buffer_id_, max_consumer_utilization_); | |
| 188 } | |
| 189 buffer_pool_->RelinquishConsumerHold(buffer_id_, 1); | |
| 190 max_consumer_utilization_ = | |
| 191 media::FrameReceiverObserver::no_utilization_recorded(); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 bool VideoCaptureController::BufferState::HasZeroConsumerHoldCount() { | |
| 196 return consumer_hold_count_ == 0; | |
| 197 } | |
| 198 | |
| 175 VideoCaptureController::VideoCaptureController(int max_buffers) | 199 VideoCaptureController::VideoCaptureController(int max_buffers) |
| 176 : buffer_pool_(new media::VideoCaptureBufferPoolImpl( | 200 : buffer_pool_(new media::VideoCaptureBufferPoolImpl( |
| 177 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), | 201 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), |
| 178 max_buffers)), | 202 max_buffers)), |
| 203 frame_receiver_observer_(nullptr), | |
| 179 state_(VIDEO_CAPTURE_STATE_STARTED), | 204 state_(VIDEO_CAPTURE_STATE_STARTED), |
| 180 has_received_frames_(false), | 205 has_received_frames_(false), |
| 181 weak_ptr_factory_(this) { | 206 weak_ptr_factory_(this) { |
| 182 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 183 } | 208 } |
| 184 | 209 |
| 185 base::WeakPtr<VideoCaptureController> | 210 base::WeakPtr<VideoCaptureController> |
| 186 VideoCaptureController::GetWeakPtrForIOThread() { | 211 VideoCaptureController::GetWeakPtrForIOThread() { |
| 187 return weak_ptr_factory_.GetWeakPtr(); | 212 return weak_ptr_factory_.GetWeakPtr(); |
| 188 } | 213 } |
| 189 | 214 |
| 190 std::unique_ptr<media::VideoCaptureDevice::Client> | 215 void VideoCaptureController::SetFrameReceiverObserver( |
| 216 media::FrameReceiverObserver* frame_receiver_observer) { | |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 218 frame_receiver_observer_ = frame_receiver_observer; | |
| 219 } | |
| 220 | |
| 221 std::unique_ptr<media::VideoCaptureDeviceClient> | |
| 191 VideoCaptureController::NewDeviceClient() { | 222 VideoCaptureController::NewDeviceClient() { |
| 192 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 193 return base::MakeUnique<media::VideoCaptureDeviceClient>( | 224 return base::MakeUnique<media::VideoCaptureDeviceClient>( |
| 194 base::MakeUnique<VideoFrameReceiverOnIOThread>( | 225 base::MakeUnique<VideoFrameReceiverOnIOThread>( |
| 195 this->GetWeakPtrForIOThread()), | 226 this->GetWeakPtrForIOThread()), |
| 196 buffer_pool_, | 227 buffer_pool_, |
| 197 base::Bind(&CreateGpuJpegDecoder, | 228 base::Bind(&CreateGpuJpegDecoder, |
| 198 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame, | 229 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame, |
| 199 this->GetWeakPtrForIOThread()))); | 230 this->GetWeakPtrForIOThread())), |
| 231 base::ThreadTaskRunnerHandle::Get()); | |
| 200 } | 232 } |
| 201 | 233 |
| 202 void VideoCaptureController::AddClient( | 234 void VideoCaptureController::AddClient( |
| 203 VideoCaptureControllerID id, | 235 VideoCaptureControllerID id, |
| 204 VideoCaptureControllerEventHandler* event_handler, | 236 VideoCaptureControllerEventHandler* event_handler, |
| 205 media::VideoCaptureSessionId session_id, | 237 media::VideoCaptureSessionId session_id, |
| 206 const media::VideoCaptureParams& params) { | 238 const media::VideoCaptureParams& params) { |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 239 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 208 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id | 240 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id |
| 209 << ", session_id=" << session_id | 241 << ", session_id=" << session_id |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 VideoCaptureControllerID id, | 284 VideoCaptureControllerID id, |
| 253 VideoCaptureControllerEventHandler* event_handler) { | 285 VideoCaptureControllerEventHandler* event_handler) { |
| 254 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 286 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 255 DVLOG(1) << "VideoCaptureController::RemoveClient, id " << id; | 287 DVLOG(1) << "VideoCaptureController::RemoveClient, id " << id; |
| 256 | 288 |
| 257 ControllerClient* client = FindClient(id, event_handler, controller_clients_); | 289 ControllerClient* client = FindClient(id, event_handler, controller_clients_); |
| 258 if (!client) | 290 if (!client) |
| 259 return kInvalidMediaCaptureSessionId; | 291 return kInvalidMediaCaptureSessionId; |
| 260 | 292 |
| 261 // Take back all buffers held by the |client|. | 293 // Take back all buffers held by the |client|. |
| 262 for (const auto& buffer : client->active_buffers) | 294 for (const auto& buffer_id : client->buffers_in_use) |
| 263 buffer_pool_->RelinquishConsumerHold(buffer.first, 1); | 295 buffer_id_to_state_map_[buffer_id].DecreaseConsumerCount(); |
| 264 client->active_buffers.clear(); | 296 client->buffers_in_use.clear(); |
| 265 | 297 |
| 266 int session_id = client->session_id; | 298 int session_id = client->session_id; |
| 267 controller_clients_.remove_if( | 299 controller_clients_.remove_if( |
| 268 [client](const std::unique_ptr<ControllerClient>& ptr) { | 300 [client](const std::unique_ptr<ControllerClient>& ptr) { |
| 269 return ptr.get() == client; | 301 return ptr.get() == client; |
| 270 }); | 302 }); |
| 271 | 303 |
| 272 return session_id; | 304 return session_id; |
| 273 } | 305 } |
| 274 | 306 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 VideoCaptureControllerEventHandler* event_handler, | 378 VideoCaptureControllerEventHandler* event_handler, |
| 347 int buffer_id, | 379 int buffer_id, |
| 348 const gpu::SyncToken& sync_token, | 380 const gpu::SyncToken& sync_token, |
| 349 double consumer_resource_utilization) { | 381 double consumer_resource_utilization) { |
| 350 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 382 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 351 | 383 |
| 352 ControllerClient* client = FindClient(id, event_handler, controller_clients_); | 384 ControllerClient* client = FindClient(id, event_handler, controller_clients_); |
| 353 | 385 |
| 354 // If this buffer is not held by this client, or this client doesn't exist | 386 // If this buffer is not held by this client, or this client doesn't exist |
| 355 // in controller, do nothing. | 387 // in controller, do nothing. |
| 356 ControllerClient::ActiveBufferMap::iterator iter; | 388 if (!client || (client->buffers_in_use.find(buffer_id) == |
| 357 if (!client || (iter = client->active_buffers.find(buffer_id)) == | 389 client->buffers_in_use.end())) { |
| 358 client->active_buffers.end()) { | |
| 359 NOTREACHED(); | 390 NOTREACHED(); |
| 360 return; | 391 return; |
| 361 } | 392 } |
| 362 | 393 |
| 363 // Set the RESOURCE_UTILIZATION to the maximum of those provided by each | 394 BufferState& buffer_state = buffer_id_to_state_map_[buffer_id]; |
| 364 // consumer (via separate calls to this method that refer to the same | 395 buffer_state.RecordConsumerUtilization(consumer_resource_utilization); |
| 365 // VideoFrame). The producer of this VideoFrame may check this value, after | 396 buffer_state.DecreaseConsumerCount(); |
| 366 // all consumer holds are relinquished, to make quality versus performance | 397 client->buffers_in_use.erase(buffer_id); |
| 367 // trade-off decisions. | |
| 368 scoped_refptr<VideoFrame> frame = iter->second; | |
| 369 if (std::isfinite(consumer_resource_utilization) && | |
| 370 consumer_resource_utilization >= 0.0) { | |
| 371 double resource_utilization = -1.0; | |
| 372 if (frame->metadata()->GetDouble(VideoFrameMetadata::RESOURCE_UTILIZATION, | |
| 373 &resource_utilization)) { | |
| 374 frame->metadata()->SetDouble(VideoFrameMetadata::RESOURCE_UTILIZATION, | |
| 375 std::max(consumer_resource_utilization, | |
| 376 resource_utilization)); | |
| 377 } else { | |
| 378 frame->metadata()->SetDouble(VideoFrameMetadata::RESOURCE_UTILIZATION, | |
| 379 consumer_resource_utilization); | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 client->active_buffers.erase(iter); | |
| 384 buffer_pool_->RelinquishConsumerHold(buffer_id, 1); | |
| 385 | |
| 386 #if defined(OS_ANDROID) | |
| 387 DCHECK(!sync_token.HasData()); | |
| 388 #endif | |
| 389 if (sync_token.HasData()) | |
| 390 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 391 base::Bind(&ReturnVideoFrame, frame, sync_token)); | |
| 392 } | 398 } |
| 393 | 399 |
| 394 const media::VideoCaptureFormat& | 400 const media::VideoCaptureFormat& |
| 395 VideoCaptureController::GetVideoCaptureFormat() const { | 401 VideoCaptureController::GetVideoCaptureFormat() const { |
| 396 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 402 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 397 return video_capture_format_; | 403 return video_capture_format_; |
| 398 } | 404 } |
| 399 | 405 |
| 400 VideoCaptureController::~VideoCaptureController() { | 406 VideoCaptureController::~VideoCaptureController() { |
| 401 } | 407 } |
| 402 | 408 |
| 403 void VideoCaptureController::OnIncomingCapturedVideoFrame( | 409 void VideoCaptureController::OnIncomingCapturedVideoFrame( |
| 404 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | 410 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, |
| 405 scoped_refptr<VideoFrame> frame) { | 411 scoped_refptr<VideoFrame> frame) { |
| 406 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 412 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 407 const int buffer_id = buffer->id(); | 413 const int buffer_id = buffer->id(); |
| 408 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId); | 414 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId); |
| 409 | 415 |
| 410 int count = 0; | 416 if (buffer_id_to_state_map_.find(buffer_id) == |
|
miu
2016/12/01 05:25:17
As mentioned in the header file, you might be able
chfremer
2016/12/02 01:28:28
Very nice solution. Thanks.
Unfortunately, in orde
| |
| 417 buffer_id_to_state_map_.end()) { | |
| 418 BufferState new_buffer_state(buffer_id, frame_receiver_observer_, | |
| 419 buffer_pool_, frame); | |
| 420 buffer_id_to_state_map_[buffer_id] = std::move(new_buffer_state); | |
| 421 } | |
| 422 BufferState& buffer_state = buffer_id_to_state_map_[buffer_id]; | |
| 423 DCHECK(buffer_state.HasZeroConsumerHoldCount()); | |
| 424 | |
| 411 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 425 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 412 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { | 426 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { |
| 413 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, | 427 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, |
| 414 video_capture_format_.frame_rate); | 428 video_capture_format_.frame_rate); |
| 415 } | 429 } |
| 416 std::unique_ptr<base::DictionaryValue> metadata( | 430 std::unique_ptr<base::DictionaryValue> metadata( |
| 417 new base::DictionaryValue()); | 431 new base::DictionaryValue()); |
| 418 frame->metadata()->MergeInternalValuesInto(metadata.get()); | 432 frame->metadata()->MergeInternalValuesInto(metadata.get()); |
| 419 | 433 |
| 420 // Only I420 and Y16 pixel formats are currently supported. | 434 // Only I420 and Y16 pixel formats are currently supported. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 435 if (client->session_closed || client->paused) | 449 if (client->session_closed || client->paused) |
| 436 continue; | 450 continue; |
| 437 | 451 |
| 438 // On the first use of a buffer on a client, share the memory handles. | 452 // On the first use of a buffer on a client, share the memory handles. |
| 439 const bool is_new_buffer = client->known_buffers.insert(buffer_id).second; | 453 const bool is_new_buffer = client->known_buffers.insert(buffer_id).second; |
| 440 if (is_new_buffer) | 454 if (is_new_buffer) |
| 441 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); | 455 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); |
| 442 | 456 |
| 443 client->event_handler->OnBufferReady(client->controller_id, buffer_id, | 457 client->event_handler->OnBufferReady(client->controller_id, buffer_id, |
| 444 frame); | 458 frame); |
| 445 const bool inserted = | 459 const bool inserted = client->buffers_in_use.insert(buffer_id).second; |
| 446 client->active_buffers.insert(std::make_pair(buffer_id, frame)) | |
| 447 .second; | |
| 448 DCHECK(inserted) << "Unexpected duplicate buffer: " << buffer_id; | 460 DCHECK(inserted) << "Unexpected duplicate buffer: " << buffer_id; |
| 449 count++; | 461 buffer_state.IncreaseConsumerCount(); |
| 450 } | 462 } |
| 451 } | 463 } |
| 452 | 464 |
| 453 if (!has_received_frames_) { | 465 if (!has_received_frames_) { |
| 454 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Width", | 466 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Width", |
| 455 frame->visible_rect().width()); | 467 frame->visible_rect().width()); |
| 456 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Height", | 468 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Height", |
| 457 frame->visible_rect().height()); | 469 frame->visible_rect().height()); |
| 458 UMA_HISTOGRAM_ASPECT_RATIO("Media.VideoCapture.AspectRatio", | 470 UMA_HISTOGRAM_ASPECT_RATIO("Media.VideoCapture.AspectRatio", |
| 459 frame->visible_rect().width(), | 471 frame->visible_rect().width(), |
| 460 frame->visible_rect().height()); | 472 frame->visible_rect().height()); |
| 461 double frame_rate = 0.0f; | 473 double frame_rate = 0.0f; |
| 462 if (!frame->metadata()->GetDouble(VideoFrameMetadata::FRAME_RATE, | 474 if (!frame->metadata()->GetDouble(VideoFrameMetadata::FRAME_RATE, |
| 463 &frame_rate)) { | 475 &frame_rate)) { |
| 464 frame_rate = video_capture_format_.frame_rate; | 476 frame_rate = video_capture_format_.frame_rate; |
| 465 } | 477 } |
| 466 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate); | 478 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate); |
| 467 has_received_frames_ = true; | 479 has_received_frames_ = true; |
| 468 } | 480 } |
| 469 | |
| 470 buffer_pool_->HoldForConsumers(buffer_id, count); | |
| 471 } | 481 } |
| 472 | 482 |
| 473 void VideoCaptureController::OnError() { | 483 void VideoCaptureController::OnError() { |
| 474 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 484 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 475 state_ = VIDEO_CAPTURE_STATE_ERROR; | 485 state_ = VIDEO_CAPTURE_STATE_ERROR; |
| 476 | 486 |
| 477 for (const auto& client : controller_clients_) { | 487 for (const auto& client : controller_clients_) { |
| 478 if (client->session_closed) | 488 if (client->session_closed) |
| 479 continue; | 489 continue; |
| 480 client->event_handler->OnError(client->controller_id); | 490 client->event_handler->OnError(client->controller_id); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 491 | 501 |
| 492 for (const auto& client : controller_clients_) { | 502 for (const auto& client : controller_clients_) { |
| 493 if (client->session_closed) | 503 if (client->session_closed) |
| 494 continue; | 504 continue; |
| 495 | 505 |
| 496 if (client->known_buffers.erase(buffer_id_to_drop)) { | 506 if (client->known_buffers.erase(buffer_id_to_drop)) { |
| 497 client->event_handler->OnBufferDestroyed(client->controller_id, | 507 client->event_handler->OnBufferDestroyed(client->controller_id, |
| 498 buffer_id_to_drop); | 508 buffer_id_to_drop); |
| 499 } | 509 } |
| 500 } | 510 } |
| 511 | |
| 512 buffer_id_to_state_map_.erase(buffer_id_to_drop); | |
| 501 } | 513 } |
| 502 | 514 |
| 503 void VideoCaptureController::DoNewBufferOnIOThread( | 515 void VideoCaptureController::DoNewBufferOnIOThread( |
| 504 ControllerClient* client, | 516 ControllerClient* client, |
| 505 media::VideoCaptureDevice::Client::Buffer* buffer, | 517 media::VideoCaptureDevice::Client::Buffer* buffer, |
| 506 const scoped_refptr<media::VideoFrame>& frame) { | 518 const scoped_refptr<media::VideoFrame>& frame) { |
| 507 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 519 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 508 DCHECK_EQ(media::VideoFrame::STORAGE_SHMEM, frame->storage_type()); | 520 DCHECK_EQ(media::VideoFrame::STORAGE_SHMEM, frame->storage_type()); |
| 509 | 521 |
| 510 const int buffer_id = buffer->id(); | 522 const int buffer_id = buffer->id(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 530 int session_id, | 542 int session_id, |
| 531 const ControllerClients& clients) { | 543 const ControllerClients& clients) { |
| 532 for (const auto& client : clients) { | 544 for (const auto& client : clients) { |
| 533 if (client->session_id == session_id) | 545 if (client->session_id == session_id) |
| 534 return client.get(); | 546 return client.get(); |
| 535 } | 547 } |
| 536 return nullptr; | 548 return nullptr; |
| 537 } | 549 } |
| 538 | 550 |
| 539 } // namespace content | 551 } // namespace content |
| OLD | NEW |