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 virtual void Destroy() override
;
37 virtual uint32_t Capacity() const override
;
38 virtual uint8_t* Data() override
;
39 virtual void SetSize(uint32_t size
) override
;
40 virtual 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
);
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 virtual ~DecryptedBlockImpl() { if (buffer_
) buffer_
->Destroy(); }
100 virtual void SetDecryptedBuffer(cdm::Buffer
* buffer
) override
{
101 buffer_
= static_cast<PpbBuffer
*>(buffer
);
103 virtual cdm::Buffer
* DecryptedBuffer() override
{ return buffer_
; }
105 virtual void SetTimestamp(int64_t timestamp
) override
{
106 timestamp_
= timestamp
;
108 virtual int64_t Timestamp() const override
{ return timestamp_
; }
114 DISALLOW_COPY_AND_ASSIGN(DecryptedBlockImpl
);
117 class VideoFrameImpl
: public cdm::VideoFrame
{
120 virtual ~VideoFrameImpl();
122 virtual void SetFormat(cdm::VideoFormat format
) override
{
125 virtual cdm::VideoFormat
Format() const override
{ return format_
; }
127 virtual void SetSize(cdm::Size size
) override
{ size_
= size
; }
128 virtual cdm::Size
Size() const override
{ return size_
; }
130 virtual void SetFrameBuffer(cdm::Buffer
* frame_buffer
) override
{
131 frame_buffer_
= static_cast<PpbBuffer
*>(frame_buffer
);
133 virtual cdm::Buffer
* FrameBuffer() override
{ return frame_buffer_
; }
135 virtual void SetPlaneOffset(cdm::VideoFrame::VideoPlane plane
,
136 uint32_t offset
) override
{
137 PP_DCHECK(plane
< kMaxPlanes
);
138 plane_offsets_
[plane
] = offset
;
140 virtual uint32_t PlaneOffset(VideoPlane plane
) override
{
141 PP_DCHECK(plane
< kMaxPlanes
);
142 return plane_offsets_
[plane
];
145 virtual void SetStride(VideoPlane plane
, uint32_t stride
) override
{
146 PP_DCHECK(plane
< kMaxPlanes
);
147 strides_
[plane
] = stride
;
149 virtual uint32_t Stride(VideoPlane plane
) override
{
150 PP_DCHECK(plane
< kMaxPlanes
);
151 return strides_
[plane
];
154 virtual void SetTimestamp(int64_t timestamp
) override
{
155 timestamp_
= timestamp
;
157 virtual int64_t Timestamp() const override
{ return timestamp_
; }
160 // The video buffer format.
161 cdm::VideoFormat format_
;
163 // Width and height of the video frame.
166 // The video frame buffer.
167 PpbBuffer
* frame_buffer_
;
169 // Array of data pointers to each plane in the video frame buffer.
170 uint32_t plane_offsets_
[kMaxPlanes
];
172 // Array of strides for each plane, typically greater or equal to the width
173 // of the surface divided by the horizontal sampling period. Note that
174 // strides can be negative.
175 uint32_t strides_
[kMaxPlanes
];
177 // Presentation timestamp in microseconds.
180 DISALLOW_COPY_AND_ASSIGN(VideoFrameImpl
);
183 class AudioFramesImpl
: public cdm::AudioFrames
{
185 AudioFramesImpl() : buffer_(NULL
), format_(cdm::kUnknownAudioFormat
) {}
186 virtual ~AudioFramesImpl() {
191 // AudioFrames implementation.
192 virtual void SetFrameBuffer(cdm::Buffer
* buffer
) override
{
193 buffer_
= static_cast<PpbBuffer
*>(buffer
);
195 virtual cdm::Buffer
* FrameBuffer() override
{
198 virtual void SetFormat(cdm::AudioFormat format
) override
{
201 virtual cdm::AudioFormat
Format() const override
{
205 cdm::Buffer
* PassFrameBuffer() {
206 PpbBuffer
* temp_buffer
= buffer_
;
213 cdm::AudioFormat format_
;
215 DISALLOW_COPY_AND_ASSIGN(AudioFramesImpl
);
220 #endif // MEDIA_CDM_PPAPI_CDM_HELPERS_H_