1// Copyright 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_COMMON_GPU_CLIENT_GPU_VIDEO_ENCODE_ACCELERATOR_HOST_H_
6#define CONTENT_COMMON_GPU_CLIENT_GPU_VIDEO_ENCODE_ACCELERATOR_HOST_H_
7
8#include <vector>
9
10#include "base/containers/hash_tables.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/weak_ptr.h"
13#include "base/threading/non_thread_safe.h"
14#include "content/common/gpu/client/command_buffer_proxy_impl.h"
15#include "ipc/ipc_listener.h"
16#include "media/video/video_encode_accelerator.h"
17
18namespace gfx {
19
20class Size;
21
22}  // namespace gfx
23
24namespace media {
25
26class VideoFrame;
27
28}  // namespace media
29
30namespace content {
31
32class GpuChannelHost;
33
34// This class is the renderer-side host for the VideoEncodeAccelerator in the
35// GPU process, coordinated over IPC.
36class GpuVideoEncodeAcceleratorHost
37    : public IPC::Listener,
38      public media::VideoEncodeAccelerator,
39      public CommandBufferProxyImpl::DeletionObserver,
40      public base::NonThreadSafe {
41 public:
42  // |this| is guaranteed not to outlive |channel| and |impl|.  (See comments
43  // for |channel_| and |impl_|.)
44  GpuVideoEncodeAcceleratorHost(GpuChannelHost* channel,
45                                CommandBufferProxyImpl* impl);
46
47  // Static query for the supported profiles.  This query proxies to
48  // GpuVideoEncodeAccelerator::GetSupportedProfiles().
49  static std::vector<SupportedProfile> GetSupportedProfiles();
50
51  // IPC::Listener implementation.
52  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
53  virtual void OnChannelError() OVERRIDE;
54
55  // media::VideoEncodeAccelerator implementation.
56  virtual bool Initialize(media::VideoFrame::Format input_format,
57                          const gfx::Size& input_visible_size,
58                          media::VideoCodecProfile output_profile,
59                          uint32 initial_bitrate,
60                          Client* client) OVERRIDE;
61  virtual void Encode(const scoped_refptr<media::VideoFrame>& frame,
62                      bool force_keyframe) OVERRIDE;
63  virtual void UseOutputBitstreamBuffer(
64      const media::BitstreamBuffer& buffer) OVERRIDE;
65  virtual void RequestEncodingParametersChange(uint32 bitrate,
66                                               uint32 framerate_num) OVERRIDE;
67  virtual void Destroy() OVERRIDE;
68
69  // CommandBufferProxyImpl::DeletionObserver implemetnation.
70  virtual void OnWillDeleteImpl() OVERRIDE;
71
72 private:
73  // Only Destroy() should be deleting |this|.
74  virtual ~GpuVideoEncodeAcceleratorHost();
75
76  // Notify |client_| of an error.  Posts a task to avoid re-entrancy.
77  void PostNotifyError(Error);
78
79  void Send(IPC::Message* message);
80
81  // IPC handlers, proxying media::VideoEncodeAccelerator::Client for the GPU
82  // process.  Should not be called directly.
83  void OnRequireBitstreamBuffers(uint32 input_count,
84                                 const gfx::Size& input_coded_size,
85                                 uint32 output_buffer_size);
86  void OnNotifyInputDone(int32 frame_id);
87  void OnBitstreamBufferReady(int32 bitstream_buffer_id,
88                              uint32 payload_size,
89                              bool key_frame);
90  void OnNotifyError(Error error);
91
92  // Unowned reference to the GpuChannelHost to send IPC messages to the GPU
93  // process.  |channel_| outlives |impl_|, so the reference is always valid as
94  // long as it is not NULL.
95  GpuChannelHost* channel_;
96
97  // Route ID for the associated encoder in the GPU process.
98  int32 encoder_route_id_;
99
100  // The client that will receive callbacks from the encoder.
101  Client* client_;
102
103  // Unowned reference to the CommandBufferProxyImpl that created us.  |this|
104  // registers as a DeletionObserver of |impl_|, so the reference is always
105  // valid as long as it is not NULL.
106  CommandBufferProxyImpl* impl_;
107
108  // media::VideoFrames sent to the encoder.
109  // base::IDMap not used here, since that takes pointers, not scoped_refptr.
110  typedef base::hash_map<int32, scoped_refptr<media::VideoFrame> > FrameMap;
111  FrameMap frame_map_;
112
113  // ID serial number for the next frame to send to the GPU process.
114  int32 next_frame_id_;
115
116  // WeakPtr factory for posting tasks back to itself.
117  base::WeakPtrFactory<GpuVideoEncodeAcceleratorHost> weak_this_factory_;
118
119  DISALLOW_COPY_AND_ASSIGN(GpuVideoEncodeAcceleratorHost);
120};
121
122}  // namespace content
123
124#endif  // CONTENT_COMMON_GPU_CLIENT_GPU_VIDEO_ENCODE_ACCELERATOR_HOST_H_
125