1 /*
2  * Copyright (C) 2023 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 MEDIA_AVCODEC_AVMUXER_H
17 #define MEDIA_AVCODEC_AVMUXER_H
18 
19 #include "meta/meta.h"
20 #include "buffer/avbuffer.h"
21 #include "buffer/avbuffer_queue_producer.h"
22 
23 namespace OHOS {
24 namespace MediaAVCodec {
25 using namespace Media;
26 class AVMuxer {
27 public:
28     virtual ~AVMuxer() = default;
29 
30     /**
31      * @brief Set the parameter for media.
32      * Note: This interface can only be called before Start.
33      * @param param The supported meta keys as: VIDEO_ROTATION, MEDIA_CREATION_TIME, MEDIA_LATITUDE, etc.
34      * @return Returns AVCS_ERR_OK if the execution is successful,
35      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
36      * @since 10
37      */
38     virtual int32_t SetParameter(const std::shared_ptr<Meta> &param) = 0;
39 
40     /**
41      * @brief Set the user meta for media.
42      * Note: This interface can only be called before Stop.
43      * @param userMeta The meta keys are user-defined, the meta values can only be string, int, float.
44      * @return Returns AVCS_ERR_OK if the execution is successful,
45      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
46      * @since 12
47      */
48     virtual int32_t SetUserMeta(const std::shared_ptr<Meta> &userMeta) = 0;
49 
50     /**
51      * @brief Add track format to the muxer.
52      * Note: This interface can only be called before Start.
53      * @param trackIndex Used to get the track index for this newly added track,
54      * and it should be used in the WriteSample. The track index is greater than or equal to 0,
55      * others is error index.
56      * @param trackDesc Meta handle pointer contain track format
57      * @return Returns AVCS_ERR_OK if the execution is successful,
58      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
59      * @since 10
60      */
61     virtual int32_t AddTrack(int32_t &trackIndex, const std::shared_ptr<Meta> &trackDesc) = 0;
62 
63     /**
64      * @brief Get the track buffer queue by track index.
65      * Note: This interface can only be called before Start.
66      * @param trackIndex Used to get the track buffer queue by track index.
67      * @return Returns the ptr of AVBufferQueueProducer if the execution is successful,
68      * otherwise returns null.
69      * @since 11
70      */
71     virtual sptr<AVBufferQueueProducer> GetInputBufferQueue(uint32_t trackIndex) = 0;
72 
73     /**
74      * @brief Start the muxer.
75      * Note: This interface is called after AddTrack and before WriteSample.
76      * @return Returns AVCS_ERR_OK if the execution is successful,
77      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
78      * @since 10
79      */
80     virtual int32_t Start() = 0;
81 
82     /**
83      * @brief Write an encoded sample to the muxer.
84      * Note: This interface can only be called after Start and before Stop. The application needs to
85      * make sure that the samples are written to the right tacks. Also, it needs to make sure the samples
86      * for each track are written in chronological order.
87      * @param trackIndex The track index for this sample
88      * @param sample The encoded or demuxer sample, which including data and buffer information
89      * @return Returns AVCS_ERR_OK if the execution is successful,
90      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
91      * @since 10
92      */
93     virtual int32_t WriteSample(uint32_t trackIndex, const std::shared_ptr<AVBuffer> &sample) = 0;
94 
95     /**
96      * @brief Stop the muxer.
97      * Note: Once the muxer stops, it can not be restarted.
98      * @return Returns AVCS_ERR_OK if the execution is successful,
99      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
100      * @since 10
101      */
102     virtual int32_t Stop() = 0;
103 };
104 
105 class __attribute__((visibility("default"))) AVMuxerFactory {
106 public:
107     /**
108      * @brief Create an AVMuxer instance by output file description and format.
109      * @param fd Must be opened with read and write permission. Caller is responsible for closing fd.
110      * @param format The output format is {@link OutputFormat} .
111      * @return Returns a pointer to an AVMuxer instance.
112      * @since 10
113      */
114     static std::shared_ptr<AVMuxer> CreateAVMuxer(int32_t fd, Plugins::OutputFormat format);
115 private:
116     AVMuxerFactory() = default;
117     ~AVMuxerFactory() = default;
118 };
119 } // namespace MediaAVCodec
120 } // namespace OHOS
121 
122 #endif // MEDIA_AVCODEC_AVMUXER_H
123