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