1 /*
2 * Copyright (c) 2024 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 "cj_avsession_manager_impl.h"
17
18 #include "av_session.h"
19 #include "avsession_manager.h"
20 #include "avsession_errors.h"
21 #include "avsession_log.h"
22 #include "cj_avsession_impl.h"
23 #include "cj_avsession_utils.h"
24 #include "cj_avsession_controller_impl.h"
25 #include "cj_avsession_cast_controller_impl.h"
26
27 namespace OHOS::AVSession {
28
CJAVSessionManagerImpl()29 CJAVSessionManagerImpl::CJAVSessionManagerImpl() {}
30
~CJAVSessionManagerImpl()31 CJAVSessionManagerImpl::~CJAVSessionManagerImpl() {}
32
CreateAVSession(OHOS::AbilityRuntime::AbilityContext & context,const std::string & tag,int32_t & type,int64_t & session,char * & sessionId)33 int32_t CJAVSessionManagerImpl::CreateAVSession(OHOS::AbilityRuntime::AbilityContext& context,
34 const std::string& tag, int32_t& type, int64_t& session, char*& sessionId)
35 {
36 auto abilityInfo = context.GetAbilityInfo();
37 AppExecFwk::ElementName elementName;
38 elementName.SetBundleName(abilityInfo->bundleName);
39 elementName.SetAbilityName(abilityInfo->name);
40 std::shared_ptr<OHOS::AVSession::AVSession> out;
41 int32_t ret = AVSessionManager::GetInstance().CreateSession(tag, type, elementName, out);
42 if (ret == AVSESSION_SUCCESS) {
43 auto sid = out->GetSessionId();
44 ConvertNativeToCJStruct(sid, sessionId);
45 auto cjSession = FFI::FFIData::Create<CJAVSessionImpl>(out);
46 if (cjSession) {
47 session = cjSession->GetID();
48 } else {
49 ret = AVSESSION_ERROR;
50 out->Destroy();
51 }
52 }
53 return ret;
54 }
55
CreateController(const std::string & sessionId)56 int32_t CJAVSessionManagerImpl::CreateController(const std::string &sessionId)
57 {
58 std::shared_ptr<AVSessionController> nativeController;
59 int32_t errCode = AVSessionManager::GetInstance().CreateController(sessionId, nativeController);
60 if (errCode != AVSESSION_SUCCESS) {
61 return errCode;
62 }
63 auto controller = CJAVSessionControllerImpl::NewInstance(nativeController);
64 if (controller) {
65 return errCode;
66 } else {
67 return ERR_NO_MEMORY;
68 }
69 }
70
71 } // namespace AVSession::OHOS