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