• 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     AddSystemAbilityListener(SUSPEND_MANAGER_SYSTEM_ABILITY_ID);
66     BackGroundEventHandler::GetInstance().RegisterSuspendObserver();
67 }
68 
OnIdle(const SystemAbilityOnDemandReason & idleReason)69 int32_t AVCodecServer::OnIdle([[maybe_unused]] const SystemAbilityOnDemandReason &idleReason)
70 {
71     std::lock_guard<std::mutex> stateLock(stateMutex_);
72     AVCODEC_LOGI("In");
73     auto instanceCount = AVCodecServerManager::GetInstance().GetInstanceCount();
74     CHECK_AND_RETURN_RET_LOG(instanceCount == 0,
75         -1, // -1: refuse to switch to idle state
76         "The transition to the idle state is rejected, because %{public}u instances is not released", instanceCount);
77     return 0;
78 }
79 
OnStop()80 void AVCodecServer::OnStop()
81 {
82     std::lock_guard<std::mutex> stateLock(stateMutex_);
83     AVCODEC_LOGI("In");
84     BackGroundEventHandler::GetInstance().UnregisterSuspendObserver();
85     AVCodecServerManager::GetInstance().NotifyProcessStatus(0);
86 }
87 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)88 void AVCodecServer::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
89 {
90     AVCODEC_LOGI("systemAbilityId:%{public}d, deviceId:%s", systemAbilityId, deviceId.c_str());
91     if (systemAbilityId == MEMORY_MANAGER_SA_ID) {
92         AVCodecServerManager::GetInstance().SetMemMgrStatus(true);
93         AVCodecServerManager::GetInstance().NotifyProcessStatus(1);
94     }
95     if (systemAbilityId == SUSPEND_MANAGER_SYSTEM_ABILITY_ID) {
96         BackGroundEventHandler::GetInstance().RegisterSuspendObserver();
97     }
98 }
99 
SwitchSystemId(IStandardAVCodecService::AVCodecSystemAbility subSystemId)100 std::optional<AVCodecServerManager::StubType> AVCodecServer::SwitchSystemId(
101     IStandardAVCodecService::AVCodecSystemAbility subSystemId)
102 {
103     switch (subSystemId) {
104 #ifdef SUPPORT_CODECLIST
105         case AVCodecSystemAbility::AVCODEC_CODECLIST: {
106             return AVCodecServerManager::CODECLIST;
107         }
108 #endif
109 #ifdef SUPPORT_CODEC
110         case AVCodecSystemAbility::AVCODEC_CODEC: {
111             return AVCodecServerManager::CODEC;
112         }
113 #endif
114         default: {
115             AVCODEC_LOGE("subSystemId is invalid");
116             return std::nullopt;
117         }
118     }
119 }
120 
GetSubSystemAbility(IStandardAVCodecService::AVCodecSystemAbility subSystemId,const sptr<IRemoteObject> & listener,sptr<IRemoteObject> & stubObject)121 int32_t AVCodecServer::GetSubSystemAbility(IStandardAVCodecService::AVCodecSystemAbility subSystemId,
122                                            const sptr<IRemoteObject> &listener, sptr<IRemoteObject> &stubObject)
123 {
124     std::lock_guard<std::mutex> stateLock(stateMutex_);
125     bool saInActiveState = (GetAbilityState() != SystemAbilityState::IDLE) || CancelIdle();
126     CHECK_AND_RETURN_RET_LOG(saInActiveState,
127         AVCS_ERR_INVALID_STATE, "AVCodec service in idle state, but cancel idle failed");
128 
129     std::optional<AVCodecServerManager::StubType> stubType = SwitchSystemId(subSystemId);
130     CHECK_AND_RETURN_RET_LOG(stubType != std::nullopt, AVCS_ERR_INVALID_OPERATION, "Get sub system type failed");
131 
132     int32_t ret = AVCodecServerManager::GetInstance().CreateStubObject(stubType.value(), stubObject);
133     CHECK_AND_RETURN_RET_LOG(stubObject != nullptr, ret, "Create sub system failed, err: %{public}d", ret);
134 
135     ret = AVCodecServiceStub::SetDeathListener(listener);
136     if (ret != AVCS_ERR_OK) {
137         AVCodecServerManager::GetInstance().DestroyStubObject(*stubType, stubObject);
138         stubObject = nullptr;
139         AVCODEC_LOGE("SetDeathListener failed");
140         return AVCS_ERR_IPC_SET_DEATH_LISTENER_FAILED;
141     }
142 
143     return AVCS_ERR_OK;
144 }
145 
Dump(int32_t fd,const std::vector<std::u16string> & args)146 int32_t AVCodecServer::Dump(int32_t fd, const std::vector<std::u16string> &args)
147 {
148     if (fd <= 0) {
149         AVCODEC_LOGW("Failed to check fd");
150         return OHOS::INVALID_OPERATION;
151     }
152     if (AVCodecServerManager::GetInstance().Dump(fd, args) != OHOS::NO_ERROR) {
153         AVCODEC_LOGW("Failed to call AVCodecServerManager::Dump");
154         return OHOS::INVALID_OPERATION;
155     }
156 
157     return OHOS::NO_ERROR;
158 }
159 } // namespace MediaAVCodec
160 } // namespace OHOS
161