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