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 PPAPI_PROXY_VIDEO_ENCODER_RESOURCE_H_
6 #define PPAPI_PROXY_VIDEO_ENCODER_RESOURCE_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "ppapi/proxy/connection.h"
14 #include "ppapi/proxy/plugin_resource.h"
15 #include "ppapi/shared_impl/media_stream_buffer_manager.h"
16 #include "ppapi/shared_impl/resource.h"
17 #include "ppapi/thunk/ppb_video_encoder_api.h"
25 class TrackedCallback
;
29 class SerializedHandle
;
30 class VideoFrameResource
;
32 class PPAPI_PROXY_EXPORT VideoEncoderResource
33 : public PluginResource
,
34 public thunk::PPB_VideoEncoder_API
,
35 public ppapi::MediaStreamBufferManager::Delegate
{
37 VideoEncoderResource(Connection connection
, PP_Instance instance
);
38 ~VideoEncoderResource() override
;
40 thunk::PPB_VideoEncoder_API
* AsPPB_VideoEncoder_API() override
;
44 ShmBuffer(uint32_t id
, scoped_ptr
<base::SharedMemory
> shm
);
47 // Index of the buffer in the ScopedVector. Buffers have the same id in
48 // the plugin and the host.
50 scoped_ptr
<base::SharedMemory
> shm
;
53 struct BitstreamBuffer
{
54 BitstreamBuffer(uint32_t id
, uint32_t size
, bool key_frame
);
57 // Index of the buffer in the ScopedVector. Same as ShmBuffer::id.
63 // PPB_VideoEncoder_API implementation.
64 int32_t GetSupportedProfiles(
65 const PP_ArrayOutput
& output
,
66 const scoped_refptr
<TrackedCallback
>& callback
) override
;
67 int32_t GetSupportedProfiles0_1(
68 const PP_ArrayOutput
& output
,
69 const scoped_refptr
<TrackedCallback
>& callback
) override
;
70 int32_t Initialize(PP_VideoFrame_Format input_format
,
71 const PP_Size
* input_visible_size
,
72 PP_VideoProfile output_profile
,
73 uint32_t initial_bitrate
,
74 PP_HardwareAcceleration acceleration
,
75 const scoped_refptr
<TrackedCallback
>& callback
) override
;
76 int32_t GetFramesRequired() override
;
77 int32_t GetFrameCodedSize(PP_Size
* size
) override
;
78 int32_t GetVideoFrame(
79 PP_Resource
* video_frame
,
80 const scoped_refptr
<TrackedCallback
>& callback
) override
;
81 int32_t Encode(PP_Resource video_frame
,
82 PP_Bool force_keyframe
,
83 const scoped_refptr
<TrackedCallback
>& callback
) override
;
84 int32_t GetBitstreamBuffer(
85 PP_BitstreamBuffer
* picture
,
86 const scoped_refptr
<TrackedCallback
>& callback
) override
;
87 void RecycleBitstreamBuffer(const PP_BitstreamBuffer
* picture
) override
;
88 void RequestEncodingParametersChange(uint32_t bitrate
,
89 uint32_t framerate
) override
;
90 void Close() override
;
92 // PluginResource implementation.
93 void OnReplyReceived(const ResourceMessageReplyParams
& params
,
94 const IPC::Message
& msg
) override
;
96 // Reply message handlers for operations that are done in the host.
97 void OnPluginMsgGetSupportedProfilesReply(
98 const PP_ArrayOutput
& output
,
100 const ResourceMessageReplyParams
& params
,
101 const std::vector
<PP_VideoProfileDescription
>& profiles
);
102 void OnPluginMsgInitializeReply(const ResourceMessageReplyParams
& params
,
103 uint32_t input_frame_count
,
104 const PP_Size
& input_coded_size
);
105 void OnPluginMsgGetVideoFramesReply(const ResourceMessageReplyParams
& params
,
106 uint32_t frame_count
,
107 uint32_t frame_length
,
108 const PP_Size
& frame_size
);
109 void OnPluginMsgEncodeReply(PP_Resource video_frame
,
110 const ResourceMessageReplyParams
& params
,
113 // Unsolicited reply message handlers.
114 void OnPluginMsgBitstreamBuffers(const ResourceMessageReplyParams
& params
,
115 uint32_t buffer_length
);
116 void OnPluginMsgBitstreamBufferReady(const ResourceMessageReplyParams
& params
,
118 uint32_t buffer_size
,
120 void OnPluginMsgNotifyError(const ResourceMessageReplyParams
& params
,
123 // Internal utility functions.
124 void NotifyError(int32_t error
);
125 void TryWriteVideoFrame();
126 void WriteBitstreamBuffer(const BitstreamBuffer
& buffer
);
127 void ReleaseFrames();
131 int32_t encoder_last_error_
;
133 int32_t input_frame_count_
;
134 PP_Size input_coded_size_
;
136 MediaStreamBufferManager buffer_manager_
;
138 typedef std::map
<PP_Resource
, scoped_refptr
<VideoFrameResource
> >
140 VideoFrameMap video_frames_
;
142 ScopedVector
<ShmBuffer
> shm_buffers_
;
144 std::deque
<BitstreamBuffer
> available_bitstream_buffers_
;
145 typedef std::map
<void*, uint32_t> BitstreamBufferMap
;
146 BitstreamBufferMap bitstream_buffer_map_
;
148 scoped_refptr
<TrackedCallback
> get_supported_profiles_callback_
;
149 scoped_refptr
<TrackedCallback
> initialize_callback_
;
150 scoped_refptr
<TrackedCallback
> get_video_frame_callback_
;
151 PP_Resource
* get_video_frame_data_
;
153 typedef std::map
<PP_Resource
, scoped_refptr
<TrackedCallback
> > EncodeMap
;
154 EncodeMap encode_callbacks_
;
156 scoped_refptr
<TrackedCallback
> get_bitstream_buffer_callback_
;
157 PP_BitstreamBuffer
* get_bitstream_buffer_data_
;
159 DISALLOW_COPY_AND_ASSIGN(VideoEncoderResource
);
165 #endif // PPAPI_PROXY_VIDEO_ENCODER_RESOURCE_H_