1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CODEC_DRM_DECRYPT_H 17 #define CODEC_DRM_DECRYPT_H 18 19 #include <mutex> 20 #include "buffer/avbuffer.h" 21 #include "meta/meta.h" 22 #include "foundation/multimedia/drm_framework/services/drm_service/ipc/i_keysession_service.h" 23 #include "foundation/multimedia/drm_framework/services/drm_service/ipc/i_mediadecryptmodule_service.h" 24 25 namespace OHOS { 26 namespace MediaAVCodec { 27 28 using namespace Media; 29 using MetaDrmSubSample = Plugins::MetaDrmSubSample; 30 using MetaDrmCencInfo = Plugins::MetaDrmCencInfo; 31 using MetaDrmCencAlgorithm = Plugins::MetaDrmCencAlgorithm; 32 using DrmBuffer = DrmStandard::IMediaDecryptModuleService::DrmBuffer; 33 34 enum SvpMode : int32_t { 35 SVP_CLEAR = -1, /* it's not a protection video */ 36 SVP_FALSE, /* it's a protection video but not need secure decoder */ 37 SVP_TRUE, /* it's a protection video and need secure decoder */ 38 }; 39 40 class CodecDrmDecrypt { 41 public: 42 void DrmCencDecrypt(std::shared_ptr<AVBuffer> inBuf, std::shared_ptr<AVBuffer> outBuf, 43 uint32_t &dataSize); 44 void SetCodecName(const std::string &codecName); 45 void SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession, 46 const bool svpFlag); 47 48 private: 49 void GetCodingType(); 50 void DrmGetSkipClearBytes(uint32_t &skipBytes) const; 51 int32_t DrmGetNalTypeAndIndex(const uint8_t *data, uint32_t dataSize, uint8_t &nalType, uint32_t &posIndex) const; 52 static void DrmGetSyncHeaderIndex(const uint8_t *data, uint32_t dataSize, uint32_t &posIndex); 53 uint8_t DrmGetFinalNalTypeAndIndex(const uint8_t *data, uint32_t dataSize, uint32_t &posStartIndex, 54 uint32_t &posEndIndex) const; 55 static void DrmRemoveAmbiguityBytes(uint8_t *data, uint32_t &posEndIndex, uint32_t offset, uint32_t &dataSize); 56 void DrmModifyCencInfo(uint8_t *data, uint32_t &dataSize, MetaDrmCencInfo *cencInfo) const; 57 int32_t DecryptMediaData(const MetaDrmCencInfo * const cencInfo, std::shared_ptr<AVBuffer> inBuf, 58 std::shared_ptr<AVBuffer> outBuf); 59 int32_t SetDrmBuffer(const std::shared_ptr<AVBuffer> &inBuf, const std::shared_ptr<AVBuffer> &outBuf, 60 DrmBuffer &inDrmBuffer, DrmBuffer &outDrmBuffer); 61 62 private: 63 std::mutex configMutex_; 64 std::string codecName_; 65 int32_t codingType_ = 0; 66 sptr<DrmStandard::IMediaKeySessionService> keySessionServiceProxy_; 67 sptr<DrmStandard::IMediaDecryptModuleService> decryptModuleProxy_; 68 int32_t svpFlag_ = SVP_CLEAR; 69 }; 70 71 } // namespace MediaAVCodec 72 } // namespace OHOS 73 #endif // CODEC_DRM_DECRYPT_H 74