1 /*
2  * Copyright (C) 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 #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     int32_t AddTrack(OH_AVMuxer* muxer, int32_t& trackIndex, AudioTrackParam param);
72     int32_t AddCoverTrack(OH_AVMuxer* muxer, int32_t& trackId, AudioTrackParam param);
73 
74     AudioTrackParam InitFormatParam(AudioMuxerFormatType type);
75     OH_AVMuxer* Create();
76     int32_t Start();
77     int32_t Stop();
78     int32_t Destroy();
79 
80     int32_t SetRotation(OH_AVMuxer* muxer, int32_t rotation);
81 
82     void WriteTrackCover(OH_AVMuxer *muxer, int32_t trackIndex);
83     void WriteSingleTrackSampleAVBuffer(OH_AVMuxer *muxer, int32_t trackIndex);
84     bool UpdateWriteBufferInfoAVBuffer(OH_AVBuffer **buffer, OH_AVCodecBufferAttr *info);
85 
86 private:
87     OH_AVMuxer *avmuxer_ = {nullptr};
88     OH_AVBuffer *avbuffer = {nullptr};
89     int32_t outputFd_ = {-1};
90     size_t inputdatasize = 0;
91     std::string inputdata;
92     std::string output_path_;
93     OH_AVOutputFormat output_format_;
94     AudioMuxerFormatType audioType_;
95 };
96 } // namespace AudioBufferDemo
97 } // namespace MediaAVCodec
98 } // namespace OHOS
99 #endif // AVCODEC_AUDIO_AVBUFFER_MUXER_DEMO_H
100