• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MEDIAKEYSYSTEMFACTORYPROXY_H
17#define OHOS_HDI_DRM_V1_0_MEDIAKEYSYSTEMFACTORYPROXY_H
18
19#include "v1_0/imedia_key_system_factory.h"
20#include <unistd.h>
21#include <iproxy_broker.h>
22
23namespace OHOS {
24namespace HDI {
25namespace Drm {
26namespace V1_0 {
27
28class MediaKeySystemFactoryProxy : public IProxyBroker<OHOS::HDI::Drm::V1_0::IMediaKeySystemFactory> {
29public:
30    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
31    public:
32        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Drm::V1_0::MediaKeySystemFactoryProxy> proxy) : proxy_(proxy) {}
33        ~IServiceManagerDeathRecipient() override = default;
34        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
35        {
36            int32_t result = HDF_FAILURE;
37            const int sleepInterval = 500000;
38            const int waitTimes = 10;
39            int currentTime = waitTimes;
40            do {
41                usleep(sleepInterval);
42                auto proxy = proxy_.promote();
43                if (proxy != nullptr) {
44                    result = OHOS::HDI::Drm::V1_0::MediaKeySystemFactoryProxy::Reconnect(proxy);
45                }
46                --currentTime;
47            } while (result != HDF_SUCCESS && currentTime >0);
48        }
49    private:
50        wptr<OHOS::HDI::Drm::V1_0::MediaKeySystemFactoryProxy> proxy_;
51    };
52
53    explicit MediaKeySystemFactoryProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Drm::V1_0::IMediaKeySystemFactory>(remote) {
54        reconnectRemote_ = nullptr;
55        servMgr_ = nullptr;
56        deathRecipient_ = nullptr;
57        isReconnected_ = false;
58    }
59    virtual ~MediaKeySystemFactoryProxy() {
60        if (servMgr_ != nullptr && deathRecipient_ != nullptr) {
61            servMgr_->RemoveDeathRecipient(deathRecipient_);
62        }
63    }
64
65    inline bool IsProxy() override
66    {
67        return true;
68    }
69
70    int32_t IsMediaKeySystemSupported(const std::string& name, const std::string& mimeType,
71         OHOS::HDI::Drm::V1_0::ContentProtectionLevel level, bool& isSupported) override;
72
73    int32_t CreateMediaKeySystem(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem>& mediaKeySystem) override;
74
75    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
76
77    static int32_t IsMediaKeySystemSupported_(const std::string& name, const std::string& mimeType,
78         OHOS::HDI::Drm::V1_0::ContentProtectionLevel level, bool& isSupported, const sptr<IRemoteObject> remote);
79
80    static int32_t CreateMediaKeySystem_(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem>& mediaKeySystem,
81         const sptr<IRemoteObject> remote);
82
83    static int32_t GetVersion_(uint32_t& majorVer, uint32_t& minorVer, const sptr<IRemoteObject> remote);
84
85    static int32_t Reconnect(sptr<OHOS::HDI::Drm::V1_0::MediaKeySystemFactoryProxy> proxy);
86
87    sptr<IRemoteObject> GetCurrentRemote() {
88        return isReconnected_ ? reconnectRemote_ : Remote();
89    }
90
91    bool isReconnected_;
92    std::string serviceName_;
93    sptr<IRemoteObject> servMgr_;
94    sptr<OHOS::HDI::Drm::V1_0::MediaKeySystemFactoryProxy::IServiceManagerDeathRecipient> deathRecipient_;
95    sptr<IRemoteObject> reconnectRemote_;
96private:
97    static inline BrokerDelegator<OHOS::HDI::Drm::V1_0::MediaKeySystemFactoryProxy> delegator_;
98};
99
100} // V1_0
101} // Drm
102} // HDI
103} // OHOS
104
105#endif // OHOS_HDI_DRM_V1_0_MEDIAKEYSYSTEMFACTORYPROXY_H
106
107