| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 ~ControllerClient() {} | 107 ~ControllerClient() {} |
| 142 | 108 |
| 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::vector<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::vector<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 int buffer_id, |
| 141 int frame_feedback_id, |
| 142 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer, |
| 143 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool, |
| 144 scoped_refptr<media::VideoFrame> frame) |
| 145 : buffer_id_(buffer_id), |
| 146 frame_feedback_id_(frame_feedback_id), |
| 147 consumer_feedback_observer_(consumer_feedback_observer), |
| 148 buffer_pool_(std::move(buffer_pool)), |
| 149 frame_(std::move(frame)), |
| 150 max_consumer_utilization_( |
| 151 media::VideoFrameConsumerFeedbackObserver::kNoUtilizationRecorded), |
| 152 consumer_hold_count_(0) {} |
| 153 |
| 154 VideoCaptureController::BufferState::~BufferState() = default; |
| 155 |
| 156 VideoCaptureController::BufferState::BufferState( |
| 157 const VideoCaptureController::BufferState& other) = default; |
| 158 |
| 159 void VideoCaptureController::BufferState::RecordConsumerUtilization( |
| 160 double utilization) { |
| 161 if (std::isfinite(utilization) && utilization >= 0.0) { |
| 162 max_consumer_utilization_ = |
| 163 std::max(max_consumer_utilization_, utilization); |
| 164 } |
| 165 } |
| 166 |
| 167 void VideoCaptureController::BufferState::IncreaseConsumerCount() { |
| 168 if (consumer_hold_count_ == 0) |
| 169 buffer_pool_->HoldForConsumers(buffer_id_, 1); |
| 170 consumer_hold_count_++; |
| 171 } |
| 172 |
| 173 void VideoCaptureController::BufferState::DecreaseConsumerCount() { |
| 174 consumer_hold_count_--; |
| 175 if (consumer_hold_count_ == 0) { |
| 176 if (consumer_feedback_observer_ != nullptr && |
| 177 max_consumer_utilization_ != |
| 178 media::VideoFrameConsumerFeedbackObserver::kNoUtilizationRecorded) { |
| 179 consumer_feedback_observer_->OnUtilizationReport( |
| 180 frame_feedback_id_, max_consumer_utilization_); |
| 181 } |
| 182 buffer_pool_->RelinquishConsumerHold(buffer_id_, 1); |
| 183 max_consumer_utilization_ = |
| 184 media::VideoFrameConsumerFeedbackObserver::kNoUtilizationRecorded; |
| 185 } |
| 186 } |
| 187 |
| 188 bool VideoCaptureController::BufferState::HasZeroConsumerHoldCount() { |
| 189 return consumer_hold_count_ == 0; |
| 190 } |
| 191 |
| 192 void VideoCaptureController::BufferState::SetConsumerFeedbackObserver( |
| 193 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer) { |
| 194 consumer_feedback_observer_ = consumer_feedback_observer; |
| 195 } |
| 196 |
| 175 VideoCaptureController::VideoCaptureController(int max_buffers) | 197 VideoCaptureController::VideoCaptureController(int max_buffers) |
| 176 : buffer_pool_(new media::VideoCaptureBufferPoolImpl( | 198 : buffer_pool_(new media::VideoCaptureBufferPoolImpl( |
| 177 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), | 199 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), |
| 178 max_buffers)), | 200 max_buffers)), |
| 201 consumer_feedback_observer_(nullptr), |
| 179 state_(VIDEO_CAPTURE_STATE_STARTED), | 202 state_(VIDEO_CAPTURE_STATE_STARTED), |
| 180 has_received_frames_(false), | 203 has_received_frames_(false), |
| 181 weak_ptr_factory_(this) { | 204 weak_ptr_factory_(this) { |
| 182 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 183 } | 206 } |
| 184 | 207 |
| 185 base::WeakPtr<VideoCaptureController> | 208 base::WeakPtr<VideoCaptureController> |
| 186 VideoCaptureController::GetWeakPtrForIOThread() { | 209 VideoCaptureController::GetWeakPtrForIOThread() { |
| 187 return weak_ptr_factory_.GetWeakPtr(); | 210 return weak_ptr_factory_.GetWeakPtr(); |
| 188 } | 211 } |
| 189 | 212 |
| 213 void VideoCaptureController::SetConsumerFeedbackObserver( |
| 214 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> |
| 215 consumer_feedback_observer) { |
| 216 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 217 consumer_feedback_observer_ = std::move(consumer_feedback_observer); |
| 218 // Update existing BufferState entries. |
| 219 for (auto& entry : buffer_id_to_state_map_) |
| 220 entry.second.SetConsumerFeedbackObserver(consumer_feedback_observer_.get()); |
| 221 } |
| 222 |
| 190 std::unique_ptr<media::VideoCaptureDevice::Client> | 223 std::unique_ptr<media::VideoCaptureDevice::Client> |
| 191 VideoCaptureController::NewDeviceClient() { | 224 VideoCaptureController::NewDeviceClient() { |
| 192 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 225 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 193 return base::MakeUnique<media::VideoCaptureDeviceClient>( | 226 return base::MakeUnique<media::VideoCaptureDeviceClient>( |
| 194 base::MakeUnique<VideoFrameReceiverOnIOThread>( | 227 base::MakeUnique<VideoFrameReceiverOnIOThread>( |
| 195 this->GetWeakPtrForIOThread()), | 228 this->GetWeakPtrForIOThread()), |
| 196 buffer_pool_, | 229 buffer_pool_, |
| 197 base::Bind(&CreateGpuJpegDecoder, | 230 base::Bind(&CreateGpuJpegDecoder, |
| 198 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame, | 231 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame, |
| 199 this->GetWeakPtrForIOThread()))); | 232 this->GetWeakPtrForIOThread()))); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 VideoCaptureControllerID id, | 285 VideoCaptureControllerID id, |
| 253 VideoCaptureControllerEventHandler* event_handler) { | 286 VideoCaptureControllerEventHandler* event_handler) { |
| 254 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 287 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 255 DVLOG(1) << "VideoCaptureController::RemoveClient, id " << id; | 288 DVLOG(1) << "VideoCaptureController::RemoveClient, id " << id; |
| 256 | 289 |
| 257 ControllerClient* client = FindClient(id, event_handler, controller_clients_); | 290 ControllerClient* client = FindClient(id, event_handler, controller_clients_); |
| 258 if (!client) | 291 if (!client) |
| 259 return kInvalidMediaCaptureSessionId; | 292 return kInvalidMediaCaptureSessionId; |
| 260 | 293 |
| 261 // Take back all buffers held by the |client|. | 294 // Take back all buffers held by the |client|. |
| 262 for (const auto& buffer : client->active_buffers) | 295 for (const auto& buffer_id : client->buffers_in_use) |
| 263 buffer_pool_->RelinquishConsumerHold(buffer.first, 1); | 296 buffer_id_to_state_map_.at(buffer_id).DecreaseConsumerCount(); |
| 264 client->active_buffers.clear(); | 297 client->buffers_in_use.clear(); |
| 265 | 298 |
| 266 int session_id = client->session_id; | 299 int session_id = client->session_id; |
| 267 controller_clients_.remove_if( | 300 controller_clients_.remove_if( |
| 268 [client](const std::unique_ptr<ControllerClient>& ptr) { | 301 [client](const std::unique_ptr<ControllerClient>& ptr) { |
| 269 return ptr.get() == client; | 302 return ptr.get() == client; |
| 270 }); | 303 }); |
| 271 | 304 |
| 272 return session_id; | 305 return session_id; |
| 273 } | 306 } |
| 274 | 307 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 VideoCaptureControllerEventHandler* event_handler, | 379 VideoCaptureControllerEventHandler* event_handler, |
| 347 int buffer_id, | 380 int buffer_id, |
| 348 const gpu::SyncToken& sync_token, | 381 const gpu::SyncToken& sync_token, |
| 349 double consumer_resource_utilization) { | 382 double consumer_resource_utilization) { |
| 350 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 383 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 351 | 384 |
| 352 ControllerClient* client = FindClient(id, event_handler, controller_clients_); | 385 ControllerClient* client = FindClient(id, event_handler, controller_clients_); |
| 353 | 386 |
| 354 // If this buffer is not held by this client, or this client doesn't exist | 387 // If this buffer is not held by this client, or this client doesn't exist |
| 355 // in controller, do nothing. | 388 // in controller, do nothing. |
| 356 ControllerClient::ActiveBufferMap::iterator iter; | 389 if (!client) { |
| 357 if (!client || (iter = client->active_buffers.find(buffer_id)) == | 390 NOTREACHED(); |
| 358 client->active_buffers.end()) { | 391 return; |
| 392 } |
| 393 auto buffers_in_use_entry_iter = |
| 394 std::find(std::begin(client->buffers_in_use), |
| 395 std::end(client->buffers_in_use), buffer_id); |
| 396 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use)) { |
| 359 NOTREACHED(); | 397 NOTREACHED(); |
| 360 return; | 398 return; |
| 361 } | 399 } |
| 362 | 400 |
| 363 // Set the RESOURCE_UTILIZATION to the maximum of those provided by each | 401 BufferState& buffer_state = buffer_id_to_state_map_.at(buffer_id); |
| 364 // consumer (via separate calls to this method that refer to the same | 402 buffer_state.RecordConsumerUtilization(consumer_resource_utilization); |
| 365 // VideoFrame). The producer of this VideoFrame may check this value, after | 403 buffer_state.DecreaseConsumerCount(); |
| 366 // all consumer holds are relinquished, to make quality versus performance | 404 client->buffers_in_use.erase(buffers_in_use_entry_iter); |
| 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 } | 405 } |
| 393 | 406 |
| 394 const media::VideoCaptureFormat& | 407 const media::VideoCaptureFormat& |
| 395 VideoCaptureController::GetVideoCaptureFormat() const { | 408 VideoCaptureController::GetVideoCaptureFormat() const { |
| 396 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 409 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 397 return video_capture_format_; | 410 return video_capture_format_; |
| 398 } | 411 } |
| 399 | 412 |
| 400 VideoCaptureController::~VideoCaptureController() { | 413 VideoCaptureController::~VideoCaptureController() { |
| 401 } | 414 } |
| 402 | 415 |
| 403 void VideoCaptureController::OnIncomingCapturedVideoFrame( | 416 void VideoCaptureController::OnIncomingCapturedVideoFrame( |
| 404 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | 417 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, |
| 405 scoped_refptr<VideoFrame> frame) { | 418 scoped_refptr<VideoFrame> frame) { |
| 406 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 419 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 407 const int buffer_id = buffer->id(); | 420 const int buffer_id = buffer->id(); |
| 408 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId); | 421 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId); |
| 409 | 422 |
| 410 int count = 0; | 423 // Insert if not exists. |
| 424 const auto it = |
| 425 buffer_id_to_state_map_ |
| 426 .insert(std::make_pair( |
| 427 buffer_id, BufferState(buffer_id, buffer->frame_feedback_id(), |
| 428 consumer_feedback_observer_.get(), |
| 429 buffer_pool_, frame))) |
| 430 .first; |
| 431 BufferState& buffer_state = it->second; |
| 432 DCHECK(buffer_state.HasZeroConsumerHoldCount()); |
| 433 |
| 411 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 434 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 412 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { | 435 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { |
| 413 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, | 436 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, |
| 414 video_capture_format_.frame_rate); | 437 video_capture_format_.frame_rate); |
| 415 } | 438 } |
| 416 std::unique_ptr<base::DictionaryValue> metadata( | 439 std::unique_ptr<base::DictionaryValue> metadata( |
| 417 new base::DictionaryValue()); | 440 new base::DictionaryValue()); |
| 418 frame->metadata()->MergeInternalValuesInto(metadata.get()); | 441 frame->metadata()->MergeInternalValuesInto(metadata.get()); |
| 419 | 442 |
| 420 // Only I420 and Y16 pixel formats are currently supported. | 443 // Only I420 and Y16 pixel formats are currently supported. |
| 421 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 || | 444 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 || |
| 422 frame->format() == media::PIXEL_FORMAT_Y16) | 445 frame->format() == media::PIXEL_FORMAT_Y16) |
| 423 << "Unsupported pixel format: " | 446 << "Unsupported pixel format: " |
| 424 << media::VideoPixelFormatToString(frame->format()); | 447 << media::VideoPixelFormatToString(frame->format()); |
| 425 | 448 |
| 426 // Sanity-checks to confirm |frame| is actually being backed by |buffer|. | 449 // Sanity-checks to confirm |frame| is actually being backed by |buffer|. |
| 427 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM); | 450 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM); |
| 428 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer->data(0) && | 451 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer->data(0) && |
| 429 (frame->data(media::VideoFrame::kYPlane) < | 452 (frame->data(media::VideoFrame::kYPlane) < |
| 430 (reinterpret_cast<const uint8_t*>(buffer->data(0)) + | 453 (reinterpret_cast<const uint8_t*>(buffer->data(0)) + |
| 431 buffer->mapped_size()))) | 454 buffer->mapped_size()))) |
| 432 << "VideoFrame does not appear to be backed by Buffer"; | 455 << "VideoFrame does not appear to be backed by Buffer"; |
| 433 | 456 |
| 434 for (const auto& client : controller_clients_) { | 457 for (const auto& client : controller_clients_) { |
| 435 if (client->session_closed || client->paused) | 458 if (client->session_closed || client->paused) |
| 436 continue; | 459 continue; |
| 437 | 460 |
| 438 // On the first use of a buffer on a client, share the memory handles. | 461 // 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; | 462 auto known_buffers_entry_iter = |
| 463 std::find(std::begin(client->known_buffers), |
| 464 std::end(client->known_buffers), buffer_id); |
| 465 bool is_new_buffer = false; |
| 466 if (known_buffers_entry_iter == std::end(client->known_buffers)) { |
| 467 client->known_buffers.push_back(buffer_id); |
| 468 is_new_buffer = true; |
| 469 } |
| 440 if (is_new_buffer) | 470 if (is_new_buffer) |
| 441 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); | 471 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); |
| 442 | 472 |
| 443 client->event_handler->OnBufferReady(client->controller_id, buffer_id, | 473 client->event_handler->OnBufferReady(client->controller_id, buffer_id, |
| 444 frame); | 474 frame); |
| 445 const bool inserted = | 475 |
| 446 client->active_buffers.insert(std::make_pair(buffer_id, frame)) | 476 auto buffers_in_use_entry_iter = |
| 447 .second; | 477 std::find(std::begin(client->buffers_in_use), |
| 448 DCHECK(inserted) << "Unexpected duplicate buffer: " << buffer_id; | 478 std::end(client->buffers_in_use), buffer_id); |
| 449 count++; | 479 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use)) |
| 480 client->buffers_in_use.push_back(buffer_id); |
| 481 else |
| 482 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id; |
| 483 buffer_state.IncreaseConsumerCount(); |
| 450 } | 484 } |
| 451 } | 485 } |
| 452 | 486 |
| 453 if (!has_received_frames_) { | 487 if (!has_received_frames_) { |
| 454 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Width", | 488 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Width", |
| 455 frame->visible_rect().width()); | 489 frame->visible_rect().width()); |
| 456 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Height", | 490 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Height", |
| 457 frame->visible_rect().height()); | 491 frame->visible_rect().height()); |
| 458 UMA_HISTOGRAM_ASPECT_RATIO("Media.VideoCapture.AspectRatio", | 492 UMA_HISTOGRAM_ASPECT_RATIO("Media.VideoCapture.AspectRatio", |
| 459 frame->visible_rect().width(), | 493 frame->visible_rect().width(), |
| 460 frame->visible_rect().height()); | 494 frame->visible_rect().height()); |
| 461 double frame_rate = 0.0f; | 495 double frame_rate = 0.0f; |
| 462 if (!frame->metadata()->GetDouble(VideoFrameMetadata::FRAME_RATE, | 496 if (!frame->metadata()->GetDouble(VideoFrameMetadata::FRAME_RATE, |
| 463 &frame_rate)) { | 497 &frame_rate)) { |
| 464 frame_rate = video_capture_format_.frame_rate; | 498 frame_rate = video_capture_format_.frame_rate; |
| 465 } | 499 } |
| 466 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate); | 500 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate); |
| 467 has_received_frames_ = true; | 501 has_received_frames_ = true; |
| 468 } | 502 } |
| 469 | |
| 470 buffer_pool_->HoldForConsumers(buffer_id, count); | |
| 471 } | 503 } |
| 472 | 504 |
| 473 void VideoCaptureController::OnError() { | 505 void VideoCaptureController::OnError() { |
| 474 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 506 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 475 state_ = VIDEO_CAPTURE_STATE_ERROR; | 507 state_ = VIDEO_CAPTURE_STATE_ERROR; |
| 476 | 508 |
| 477 for (const auto& client : controller_clients_) { | 509 for (const auto& client : controller_clients_) { |
| 478 if (client->session_closed) | 510 if (client->session_closed) |
| 479 continue; | 511 continue; |
| 480 client->event_handler->OnError(client->controller_id); | 512 client->event_handler->OnError(client->controller_id); |
| 481 } | 513 } |
| 482 } | 514 } |
| 483 | 515 |
| 484 void VideoCaptureController::OnLog(const std::string& message) { | 516 void VideoCaptureController::OnLog(const std::string& message) { |
| 485 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 517 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 486 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); | 518 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); |
| 487 } | 519 } |
| 488 | 520 |
| 489 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { | 521 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { |
| 490 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 522 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 491 | 523 |
| 492 for (const auto& client : controller_clients_) { | 524 for (const auto& client : controller_clients_) { |
| 493 if (client->session_closed) | 525 if (client->session_closed) |
| 494 continue; | 526 continue; |
| 495 | 527 |
| 496 if (client->known_buffers.erase(buffer_id_to_drop)) { | 528 auto known_buffers_entry_iter = |
| 529 std::find(std::begin(client->known_buffers), |
| 530 std::end(client->known_buffers), buffer_id_to_drop); |
| 531 if (known_buffers_entry_iter != std::end(client->known_buffers)) { |
| 532 client->known_buffers.erase(known_buffers_entry_iter); |
| 497 client->event_handler->OnBufferDestroyed(client->controller_id, | 533 client->event_handler->OnBufferDestroyed(client->controller_id, |
| 498 buffer_id_to_drop); | 534 buffer_id_to_drop); |
| 499 } | 535 } |
| 500 } | 536 } |
| 537 |
| 538 buffer_id_to_state_map_.erase(buffer_id_to_drop); |
| 501 } | 539 } |
| 502 | 540 |
| 503 void VideoCaptureController::DoNewBufferOnIOThread( | 541 void VideoCaptureController::DoNewBufferOnIOThread( |
| 504 ControllerClient* client, | 542 ControllerClient* client, |
| 505 media::VideoCaptureDevice::Client::Buffer* buffer, | 543 media::VideoCaptureDevice::Client::Buffer* buffer, |
| 506 const scoped_refptr<media::VideoFrame>& frame) { | 544 const scoped_refptr<media::VideoFrame>& frame) { |
| 507 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 545 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 508 DCHECK_EQ(media::VideoFrame::STORAGE_SHMEM, frame->storage_type()); | 546 DCHECK_EQ(media::VideoFrame::STORAGE_SHMEM, frame->storage_type()); |
| 509 | 547 |
| 510 const int buffer_id = buffer->id(); | 548 const int buffer_id = buffer->id(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 530 int session_id, | 568 int session_id, |
| 531 const ControllerClients& clients) { | 569 const ControllerClients& clients) { |
| 532 for (const auto& client : clients) { | 570 for (const auto& client : clients) { |
| 533 if (client->session_id == session_id) | 571 if (client->session_id == session_id) |
| 534 return client.get(); | 572 return client.get(); |
| 535 } | 573 } |
| 536 return nullptr; | 574 return nullptr; |
| 537 } | 575 } |
| 538 | 576 |
| 539 } // namespace content | 577 } // namespace content |
| OLD | NEW |