1 // Copyright 2015 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.
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_ENCODER_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_ENCODER_HOST_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/scoped_vector.h"
11 #include "content/common/content_export.h"
12 #include "media/video/video_encode_accelerator.h"
13 #include "ppapi/c/pp_codecs.h"
14 #include "ppapi/c/ppb_video_frame.h"
15 #include "ppapi/host/host_message_context.h"
16 #include "ppapi/host/resource_host.h"
17 #include "ppapi/proxy/resource_message_params.h"
18 #include "ppapi/shared_impl/media_stream_buffer_manager.h"
21 class GpuVideoAcceleratorFactories
;
26 class CommandBufferProxyImpl
;
28 class RendererPpapiHost
;
30 class CONTENT_EXPORT PepperVideoEncoderHost
31 : public ppapi::host::ResourceHost
,
32 public media::VideoEncodeAccelerator::Client
,
33 public ppapi::MediaStreamBufferManager::Delegate
{
35 PepperVideoEncoderHost(RendererPpapiHost
* host
,
37 PP_Resource resource
);
38 ~PepperVideoEncoderHost() override
;
41 // Shared memory buffers.
43 ShmBuffer(uint32_t id
, scoped_ptr
<base::SharedMemory
> shm
);
46 media::BitstreamBuffer
ToBitstreamBuffer();
48 // Index of the buffer in the ScopedVector. Buffers have the same id in
49 // the plugin and the host.
51 scoped_ptr
<base::SharedMemory
> shm
;
55 // media::VideoEncodeAccelerator implementation.
56 void RequireBitstreamBuffers(unsigned int input_count
,
57 const gfx::Size
& input_coded_size
,
58 size_t output_buffer_size
) override
;
59 void BitstreamBufferReady(int32 bitstream_buffer_id
,
61 bool key_frame
) override
;
62 void NotifyError(media::VideoEncodeAccelerator::Error error
) override
;
64 // ResourceHost implementation.
65 int32_t OnResourceMessageReceived(
66 const IPC::Message
& msg
,
67 ppapi::host::HostMessageContext
* context
) override
;
69 int32_t OnHostMsgGetSupportedProfiles(
70 ppapi::host::HostMessageContext
* context
);
71 int32_t OnHostMsgInitialize(ppapi::host::HostMessageContext
* context
,
72 PP_VideoFrame_Format input_format
,
73 const PP_Size
& input_visible_size
,
74 PP_VideoProfile output_profile
,
75 uint32_t initial_bitrate
,
76 PP_HardwareAcceleration acceleration
);
77 int32_t OnHostMsgGetVideoFrames(ppapi::host::HostMessageContext
* context
);
78 int32_t OnHostMsgEncode(ppapi::host::HostMessageContext
* context
,
81 int32_t OnHostMsgRecycleBitstreamBuffer(
82 ppapi::host::HostMessageContext
* context
,
84 int32_t OnHostMsgRequestEncodingParametersChange(
85 ppapi::host::HostMessageContext
* context
,
88 int32_t OnHostMsgClose(ppapi::host::HostMessageContext
* context
);
91 void GetSupportedProfiles(
92 std::vector
<PP_VideoProfileDescription
>* pp_profiles
);
93 bool IsInitializationValid(const PP_Size
& input_size
,
94 PP_VideoProfile ouput_profile
,
95 PP_HardwareAcceleration acceleration
);
96 bool EnsureGpuChannel();
97 bool InitializeHardware(media::VideoFrame::Format input_format
,
98 const gfx::Size
& input_visible_size
,
99 media::VideoCodecProfile output_profile
,
100 uint32_t initial_bitrate
);
102 void AllocateVideoFrames();
103 void SendGetFramesErrorReply(int32_t error
);
104 scoped_refptr
<media::VideoFrame
> CreateVideoFrame(
106 const ppapi::host::ReplyMessageContext
& reply_context
);
107 void FrameReleased(const ppapi::host::ReplyMessageContext
& reply_context
,
109 void NotifyPepperError(int32_t error
);
111 // Non-owning pointer.
112 RendererPpapiHost
* renderer_ppapi_host_
;
114 ScopedVector
<ShmBuffer
> shm_buffers_
;
116 // Buffer manager for shared memory that holds video frames.
117 ppapi::MediaStreamBufferManager buffer_manager_
;
119 scoped_refptr
<GpuChannelHost
> channel_
;
120 CommandBufferProxyImpl
* command_buffer_
;
122 scoped_ptr
<media::VideoEncodeAccelerator
> encoder_
;
124 // Whether the encoder has been successfully initialized.
127 // Saved context to answer an Initialize message from the plugin.
128 ppapi::host::ReplyMessageContext initialize_reply_context_
;
130 // Saved context to answer a GetVideoFrames message from the plugin.
131 ppapi::host::ReplyMessageContext get_video_frames_reply_context_
;
133 // This represents the current error state of the encoder, i.e. PP_OK
134 // normally, or a Pepper error code if the encoder is uninitialized,
135 // has been notified of an encoder error, has encountered some
136 // other unrecoverable error, or has been closed by the plugin.
137 // This field is checked in most message handlers to decide whether
138 // operations should proceed or fail.
139 int32_t encoder_last_error_
;
141 // Size of the frames allocated for the encoder (matching hardware
143 gfx::Size input_coded_size_
;
145 // Number of frames the encoder needs.
146 uint32_t frame_count_
;
148 // Format of the frames to give to the encoder.
149 media::VideoFrame::Format media_input_format_
;
151 base::WeakPtrFactory
<PepperVideoEncoderHost
> weak_ptr_factory_
;
153 DISALLOW_COPY_AND_ASSIGN(PepperVideoEncoderHost
);
156 } // namespace content
158 #endif // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_ENCODER_HOST_H_