Chromium Code Reviews| 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 "content/browser/browser_thread.h" | |
| 10 #include "content/browser/media_stream/video_capture_manager.h" | |
| 11 #include "media/base/yuv_convert.h" | |
| 12 | |
| 13 // The number of TransportDIBs VideoCaptureMemory allocate. | |
| 14 static const unsigned int kNoOfDIBS = 3; | |
| 15 | |
| 16 VideoCaptureMemory::VideoCaptureMemory( | |
| 17 VideoCaptureMemoryId id, | |
| 18 VideoCaptureMemory::EventHandler* event_handler) | |
| 19 : report_ready_to_delete_(false), | |
| 20 event_handler_(*event_handler), | |
|
wjia(left Chromium)
2011/05/19 04:24:37
any reason to duplicate event_handler?
Per K
2011/05/19 08:55:31
Sorry I don't understand? event_handler_ is a refe
| |
| 21 id_(id) {} | |
| 22 | |
| 23 VideoCaptureMemory::~VideoCaptureMemory() { | |
| 24 // Delete all TransportDIBs | |
| 25 STLDeleteContainerPairSecondPointers(owned_dibs_.begin(), | |
| 26 owned_dibs_.end()); | |
| 27 owned_dibs_.clear(); | |
|
wjia(left Chromium)
2011/05/19 04:24:37
dtor of std::map will clear the objects.
Per K
2011/05/19 08:55:31
removed.
| |
| 28 } | |
| 29 | |
| 30 void VideoCaptureMemory::StartCapture(const media::VideoCaptureParams& params) { | |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 32 | |
| 33 params_ = params; | |
| 34 media_stream::VideoCaptureManager* manager = | |
| 35 media_stream::VideoCaptureManager::Get(); | |
| 36 // Order the manager to start the actual capture. | |
| 37 manager->Start(params, this); | |
| 38 } | |
| 39 | |
| 40 void VideoCaptureMemory::StopCapture(Task* stopped_task) { | |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 42 | |
| 43 media_stream::VideoCaptureManager* manager = | |
| 44 media_stream::VideoCaptureManager::Get(); | |
| 45 manager->Stop(params_.session_id, | |
| 46 NewRunnableMethod(this, &VideoCaptureMemory::OnDeviceStopped, | |
| 47 stopped_task)); | |
| 48 } | |
| 49 | |
| 50 void VideoCaptureMemory::ReturnTransportDIB(TransportDIB::Handle handle) { | |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 52 // Check if we own this handle | |
| 53 CHECK(owned_dibs_.find(handle) != owned_dibs_.end()); | |
| 54 | |
| 55 bool ready_to_delete = false; | |
|
wjia(left Chromium)
2011/05/19 04:24:37
ready_to_delete will be set by line 58 anyway.
Per K
2011/05/19 08:55:31
Done.
| |
| 56 lock_.Acquire(); | |
| 57 free_dibs_.push_back(handle); | |
| 58 ready_to_delete = (free_dibs_.size() == owned_dibs_.size()); | |
| 59 lock_.Release(); | |
| 60 | |
| 61 if (report_ready_to_delete_ && ready_to_delete) { | |
| 62 event_handler_.OnReadyToDelete(id_); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 /////////////////////////////////////////////////////////////////////////////// | |
| 67 // Implements VideoCaptureDevice::EventHandler. | |
| 68 void VideoCaptureMemory::OnIncomingCapturedFrame(const uint8* data, | |
| 69 int length, | |
| 70 base::Time timestamp) { | |
| 71 TransportDIB::Handle handle; | |
| 72 // Check if there is a TransportDIB to fill. | |
| 73 bool buffer_exist = false; | |
| 74 lock_.Acquire(); | |
| 75 if (!report_ready_to_delete_ && free_dibs_.size() > 0) { | |
| 76 handle = free_dibs_.back(); | |
| 77 free_dibs_.pop_back(); | |
| 78 buffer_exist = true; | |
| 79 } | |
| 80 lock_.Release(); | |
| 81 | |
| 82 if (!buffer_exist) { | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 TransportDIB* dib = TransportDIB::Map(handle); | |
| 87 uint8* target = static_cast<uint8*> (dib->memory()); | |
| 88 CHECK(dib->size() >= (unsigned int) (frame_info_.width*frame_info_.height*3) / | |
| 89 2); | |
| 90 | |
| 91 // Do color conversion from the camera format to I420. | |
| 92 switch (frame_info_.color) { | |
| 93 case media::VideoCaptureDevice::kColorUnknown: // Color format not set | |
| 94 break; | |
| 95 case media::VideoCaptureDevice::kI420: { | |
| 96 memcpy(target, data, (frame_info_.width * frame_info_.height * 3) / 2); | |
| 97 break; | |
| 98 } | |
| 99 case media::VideoCaptureDevice::kYUY2: { | |
| 100 uint8* yplane = target; | |
| 101 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 102 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 103 media::ConvertYUY2ToYUV(data, yplane, uplane, vplane, frame_info_.width, | |
| 104 frame_info_.height); | |
| 105 break; | |
| 106 } | |
| 107 case media::VideoCaptureDevice::kRGB24: { | |
| 108 #if defined(OS_WIN) // RGB on Windows start at the bottom line. | |
|
wjia(left Chromium)
2011/05/19 04:24:37
all # should start at the beginning of the line.
Per K
2011/05/19 08:55:31
Done.
| |
| 109 uint8* yplane = target; | |
| 110 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 111 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 112 int ystride = frame_info_.width; | |
| 113 int uvstride = frame_info_.width / 2; | |
| 114 int rgb_stride = - 3 * frame_info_.width; | |
| 115 const uint8* rgb_src = data + 3 * frame_info_.width * | |
| 116 (frame_info_.height -1); | |
| 117 #else | |
| 118 uint8* yplane = target; | |
| 119 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 120 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 121 int ystride = frame_info_.width; | |
| 122 int uvstride = frame_info_.width / 2; | |
| 123 int rgb_stride = 3 * frame_info_.width; | |
| 124 const uint8* rgb_src = data; | |
| 125 #endif | |
| 126 media::ConvertRGB24ToYUV(rgb_src, yplane, uplane, vplane, | |
| 127 frame_info_.width, frame_info_.height, | |
| 128 rgb_stride, ystride, uvstride); | |
| 129 break; | |
| 130 } | |
| 131 case media::VideoCaptureDevice::kARGB: { | |
| 132 uint8* yplane = target; | |
| 133 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 134 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 135 media::ConvertRGB32ToYUV(data, yplane, uplane, vplane, frame_info_.width, | |
| 136 frame_info_.height, frame_info_.width * 4, | |
| 137 frame_info_.width, frame_info_.width / 2); | |
|
wjia(left Chromium)
2011/05/19 04:24:37
indent
Per K
2011/05/19 08:55:31
Done.
| |
| 138 break; | |
| 139 } | |
| 140 default: | |
| 141 NOTREACHED(); | |
| 142 } | |
| 143 | |
| 144 event_handler_.OnBufferReady(id_, handle, timestamp); | |
| 145 } | |
| 146 | |
| 147 void VideoCaptureMemory::OnError() { | |
| 148 event_handler_.OnError(id_); | |
| 149 } | |
| 150 | |
| 151 void VideoCaptureMemory::OnFrameInfo( | |
| 152 const media::VideoCaptureDevice::Capability& info) { | |
| 153 DCHECK(owned_dibs_.empty()); | |
| 154 // Lock needed since the buffers are used in OnIncomingFrame | |
| 155 // and we need to use it there in order to avoid memcpy of complete frames. | |
| 156 lock_.Acquire(); | |
| 157 const size_t needed_size = (info.width * info.height * 3) / 2; | |
| 158 for (unsigned int i = 0; i < kNoOfDIBS; ++i) { | |
| 159 TransportDIB* dib = TransportDIB::Create(needed_size, i); | |
|
wjia(left Chromium)
2011/05/19 04:24:37
is it good to move dib creation out of critical se
Per K
2011/05/19 08:55:31
Done.
| |
| 160 if (!dib) { | |
| 161 break; | |
| 162 } | |
| 163 owned_dibs_.insert(std::make_pair(dib->handle(), dib)); | |
| 164 free_dibs_.push_back(dib->handle()); | |
| 165 } | |
| 166 frame_info_= info; | |
| 167 lock_.Release(); | |
| 168 | |
| 169 // Check that all Dibs where created. | |
| 170 if (owned_dibs_.size() != kNoOfDIBS) { | |
| 171 event_handler_.OnError(id_); | |
| 172 } | |
| 173 event_handler_.OnFrameInfo(id_, info.width, info.height, | |
| 174 info.frame_rate); | |
| 175 } | |
| 176 | |
| 177 /////////////////////////////////////////////////////////////////////////////// | |
| 178 // Called by VideoCaptureManager when a device have been stopped. | |
| 179 // This will report to the event handler that it object is ready to delete | |
|
wjia(left Chromium)
2011/05/19 04:24:37
"the object" or "this object"
Per K
2011/05/19 08:55:31
Done.
| |
| 180 // if all DIBS have been returned. | |
| 181 void VideoCaptureMemory::OnDeviceStopped(Task* stopped_task) { | |
| 182 bool ready_to_delete_now = false; | |
|
wjia(left Chromium)
2011/05/19 04:24:37
ditto. this variable will set below anyway.
Per K
2011/05/19 08:55:31
Done.
| |
| 183 lock_.Acquire(); | |
| 184 // Set flag to indicate we need to report when all DIBs have been returned. | |
| 185 report_ready_to_delete_ = true; | |
| 186 ready_to_delete_now = (free_dibs_.size() == owned_dibs_.size()); | |
| 187 lock_.Release(); | |
| 188 if (ready_to_delete_now) { | |
| 189 event_handler_.OnReadyToDelete(id_); | |
| 190 } | |
| 191 if (stopped_task) { | |
| 192 stopped_task->Run(); | |
| 193 delete stopped_task; | |
| 194 } | |
| 195 } | |
| OLD | NEW |