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/host/host_message_context.h"
16#include "ppapi/host/resource_host.h"
17
18struct PP_ImageDataDesc;
19
20namespace content {
21
22class RendererPpapiHost;
23
24class CONTENT_EXPORT PepperVideoSourceHost : public ppapi::host::ResourceHost {
25 public:
26  PepperVideoSourceHost(RendererPpapiHost* host,
27                        PP_Instance instance,
28                        PP_Resource resource);
29
30  virtual ~PepperVideoSourceHost();
31
32  virtual int32_t OnResourceMessageReceived(
33      const IPC::Message& msg,
34      ppapi::host::HostMessageContext* context) OVERRIDE;
35
36 private:
37  // This helper object receives frames on a video worker thread and passes
38  // them on to us.
39  class FrameReceiver : public FrameReaderInterface,
40                        public base::RefCountedThreadSafe<FrameReceiver> {
41   public:
42    explicit FrameReceiver(const base::WeakPtr<PepperVideoSourceHost>& host);
43
44    // FrameReaderInterface implementation.
45    virtual bool GotFrame(cricket::VideoFrame* frame) OVERRIDE;
46
47    void OnGotFrame(scoped_ptr<cricket::VideoFrame> frame);
48
49   private:
50    friend class base::RefCountedThreadSafe<FrameReceiver>;
51    virtual ~FrameReceiver();
52
53    base::WeakPtr<PepperVideoSourceHost> host_;
54    scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
55  };
56
57  friend class FrameReceiver;
58
59  int32_t OnHostMsgOpen(ppapi::host::HostMessageContext* context,
60                        const std::string& stream_url);
61  int32_t OnHostMsgGetFrame(ppapi::host::HostMessageContext* context);
62  int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context);
63
64  // Sends the reply to a GetFrame message from the plugin. A reply is always
65  // sent and last_frame_, reply_context_, and get_frame_pending_ are all reset.
66  void SendGetFrameReply();
67  // Sends the reply to a GetFrame message from the plugin in case of an error.
68  void SendGetFrameErrorReply(int32_t error);
69
70  void Close();
71
72  RendererPpapiHost* renderer_ppapi_host_;
73
74  base::WeakPtrFactory<PepperVideoSourceHost> weak_factory_;
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_ptr<cricket::VideoFrame> last_frame_;
82  bool get_frame_pending_;
83
84  DISALLOW_COPY_AND_ASSIGN(PepperVideoSourceHost);
85};
86
87}  // namespace content
88
89#endif  // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_SOURCE_HOST_H_
90