Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_video_source_host.h
1 // Copyright (c) 2013 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_SOURCE_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_SOURCE_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 "base/memory/weak_ptr.h"
12 #include "content/common/content_export.h"
13 #include "content/renderer/media/video_source_handler.h"
14 #include "ppapi/c/pp_time.h"
15 #include "ppapi/c/ppb_image_data.h"
16 #include "ppapi/host/host_message_context.h"
17 #include "ppapi/host/resource_host.h"
18
19 struct PP_ImageDataDesc;
20
21 namespace content {
22
23 class PPB_ImageData_Impl;
24 class RendererPpapiHost;
25
26 class CONTENT_EXPORT PepperVideoSourceHost : public ppapi::host::ResourceHost {
27  public:
28   PepperVideoSourceHost(RendererPpapiHost* host,
29                         PP_Instance instance,
30                         PP_Resource resource);
31
32   virtual ~PepperVideoSourceHost();
33
34   virtual int32_t OnResourceMessageReceived(
35       const IPC::Message& msg,
36       ppapi::host::HostMessageContext* context) OVERRIDE;
37
38  private:
39   // This helper object receives frames on a video worker thread and passes
40   // them on to us.
41   class FrameReceiver : public FrameReaderInterface,
42                         public base::RefCountedThreadSafe<FrameReceiver> {
43    public:
44     explicit FrameReceiver(const base::WeakPtr<PepperVideoSourceHost>& host);
45
46     // FrameReaderInterface implementation.
47     virtual bool GotFrame(const scoped_refptr<media::VideoFrame>& frame)
48         OVERRIDE;
49
50     void OnGotFrame(const scoped_refptr<media::VideoFrame>& frame);
51
52    private:
53     friend class base::RefCountedThreadSafe<FrameReceiver>;
54     virtual ~FrameReceiver();
55
56     base::WeakPtr<PepperVideoSourceHost> host_;
57     scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
58   };
59
60   friend class FrameReceiver;
61
62   int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context,
63                         const std::string& stream_url);
64   int32_t OnHostMsgGetFrame(ppapi::host::HostMessageContext* context);
65   int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context);
66
67   // Sends the reply to a GetFrame message from the plugin. A reply is always
68   // sent and last_frame_, reply_context_, and get_frame_pending_ are all reset.
69   void SendGetFrameReply();
70   // Sends the reply to a GetFrame message from the plugin in case of an error.
71   void SendGetFrameErrorReply(int32_t error);
72
73   void Close();
74
75   RendererPpapiHost* renderer_ppapi_host_;
76
77   ppapi::host::ReplyMessageContext reply_context_;
78
79   scoped_ptr<VideoSourceHandler> source_handler_;
80   scoped_refptr<FrameReceiver> frame_receiver_;
81   std::string stream_url_;
82   scoped_refptr<media::VideoFrame> last_frame_;
83   bool get_frame_pending_;
84   // We use only one ImageData resource in order to avoid allocating
85   // shared memory repeatedly. We send the same one each time the plugin
86   // requests a frame. For this to work, the plugin must finish using
87   // the ImageData it receives prior to calling GetFrame, and not access
88   // the ImageData until it gets its next callback to GetFrame.
89   scoped_refptr<PPB_ImageData_Impl> shared_image_;
90   PP_ImageDataDesc shared_image_desc_;
91
92   base::WeakPtrFactory<PepperVideoSourceHost> weak_factory_;
93
94   DISALLOW_COPY_AND_ASSIGN(PepperVideoSourceHost);
95 };
96
97 }  // namespace content
98
99 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_SOURCE_HOST_H_