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 OHOS_DRM_KEY_SESSION_SERVICE_H
17 #define OHOS_DRM_KEY_SESSION_SERVICE_H
18 
19 #include <iostream>
20 #include <refbase.h>
21 #include "drm_dfx_utils.h"
22 #include "drm_dfx.h"
23 #include "drm_log.h"
24 #include "drm_host_manager.h"
25 #include "key_session_service_stub.h"
26 #include "media_decrypt_module_service.h"
27 #include "v1_0/media_key_session_proxy.h"
28 #include "v1_0/media_decrypt_module_proxy.h"
29 
30 namespace OHOS {
31 namespace DrmStandard {
32 using namespace OHOS::HDI::Drm::V1_0;
33 using namespace OHOS::HDI;
34 
35 class IMediaKeySessionServiceOperatorsCallback;
36 
37 class MediaKeySessionService : public MediaKeySessionServiceStub, public IMediaKeySessionCallback {
38 public:
39     explicit MediaKeySessionService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySession> hdiMediaKeySession);
40     explicit MediaKeySessionService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySession> hdiMediaKeySession,
41         StatisticsInfo statisticsInfo);
42     ~MediaKeySessionService();
43     int32_t Release() override;
44     int32_t GetMediaDecryptModule(sptr<IMediaDecryptModuleService> &decryptModule) override;
45     int32_t SetMediaKeySessionServiceOperatorsCallback(wptr<IMediaKeySessionServiceOperatorsCallback> callback);
46     int32_t CloseMediaKeySessionServiceByCallback();
47     int32_t GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo &licenseRequestInfo,
48         IMediaKeySessionService::MediaKeyRequest &licenseRequest) override;
49     int32_t ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &licenseResponse) override;
50     int32_t GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId,
51         std::vector<uint8_t> &releaseRequest) override;
52     int32_t ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId,
53         std::vector<uint8_t> &releaseResponse) override;
54     int32_t CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus) override;
55     int32_t RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId) override;
56     int32_t ClearMediaKeys() override;
57     int32_t GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel) override;
58     int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status) override;
59     int32_t SetCallback(sptr<IMediaKeySessionServiceCallback> &callback) override;
60 
61     // for hdi callback
62     int32_t SendEvent(OHOS::HDI::Drm::V1_0::EventType eventType, int32_t extra,
63         const std::vector<uint8_t> &data) override;
64     int32_t SendEventKeyChange(
65         const std::map<std::vector<uint8_t>, OHOS::HDI::Drm::V1_0::MediaKeySessionKeyStatus> &keyStatus,
66         bool hasNewGoodLicense) override;
67     std::string GetDecryptModuleDumpInfo();
68 
69 private:
70     std::recursive_mutex sessionMutex_;
71     std::recursive_mutex callbackMutex_;
72     sptr<IMediaKeySessionServiceCallback> callback_;
73     wptr<IMediaKeySessionServiceOperatorsCallback> sessionOperatorsCallback_;
74     sptr<OHOS::HDI::Drm::V1_0::IMediaKeySession> hdiMediaKeySession_;
75     sptr<MediaDecryptModuleService> decryptModule_;
76     StatisticsInfo statisticsInfo_;
77     std::string generationResult_;
78     std::string mediaKeyType_;
79     uint32_t generationDuration_ = 0;
80 };
81 
82 class IMediaKeySessionServiceOperatorsCallback : public virtual RefBase {
83 public:
84     IMediaKeySessionServiceOperatorsCallback() = default;
85     virtual ~IMediaKeySessionServiceOperatorsCallback() = default;
86     virtual int32_t CloseMediaKeySessionService(sptr<MediaKeySessionService> sessionService) = 0;
87 };
88 } // DrmStandard
89 } // OHOS
90 
91 #endif // OHOS_DRM_KEY_SESSION_SERVICE_H
92