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 Initialize(PP_VideoFrame_Format input_format
,
68 const PP_Size
* input_visible_size
,
69 PP_VideoProfile output_profile
,
70 uint32_t initial_bitrate
,
71 PP_HardwareAcceleration acceleration
,
72 const scoped_refptr
<TrackedCallback
>& callback
) override
;
73 int32_t GetFramesRequired() override
;
74 int32_t GetFrameCodedSize(PP_Size
* size
) override
;
75 int32_t GetVideoFrame(
76 PP_Resource
* video_frame
,
77 const scoped_refptr
<TrackedCallback
>& callback
) override
;
78 int32_t Encode(PP_Resource video_frame
,
79 PP_Bool force_keyframe
,
80 const scoped_refptr
<TrackedCallback
>& callback
) override
;
81 int32_t GetBitstreamBuffer(
82 PP_BitstreamBuffer
* picture
,
83 const scoped_refptr
<TrackedCallback
>& callback
) override
;
84 void RecycleBitstreamBuffer(const PP_BitstreamBuffer
* picture
) override
;
85 void RequestEncodingParametersChange(uint32_t bitrate
,
86 uint32_t framerate
) override
;
87 void Close() override
;
89 // PluginResource implementation.
90 void OnReplyReceived(const ResourceMessageReplyParams
& params
,
91 const IPC::Message
& msg
) override
;
93 // Reply message handlers for operations that are done in the host.
94 void OnPluginMsgGetSupportedProfilesReply(
95 const PP_ArrayOutput
& output
,
96 const ResourceMessageReplyParams
& params
,
97 const std::vector
<PP_VideoProfileDescription
>& profiles
);
98 void OnPluginMsgInitializeReply(const ResourceMessageReplyParams
& params
,
99 uint32_t input_frame_count
,
100 const PP_Size
& input_coded_size
);
101 void OnPluginMsgGetVideoFramesReply(const ResourceMessageReplyParams
& params
,
102 uint32_t frame_count
,
103 uint32_t frame_length
,
104 const PP_Size
& frame_size
);
105 void OnPluginMsgEncodeReply(PP_Resource video_frame
,
106 const ResourceMessageReplyParams
& params
,
109 // Unsolicited reply message handlers.
110 void OnPluginMsgBitstreamBuffers(const ResourceMessageReplyParams
& params
,
111 uint32_t buffer_length
);
112 void OnPluginMsgBitstreamBufferReady(const ResourceMessageReplyParams
& params
,
114 uint32_t buffer_size
,
116 void OnPluginMsgNotifyError(const ResourceMessageReplyParams
& params
,
119 // Internal utility functions.
120 void NotifyError(int32_t error
);
121 void TryWriteVideoFrame();
122 void WriteBitstreamBuffer(const BitstreamBuffer
& buffer
);
123 void ReleaseFrames();
127 int32_t encoder_last_error_
;
129 int32_t input_frame_count_
;
130 PP_Size input_coded_size_
;
132 MediaStreamBufferManager buffer_manager_
;
134 typedef std::map
<PP_Resource
, scoped_refptr
<VideoFrameResource
> >
136 VideoFrameMap video_frames_
;
138 ScopedVector
<ShmBuffer
> shm_buffers_
;
140 std::deque
<BitstreamBuffer
> available_bitstream_buffers_
;
141 typedef std::map
<void*, uint32_t> BitstreamBufferMap
;
142 BitstreamBufferMap bitstream_buffer_map_
;
144 scoped_refptr
<TrackedCallback
> get_supported_profiles_callback_
;
145 scoped_refptr
<TrackedCallback
> initialize_callback_
;
146 scoped_refptr
<TrackedCallback
> get_video_frame_callback_
;
147 PP_Resource
* get_video_frame_data_
;
149 typedef std::map
<PP_Resource
, scoped_refptr
<TrackedCallback
> > EncodeMap
;
150 EncodeMap encode_callbacks_
;
152 scoped_refptr
<TrackedCallback
> get_bitstream_buffer_callback_
;
153 PP_BitstreamBuffer
* get_bitstream_buffer_data_
;
155 DISALLOW_COPY_AND_ASSIGN(VideoEncoderResource
);
161 #endif // PPAPI_PROXY_VIDEO_ENCODER_RESOURCE_H_