Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef CODEC2_BUFFER_H_ |
| 18 | |
| 19 | #define CODEC2_BUFFER_H_ |
| 20 | |
| 21 | #include <C2Buffer.h> |
| 22 | |
| 23 | #include <android/hardware/cas/native/1.0/types.h> |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 24 | #include <android/hardware/drm/1.0/types.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 25 | #include <binder/IMemory.h> |
| 26 | #include <media/hardware/VideoAPI.h> |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 27 | #include <media/stagefright/foundation/ABuffer.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 28 | #include <media/MediaCodecBuffer.h> |
Marco Nelissen | 13aa1a4 | 2019-09-27 10:21:55 -0700 | [diff] [blame] | 29 | #include <mediadrm/ICrypto.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | /** |
| 34 | * Copies a graphic view into a media image. |
| 35 | * |
| 36 | * \param imgBase base of MediaImage |
| 37 | * \param img MediaImage data |
| 38 | * \param view graphic view |
| 39 | * |
| 40 | * \return OK on success |
| 41 | */ |
| 42 | status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view); |
| 43 | |
| 44 | /** |
| 45 | * Copies a media image into a graphic view. |
| 46 | * |
| 47 | * \param view graphic view |
| 48 | * \param imgBase base of MediaImage |
| 49 | * \param img MediaImage data |
| 50 | * |
| 51 | * \return OK on success |
| 52 | */ |
| 53 | status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img); |
| 54 | |
| 55 | class Codec2Buffer : public MediaCodecBuffer { |
| 56 | public: |
| 57 | using MediaCodecBuffer::MediaCodecBuffer; |
| 58 | ~Codec2Buffer() override = default; |
| 59 | |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 60 | sp<ABuffer> getImageData() const { return mImageData; } |
| 61 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 62 | protected: |
| 63 | /** |
| 64 | * canCopy() implementation for linear buffers. |
| 65 | */ |
| 66 | bool canCopyLinear(const std::shared_ptr<C2Buffer> &buffer) const; |
| 67 | |
| 68 | /** |
| 69 | * copy() implementation for linear buffers. |
| 70 | */ |
| 71 | bool copyLinear(const std::shared_ptr<C2Buffer> &buffer); |
| 72 | |
| 73 | /** |
| 74 | * sets MediaImage data for flexible graphic buffers |
| 75 | */ |
| 76 | void setImageData(const sp<ABuffer> &imageData); |
Wonsik Kim | c48ddcf | 2019-02-11 16:16:57 -0800 | [diff] [blame] | 77 | |
| 78 | sp<ABuffer> mImageData; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * MediaCodecBuffer implementation on top of local linear buffer. This cannot |
| 83 | * cross process boundary so asC2Buffer() returns only nullptr. |
| 84 | */ |
| 85 | class LocalLinearBuffer : public Codec2Buffer { |
| 86 | public: |
| 87 | using Codec2Buffer::Codec2Buffer; |
| 88 | |
| 89 | std::shared_ptr<C2Buffer> asC2Buffer() override { return nullptr; } |
| 90 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 91 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * MediaCodecBuffer implementation to be used only as a dummy wrapper around a |
| 96 | * C2Buffer object. |
| 97 | */ |
| 98 | class DummyContainerBuffer : public Codec2Buffer { |
| 99 | public: |
| 100 | DummyContainerBuffer( |
| 101 | const sp<AMessage> &format, |
| 102 | const std::shared_ptr<C2Buffer> &buffer = nullptr); |
| 103 | |
| 104 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 105 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 106 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 107 | |
| 108 | private: |
| 109 | std::shared_ptr<C2Buffer> mBufferRef; |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * MediaCodecBuffer implementation wraps around C2LinearBlock. |
| 114 | */ |
| 115 | class LinearBlockBuffer : public Codec2Buffer { |
| 116 | public: |
| 117 | /** |
| 118 | * Allocate a new LinearBufferBlock wrapping around C2LinearBlock object. |
| 119 | * |
| 120 | * \param format mandatory buffer format for MediaCodecBuffer |
| 121 | * \param block C2LinearBlock object to wrap around. |
| 122 | * \return LinearBlockBuffer object with writable mapping. |
| 123 | * nullptr if unsuccessful. |
| 124 | */ |
| 125 | static sp<LinearBlockBuffer> Allocate( |
| 126 | const sp<AMessage> &format, const std::shared_ptr<C2LinearBlock> &block); |
| 127 | |
| 128 | virtual ~LinearBlockBuffer() = default; |
| 129 | |
| 130 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 131 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 132 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 133 | |
| 134 | private: |
| 135 | LinearBlockBuffer( |
| 136 | const sp<AMessage> &format, |
| 137 | C2WriteView &&writeView, |
| 138 | const std::shared_ptr<C2LinearBlock> &block); |
| 139 | LinearBlockBuffer() = delete; |
| 140 | |
| 141 | C2WriteView mWriteView; |
| 142 | std::shared_ptr<C2LinearBlock> mBlock; |
| 143 | }; |
| 144 | |
| 145 | /** |
| 146 | * MediaCodecBuffer implementation wraps around C2ConstLinearBlock. |
| 147 | */ |
| 148 | class ConstLinearBlockBuffer : public Codec2Buffer { |
| 149 | public: |
| 150 | /** |
| 151 | * Allocate a new ConstLinearBlockBuffer wrapping around C2Buffer object. |
| 152 | * |
| 153 | * \param format mandatory buffer format for MediaCodecBuffer |
| 154 | * \param buffer linear C2Buffer object to wrap around. |
| 155 | * \return ConstLinearBlockBuffer object with readable mapping. |
| 156 | * nullptr if unsuccessful. |
| 157 | */ |
| 158 | static sp<ConstLinearBlockBuffer> Allocate( |
| 159 | const sp<AMessage> &format, const std::shared_ptr<C2Buffer> &buffer); |
| 160 | |
| 161 | virtual ~ConstLinearBlockBuffer() = default; |
| 162 | |
| 163 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 164 | |
| 165 | private: |
| 166 | ConstLinearBlockBuffer( |
| 167 | const sp<AMessage> &format, |
| 168 | C2ReadView &&readView, |
| 169 | const std::shared_ptr<C2Buffer> &buffer); |
| 170 | ConstLinearBlockBuffer() = delete; |
| 171 | |
| 172 | C2ReadView mReadView; |
| 173 | std::shared_ptr<C2Buffer> mBufferRef; |
| 174 | }; |
| 175 | |
| 176 | /** |
| 177 | * MediaCodecBuffer implementation wraps around C2GraphicBlock. |
| 178 | * |
| 179 | * This object exposes the underlying bits via accessor APIs and "image-data" |
| 180 | * metadata, created automatically at allocation time. |
| 181 | */ |
| 182 | class GraphicBlockBuffer : public Codec2Buffer { |
| 183 | public: |
| 184 | /** |
| 185 | * Allocate a new GraphicBlockBuffer wrapping around C2GraphicBlock object. |
| 186 | * If |block| is not in good color formats, it allocates YV12 local buffer |
| 187 | * and copies the content over at asC2Buffer(). |
| 188 | * |
| 189 | * \param format mandatory buffer format for MediaCodecBuffer |
| 190 | * \param block C2GraphicBlock object to wrap around. |
| 191 | * \param alloc a function to allocate backing ABuffer if needed. |
| 192 | * \return GraphicBlockBuffer object with writable mapping. |
| 193 | * nullptr if unsuccessful. |
| 194 | */ |
| 195 | static sp<GraphicBlockBuffer> Allocate( |
| 196 | const sp<AMessage> &format, |
| 197 | const std::shared_ptr<C2GraphicBlock> &block, |
| 198 | std::function<sp<ABuffer>(size_t)> alloc); |
| 199 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 200 | virtual ~GraphicBlockBuffer() = default; |
| 201 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 202 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 203 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 204 | private: |
| 205 | GraphicBlockBuffer( |
| 206 | const sp<AMessage> &format, |
| 207 | const sp<ABuffer> &buffer, |
| 208 | C2GraphicView &&view, |
| 209 | const std::shared_ptr<C2GraphicBlock> &block, |
| 210 | const sp<ABuffer> &imageData, |
| 211 | bool wrapped); |
| 212 | GraphicBlockBuffer() = delete; |
| 213 | |
| 214 | inline MediaImage2 *imageData() { return (MediaImage2 *)mImageData->data(); } |
| 215 | |
| 216 | C2GraphicView mView; |
| 217 | std::shared_ptr<C2GraphicBlock> mBlock; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 218 | const bool mWrapped; |
| 219 | }; |
| 220 | |
| 221 | /** |
| 222 | * MediaCodecBuffer implementation wraps around VideoNativeMetadata. |
| 223 | */ |
| 224 | class GraphicMetadataBuffer : public Codec2Buffer { |
| 225 | public: |
| 226 | /** |
| 227 | * Construct a new GraphicMetadataBuffer with local linear buffer for |
| 228 | * VideoNativeMetadata. |
| 229 | * |
| 230 | * \param format mandatory buffer format for MediaCodecBuffer |
| 231 | */ |
| 232 | GraphicMetadataBuffer( |
| 233 | const sp<AMessage> &format, const std::shared_ptr<C2Allocator> &alloc); |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 234 | virtual ~GraphicMetadataBuffer() = default; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 235 | |
| 236 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 237 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 238 | private: |
| 239 | GraphicMetadataBuffer() = delete; |
| 240 | |
| 241 | std::shared_ptr<C2Allocator> mAlloc; |
| 242 | }; |
| 243 | |
| 244 | /** |
| 245 | * MediaCodecBuffer implementation wraps around graphic C2Buffer object. |
| 246 | * |
| 247 | * This object exposes the underlying bits via accessor APIs and "image-data" |
| 248 | * metadata, created automatically at allocation time. |
| 249 | */ |
| 250 | class ConstGraphicBlockBuffer : public Codec2Buffer { |
| 251 | public: |
| 252 | /** |
| 253 | * Allocate a new ConstGraphicBlockBuffer wrapping around C2Buffer object. |
| 254 | * If |buffer| is not in good color formats, it allocates YV12 local buffer |
| 255 | * and copies the content of |buffer| over to expose. |
| 256 | * |
| 257 | * \param format mandatory buffer format for MediaCodecBuffer |
| 258 | * \param buffer graphic C2Buffer object to wrap around. |
| 259 | * \param alloc a function to allocate backing ABuffer if needed. |
| 260 | * \return ConstGraphicBlockBuffer object with readable mapping. |
| 261 | * nullptr if unsuccessful. |
| 262 | */ |
| 263 | static sp<ConstGraphicBlockBuffer> Allocate( |
| 264 | const sp<AMessage> &format, |
| 265 | const std::shared_ptr<C2Buffer> &buffer, |
| 266 | std::function<sp<ABuffer>(size_t)> alloc); |
| 267 | |
| 268 | /** |
| 269 | * Allocate a new ConstGraphicBlockBuffer which allocates YV12 local buffer |
| 270 | * and copies the content of |buffer| over to expose. |
| 271 | * |
| 272 | * \param format mandatory buffer format for MediaCodecBuffer |
| 273 | * \param alloc a function to allocate backing ABuffer if needed. |
| 274 | * \return ConstGraphicBlockBuffer object with no wrapping buffer. |
| 275 | */ |
| 276 | static sp<ConstGraphicBlockBuffer> AllocateEmpty( |
| 277 | const sp<AMessage> &format, |
| 278 | std::function<sp<ABuffer>(size_t)> alloc); |
| 279 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 280 | virtual ~ConstGraphicBlockBuffer() = default; |
| 281 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 282 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 283 | bool canCopy(const std::shared_ptr<C2Buffer> &buffer) const override; |
| 284 | bool copy(const std::shared_ptr<C2Buffer> &buffer) override; |
| 285 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 286 | private: |
| 287 | ConstGraphicBlockBuffer( |
| 288 | const sp<AMessage> &format, |
| 289 | const sp<ABuffer> &aBuffer, |
| 290 | std::unique_ptr<const C2GraphicView> &&view, |
| 291 | const std::shared_ptr<C2Buffer> &buffer, |
| 292 | const sp<ABuffer> &imageData, |
| 293 | bool wrapped); |
| 294 | ConstGraphicBlockBuffer() = delete; |
| 295 | |
| 296 | sp<ABuffer> mImageData; |
| 297 | std::unique_ptr<const C2GraphicView> mView; |
| 298 | std::shared_ptr<C2Buffer> mBufferRef; |
| 299 | const bool mWrapped; |
| 300 | }; |
| 301 | |
| 302 | /** |
| 303 | * MediaCodecBuffer implementation wraps around C2LinearBlock for component |
| 304 | * and IMemory for client. Underlying C2LinearBlock won't be mapped for secure |
| 305 | * usecases.. |
| 306 | */ |
| 307 | class EncryptedLinearBlockBuffer : public Codec2Buffer { |
| 308 | public: |
| 309 | /** |
| 310 | * Construct a new EncryptedLinearBufferBlock wrapping around C2LinearBlock |
| 311 | * object and writable IMemory region. |
| 312 | * |
| 313 | * \param format mandatory buffer format for MediaCodecBuffer |
| 314 | * \param block C2LinearBlock object to wrap around. |
| 315 | * \param memory IMemory object to store encrypted content. |
| 316 | * \param heapSeqNum Heap sequence number from ICrypto; -1 if N/A |
| 317 | */ |
| 318 | EncryptedLinearBlockBuffer( |
| 319 | const sp<AMessage> &format, |
| 320 | const std::shared_ptr<C2LinearBlock> &block, |
| 321 | const sp<IMemory> &memory, |
| 322 | int32_t heapSeqNum = -1); |
| 323 | EncryptedLinearBlockBuffer() = delete; |
| 324 | |
| 325 | virtual ~EncryptedLinearBlockBuffer() = default; |
| 326 | |
| 327 | std::shared_ptr<C2Buffer> asC2Buffer() override; |
| 328 | |
| 329 | /** |
| 330 | * Fill the source buffer structure with appropriate value based on |
| 331 | * internal IMemory object. |
| 332 | * |
| 333 | * \param source source buffer structure to fill. |
| 334 | */ |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 335 | void fillSourceBuffer( |
| 336 | hardware::drm::V1_0::SharedBuffer *source); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 337 | void fillSourceBuffer( |
| 338 | hardware::cas::native::V1_0::SharedBuffer *source); |
| 339 | |
| 340 | /** |
| 341 | * Copy the content of |decrypted| into C2LinearBlock inside. This shall |
| 342 | * only be called in non-secure usecases. |
| 343 | * |
| 344 | * \param decrypted decrypted content to copy from. |
| 345 | * \param length length of the content |
| 346 | * \return true if successful |
| 347 | * false otherwise. |
| 348 | */ |
| 349 | bool copyDecryptedContent(const sp<IMemory> &decrypted, size_t length); |
| 350 | |
| 351 | /** |
| 352 | * Copy the content of internal IMemory object into C2LinearBlock inside. |
| 353 | * This shall only be called in non-secure usecases. |
| 354 | * |
| 355 | * \param length length of the content |
| 356 | * \return true if successful |
| 357 | * false otherwise. |
| 358 | */ |
| 359 | bool copyDecryptedContentFromMemory(size_t length); |
| 360 | |
| 361 | /** |
| 362 | * Return native handle of secure buffer understood by ICrypto. |
| 363 | * |
| 364 | * \return secure buffer handle |
| 365 | */ |
| 366 | native_handle_t *handle() const; |
| 367 | |
| 368 | private: |
| 369 | |
| 370 | std::shared_ptr<C2LinearBlock> mBlock; |
| 371 | sp<IMemory> mMemory; |
| 372 | sp<hardware::HidlMemory> mHidlMemory; |
| 373 | int32_t mHeapSeqNum; |
| 374 | }; |
| 375 | |
| 376 | } // namespace android |
| 377 | |
| 378 | #endif // CODEC2_BUFFER_H_ |