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_MEDIA_GPU_VIDEO_ENCODE_ACCELERATOR_H_
6#define CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_ENCODE_ACCELERATOR_H_
7
8#include <vector>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "content/common/gpu/gpu_command_buffer_stub.h"
13#include "ipc/ipc_listener.h"
14#include "media/video/video_encode_accelerator.h"
15#include "ui/gfx/size.h"
16
17namespace base {
18
19class SharedMemory;
20
21}  // namespace base
22
23namespace content {
24
25// This class encapsulates the GPU process view of a VideoEncodeAccelerator,
26// wrapping the platform-specific VideoEncodeAccelerator instance.  It handles
27// IPC coming in from the renderer and passes it to the underlying VEA.
28class GpuVideoEncodeAccelerator
29    : public IPC::Listener,
30      public media::VideoEncodeAccelerator::Client,
31      public GpuCommandBufferStub::DestructionObserver {
32 public:
33  GpuVideoEncodeAccelerator(int32 host_route_id, GpuCommandBufferStub* stub);
34  virtual ~GpuVideoEncodeAccelerator();
35
36  // Initialize this accelerator with the given parameters and send
37  // |init_done_msg| when complete.
38  void Initialize(media::VideoFrame::Format input_format,
39                  const gfx::Size& input_visible_size,
40                  media::VideoCodecProfile output_profile,
41                  uint32 initial_bitrate,
42                  IPC::Message* init_done_msg);
43
44  // IPC::Listener implementation
45  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
46
47  // media::VideoEncodeAccelerator::Client implementation.
48  virtual void RequireBitstreamBuffers(unsigned int input_count,
49                                       const gfx::Size& input_coded_size,
50                                       size_t output_buffer_size) OVERRIDE;
51  virtual void BitstreamBufferReady(int32 bitstream_buffer_id,
52                                    size_t payload_size,
53                                    bool key_frame) OVERRIDE;
54  virtual void NotifyError(media::VideoEncodeAccelerator::Error error) OVERRIDE;
55
56  // GpuCommandBufferStub::DestructionObserver implementation.
57  virtual void OnWillDestroyStub() OVERRIDE;
58
59  // Static query for supported profiles.  This query calls the appropriate
60  // platform-specific version.
61  static std::vector<media::VideoEncodeAccelerator::SupportedProfile>
62      GetSupportedProfiles();
63
64 private:
65  // Create the appropriate platform-specific VEA.
66  static scoped_ptr<media::VideoEncodeAccelerator> CreateEncoder();
67
68  // IPC handlers, proxying media::VideoEncodeAccelerator for the renderer
69  // process.
70  void OnEncode(int32 frame_id,
71                base::SharedMemoryHandle buffer_handle,
72                uint32 buffer_size,
73                bool force_keyframe);
74  void OnUseOutputBitstreamBuffer(int32 buffer_id,
75                                  base::SharedMemoryHandle buffer_handle,
76                                  uint32 buffer_size);
77  void OnRequestEncodingParametersChange(uint32 bitrate, uint32 framerate);
78
79  void OnDestroy();
80
81  void EncodeFrameFinished(int32 frame_id, scoped_ptr<base::SharedMemory> shm);
82
83  void Send(IPC::Message* message);
84  // Helper for replying to the creation request.
85  void SendCreateEncoderReply(IPC::Message* message, bool succeeded);
86
87  // Route ID to communicate with the host.
88  int32 host_route_id_;
89
90  // Unowned pointer to the underlying GpuCommandBufferStub.  |this| is
91  // registered as a DestuctionObserver of |stub_| and will self-delete when
92  // |stub_| is destroyed.
93  GpuCommandBufferStub* stub_;
94
95  // Owned pointer to the underlying VideoEncodeAccelerator.
96  scoped_ptr<media::VideoEncodeAccelerator> encoder_;
97  base::Callback<bool(void)> make_context_current_;
98
99  // Video encoding parameters.
100  media::VideoFrame::Format input_format_;
101  gfx::Size input_visible_size_;
102  gfx::Size input_coded_size_;
103  size_t output_buffer_size_;
104
105  // Weak pointer for media::VideoFrames that refer back to |this|.
106  base::WeakPtrFactory<GpuVideoEncodeAccelerator> weak_this_factory_;
107
108  DISALLOW_COPY_AND_ASSIGN(GpuVideoEncodeAccelerator);
109};
110
111}  // namespace content
112
113#endif  // CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_ENCODE_ACCELERATOR_H_
114