Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_video_capture_host.cc
index 5495712..871b94d 100644 (file)
@@ -11,6 +11,7 @@
 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
 #include "content/renderer/render_view_impl.h"
 #include "media/base/limits.h"
+#include "media/base/video_frame.h"
 #include "ppapi/host/dispatch_host_message.h"
 #include "ppapi/host/ppapi_host.h"
 #include "ppapi/proxy/host_dispatcher.h"
@@ -40,12 +41,11 @@ PepperVideoCaptureHost::PepperVideoCaptureHost(RendererPpapiHostImpl* host,
       renderer_ppapi_host_(host),
       buffer_count_hint_(0),
       status_(PP_VIDEO_CAPTURE_STATUS_STOPPED),
-      enumeration_helper_(
-          this,
-          PepperMediaDeviceManager::GetForRenderView(
-              host->GetRenderViewForInstance(pp_instance())),
-          PP_DEVICETYPE_DEV_VIDEOCAPTURE,
-          host->GetDocumentURL(instance)) {
+      enumeration_helper_(this,
+                          PepperMediaDeviceManager::GetForRenderView(
+                              host->GetRenderViewForInstance(pp_instance())),
+                          PP_DEVICETYPE_DEV_VIDEOCAPTURE,
+                          host->GetDocumentURL(instance)) {
 }
 
 PepperVideoCaptureHost::~PepperVideoCaptureHost() {
@@ -64,29 +64,19 @@ int32_t PepperVideoCaptureHost::OnResourceMessageReceived(
     return result;
 
   IPC_BEGIN_MESSAGE_MAP(PepperVideoCaptureHost, msg)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL(
-        PpapiHostMsg_VideoCapture_Open,
-        OnOpen)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
-        PpapiHostMsg_VideoCapture_StartCapture,
-        OnStartCapture)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL(
-        PpapiHostMsg_VideoCapture_ReuseBuffer,
-        OnReuseBuffer)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
-        PpapiHostMsg_VideoCapture_StopCapture,
-        OnStopCapture)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
-        PpapiHostMsg_VideoCapture_Close,
-        OnClose)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoCapture_Open, OnOpen)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoCapture_StartCapture,
+                                      OnStartCapture)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoCapture_ReuseBuffer,
+                                    OnReuseBuffer)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoCapture_StopCapture,
+                                      OnStopCapture)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoCapture_Close, OnClose)
   IPC_END_MESSAGE_MAP()
   return PP_ERROR_FAILED;
 }
 
-void PepperVideoCaptureHost::OnInitialized(media::VideoCapture* capture,
-                                           bool succeeded) {
-  DCHECK(capture == platform_video_capture_.get());
-
+void PepperVideoCaptureHost::OnInitialized(bool succeeded) {
   if (succeeded) {
     open_reply_context_.params.set_result(PP_OK);
   } else {
@@ -98,25 +88,22 @@ void PepperVideoCaptureHost::OnInitialized(media::VideoCapture* capture,
                     PpapiPluginMsg_VideoCapture_OpenReply());
 }
 
-void PepperVideoCaptureHost::OnStarted(media::VideoCapture* capture) {
+void PepperVideoCaptureHost::OnStarted() {
   if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTED, false))
     SendStatus();
 }
 
-void PepperVideoCaptureHost::OnStopped(media::VideoCapture* capture) {
+void PepperVideoCaptureHost::OnStopped() {
   if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, false))
     SendStatus();
 }
 
-void PepperVideoCaptureHost::OnPaused(media::VideoCapture* capture) {
+void PepperVideoCaptureHost::OnPaused() {
   if (SetStatus(PP_VIDEO_CAPTURE_STATUS_PAUSED, false))
     SendStatus();
 }
 
-void PepperVideoCaptureHost::OnError(media::VideoCapture* capture,
-                                     int error_code) {
-  // Today, the media layer only sends "1" as an error.
-  DCHECK(error_code == 1);
+void PepperVideoCaptureHost::OnError() {
   PostErrorReply();
 }
 
@@ -125,20 +112,17 @@ void PepperVideoCaptureHost::PostErrorReply() {
   // conflicting "master" resolution), or because the browser failed to start
   // the capture.
   SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, true);
-  host()->SendUnsolicitedReply(pp_resource(),
-      PpapiPluginMsg_VideoCapture_OnError(PP_ERROR_FAILED));
-}
-
-void PepperVideoCaptureHost::OnRemoved(media::VideoCapture* capture) {
+  host()->SendUnsolicitedReply(
+      pp_resource(), PpapiPluginMsg_VideoCapture_OnError(PP_ERROR_FAILED));
 }
 
 void PepperVideoCaptureHost::OnFrameReady(
-    media::VideoCapture* capture,
-    const scoped_refptr<media::VideoFrame>& frame) {
+    const scoped_refptr<media::VideoFrame>& frame,
+    media::VideoCaptureFormat format) {
   DCHECK(frame.get());
 
-  if (alloc_size_ != frame->coded_size()) {
-    AllocBuffers(frame->coded_size(), capture->CaptureFrameRate());
+  if (alloc_size_ != frame->coded_size() || buffers_.empty()) {
+    AllocBuffers(frame->coded_size(), format.frame_rate);
     alloc_size_ = frame->coded_size();
   }
 
@@ -167,21 +151,19 @@ void PepperVideoCaptureHost::OnFrameReady(
         }
       }
       buffers_[i].in_use = true;
-      host()->SendUnsolicitedReply(pp_resource(),
-          PpapiPluginMsg_VideoCapture_OnBufferReady(i));
+      host()->SendUnsolicitedReply(
+          pp_resource(), PpapiPluginMsg_VideoCapture_OnBufferReady(i));
       return;
     }
   }
 }
 
