| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/renderer_host/video_capture_memory.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/stl_util-inl.h" |
| 9 #include "media/video/capture/video_capture_color_conversion.h" |
| 10 |
| 11 // The number of TransportDIBs VideoCaptureMemory allocate. |
| 12 static const unsigned int kNoOfDIBS = 3; |
| 13 |
| 14 VideoCaptureMemory::VideoCaptureMemory( |
| 15 VideoCaptureMemory::EventHandler* event_handler, |
| 16 VCEntryId entry) |
| 17 : event_handler_(*event_handler), |
| 18 entry_id_(entry) {} |
| 19 |
| 20 VideoCaptureMemory::~VideoCaptureMemory() { |
| 21 // Delete all TransportDIBs |
| 22 STLDeleteContainerPairSecondPointers(owned_dibs_.begin(), |
| 23 owned_dibs_.end()); |
| 24 owned_dibs_.clear(); |
| 25 } |
| 26 |
| 27 VideoCaptureMemory::VCEntryId VideoCaptureMemory::entry_id() { |
| 28 return entry_id_; |
| 29 } |
| 30 |
| 31 bool VideoCaptureMemory::AddTransportDIB(TransportDIB::Handle handle) { |
| 32 // Check if we own this handle |
| 33 const bool own_dib = owned_dibs_.find(handle) != owned_dibs_.end(); |
| 34 if (!own_dib) { |
| 35 return false; |
| 36 } |
| 37 |
| 38 lock_.Acquire(); |
| 39 free_dibs_.push_back(handle); |
| 40 lock_.Release(); |
| 41 return true; |
| 42 } |
| 43 |
| 44 bool VideoCaptureMemory::ReadyToDelete() { |
| 45 base::AutoLock lock(lock_); |
| 46 return free_dibs_.size() == owned_dibs_.size(); |
| 47 } |
| 48 |
| 49 void VideoCaptureMemory::OnIncomingCapturedFrame(const uint8* data, |
| 50 int length, |
| 51 base::Time timestamp) { |
| 52 TransportDIB::Handle handle; |
| 53 // Check if there is a TransportDIB to fill. |
| 54 bool buffer_exist = false; |
| 55 lock_.Acquire(); |
| 56 if (free_dibs_.size() > 0) { |
| 57 handle = free_dibs_.back(); |
| 58 free_dibs_.pop_back(); |
| 59 buffer_exist = true; |
| 60 } |
| 61 lock_.Release(); |
| 62 |
| 63 if (!buffer_exist) { |
| 64 return; |
| 65 } |
| 66 |
| 67 TransportDIB* dib = TransportDIB::Map(handle); |
| 68 uint8* target = static_cast<uint8*> (dib->memory()); |
| 69 CHECK(dib->size() >= (unsigned int) (frame_info_.width*frame_info_.height*3) / |
| 70 2); |
| 71 |
| 72 // Do color conversion from the camera format to I420. |
| 73 media::VideoCaptureColorConversion::ConvertToI420(frame_info_.color, data, |
| 74 frame_info_.width, |
| 75 frame_info_.height, |
| 76 target); |
| 77 event_handler_.OnBufferReady(entry_id_, handle, timestamp); |
| 78 } |
| 79 |
| 80 void VideoCaptureMemory::OnError() { |
| 81 event_handler_.OnError(entry_id_); |
| 82 } |
| 83 |
| 84 void VideoCaptureMemory::OnFrameInfo( |
| 85 const media::VideoCaptureDevice::Capability& info) { |
| 86 DCHECK(owned_dibs_.empty()); |
| 87 // Lock needed since the buffers are used in OnIncomingFrame. |
| 88 // And we need to use it there in order to avoid memcpy of complete frames |
| 89 lock_.Acquire(); |
| 90 const size_t needed_size = (info.width * info.height * 3) / 2; |
| 91 for (unsigned int i = 0; i < kNoOfDIBS; ++i) { |
| 92 TransportDIB* dib = TransportDIB::Create(needed_size, i); |
| 93 if (!dib) { |
| 94 break; |
| 95 } |
| 96 owned_dibs_.insert(std::make_pair(dib->handle(), dib)); |
| 97 free_dibs_.push_back(dib->handle()); |
| 98 } |
| 99 frame_info_= info; |
| 100 lock_.Release(); |
| 101 |
| 102 // Check that all Dibs where created |
| 103 if (owned_dibs_.size() != kNoOfDIBS) { |
| 104 event_handler_.OnError(entry_id_); |
| 105 } |
| 106 event_handler_.OnFrameInfo(entry_id_, info.width, info.height, |
| 107 info.frame_rate); |
| 108 } |
| OLD | NEW |