• 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 "source_engine_impl.h"
17 #include <set>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include "securec.h"
21 #include "source.h"
22 #include "avcodec_log.h"
23 #include "avcodec_dfx.h"
24 #include "avcodec_errors.h"
25 
26 namespace {
27     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "SourceEngineImpl"};
28 }
29 
30 namespace OHOS {
31 namespace MediaAVCodec {
CreateSourceEngine(const std::string & uri)32 std::shared_ptr<ISourceEngine> ISourceEngineFactory::CreateSourceEngine(const std::string& uri)
33 {
34     AVCodecTrace trace("ISourceEngineFactory::CreateSourceEngine");
35     std::shared_ptr<ISourceEngine> sourceEngineImpl = std::make_shared<SourceEngineImpl>(uri);
36     CHECK_AND_RETURN_RET_LOG(sourceEngineImpl != nullptr, nullptr, "create SourceEngine implementation failed");
37     return sourceEngineImpl;
38 }
39 
Init()40 int32_t SourceEngineImpl::Init()
41 {
42     AVCodecTrace trace("SourceEngineImpl::Init");
43     AVCODEC_LOGI("Init");
44     std::unique_lock<std::mutex> lock(mutex_);
45     CHECK_AND_RETURN_RET_LOG(source_ != nullptr, AVCS_ERR_CREATE_SOURCE_SUB_SERVICE_FAILED, "source_ is nullptr");
46     return source_->Init(uri_);
47 }
48 
SourceEngineImpl(const std::string & uri)49 SourceEngineImpl::SourceEngineImpl(const std::string& uri)
50     : uri_(uri)
51 {
52     AVCODEC_LOGI("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
53     source_ = std::make_shared<Plugin::Source>();
54     if (source_ == nullptr) {
55         AVCODEC_LOGE("create source engine failed");
56     }
57 }
58 
~SourceEngineImpl()59 SourceEngineImpl::~SourceEngineImpl()
60 {
61     AVCODEC_LOGD("Destroy SourceEngineImpl");
62     source_ = nullptr;
63     AVCODEC_LOGI("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
64 }
65 
GetTrackCount(uint32_t & trackCount)66 int32_t SourceEngineImpl::GetTrackCount(uint32_t &trackCount)
67 {
68     AVCodecTrace trace("SourceEngineImpl::GetTrackCount");
69     AVCODEC_LOGI("GetTrackCount");
70     std::unique_lock<std::mutex> lock(mutex_);
71     CHECK_AND_RETURN_RET_LOG(source_ != nullptr, AVCS_ERR_INVALID_OPERATION, "source_ is nullptr");
72     return source_->GetTrackCount(trackCount);
73 }
74 
GetSourceFormat(Format & format)75 int32_t SourceEngineImpl::GetSourceFormat(Format &format)
76 {
77     AVCodecTrace trace("SourceEngineImpl::GetSourceFormat");
78     AVCODEC_LOGI("GetSourceFormat");
79     std::unique_lock<std::mutex> lock(mutex_);
80     CHECK_AND_RETURN_RET_LOG(source_ != nullptr, AVCS_ERR_INVALID_OPERATION, "source_ is nullptr");
81     return source_->GetSourceFormat(format);
82 }
83 
GetTrackFormat(Format & format,uint32_t trackIndex)84 int32_t SourceEngineImpl::GetTrackFormat(Format &format, uint32_t trackIndex)
85 {
86     AVCodecTrace trace("SourceEngineImpl::GetTrackFormat");
87     AVCODEC_LOGI("GetTrackFormat");
88     std::unique_lock<std::mutex> lock(mutex_);
89     CHECK_AND_RETURN_RET_LOG(source_ != nullptr, AVCS_ERR_INVALID_OPERATION, "source_ is nullptr");
90     return source_->GetTrackFormat(format, trackIndex);
91 }
92 
GetSourceAddr()93 uintptr_t SourceEngineImpl::GetSourceAddr()
94 {
95     AVCodecTrace trace("SourceEngineImpl::GetSourceAddr");
96     AVCODEC_LOGI("GetSourceAddr");
97     std::unique_lock<std::mutex> lock(mutex_);
98     CHECK_AND_RETURN_RET_LOG(source_ != nullptr, AVCS_ERR_INVALID_OPERATION, "source_ is nullptr");
99     return source_->GetSourceAddr();
100 }
101 } // MediaAVCodec
102 } // OHOS
103