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 // cdm::Buffer implementation that provides access to memory owned by a
25 // This class holds a reference to the Buffer_Dev throughout its lifetime.
26 // TODO(xhwang): Find a better name. It's confusing to have PpbBuffer,
27 // pp::Buffer_Dev and PPB_Buffer_Dev.
28 class PpbBuffer
: public cdm::Buffer
{
30 static PpbBuffer
* Create(const pp::Buffer_Dev
& buffer
, uint32_t buffer_id
) {
31 PP_DCHECK(buffer
.data());
32 PP_DCHECK(buffer
.size());
34 return new PpbBuffer(buffer
, buffer_id
);
37 // cdm::Buffer implementation.
38 virtual void Destroy() OVERRIDE
{ delete this; }
40 virtual uint32_t Capacity() const OVERRIDE
{ return buffer_
.size(); }
42 virtual uint8_t* Data() OVERRIDE
{
43 return static_cast<uint8_t*>(buffer_
.data());
46 virtual void SetSize(uint32_t size
) OVERRIDE
{
47 PP_DCHECK(size
<= Capacity());
48 if (size
> Capacity()) {
56 virtual uint32_t Size() const OVERRIDE
{ return size_
; }
58 pp::Buffer_Dev
buffer_dev() const { return buffer_
; }
60 uint32_t buffer_id() const { return buffer_id_
; }
63 PpbBuffer(pp::Buffer_Dev buffer
, uint32_t buffer_id
)
65 buffer_id_(buffer_id
),
67 virtual ~PpbBuffer() {}
69 pp::Buffer_Dev buffer_
;
73 DISALLOW_COPY_AND_ASSIGN(PpbBuffer
);
76 class PpbBufferAllocator
{
78 explicit PpbBufferAllocator(pp::Instance
* instance
)
79 : instance_(instance
),
81 ~PpbBufferAllocator() {}
83 cdm::Buffer
* Allocate(uint32_t capacity
);
85 // Releases the buffer with |buffer_id|. A buffer can be recycled after
87 void Release(uint32_t buffer_id
);
90 typedef std::map
<uint32_t, pp::Buffer_Dev
> AllocatedBufferMap
;
91 typedef std::multimap
<uint32_t, std::pair
<uint32_t, pp::Buffer_Dev
> >
94 pp::Buffer_Dev
AllocateNewBuffer(uint32_t capacity
);
96 pp::Instance
* const instance_
;
97 uint32_t next_buffer_id_
;
98 AllocatedBufferMap allocated_buffers_
;
99 FreeBufferMap free_buffers_
;
101 DISALLOW_COPY_AND_ASSIGN(PpbBufferAllocator
);
104 class DecryptedBlockImpl
: public cdm::DecryptedBlock
{
106 DecryptedBlockImpl() : buffer_(NULL
), timestamp_(0) {}
107 virtual ~DecryptedBlockImpl() { if (buffer_
) buffer_
->Destroy(); }
109 virtual void SetDecryptedBuffer(cdm::Buffer
* buffer
) OVERRIDE
{
110 buffer_
= static_cast<PpbBuffer
*>(buffer
);
112 virtual cdm::Buffer
* DecryptedBuffer() OVERRIDE
{ return buffer_
; }
114 virtual void SetTimestamp(int64_t timestamp
) OVERRIDE
{
115 timestamp_
= timestamp
;
117 virtual int64_t Timestamp() const OVERRIDE
{ return timestamp_
; }
123 DISALLOW_COPY_AND_ASSIGN(DecryptedBlockImpl
);
126 class VideoFrameImpl
: public cdm::VideoFrame
{
129 virtual ~VideoFrameImpl();
131 virtual void SetFormat(cdm::VideoFormat format
) OVERRIDE
{
134 virtual cdm::VideoFormat
Format() const OVERRIDE
{ return format_
; }
136 virtual void SetSize(cdm::Size size
) OVERRIDE
{ size_
= size
; }
137 virtual cdm::Size
Size() const OVERRIDE
{ return size_
; }
139 virtual void SetFrameBuffer(cdm::Buffer
* frame_buffer
) OVERRIDE
{
140 frame_buffer_
= static_cast<PpbBuffer
*>(frame_buffer
);
142 virtual cdm::Buffer
* FrameBuffer() OVERRIDE
{ return frame_buffer_
; }
144 virtual void SetPlaneOffset(cdm::VideoFrame::VideoPlane plane
,
145 uint32_t offset
) OVERRIDE
{
146 PP_DCHECK(plane
< kMaxPlanes
);
147 plane_offsets_
[plane
] = offset
;
149 virtual uint32_t PlaneOffset(VideoPlane plane
) OVERRIDE
{
150 PP_DCHECK(plane
< kMaxPlanes
);
151 return plane_offsets_
[plane
];
154 virtual void SetStride(VideoPlane plane
, uint32_t stride
) OVERRIDE
{
155 PP_DCHECK(plane
< kMaxPlanes
);
156 strides_
[plane
] = stride
;
158 virtual uint32_t Stride(VideoPlane plane
) OVERRIDE
{
159 PP_DCHECK(plane
< kMaxPlanes
);
160 return strides_
[plane
];
163 virtual void SetTimestamp(int64_t timestamp
) OVERRIDE
{
164 timestamp_
= timestamp
;
166 virtual int64_t Timestamp() const OVERRIDE
{ return timestamp_
; }
169 // The video buffer format.
170 cdm::VideoFormat format_
;
172 // Width and height of the video frame.
175 // The video frame buffer.
176 PpbBuffer
* frame_buffer_
;
178 // Array of data pointers to each plane in the video frame buffer.
179 uint32_t plane_offsets_
[kMaxPlanes
];
181 // Array of strides for each plane, typically greater or equal to the width
182 // of the surface divided by the horizontal sampling period. Note that
183 // strides can be negative.
184 uint32_t strides_
[kMaxPlanes
];
186 // Presentation timestamp in microseconds.
189 DISALLOW_COPY_AND_ASSIGN(VideoFrameImpl
);
192 class AudioFramesImpl
: public cdm::AudioFrames_1
,
193 public cdm::AudioFrames_2
{
195 AudioFramesImpl() : buffer_(NULL
), format_(cdm::kUnknownAudioFormat
) {}
196 virtual ~AudioFramesImpl() {
201 // AudioFrames implementation.
202 virtual void SetFrameBuffer(cdm::Buffer
* buffer
) OVERRIDE
{
203 buffer_
= static_cast<PpbBuffer
*>(buffer
);
205 virtual cdm::Buffer
* FrameBuffer() OVERRIDE
{
208 virtual void SetFormat(cdm::AudioFormat format
) OVERRIDE
{
211 virtual cdm::AudioFormat
Format() const OVERRIDE
{
215 cdm::Buffer
* PassFrameBuffer() {
216 PpbBuffer
* temp_buffer
= buffer_
;
223 cdm::AudioFormat format_
;
225 DISALLOW_COPY_AND_ASSIGN(AudioFramesImpl
);
230 #endif // MEDIA_CDM_PPAPI_CDM_HELPERS_H_