1 // Copyright 2013 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 MEDIA_CDM_PPAPI_CDM_HELPERS_H_
6 #define MEDIA_CDM_PPAPI_CDM_HELPERS_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "build/build_config.h"
14 #include "media/cdm/ppapi/api/content_decryption_module.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/c/pp_stdint.h"
17 #include "ppapi/cpp/dev/buffer_dev.h"
18 #include "ppapi/cpp/instance.h"
19 #include "ppapi/cpp/logging.h"
23 class PpbBufferAllocator
;
25 // cdm::Buffer implementation that provides access to memory owned by a
27 // This class holds a reference to the Buffer_Dev throughout its lifetime.
28 // TODO(xhwang): Find a better name. It's confusing to have PpbBuffer,
29 // pp::Buffer_Dev and PPB_Buffer_Dev.
30 class PpbBuffer
: public cdm::Buffer
{
32 static PpbBuffer
* Create(const pp::Buffer_Dev
& buffer
, uint32_t buffer_id
,
33 PpbBufferAllocator
* allocator
);
35 // cdm::Buffer implementation.
36 void Destroy() override
;
37 uint32_t Capacity() const override
;
38 uint8_t* Data() override
;
39 void SetSize(uint32_t size
) override
;
40 uint32_t Size() const override
{ return size_
; }
42 // Takes the |buffer_| from this class and returns it.
43 // Note: The caller must ensure |allocator->Release()| is called later so that
44 // the buffer can be reused by the allocator.
45 // Since pp::Buffer_Dev is ref-counted, the caller now holds one reference to
46 // the buffer and this class holds no reference. Note that other references
47 // may still exist. For example, PpbBufferAllocator always holds a reference
48 // to all allocated buffers.
49 pp::Buffer_Dev
TakeBuffer();
51 uint32_t buffer_id() const { return buffer_id_
; }
54 PpbBuffer(pp::Buffer_Dev buffer
,
56 PpbBufferAllocator
* allocator
);
57 ~PpbBuffer() override
;
59 pp::Buffer_Dev buffer_
;
62 PpbBufferAllocator
* allocator_
;
64 DISALLOW_COPY_AND_ASSIGN(PpbBuffer
);
67 class PpbBufferAllocator
{
69 explicit PpbBufferAllocator(pp::Instance
* instance
)
70 : instance_(instance
),
72 ~PpbBufferAllocator() {}
74 cdm::Buffer
* Allocate(uint32_t capacity
);
76 // Releases the buffer with |buffer_id|. A buffer can be recycled after
78 void Release(uint32_t buffer_id
);
81 typedef std::map
<uint32_t, pp::Buffer_Dev
> AllocatedBufferMap
;
82 typedef std::multimap
<uint32_t, std::pair
<uint32_t, pp::Buffer_Dev
> >
85 pp::Buffer_Dev
AllocateNewBuffer(uint32_t capacity
);
87 pp::Instance
* const instance_
;
88 uint32_t next_buffer_id_
;
89 AllocatedBufferMap allocated_buffers_
;
90 FreeBufferMap free_buffers_
;
92 DISALLOW_COPY_AND_ASSIGN(PpbBufferAllocator
);
95 class DecryptedBlockImpl
: public cdm::DecryptedBlock
{
97 DecryptedBlockImpl() : buffer_(NULL
), timestamp_(0) {}
98 ~DecryptedBlockImpl() override
{
103 void SetDecryptedBuffer(cdm::Buffer
* buffer
) override
{
104 buffer_
= static_cast<PpbBuffer
*>(buffer
);
106 cdm::Buffer
* DecryptedBuffer() override
{ return buffer_
; }
108 void SetTimestamp(int64_t timestamp
) override
{
109 timestamp_
= timestamp
;
111 int64_t Timestamp() const override
{ return timestamp_
; }
117 DISALLOW_COPY_AND_ASSIGN(DecryptedBlockImpl
);
120 class VideoFrameImpl
: public cdm::VideoFrame
{
123 ~VideoFrameImpl() override
;
125 void SetFormat(cdm::VideoFormat format
) override
{
128 cdm::VideoFormat
Format() const override
{ return format_
; }
130 void SetSize(cdm::Size size
) override
{ size_
= size
; }
131 cdm::Size
Size() const override
{ return size_
; }
133 void SetFrameBuffer(cdm::Buffer
* frame_buffer
) override
{
134 frame_buffer_
= static_cast<PpbBuffer
*>(frame_buffer
);
136 cdm::Buffer
* FrameBuffer() override
{ return frame_buffer_
; }
138 void SetPlaneOffset(cdm::VideoFrame::VideoPlane plane
,
139 uint32_t offset
) override
{
140 PP_DCHECK(plane
< kMaxPlanes
);
141 plane_offsets_
[plane
] = offset
;
143 uint32_t PlaneOffset(VideoPlane plane
) override
{
144 PP_DCHECK(plane
< kMaxPlanes
);
145 return plane_offsets_
[plane
];
148 void SetStride(VideoPlane plane
, uint32_t stride
) override
{
149 PP_DCHECK(plane
< kMaxPlanes
);
150 strides_
[plane
] = stride
;
152 uint32_t Stride(VideoPlane plane
) override
{
153 PP_DCHECK(plane
< kMaxPlanes
);
154 return strides_
[plane
];
157 void SetTimestamp(int64_t timestamp
) override
{
158 timestamp_
= timestamp
;
160 int64_t Timestamp() const override
{ return timestamp_
; }
163 // The video buffer format.
164 cdm::VideoFormat format_
;
166 // Width and height of the video frame.
169 // The video frame buffer.
170 PpbBuffer
* frame_buffer_
;
172 // Array of data pointers to each plane in the video frame buffer.
173 uint32_t plane_offsets_
[kMaxPlanes
];
175 // Array of strides for each plane, typically greater or equal to the width
176 // of the surface divided by the horizontal sampling period. Note that
177 // strides can be negative.
178 uint32_t strides_
[kMaxPlanes
];
180 // Presentation timestamp in microseconds.
183 DISALLOW_COPY_AND_ASSIGN(VideoFrameImpl
);
186 class AudioFramesImpl
: public cdm::AudioFrames
{
188 AudioFramesImpl() : buffer_(NULL
), format_(cdm::kUnknownAudioFormat
) {}
189 ~AudioFramesImpl() override
{
194 // AudioFrames implementation.
195 void SetFrameBuffer(cdm::Buffer
* buffer
) override
{
196 buffer_
= static_cast<PpbBuffer
*>(buffer
);
198 cdm::Buffer
* FrameBuffer() override
{
201 void SetFormat(cdm::AudioFormat format
) override
{
204 cdm::AudioFormat
Format() const override
{
208 cdm::Buffer
* PassFrameBuffer() {
209 PpbBuffer
* temp_buffer
= buffer_
;
216 cdm::AudioFormat format_
;
218 DISALLOW_COPY_AND_ASSIGN(AudioFramesImpl
);
223 #endif // MEDIA_CDM_PPAPI_CDM_HELPERS_H_