1 /*
2 * Copyright (c) 2024-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 <cstdint>
17 #include <unistd.h>
18 #include "audio_video_muxer.h"
19 #include "camera_log.h"
20 #include "native_mfmagic.h"
21 #include "camera_dynamic_loader.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25
AudioVideoMuxer()26 AudioVideoMuxer::AudioVideoMuxer()
27 {
28 }
29
~AudioVideoMuxer()30 AudioVideoMuxer::~AudioVideoMuxer()
31 {
32 MEDIA_INFO_LOG("~AudioVideoMuxer enter");
33 }
34
Create(OH_AVOutputFormat format,PhotoAssetIntf * photoAssetProxy)35 int32_t AudioVideoMuxer::Create(OH_AVOutputFormat format, PhotoAssetIntf* photoAssetProxy)
36 {
37 photoAssetProxy_ = photoAssetProxy;
38 if (photoAssetProxy_) {
39 fd_ = photoAssetProxy_->GetVideoFd();
40 }
41 MEDIA_INFO_LOG("CreateAVMuxer with videoFd: %{public}d", fd_);
42 muxer_ = AVMuxerFactory::CreateAVMuxer(fd_, static_cast<Plugins::OutputFormat>(format));
43 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "create muxer failed!");
44 return 0;
45 }
46
Start()47 int32_t AudioVideoMuxer::Start()
48 {
49 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "muxer_ is null");
50 int32_t ret = muxer_->Start();
51 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, 1, "Start failed, ret: %{public}d", ret);
52 return 0;
53 }
54
SetRotation(int32_t rotation)55 int32_t AudioVideoMuxer::SetRotation(int32_t rotation)
56 {
57 MEDIA_INFO_LOG("SetRotation rotation : %{public}d", rotation);
58 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "muxer_ is null");
59 std::shared_ptr<Meta> param = std::make_shared<Meta>();
60 param->Set<Tag::VIDEO_ROTATION>(static_cast<Plugins::VideoRotation>(rotation));
61 int32_t ret = muxer_->SetParameter(param);
62 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, 1, "SetRotation failed, ret: %{public}d", ret);
63 return 0;
64 }
65
SetCoverTime(float timems)66 int32_t AudioVideoMuxer::SetCoverTime(float timems)
67 {
68 MEDIA_INFO_LOG("SetCoverTime coverTime : %{public}f", timems);
69 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "muxer_ is null");
70 std::shared_ptr<Meta> userMeta = std::make_shared<Meta>();
71 userMeta->SetData("com.openharmony.covertime", timems);
72 int32_t ret = muxer_->SetUserMeta(userMeta);
73 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, 1, "SetCoverTime failed, ret: %{public}d", ret);
74 return 0;
75 }
76
SetTimedMetadata()77 int32_t AudioVideoMuxer::SetTimedMetadata()
78 {
79 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "muxer_ is null");
80 std::shared_ptr<Meta> param = std::make_shared<Meta>();
81 param->SetData("use_timed_meta_track", 1);
82 return muxer_->SetParameter(param);
83 }
84
WriteSampleBuffer(std::shared_ptr<OHOS::Media::AVBuffer> sample,TrackType type)85 int32_t AudioVideoMuxer::WriteSampleBuffer(std::shared_ptr<OHOS::Media::AVBuffer> sample, TrackType type)
86 {
87 CAMERA_SYNC_TRACE;
88 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "muxer_ is null");
89 CHECK_AND_RETURN_RET_LOG(sample != nullptr, AV_ERR_INVALID_VAL, "input sample is nullptr!");
90 int32_t ret = AV_ERR_OK;
91 int trackId = -1;
92 switch (type) {
93 case TrackType::AUDIO_TRACK:
94 trackId = audioTrackId_;
95 break;
96 case TrackType::VIDEO_TRACK:
97 trackId = videoTrackId_;
98 break;
99 case TrackType::META_TRACK:
100 trackId = metaTrackId_;
101 break;
102 default:
103 MEDIA_ERR_LOG("TrackType type = %{public}d not supported", type);
104 }
105 ret = muxer_->WriteSample(trackId, sample);
106 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, 1, "WriteSampleBuffer failed, ret: %{public}d", ret);
107 return 0;
108 }
109
GetVideoFd()110 int32_t AudioVideoMuxer::GetVideoFd()
111 {
112 return fd_;
113 }
114
GetPhotoAssetProxy()115 PhotoAssetIntf* AudioVideoMuxer::GetPhotoAssetProxy()
116 {
117 return photoAssetProxy_;
118 }
119
120
AddTrack(int & trackId,std::shared_ptr<Format> format,TrackType type)121 int32_t AudioVideoMuxer::AddTrack(int &trackId, std::shared_ptr<Format> format, TrackType type)
122 {
123 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "muxer_ is null");
124 CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input track format is nullptr!");
125 int32_t ret = muxer_->AddTrack(trackId, format->GetMeta());
126 switch (type) {
127 case TrackType::AUDIO_TRACK:
128 audioTrackId_ = trackId;
129 break;
130 case TrackType::VIDEO_TRACK:
131 videoTrackId_ = trackId;
132 break;
133 case TrackType::META_TRACK:
134 metaTrackId_ = trackId;
135 break;
136 default:
137 MEDIA_ERR_LOG("TrackType type = %{public}d not supported", type);
138 }
139 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK || trackId < 0, 1, "AddTrack failed, ret: %{public}d", ret);
140 return 0;
141 }
142
Stop()143 int32_t AudioVideoMuxer::Stop()
144 {
145 CHECK_AND_RETURN_RET_LOG(muxer_ != nullptr, 1, "muxer_ is null");
146 int32_t ret = muxer_->Stop();
147 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, 1, "Stop failed, ret: %{public}d", ret);
148 return 0;
149 }
150
Release()151 int32_t AudioVideoMuxer::Release()
152 {
153 MEDIA_INFO_LOG("AudioVideoMuxer::Release enter");
154 if (muxer_ != nullptr) {
155 muxer_ = nullptr;
156 close(fd_);
157 }
158 return 0;
159 }
160
161 } // CameraStandard
162 } // OHOS