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_controller.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 VideoCaptureController allocate. | |
| 14 static const size_t kNoOfDIBS = 3; | |
| 15 | |
| 16 VideoCaptureController::VideoCaptureController( | |
| 17 ControllerId id, | |
| 18 EventHandler* event_handler) | |
| 19 : report_ready_to_delete_(false), | |
| 20 event_handler_(event_handler), | |
| 21 id_(id) {} | |
| 22 | |
| 23 VideoCaptureController::~VideoCaptureController() { | |
| 24 // Delete all TransportDIBs. | |
| 25 STLDeleteContainerPairSecondPointers(owned_dibs_.begin(), | |
| 26 owned_dibs_.end()); | |
| 27 } | |
| 28 | |
| 29 void VideoCaptureController::StartCapture( | |
| 30 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 VideoCaptureController::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, | |
| 47 &VideoCaptureController::OnDeviceStopped, | |
| 48 stopped_task)); | |
| 49 } | |
| 50 | |
| 51 void VideoCaptureController::ReturnTransportDIB(TransportDIB::Handle handle) { | |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 53 // Check if we own this handle. | |
| 54 CHECK(owned_dibs_.find(handle) != owned_dibs_.end()); | |
| 55 | |
| 56 bool ready_to_delete; | |
| 57 { | |
| 58 base::AutoLock lock(lock_); | |
| 59 free_dibs_.push_back(handle); | |
| 60 ready_to_delete = (free_dibs_.size() == owned_dibs_.size()); | |
| 61 } | |
| 62 if (report_ready_to_delete_ && ready_to_delete) { | |
| 63 event_handler_->OnReadyToDelete(id_); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 /////////////////////////////////////////////////////////////////////////////// | |
| 68 // Implements VideoCaptureDevice::EventHandler. | |
| 69 // OnIncomingCapturedFrame is called the thread running the capture device. | |
| 70 // I.e.- DirectShow thread on windows and v4l2_thread on Linux. | |
| 71 void VideoCaptureController::OnIncomingCapturedFrame(const uint8* data, | |
| 72 int length, | |
| 73 base::Time timestamp) { | |
| 74 TransportDIB::Handle handle; | |
| 75 // Check if there is a TransportDIB to fill. | |
| 76 bool buffer_exist = false; | |
| 77 { | |
| 78 base::AutoLock lock(lock_); | |
| 79 if (!report_ready_to_delete_ && free_dibs_.size() > 0) { | |
| 80 handle = free_dibs_.back(); | |
| 81 free_dibs_.pop_back(); | |
| 82 buffer_exist = true; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 if (!buffer_exist) { | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 TransportDIB* dib = TransportDIB::Map(handle); | |
| 91 if (!dib) { | |
| 92 VLOG(1) << "OnIncomingCapturedFrame - Failed to map handle."; | |
| 93 base::AutoLock lock(lock_); | |
| 94 free_dibs_.push_back(handle); | |
| 95 return; | |
| 96 } | |
| 97 uint8* target = static_cast<uint8*>(dib->memory()); | |
| 98 CHECK(dib->size() >= static_cast<size_t> (frame_info_.width * | |
| 99 frame_info_.height*3) / | |
|
scherkus (not reviewing)
2011/05/23 21:19:17
spaces around *
Per K
2011/05/25 08:30:51
Done.
| |
| 100 2); | |
| 101 | |
| 102 // Do color conversion from the camera format to I420. | |
| 103 switch (frame_info_.color) { | |
| 104 case media::VideoCaptureDevice::kColorUnknown: // Color format not set. | |
| 105 break; | |
| 106 case media::VideoCaptureDevice::kI420: { | |
| 107 memcpy(target, data, (frame_info_.width * frame_info_.height * 3) / 2); | |
| 108 break; | |
| 109 } | |
| 110 case media::VideoCaptureDevice::kYUY2: { | |
| 111 uint8* yplane = target; | |
| 112 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 113 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 114 media::ConvertYUY2ToYUV(data, yplane, uplane, vplane, frame_info_.width, | |
| 115 frame_info_.height); | |
| 116 break; | |
| 117 } | |
| 118 case media::VideoCaptureDevice::kRGB24: { | |
| 119 #if defined(OS_WIN) // RGB on Windows start at the bottom line. | |
| 120 uint8* yplane = target; | |
| 121 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 122 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 123 int ystride = frame_info_.width; | |
| 124 int uvstride = frame_info_.width / 2; | |
| 125 int rgb_stride = - 3 * frame_info_.width; | |
| 126 const uint8* rgb_src = data + 3 * frame_info_.width * | |
| 127 (frame_info_.height -1); | |
| 128 #else | |
| 129 uint8* yplane = target; | |
| 130 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 131 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 132 int ystride = frame_info_.width; | |
| 133 int uvstride = frame_info_.width / 2; | |
| 134 int rgb_stride = 3 * frame_info_.width; | |
| 135 const uint8* rgb_src = data; | |
| 136 #endif | |
| 137 media::ConvertRGB24ToYUV(rgb_src, yplane, uplane, vplane, | |
| 138 frame_info_.width, frame_info_.height, | |
| 139 rgb_stride, ystride, uvstride); | |
| 140 break; | |
| 141 } | |
| 142 case media::VideoCaptureDevice::kARGB: { | |
| 143 uint8* yplane = target; | |
| 144 uint8* uplane = target + frame_info_.width * frame_info_.height; | |
| 145 uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4; | |
| 146 media::ConvertRGB32ToYUV(data, yplane, uplane, vplane, frame_info_.width, | |
| 147 frame_info_.height, frame_info_.width * 4, | |
| 148 frame_info_.width, frame_info_.width / 2); | |
| 149 break; | |
| 150 } | |
| 151 default: | |
| 152 NOTREACHED(); | |
| 153 } | |
| 154 | |
| 155 event_handler_->OnBufferReady(id_, handle, timestamp); | |
| 156 } | |
| 157 | |
| 158 void VideoCaptureController::OnError() { | |
| 159 event_handler_->OnError(id_); | |
| 160 } | |
| 161 | |
| 162 void VideoCaptureController::OnFrameInfo( | |
| 163 const media::VideoCaptureDevice::Capability& info) { | |
| 164 DCHECK(owned_dibs_.empty()); | |
| 165 bool frames_created = true; | |
| 166 const size_t needed_size = (info.width * info.height * 3) / 2; | |
| 167 for (size_t i = 0; i < kNoOfDIBS; ++i) { | |
| 168 TransportDIB* dib = TransportDIB::Create(needed_size, i); | |
| 169 if (!dib) { | |
| 170 frames_created = false; | |
| 171 break; | |
| 172 } | |
| 173 // Lock needed since the buffers are used in OnIncomingFrame | |
| 174 // and we need to use it there in order to avoid memcpy of complete frames. | |
| 175 base::AutoLock lock(lock_); | |
| 176 owned_dibs_.insert(std::make_pair(dib->handle(), dib)); | |
| 177 free_dibs_.push_back(dib->handle()); | |
| 178 } | |
| 179 frame_info_= info; | |
| 180 | |
| 181 // Check that all DIBs where created successfully. | |
| 182 if (!frames_created) { | |
| 183 event_handler_->OnError(id_); | |
| 184 } | |
| 185 event_handler_->OnFrameInfo(id_, info.width, info.height, | |
| 186 info.frame_rate); | |
| 187 } | |
| 188 | |
| 189 /////////////////////////////////////////////////////////////////////////////// | |
| 190 // Called by VideoCaptureManager when a device have been stopped. | |
| 191 // This will report to the event handler that this object is ready to be deleted | |
| 192 // if all DIBS have been returned. | |
| 193 void VideoCaptureController::OnDeviceStopped(Task* stopped_task) { | |
| 194 bool ready_to_delete_now; | |
| 195 | |
| 196 // Set flag to indicate we need to report when all DIBs have been returned. | |
| 197 report_ready_to_delete_ = true; | |
| 198 { | |
| 199 base::AutoLock lock(lock_); | |
| 200 ready_to_delete_now = (free_dibs_.size() == owned_dibs_.size()); | |
| 201 } | |
| 202 | |
| 203 if (ready_to_delete_now) { | |
| 204 event_handler_->OnReadyToDelete(id_); | |
| 205 } | |
| 206 if (stopped_task) { | |
| 207 stopped_task->Run(); | |
| 208 delete stopped_task; | |
| 209 } | |
| 210 } | |
| OLD | NEW |