-void PepperVideoCaptureHost::AllocBuffers(
-    const gfx::Size& resolution,
-    int frame_rate) {
+void PepperVideoCaptureHost::AllocBuffers(const gfx::Size& resolution,
+                                          int frame_rate) {
   PP_VideoCaptureDeviceInfo_Dev info = {
-    static_cast<uint32_t>(resolution.width()),
-    static_cast<uint32_t>(resolution.height()),
-    static_cast<uint32_t>(frame_rate)
-  };
+      static_cast<uint32_t>(resolution.width()),
+      static_cast<uint32_t>(resolution.height()),
+      static_cast<uint32_t>(frame_rate)};
   ReleaseBuffers();
 
   const size_t size = media::VideoFrame::AllocationSize(
@@ -194,8 +176,7 @@ void PepperVideoCaptureHost::AllocBuffers(
   // for sending below.
   std::vector<HostResource> buffer_host_resources;
   buffers_.reserve(buffer_count_hint_);
-  ppapi::ResourceTracker* tracker =
-      HostGlobals::Get()->GetResourceTracker();
+  ppapi::ResourceTracker* tracker = HostGlobals::Get()->GetResourceTracker();
   ppapi::proxy::HostDispatcher* dispatcher =
       ppapi::proxy::HostDispatcher::GetForInstance(pp_instance());
   for (size_t i = 0; i < buffer_count_hint_; ++i) {
@@ -243,10 +224,8 @@ void PepperVideoCaptureHost::AllocBuffers(
 #else
 #error Not implemented.
 #endif
-      params.AppendHandle(
-          ppapi::proxy::SerializedHandle(
-              dispatcher->ShareHandleWithRemote(platform_file, false),
-              size));
+      params.AppendHandle(ppapi::proxy::SerializedHandle(
+          dispatcher->ShareHandleWithRemote(platform_file, false), size));
     }
   }
 
@@ -254,14 +233,15 @@ void PepperVideoCaptureHost::AllocBuffers(
     // We couldn't allocate/map buffers at all. Send an error and stop the
     // capture.
     SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, true);
-    platform_video_capture_->StopCapture(this);
+    platform_video_capture_->StopCapture();
     PostErrorReply();
     return;
   }
 
-  host()->Send(new PpapiPluginMsg_ResourceReply(
-      params, PpapiPluginMsg_VideoCapture_OnDeviceInfo(
-          info, buffer_host_resources, size)));
+  host()->Send(
+      new PpapiPluginMsg_ResourceReply(params,
+                                       PpapiPluginMsg_VideoCapture_OnDeviceInfo(
+                                           info, buffer_host_resources, size)));
 }
 
 int32_t PepperVideoCaptureHost::OnOpen(
@@ -281,9 +261,8 @@ int32_t PepperVideoCaptureHost::OnOpen(
   RenderViewImpl* render_view = static_cast<RenderViewImpl*>(
       renderer_ppapi_host_->GetRenderViewForInstance(pp_instance()));
 
-  platform_video_capture_ = new PepperPlatformVideoCapture(
-      render_view->AsWeakPtr(), device_id,
-      document_url, this);
+  platform_video_capture_.reset(new PepperPlatformVideoCapture(
+      render_view->AsWeakPtr(), device_id, document_url, this));
 
   open_reply_context_ = context->MakeReplyMessageContext();
 
@@ -300,7 +279,7 @@ int32_t PepperVideoCaptureHost::OnStartCapture(
 
   // It's safe to call this regardless it's capturing or not, because
   // PepperPlatformVideoCapture maintains the state.
-  platform_video_capture_->StartCapture(this, video_capture_params_);
+  platform_video_capture_->StartCapture(video_capture_params_);
   return PP_OK;
 }
 
@@ -332,7 +311,7 @@ int32_t PepperVideoCaptureHost::StopCapture() {
   ReleaseBuffers();
   // It's safe to call this regardless it's capturing or not, because
   // PepperPlatformVideoCapture maintains the state.
-  platform_video_capture_->StopCapture(this);
+  platform_video_capture_->StopCapture();
   return PP_OK;
 }
 
@@ -357,7 +336,7 @@ void PepperVideoCaptureHost::ReleaseBuffers() {
 
 void PepperVideoCaptureHost::SendStatus() {
   host()->SendUnsolicitedReply(pp_resource(),
-      PpapiPluginMsg_VideoCapture_OnStatus(status_));
+                               PpapiPluginMsg_VideoCapture_OnStatus(status_));
 }
 
 void PepperVideoCaptureHost::SetRequestedInfo(
@@ -378,9 +357,9 @@ void PepperVideoCaptureHost::SetRequestedInfo(
 }
 
 void PepperVideoCaptureHost::DetachPlatformVideoCapture() {
-  if (platform_video_capture_.get()) {
+  if (platform_video_capture_) {
     platform_video_capture_->DetachEventHandler();
-    platform_video_capture_ = NULL;
+    platform_video_capture_.reset();
   }
 }
 
@@ -432,9 +411,7 @@ bool PepperVideoCaptureHost::SetStatus(PP_VideoCaptureStatus_Dev status,
 }
 
 PepperVideoCaptureHost::BufferInfo::BufferInfo()
-    : in_use(false),
-      data(NULL),
-      buffer() {
+    : in_use(false), data(NULL), buffer() {
 }
 
 PepperVideoCaptureHost::BufferInfo::~BufferInfo() {