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     int32_t trackId;
47     const char* mimeType;
48     int32_t sampleRate;
49     long bitRate;
50     int32_t channels;
51     int32_t frameSize;
52     int32_t width;
53     int32_t height;
54     double frameRate;
55     int32_t videoDelay;
56     int32_t colorPrimaries;
57     int32_t colorTransfer;
58     int32_t colorMatrixCoeff;
59     int32_t colorRange;
60     int32_t isHdrVivid;
61     bool isNeedCover;
62     bool isNeedVideo;
63 } AudioTrackParam;
64 
65 class AVMuxerDemo : public NoCopyable {
66 public:
67     AVMuxerDemo();
68     ~AVMuxerDemo();
69 
70     bool InitFile(const std::string& inputFile);
71     bool RunCase(const uint8_t *data, size_t size);
72 
73     int32_t AddTrack(OH_AVMuxer* muxer, int32_t& trackIndex, AudioTrackParam param);
74     int32_t AddCoverTrack(OH_AVMuxer* muxer, int32_t& trackId, AudioTrackParam param);
75 
76     AudioTrackParam InitFormatParam(AudioMuxerFormatType type);
77     OH_AVMuxer* Create();
78     int32_t Start();
79     int32_t Stop();
80     int32_t Destroy();
81 
82     int32_t SetRotation(OH_AVMuxer* muxer, int32_t rotation);
83 
84     void WriteTrackCover(OH_AVMuxer *muxer, int32_t trackIndex);
85     void WriteSingleTrackSampleAVBuffer(OH_AVMuxer *muxer, int32_t trackIndex);
86     bool UpdateWriteBufferInfoAVBuffer(OH_AVBuffer **buffer, OH_AVCodecBufferAttr *info);
87 
88 private:
89     OH_AVMuxer *avmuxer_ = {nullptr};
90     OH_AVBuffer *avbuffer = {nullptr};
91     int32_t outputFd_ = {-1};
92     size_t inputdatasize = 0;
93     std::string inputdata;
94     std::string output_path_;
95     OH_AVOutputFormat output_format_;
96     AudioMuxerFormatType audioType_;
97 };
98 } // namespace AudioBufferDemo
99 } // namespace MediaAVCodec
100 } // namespace OHOS
101 #endif // AVCODEC_AUDIO_AVBUFFER_MUXER_DEMO_H
102