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