1 // Copyright (c) 2014 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_DECODER_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_DECODER_HOST_H_
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "content/common/content_export.h"
18 #include "media/video/video_decode_accelerator.h"
19 #include "ppapi/c/pp_codecs.h"
20 #include "ppapi/host/host_message_context.h"
21 #include "ppapi/host/resource_host.h"
22 #include "ppapi/proxy/resource_message_params.h"
30 class PPB_Graphics3D_Impl
;
31 class RendererPpapiHost
;
33 class VideoDecoderShim
;
35 class CONTENT_EXPORT PepperVideoDecoderHost
36 : public ppapi::host::ResourceHost
,
37 public media::VideoDecodeAccelerator::Client
{
39 PepperVideoDecoderHost(RendererPpapiHost
* host
,
41 PP_Resource resource
);
42 ~PepperVideoDecoderHost() override
;
45 enum class PictureBufferState
{
51 struct PendingDecode
{
52 PendingDecode(int32_t decode_id
,
55 const ppapi::host::ReplyMessageContext
& reply_context
);
58 const int32_t decode_id
;
59 const uint32_t shm_id
;
61 const ppapi::host::ReplyMessageContext reply_context
;
63 typedef std::list
<PendingDecode
> PendingDecodeList
;
65 friend class VideoDecoderShim
;
67 // ResourceHost implementation.
68 int32_t OnResourceMessageReceived(
69 const IPC::Message
& msg
,
70 ppapi::host::HostMessageContext
* context
) override
;
72 // media::VideoDecodeAccelerator::Client implementation.
73 void ProvidePictureBuffers(uint32 requested_num_of_buffers
,
74 const gfx::Size
& dimensions
,
75 uint32 texture_target
) override
;
76 void DismissPictureBuffer(int32 picture_buffer_id
) override
;
77 void PictureReady(const media::Picture
& picture
) override
;
78 void NotifyEndOfBitstreamBuffer(int32 bitstream_buffer_id
) override
;
79 void NotifyFlushDone() override
;
80 void NotifyResetDone() override
;
81 void NotifyError(media::VideoDecodeAccelerator::Error error
) override
;
83 int32_t OnHostMsgInitialize(ppapi::host::HostMessageContext
* context
,
84 const ppapi::HostResource
& graphics_context
,
85 PP_VideoProfile profile
,
86 PP_HardwareAcceleration acceleration
,
87 uint32_t min_picture_count
);
88 int32_t OnHostMsgGetShm(ppapi::host::HostMessageContext
* context
,
91 int32_t OnHostMsgDecode(ppapi::host::HostMessageContext
* context
,
95 int32_t OnHostMsgAssignTextures(ppapi::host::HostMessageContext
* context
,
97 const std::vector
<uint32_t>& texture_ids
);
98 int32_t OnHostMsgRecyclePicture(ppapi::host::HostMessageContext
* context
,
100 int32_t OnHostMsgFlush(ppapi::host::HostMessageContext
* context
);
101 int32_t OnHostMsgReset(ppapi::host::HostMessageContext
* context
);
103 // These methods are needed by VideoDecodeShim, to look like a
104 // VideoDecodeAccelerator.
105 const uint8_t* DecodeIdToAddress(uint32_t decode_id
);
106 void RequestTextures(uint32 requested_num_of_buffers
,
107 const gfx::Size
& dimensions
,
108 uint32 texture_target
,
109 const std::vector
<gpu::Mailbox
>& mailboxes
);
111 // Tries to initialize software decoder. Returns true on success.
112 bool TryFallbackToSoftwareDecoder();
114 PendingDecodeList::iterator
GetPendingDecodeById(int32_t decode_id
);
116 // Non-owning pointer.
117 RendererPpapiHost
* renderer_ppapi_host_
;
119 media::VideoCodecProfile profile_
;
121 scoped_ptr
<media::VideoDecodeAccelerator
> decoder_
;
123 bool software_fallback_allowed_
= false;
124 bool software_fallback_used_
= false;
126 // A vector holding our shm buffers, in sync with a similar vector in the
127 // resource. We use a buffer's index in these vectors as its id on both sides
128 // of the proxy. Only add buffers or update them in place so as not to
129 // invalidate these ids.
130 ScopedVector
<base::SharedMemory
> shm_buffers_
;
131 // A vector of true/false indicating if a shm buffer is in use by the decoder.
132 // This is parallel to |shm_buffers_|.
133 std::vector
<uint8_t> shm_buffer_busy_
;
135 uint32_t min_picture_count_
;
136 typedef std::map
<uint32_t, PictureBufferState
> PictureBufferMap
;
137 PictureBufferMap picture_buffer_map_
;
139 // Keeps list of pending decodes.
140 PendingDecodeList pending_decodes_
;
142 ppapi::host::ReplyMessageContext flush_reply_context_
;
143 ppapi::host::ReplyMessageContext reset_reply_context_
;
145 bool initialized_
= false;
147 DISALLOW_COPY_AND_ASSIGN(PepperVideoDecoderHost
);
150 } // namespace content
152 #endif // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_DECODER_HOST_H_