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_MEDIA_KEY_SYSTEM_SERVICE_H
17 #define OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_H
18 
19 #include <iostream>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 #include <set>
25 #include <unordered_set>
26 #include <refbase.h>
27 #include "mediakeysystem_service_stub.h"
28 #include "key_session_service.h"
29 #include "drm_dfx_utils.h"
30 #include "drm_host_manager.h"
31 #include "drm_log.h"
32 #include "iremote_stub.h"
33 #include "system_ability.h"
34 
35 #include "v1_0/media_key_session_proxy.h"
36 #include "v1_0/media_key_system_proxy.h"
37 
38 namespace OHOS {
39 namespace DrmStandard {
40 using namespace OHOS::HDI::Drm::V1_0;
41 using namespace OHOS::HDI;
42 class IMediaKeySystemServiceOperatorsCallback;
43 class MediaKeySystemService : public MediaKeySystemServiceStub,
44     public DrmHostManager::StatusCallback,
45     public IMediaKeySessionServiceOperatorsCallback,
46     public IMediaKeySystemCallback {
47 public:
48     explicit MediaKeySystemService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> hdiKeySystem);
49     explicit MediaKeySystemService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> hdiKeySystem,
50         StatisticsInfo statisticsInfo);
51     ~MediaKeySystemService() override;
52     int32_t Release() override;
53     int32_t SetMediaKeySystemServiceOperatorsCallback(wptr<IMediaKeySystemServiceOperatorsCallback> callback);
54     int32_t CloseMediaKeySystemServiceByCallback();
55     int32_t SetConfigurationString(std::string &configName, std::string &value) override;
56     int32_t GetConfigurationString(std::string &configName, std::string &value) override;
57     int32_t SetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value) override;
58     int32_t GetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value) override;
59     int32_t GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel) override;
60     int32_t CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,
61         sptr<IMediaKeySessionService> &keySessionProxy) override;
62     int32_t GenerateKeySystemRequest(std::vector<uint8_t> &request, std::string &defaultUrl) override;
63     int32_t ProcessKeySystemResponse(const std::vector<uint8_t> &response) override;
64     int32_t GetCertificateStatus(IMediaKeySystemService::CertificateStatus *certStatus) override;
65 
66     int32_t GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &licenseIds) override;
67     int32_t GetOfflineMediaKeyStatus(std::vector<uint8_t> &licenseId,
68         IMediaKeySessionService::OfflineMediaKeyStatus &status) override;
69     int32_t ClearOfflineMediaKeys(std::vector<uint8_t> &licenseId) override;
70 
71     int32_t CloseMediaKeySessionService(sptr<MediaKeySessionService> sessionService) override;
72     int32_t GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics) override;
73     int32_t SetCallback(sptr<IMediaKeySystemServiceCallback> &callback) override;
74     std::string GetPluginName();
75     sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> getMediaKeySystem();
76     int32_t SetBundleName();
77 
78     // for hdi callback
79     int32_t SendEvent(OHOS::HDI::Drm::V1_0::EventType eventType, int32_t extra,
80         const std::vector<uint8_t> &data) override;
81     std::string GetSessionsDumpInfo();
82 
83 private:
84     void OnDrmPluginDied(std::string &name) override;
85     std::recursive_mutex mutex_;
86     std::recursive_mutex callbackMutex_;
87     sptr<IMediaKeySystemServiceCallback> callback_;
88     wptr<IMediaKeySystemServiceOperatorsCallback> keySystemOperatoersCallback_;
89     std::mutex sessionsSetMutex_;
90     std::set<sptr<MediaKeySessionService>> sessionsSet_;
91     sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> hdiKeySystem_;
92     StatisticsInfo statisticsInfo_;
93     std::string generationResult_;
94     uint32_t generationDuration_ = 0;
95     uint32_t currentKeySessionNumber = 0;
96 };
97 
98 class IMediaKeySystemServiceOperatorsCallback : public virtual RefBase {
99 public:
100     IMediaKeySystemServiceOperatorsCallback() = default;
101     virtual ~IMediaKeySystemServiceOperatorsCallback() = default;
102     virtual int32_t CloseMediaKeySystemService(sptr<MediaKeySystemService> mediaKeySystemService) = 0;
103 };
104 } // DrmStandard
105 } // OHOS
106 
107 #endif // OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_H
108