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
;
29 class VideoEncoderShim
;
31 class CONTENT_EXPORT PepperVideoEncoderHost
32 : public ppapi::host::ResourceHost
,
33 public media::VideoEncodeAccelerator::Client
,
34 public ppapi::MediaStreamBufferManager::Delegate
{
36 PepperVideoEncoderHost(RendererPpapiHost
* host
,
38 PP_Resource resource
);
39 ~PepperVideoEncoderHost() override
;
42 friend class VideoEncoderShim
;
44 // Shared memory buffers.
46 ShmBuffer(uint32_t id
, scoped_ptr
<base::SharedMemory
> shm
);
49 media::BitstreamBuffer
ToBitstreamBuffer();
51 // Index of the buffer in the ScopedVector. Buffers have the same id in
52 // the plugin and the host.
54 scoped_ptr
<base::SharedMemory
> shm
;
58 // media::VideoEncodeAccelerator implementation.
59 void RequireBitstreamBuffers(unsigned int input_count
,
60 const gfx::Size
& input_coded_size
,
61 size_t output_buffer_size
) override
;
62 void BitstreamBufferReady(int32 bitstream_buffer_id
,
64 bool key_frame
) override
;
65 void NotifyError(media::VideoEncodeAccelerator::Error error
) override
;
67 // ResourceHost implementation.
68 int32_t OnResourceMessageReceived(
69 const IPC::Message
& msg
,
70 ppapi::host::HostMessageContext
* context
) override
;
72 int32_t OnHostMsgGetSupportedProfiles(
73 ppapi::host::HostMessageContext
* context
);
74 int32_t OnHostMsgInitialize(ppapi::host::HostMessageContext
* context
,
75 PP_VideoFrame_Format input_format
,
76 const PP_Size
& input_visible_size
,
77 PP_VideoProfile output_profile
,
78 uint32_t initial_bitrate
,
79 PP_HardwareAcceleration acceleration
);
80 int32_t OnHostMsgGetVideoFrames(ppapi::host::HostMessageContext
* context
);
81 int32_t OnHostMsgEncode(ppapi::host::HostMessageContext
* context
,
84 int32_t OnHostMsgRecycleBitstreamBuffer(
85 ppapi::host::HostMessageContext
* context
,
87 int32_t OnHostMsgRequestEncodingParametersChange(
88 ppapi::host::HostMessageContext
* context
,
91 int32_t OnHostMsgClose(ppapi::host::HostMessageContext
* context
);
94 void GetSupportedProfiles(
95 std::vector
<PP_VideoProfileDescription
>* pp_profiles
);
96 bool IsInitializationValid(const PP_Size
& input_size
,
97 PP_VideoProfile ouput_profile
,
98 PP_HardwareAcceleration acceleration
);
99 bool EnsureGpuChannel();
100 bool InitializeHardware(media::VideoFrame::Format input_format
,
101 const gfx::Size
& input_visible_size
,
102 media::VideoCodecProfile output_profile
,
103 uint32_t initial_bitrate
);
105 void AllocateVideoFrames();
106 void SendGetFramesErrorReply(int32_t error
);
107 scoped_refptr
<media::VideoFrame
> CreateVideoFrame(
109 const ppapi::host::ReplyMessageContext
& reply_context
);
110 void FrameReleased(const ppapi::host::ReplyMessageContext
& reply_context
,
112 void NotifyPepperError(int32_t error
);
114 // Helper method for VideoEncoderShim.
115 uint8_t* ShmHandleToAddress(int32 buffer_id
);
117 // Non-owning pointer.
118 RendererPpapiHost
* renderer_ppapi_host_
;
120 ScopedVector
<ShmBuffer
> shm_buffers_
;
122 // Buffer manager for shared memory that holds video frames.
123 ppapi::MediaStreamBufferManager buffer_manager_
;
125 scoped_refptr
<GpuChannelHost
> channel_
;
126 CommandBufferProxyImpl
* command_buffer_
;
128 scoped_ptr
<media::VideoEncodeAccelerator
> encoder_
;
130 // Whether the encoder has been successfully initialized.
133 // Saved context to answer an Initialize message from the plugin.
134 ppapi::host::ReplyMessageContext initialize_reply_context_
;
136 // Saved context to answer a GetVideoFrames message from the plugin.
137 ppapi::host::ReplyMessageContext get_video_frames_reply_context_
;
139 // This represents the current error state of the encoder, i.e. PP_OK
140 // normally, or a Pepper error code if the encoder is uninitialized,
141 // has been notified of an encoder error, has encountered some
142 // other unrecoverable error, or has been closed by the plugin.
143 // This field is checked in most message handlers to decide whether
144 // operations should proceed or fail.
145 int32_t encoder_last_error_
;
147 // Size of the frames allocated for the encoder (matching hardware
149 gfx::Size input_coded_size_
;
151 // Number of frames the encoder needs.
152 uint32_t frame_count_
;
154 // Format of the frames to give to the encoder.
155 media::VideoFrame::Format media_input_format_
;
157 base::WeakPtrFactory
<PepperVideoEncoderHost
> weak_ptr_factory_
;
159 DISALLOW_COPY_AND_ASSIGN(PepperVideoEncoderHost
);
162 } // namespace content
164 #endif // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_ENCODER_HOST_H_