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_HDI_DRM_V1_0_MEDIAKEYSESSIONSERVICE_H
17 #define OHOS_HDI_DRM_V1_0_MEDIAKEYSESSIONSERVICE_H
18 
19 #include "v1_0/imedia_key_session.h"
20 #include "v1_0/media_decrypt_module_service.h"
21 #include "v1_0/media_key_session_callback_service.h"
22 #include "v1_0/media_key_system_types.h"
23 #include "media_key_session_callback_service.h"
24 #include "session.h"
25 #include <mutex>
26 #include <map>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 
30 namespace OHOS {
31 namespace HDI {
32 namespace Drm {
33 namespace V1_0 {
34 class KeySessionServiceCallback;
35 class MediaKeySessionService : public OHOS::HDI::Drm::V1_0::IMediaKeySession {
36 public:
37     MediaKeySessionService();
38     MediaKeySessionService(ContentProtectionLevel level);
39     virtual ~MediaKeySessionService() = default;
40 
41     int32_t GenerateMediaKeyRequest(const MediaKeyRequestInfo& mediaKeyRequestInfo,
42          MediaKeyRequest& mediaKeyRequest) override;
43 
44     int32_t ProcessMediaKeyResponse(const std::vector<uint8_t>& mediaKeyResponse,
45          std::vector<uint8_t>& mediaKeyId) override;
46 
47     int32_t CheckMediaKeyStatus(std::map<std::string, std::string>& mediaKeyStatus) override;
48 
49     int32_t ClearMediaKeys() override;
50 
51     int32_t GetOfflineReleaseRequest(const std::vector<uint8_t>& mediaKeyId,
52          std::vector<uint8_t>& releaseRequest) override;
53 
54     int32_t ProcessOfflineReleaseResponse(const std::vector<uint8_t>& mediaKeyId,
55          const std::vector<uint8_t>& response) override;
56 
57     int32_t RestoreOfflineMediaKeys(const std::vector<uint8_t>& mediaKeyId) override;
58 
59     int32_t GetContentProtectionLevel(ContentProtectionLevel& level) override;
60 
61     int32_t RequiresSecureDecoderModule(const std::string& mimeType, bool& required) override;
62 
63     int32_t SetCallback(const sptr<OHOS::HDI::Drm::V1_0::IMediaKeySessionCallback>& sessionCallback) override;
64 
65     int32_t GetMediaDecryptModule(sptr<OHOS::HDI::Drm::V1_0::IMediaDecryptModule>& decryptModule) override;
66 
67     int32_t Destroy() override;
68 
69     int32_t Init();
70     int32_t GetDecryptNumber();
71     int32_t GetErrorDecryptNumber();
72     int32_t GetDecryptTimes(std::vector<double> &times);
73     void GetDecryptMaxTimes(double time, std::vector<double> &topThreeTimes);
74     int32_t SetKeySessionServiceCallback(sptr<KeySessionServiceCallback> callback);
75     sptr<Session> session_;
76 private:
77     int32_t GetOfflineKeyFromFile();
78     int32_t SetOfflineKeyToFile();
79     ContentProtectionLevel level_;
80     sptr<OHOS::HDI::Drm::V1_0::MediaDecryptModuleService> decryptModule_;
81     sptr<KeySessionServiceCallback> sessionCallback_;
82     std::mutex offlineKeyMutex_;
83     std::map<std::string, std::string> offlineKeyIdAndKeyValueBase64_;
84     const char* offlineKeyFileName = "/data/local/traces/offline_key.txt";
85     const int keyIdMaxLength = 255;
86     OHOS::sptr<MediaKeySessionCallbackService> vdiCallbackObj;
87     static const int32_t topThree = 3;
88 };
89 class KeySessionServiceCallback : public virtual RefBase {
90 public:
91     KeySessionServiceCallback() = default;
92     virtual ~KeySessionServiceCallback() = default;
93     virtual int32_t CloseKeySessionService(sptr<MediaKeySessionService> mediaKeySession) = 0;
94 };
95 } // V1_0
96 } // Drm
97 } // HDI
98 } // OHOS
99 
100 #endif // OHOS_HDI_DRM_V1_0_MEDIAKEYSESSIONSERVICE_H