Make castv2 performance test work.
[chromium-blink-merge.git] / ppapi / proxy / video_encoder_resource.h
blob3b46759d093b8af06a221adffa0eefa3a87dd4d0
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_
8 #include <deque>
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"
19 namespace base {
20 class SharedMemory;
23 namespace ppapi {
25 class TrackedCallback;
27 namespace proxy {
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 {
36 public:
37 VideoEncoderResource(Connection connection, PP_Instance instance);
38 ~VideoEncoderResource() override;
40 thunk::PPB_VideoEncoder_API* AsPPB_VideoEncoder_API() override;
42 private:
43 struct ShmBuffer {
44 ShmBuffer(uint32_t id, scoped_ptr<base::SharedMemory> shm);
45 ~ShmBuffer();
47 // Index of the buffer in the ScopedVector. Buffers have the same id in
48 // the plugin and the host.
49 uint32_t id;
50 scoped_ptr<base::SharedMemory> shm;
53 struct BitstreamBuffer {
54 BitstreamBuffer(uint32_t id, uint32_t size, bool key_frame);
55 ~BitstreamBuffer();
57 // Index of the buffer in the ScopedVector. Same as ShmBuffer::id.
58 uint32_t id;
59 uint32_t size;
60 bool key_frame;
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,
107 uint32_t frame_id);
109 // Unsolicited reply message handlers.
110 void OnPluginMsgBitstreamBuffers(const ResourceMessageReplyParams& params,
111 uint32_t buffer_length);
112 void OnPluginMsgBitstreamBufferReady(const ResourceMessageReplyParams& params,
113 uint32_t buffer_id,
114 uint32_t buffer_size,
115 bool key_frame);
116 void OnPluginMsgNotifyError(const ResourceMessageReplyParams& params,
117 int32_t error);
119 // Internal utility functions.
120 void NotifyError(int32_t error);
121 void TryWriteVideoFrame();
122 void WriteBitstreamBuffer(const BitstreamBuffer& buffer);
123 void ReleaseFrames();
125 bool initialized_;
126 bool closed_;
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> >
135 VideoFrameMap;
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);
158 } // namespace proxy
159 } // namespace ppapi
161 #endif // PPAPI_PROXY_VIDEO_ENCODER_RESOURCE_H_