Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(705)

Unified Diff: content/browser/renderer_host/video_capture_memory.cc

Issue 7002027: VideoCaptureHost (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/renderer_host/video_capture_memory.cc
===================================================================
--- content/browser/renderer_host/video_capture_memory.cc (revision 0)
+++ content/browser/renderer_host/video_capture_memory.cc (revision 0)
@@ -0,0 +1,195 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/renderer_host/video_capture_memory.h"
+
+#include "base/logging.h"
+#include "base/stl_util-inl.h"
+#include "content/browser/browser_thread.h"
+#include "content/browser/media_stream/video_capture_manager.h"
+#include "media/base/yuv_convert.h"
+
+// The number of TransportDIBs VideoCaptureMemory allocate.
+static const unsigned int kNoOfDIBS = 3;
+
+VideoCaptureMemory::VideoCaptureMemory(
+ VideoCaptureMemoryId id,
+ VideoCaptureMemory::EventHandler* event_handler)
+ : report_ready_to_delete_(false),
+ 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
+ id_(id) {}
+
+VideoCaptureMemory::~VideoCaptureMemory() {
+ // Delete all TransportDIBs
+ STLDeleteContainerPairSecondPointers(owned_dibs_.begin(),
+ owned_dibs_.end());
+ 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.
+}
+
+void VideoCaptureMemory::StartCapture(const media::VideoCaptureParams& params) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ params_ = params;
+ media_stream::VideoCaptureManager* manager =
+ media_stream::VideoCaptureManager::Get();
+ // Order the manager to start the actual capture.
+ manager->Start(params, this);
+}
+
+void VideoCaptureMemory::StopCapture(Task* stopped_task) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ media_stream::VideoCaptureManager* manager =
+ media_stream::VideoCaptureManager::Get();
+ manager->Stop(params_.session_id,
+ NewRunnableMethod(this, &VideoCaptureMemory::OnDeviceStopped,
+ stopped_task));
+}
+
+void VideoCaptureMemory::ReturnTransportDIB(TransportDIB::Handle handle) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+// Check if we own this handle
+ CHECK(owned_dibs_.find(handle) != owned_dibs_.end());
+
+ 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.
+ lock_.Acquire();
+ free_dibs_.push_back(handle);
+ ready_to_delete = (free_dibs_.size() == owned_dibs_.size());
+ lock_.Release();
+
+ if (report_ready_to_delete_ && ready_to_delete) {
+ event_handler_.OnReadyToDelete(id_);
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Implements VideoCaptureDevice::EventHandler.
+void VideoCaptureMemory::OnIncomingCapturedFrame(const uint8* data,
+ int length,
+ base::Time timestamp) {
+ TransportDIB::Handle handle;
+ // Check if there is a TransportDIB to fill.
+ bool buffer_exist = false;
+ lock_.Acquire();
+ if (!report_ready_to_delete_ && free_dibs_.size() > 0) {
+ handle = free_dibs_.back();
+ free_dibs_.pop_back();
+ buffer_exist = true;
+ }
+ lock_.Release();
+
+ if (!buffer_exist) {
+ return;
+ }
+
+ TransportDIB* dib = TransportDIB::Map(handle);
+ uint8* target = static_cast<uint8*> (dib->memory());
+ CHECK(dib->size() >= (unsigned int) (frame_info_.width*frame_info_.height*3) /
+ 2);
+
+ // Do color conversion from the camera format to I420.
+ switch (frame_info_.color) {
+ case media::VideoCaptureDevice::kColorUnknown: // Color format not set
+ break;
+ case media::VideoCaptureDevice::kI420: {
+ memcpy(target, data, (frame_info_.width * frame_info_.height * 3) / 2);
+ break;
+ }
+ case media::VideoCaptureDevice::kYUY2: {
+ uint8* yplane = target;
+ uint8* uplane = target + frame_info_.width * frame_info_.height;
+ uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4;
+ media::ConvertYUY2ToYUV(data, yplane, uplane, vplane, frame_info_.width,
+ frame_info_.height);
+ break;
+ }
+ case media::VideoCaptureDevice::kRGB24: {
+ #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.
+ uint8* yplane = target;
+ uint8* uplane = target + frame_info_.width * frame_info_.height;
+ uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4;
+ int ystride = frame_info_.width;
+ int uvstride = frame_info_.width / 2;
+ int rgb_stride = - 3 * frame_info_.width;
+ const uint8* rgb_src = data + 3 * frame_info_.width *
+ (frame_info_.height -1);
+ #else
+ uint8* yplane = target;
+ uint8* uplane = target + frame_info_.width * frame_info_.height;
+ uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4;
+ int ystride = frame_info_.width;
+ int uvstride = frame_info_.width / 2;
+ int rgb_stride = 3 * frame_info_.width;
+ const uint8* rgb_src = data;
+ #endif
+ media::ConvertRGB24ToYUV(rgb_src, yplane, uplane, vplane,
+ frame_info_.width, frame_info_.height,
+ rgb_stride, ystride, uvstride);
+ break;
+ }
+ case media::VideoCaptureDevice::kARGB: {
+ uint8* yplane = target;
+ uint8* uplane = target + frame_info_.width * frame_info_.height;
+ uint8* vplane = uplane + (frame_info_.width * frame_info_.height) / 4;
+ media::ConvertRGB32ToYUV(data, yplane, uplane, vplane, frame_info_.width,
+ frame_info_.height, frame_info_.width * 4,
+ 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.
+ break;
+ }
+ default:
+ NOTREACHED();
+ }
+
+ event_handler_.OnBufferReady(id_, handle, timestamp);
+}
+
+void VideoCaptureMemory::OnError() {
+ event_handler_.OnError(id_);
+}
+
+void VideoCaptureMemory::OnFrameInfo(
+ const media::VideoCaptureDevice::Capability& info) {
+ DCHECK(owned_dibs_.empty());
+ // Lock needed since the buffers are used in OnIncomingFrame
+ // and we need to use it there in order to avoid memcpy of complete frames.
+ lock_.Acquire();
+ const size_t needed_size = (info.width * info.height * 3) / 2;
+ for (unsigned int i = 0; i < kNoOfDIBS; ++i) {
+ 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.
+ if (!dib) {
+ break;
+ }
+ owned_dibs_.insert(std::make_pair(dib->handle(), dib));
+ free_dibs_.push_back(dib->handle());
+ }
+ frame_info_= info;
+ lock_.Release();
+
+ // Check that all Dibs where created.
+ if (owned_dibs_.size() != kNoOfDIBS) {
+ event_handler_.OnError(id_);
+ }
+ event_handler_.OnFrameInfo(id_, info.width, info.height,
+ info.frame_rate);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Called by VideoCaptureManager when a device have been stopped.
+// 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.
+// if all DIBS have been returned.
+void VideoCaptureMemory::OnDeviceStopped(Task* stopped_task) {
+ 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.
+ lock_.Acquire();
+ // Set flag to indicate we need to report when all DIBs have been returned.
+ report_ready_to_delete_ = true;
+ ready_to_delete_now = (free_dibs_.size() == owned_dibs_.size());
+ lock_.Release();
+ if (ready_to_delete_now) {
+ event_handler_.OnReadyToDelete(id_);
+ }
+ if (stopped_task) {
+ stopped_task->Run();
+ delete stopped_task;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698