1 /*
2 * Copyright (C) 2022 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 "avmuxer_impl.h"
17 #include "securec.h"
18 #include "i_media_service.h"
19 #include "media_log.h"
20 #include "media_errors.h"
21 #include "avsharedmemorybase.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVMuxerImpl"};
25 }
26
27 namespace OHOS {
28 namespace Media {
CreateAVMuxer()29 std::shared_ptr<AVMuxer> AVMuxerFactory::CreateAVMuxer()
30 {
31 std::shared_ptr<AVMuxerImpl> impl = std::make_shared<AVMuxerImpl>();
32 CHECK_AND_RETURN_RET_LOG(impl != nullptr, nullptr, "Failed to create avmuxer implementation");
33
34 int32_t ret = impl->Init();
35 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "Failed to init avmuxer implementation");
36 return impl;
37 }
38
Init()39 int32_t AVMuxerImpl::Init()
40 {
41 avmuxerService_ = MediaServiceFactory::GetInstance().CreateAVMuxerService();
42 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_NO_MEMORY, "Failed to create avmuxer service");
43 return MSERR_OK;
44 }
45
AVMuxerImpl()46 AVMuxerImpl::AVMuxerImpl()
47 {
48 MEDIA_LOGD("AVMuxerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
49 }
50
~AVMuxerImpl()51 AVMuxerImpl::~AVMuxerImpl()
52 {
53 if (avmuxerService_ != nullptr) {
54 (void)MediaServiceFactory::GetInstance().DestroyAVMuxerService(avmuxerService_);
55 avmuxerService_ = nullptr;
56 }
57 MEDIA_LOGD("AVMuxerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
58 }
59
GetAVMuxerFormatList()60 std::vector<std::string> AVMuxerImpl::GetAVMuxerFormatList()
61 {
62 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, std::vector<std::string>(), "AVMuxer Service does not exist");
63 return avmuxerService_->GetAVMuxerFormatList();
64 }
65
SetOutput(int32_t fd,const std::string & format)66 int32_t AVMuxerImpl::SetOutput(int32_t fd, const std::string &format)
67 {
68 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_INVALID_OPERATION, "AVMuxer Service does not exist");
69 CHECK_AND_RETURN_RET_LOG(fd > 0, MSERR_INVALID_VAL, "Fd is invalid");
70 CHECK_AND_RETURN_RET_LOG(!format.empty(), MSERR_INVALID_VAL, "Format is empty");
71
72 return avmuxerService_->SetOutput(fd, format);
73 }
74
SetLocation(float latitude,float longtitude)75 int32_t AVMuxerImpl::SetLocation(float latitude, float longtitude)
76 {
77 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_INVALID_OPERATION, "AVMuxer Service does not exist");
78 return avmuxerService_->SetLocation(latitude, longtitude);
79 }
80
SetRotation(int32_t rotation)81 int32_t AVMuxerImpl::SetRotation(int32_t rotation)
82 {
83 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_INVALID_OPERATION, "AVMuxer Service does not exist");
84 return avmuxerService_->SetRotation(rotation);
85 }
86
AddTrack(const MediaDescription & trackDesc,int32_t & trackId)87 int32_t AVMuxerImpl::AddTrack(const MediaDescription &trackDesc, int32_t &trackId)
88 {
89 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_INVALID_OPERATION, "AVMuxer Service does not exist");
90 return avmuxerService_->AddTrack(trackDesc, trackId);
91 }
92
Start()93 int32_t AVMuxerImpl::Start()
94 {
95 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_INVALID_OPERATION, "AVMuxer Service does not exist");
96 return avmuxerService_->Start();
97 }
98
WriteTrackSample(std::shared_ptr<AVContainerMemory> sampleData,const TrackSampleInfo & info)99 int32_t AVMuxerImpl::WriteTrackSample(std::shared_ptr<AVContainerMemory> sampleData, const TrackSampleInfo &info)
100 {
101 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_INVALID_OPERATION, "AVMuxer Service does not exist");
102 CHECK_AND_RETURN_RET_LOG(sampleData != nullptr &&
103 sampleData->Offset() + sampleData->Size() <= sampleData->Capacity() &&
104 sampleData->Offset() + sampleData->Size() >= sampleData->Size(),
105 MSERR_INVALID_VAL, "Invalid memory");
106 std::shared_ptr<AVSharedMemoryBase> avSharedMem =
107 std::make_shared<AVSharedMemoryBase>(sampleData->Size(), AVSharedMemory::FLAGS_READ_ONLY, "sampleData");
108 int32_t ret = avSharedMem->Init();
109 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_NO_MEMORY, "Failed to create AVSharedMemoryBase");
110 errno_t rc = memcpy_s(avSharedMem->GetBase(), avSharedMem->GetSize(), sampleData->Data(), sampleData->Size());
111 CHECK_AND_RETURN_RET_LOG(rc == EOK, MSERR_UNKNOWN, "memcpy_s failed");
112 return avmuxerService_->WriteTrackSample(avSharedMem, info);
113 }
114
Stop()115 int32_t AVMuxerImpl::Stop()
116 {
117 CHECK_AND_RETURN_RET_LOG(avmuxerService_ != nullptr, MSERR_INVALID_OPERATION, "AVMuxer Service does not exist");
118 return avmuxerService_->Stop();
119 }
120
Release()121 void AVMuxerImpl::Release()
122 {
123 CHECK_AND_RETURN_LOG(avmuxerService_ != nullptr, "AVMuxer Service does not exist");
124 (void)avmuxerService_->Release();
125 (void)MediaServiceFactory::GetInstance().DestroyAVMuxerService(avmuxerService_);
126 avmuxerService_ = nullptr;
127 }
128 } // namespace Media
129 } // namespace OHOS