1// Copyright (c) 2012 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#ifndef CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_CAPTURE_HOST_H_
6#define CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_CAPTURE_HOST_H_
7
8#include "base/compiler_specific.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "content/common/media/video_capture.h"
12#include "content/public/renderer/renderer_ppapi_host.h"
13#include "content/renderer/pepper/pepper_device_enumeration_host_helper.h"
14#include "content/renderer/pepper/ppb_buffer_impl.h"
15#include "media/video/capture/video_capture_types.h"
16#include "ppapi/c/dev/ppp_video_capture_dev.h"
17#include "ppapi/host/host_message_context.h"
18#include "ppapi/host/resource_host.h"
19
20namespace media {
21class VideoFrame;
22}  // namespace media
23
24namespace content {
25class PepperPlatformVideoCapture;
26class RendererPpapiHostImpl;
27
28class PepperVideoCaptureHost : public ppapi::host::ResourceHost {
29 public:
30  PepperVideoCaptureHost(RendererPpapiHostImpl* host,
31                         PP_Instance instance,
32                         PP_Resource resource);
33
34  virtual ~PepperVideoCaptureHost();
35
36  bool Init();
37
38  virtual int32_t OnResourceMessageReceived(
39      const IPC::Message& msg,
40      ppapi::host::HostMessageContext* context) OVERRIDE;
41
42  // These methods are called by PepperPlatformVideoCapture only.
43
44  // Called when video capture is initialized. We can start
45  // video capture if |succeeded| is true.
46  void OnInitialized(bool succeeded);
47
48  // Called when video capture has started successfully.
49  void OnStarted();
50
51  // Called when video capture has stopped. There will be no more
52  // frames delivered.
53  void OnStopped();
54
55  // Called when video capture has paused.
56  void OnPaused();
57
58  // Called when video capture cannot be started because of an error.
59  void OnError();
60
61  // Called when a video frame is ready.
62  void OnFrameReady(const scoped_refptr<media::VideoFrame>& frame,
63                    media::VideoCaptureFormat format);
64
65 private:
66  int32_t OnOpen(ppapi::host::HostMessageContext* context,
67                 const std::string& device_id,
68                 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
69                 uint32_t buffer_count);
70  int32_t OnStartCapture(ppapi::host::HostMessageContext* context);
71  int32_t OnReuseBuffer(ppapi::host::HostMessageContext* context,
72                        uint32_t buffer);
73  int32_t OnStopCapture(ppapi::host::HostMessageContext* context);
74  int32_t OnClose(ppapi::host::HostMessageContext* context);
75
76  int32_t StopCapture();
77  int32_t Close();
78  void PostErrorReply();
79  void AllocBuffers(const gfx::Size& resolution, int frame_rate);
80  void ReleaseBuffers();
81  void SendStatus();
82
83  void SetRequestedInfo(const PP_VideoCaptureDeviceInfo_Dev& device_info,
84                        uint32_t buffer_count);
85
86  void DetachPlatformVideoCapture();
87
88  bool SetStatus(PP_VideoCaptureStatus_Dev status, bool forced);
89
90  scoped_ptr<PepperPlatformVideoCapture> platform_video_capture_;
91
92  // Buffers of video frame.
93  struct BufferInfo {
94    BufferInfo();
95    ~BufferInfo();
96
97    bool in_use;
98    void* data;
99    scoped_refptr<PPB_Buffer_Impl> buffer;
100  };
101
102  RendererPpapiHostImpl* renderer_ppapi_host_;
103
104  gfx::Size alloc_size_;
105  std::vector<BufferInfo> buffers_;
106  size_t buffer_count_hint_;
107
108  media::VideoCaptureParams video_capture_params_;
109
110  PP_VideoCaptureStatus_Dev status_;
111
112  ppapi::host::ReplyMessageContext open_reply_context_;
113
114  PepperDeviceEnumerationHostHelper enumeration_helper_;
115
116  DISALLOW_COPY_AND_ASSIGN(PepperVideoCaptureHost);
117};
118
119}  // namespace content
120
121#endif  // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_CAPTURE_HOST_H_
122