• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #ifndef AVCODEC_AUDIO_AVBUFFER_MUXER_DEMO_H
17 #define AVCODEC_AUDIO_AVBUFFER_MUXER_DEMO_H
18 
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include "avmuxer.h"
22 #include "nocopyable.h"
23 #include "avcodec_errors.h"
24 #include "native_avmuxer.h"
25 #include "native_avcodec_base.h"
26 #include "common/native_mfmagic.h"
27 
28 namespace OHOS {
29 namespace MediaAVCodec {
30 namespace AudioBufferDemo {
31 
32 enum class AudioMuxerFormatType : int32_t {
33     TYPE_AAC = 0,
34     TYPE_H264 = 1,
35     TYPE_H265 = 2,
36     TYPE_MPEG4 = 3,
37     TYPE_HDR = 4,
38     TYPE_JPG = 5,
39     TYPE_AMRNB = 6,
40     TYPE_AMRWB = 7,
41     TYPE_MPEG3 = 8,
42     TYPE_MAX = 20,
43 };
44 
45 typedef struct AudioTrackParam {
46     const char* mimeType;
47     int32_t sampleRate;
48     int32_t channels;
49     int32_t frameSize;
50     int32_t width;
51     int32_t height;
52     double frameRate;
53     int32_t videoDelay;
54     int32_t colorPrimaries;
55     int32_t colorTransfer;
56     int32_t colorMatrixCoeff;
57     int32_t colorRange;
58     int32_t isHdrVivid;
59     bool isNeedCover;
60     bool isNeedVideo;
61 } AudioTrackParam;
62 
63 class AVMuxerDemo : public NoCopyable {
64 public:
65     AVMuxerDemo();
66     ~AVMuxerDemo();
67 
68     bool InitFile(const std::string& inputFile);
69     bool RunCase(const uint8_t *data, size_t size);
70 
71 private:
72     // 创建封装器
73     OH_AVMuxer* Create();
74     // 设置旋转角
75     int32_t SetRotation(OH_AVMuxer* muxer, int32_t rotation);
76     // 获取封装参数
77     AudioTrackParam InitFormatParam(AudioMuxerFormatType type);
78     // 设置meta信息
79     int32_t SetFormat(const uint8_t* data, size_t size);
80     // 添加轨
81     int32_t AddTrack(OH_AVMuxer* muxer, int32_t& trackIndex, AudioTrackParam param);
82     // 开始
83     int32_t Start();
84     // 停止
85     int32_t Stop();
86     // 释放资源
87     int32_t Destroy();
88     // 封装数据
89     void WriteSingleTrackSampleAVBuffer(OH_AVMuxer *muxer, int32_t trackIndex);
90     bool UpdateWriteBufferInfoAVBuffer(OH_AVBuffer **buffer, OH_AVCodecBufferAttr *info, int32_t trackIndex);
91 
92 private:
93     // 文件句柄
94     int32_t inputFd_ = {-1};
95     int32_t outputFd_ = {-1};
96     // fuzz data size
97     std::string inputData_;
98     size_t inputDataSize_ = 0;
99     // 输出文件路径
100     std::string output_path_;
101     AudioMuxerFormatType audioType_;
102     OH_AVOutputFormat output_format_;
103     // 封装
104     OH_AVMuxer *avMuxer_ = { nullptr };
105     OH_AVFormat *metaFormat_ = { nullptr };
106     OH_AVFormat *trackFormat_ = { nullptr };
107 };
108 } // namespace AudioBufferDemo
109 } // namespace MediaAVCodec
110 } // namespace OHOS
111 #endif // AVCODEC_AUDIO_AVBUFFER_MUXER_DEMO_H
112