• 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 #include "avcodec_server.h"
17 #include <sys/time.h>
18 #include "avcodec_errors.h"
19 #include "avcodec_log.h"
20 #include "avcodec_sysevent.h"
21 #include "avcodec_trace.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "background_event_handler.h"
25 
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "AVCodecServer"};
28 constexpr uint32_t SERVER_MAX_IPC_THREAD_NUM = 64;
29 } // namespace
30 
31 namespace OHOS {
32 namespace MediaAVCodec {
REGISTER_SYSTEM_ABILITY_BY_ID(AVCodecServer,AV_CODEC_SERVICE_ID,true)33 REGISTER_SYSTEM_ABILITY_BY_ID(AVCodecServer, AV_CODEC_SERVICE_ID, true)
34 AVCodecServer::AVCodecServer(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate)
35 {
36     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
37 }
38 
~AVCodecServer()39 AVCodecServer::~AVCodecServer()
40 {
41     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
42 }
43 
OnDump()44 void AVCodecServer::OnDump()
45 {
46     AVCODEC_LOGD("AVCodecServer OnDump");
47 }
48 
OnStart()49 void AVCodecServer::OnStart()
50 {
51     std::lock_guard<std::mutex> stateLock(stateMutex_);
52     AVCODEC_LOGI("In");
53     struct timeval start = {};
54     struct timeval end = {};
55     (void)gettimeofday(&start, nullptr);
56     bool res = Publish(this);
57     if (res) {
58         AVCODEC_LOGD("AVCodecServer OnStart res=%{public}d", res);
59     }
60     (void)gettimeofday(&end, nullptr);
61     uint32_t useTime = static_cast<uint32_t>((end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec);
62     IPCSkeleton::SetMaxWorkThreadNum(SERVER_MAX_IPC_THREAD_NUM);
63     AddSystemAbilityListener(MEMORY_MANAGER_SA_ID);
64     ServiceStartEventWrite(useTime, "AV_CODEC service");
65 }
66 
OnIdle(const SystemAbilityOnDemandReason & idleReason)67 int32_t AVCodecServer::OnIdle([[maybe_unused]] const SystemAbilityOnDemandReason &idleReason)
68 {
69     std::lock_guard<std::mutex> stateLock(stateMutex_);
70     AVCODEC_LOGI("In");
71     auto instanceCount = AVCodecServerManager::GetInstance().GetInstanceCount();
72     CHECK_AND_RETURN_RET_LOG(instanceCount == 0,
73         -1, // -1: refuse to switch to idle state
74         "The transition to the idle state is rejected, because %{public}u instances is not released", instanceCount);
75     return 0;
76 }
77 
OnStop()78 void AVCodecServer::OnStop()
79 {
80     std::lock_guard<std::mutex> stateLock(stateMutex_);
81     AVCODEC_LOGI("In");
82     AVCodecServerManager::GetInstance().NotifyProcessStatus(0);
83 }
84 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)85 void AVCodecServer::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
86 {
87     AVCODEC_LOGI("systemAbilityId:%{public}d, deviceId:%s", systemAbilityId, deviceId.c_str());
88     if (systemAbilityId == MEMORY_MANAGER_SA_ID) {
89         AVCodecServerManager::GetInstance().SetMemMgrStatus(true);
90         AVCodecServerManager::GetInstance().NotifyProcessStatus(1);
91     }
92 }
93 
SwitchSystemId(IStandardAVCodecService::AVCodecSystemAbility subSystemId)94 std::optional<AVCodecServerManager::StubType> AVCodecServer::SwitchSystemId(
95     IStandardAVCodecService::AVCodecSystemAbility subSystemId)
96 {
97     switch (subSystemId) {
98 #ifdef SUPPORT_CODECLIST
99         case AVCodecSystemAbility::AVCODEC_CODECLIST: {
100             return AVCodecServerManager::CODECLIST;
101         }
102 #endif
103 #ifdef SUPPORT_CODEC
104         case AVCodecSystemAbility::AVCODEC_CODEC: {
105             return AVCodecServerManager::CODEC;
106         }
107 #endif
108         default: {
109             AVCODEC_LOGE("subSystemId is invalid");
110             return std::nullopt;
111         }
112     }
113 }
114 
GetSubSystemAbility(IStandardAVCodecService::AVCodecSystemAbility subSystemId,const sptr<IRemoteObject> & listener,sptr<IRemoteObject> & stubObject)115 int32_t AVCodecServer::GetSubSystemAbility(IStandardAVCodecService::AVCodecSystemAbility subSystemId,
116                                            const sptr<IRemoteObject> &listener, sptr<IRemoteObject> &stubObject)
117 {
118     std::lock_guard<std::mutex> stateLock(stateMutex_);
119     bool saInActiveState = (GetAbilityState() != SystemAbilityState::IDLE) || CancelIdle();
120     CHECK_AND_RETURN_RET_LOG(saInActiveState,
121         AVCS_ERR_INVALID_STATE, "AVCodec service in idle state, but cancel idle failed");
122 
123     std::optional<AVCodecServerManager::StubType> stubType = SwitchSystemId(subSystemId);
124     CHECK_AND_RETURN_RET_LOG(stubType != std::nullopt, AVCS_ERR_INVALID_OPERATION, "Get sub system type failed");
125 
126     int32_t ret = AVCodecServerManager::GetInstance().CreateStubObject(stubType.value(), stubObject);
127     CHECK_AND_RETURN_RET_LOG(stubObject != nullptr, ret, "Create sub system failed, err: %{public}d", ret);
128 
129     ret = AVCodecServiceStub::SetDeathListener(listener);
130     if (ret != AVCS_ERR_OK) {
131         AVCodecServerManager::GetInstance().DestroyStubObject(*stubType, stubObject);
132         stubObject = nullptr;
133         AVCODEC_LOGE("SetDeathListener failed");
134         return AVCS_ERR_IPC_SET_DEATH_LISTENER_FAILED;
135     }
136 
137     return AVCS_ERR_OK;
138 }
139 
SuspendFreeze(const std::vector<pid_t> & pidList)140 int32_t AVCodecServer::SuspendFreeze(const std::vector<pid_t> &pidList)
141 {
142     BackGroundEventHandler::GetInstance().NotifyFreeze(pidList);
143     return AVCS_ERR_OK;
144 }
145 
SuspendActive(const std::vector<pid_t> & pidList)146 int32_t AVCodecServer::SuspendActive(const std::vector<pid_t> &pidList)
147 {
148     BackGroundEventHandler::GetInstance().NotifyActive(pidList);
149     return AVCS_ERR_OK;
150 }
151 
SuspendActiveAll()152 int32_t AVCodecServer::SuspendActiveAll()
153 {
154     BackGroundEventHandler::GetInstance().NotifyActiveAll();
155     return AVCS_ERR_OK;
156 }
157 
Dump(int32_t fd,const std::vector<std::u16string> & args)158 int32_t AVCodecServer::Dump(int32_t fd, const std::vector<std::u16string> &args)
159 {
160     if (fd <= 0) {
161         AVCODEC_LOGW("Failed to check fd");
162         return OHOS::INVALID_OPERATION;
163     }
164     if (AVCodecServerManager::GetInstance().Dump(fd, args) != OHOS::NO_ERROR) {
165         AVCODEC_LOGW("Failed to call AVCodecServerManager::Dump");
166         return OHOS::INVALID_OPERATION;
167     }
168 
169     return OHOS::NO_ERROR;
170 }
171 } // namespace MediaAVCodec
172 } // namespace OHOS
173