• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Add track format to the muxer.
42      * Note: This interface can only be called before Start.
43      * @param trackIndex Used to get the track index for this newly added track,
44      * and it should be used in the WriteSample. The track index is greater than or equal to 0,
45      * others is error index.
46      * @param trackDesc Meta handle pointer contain track format
47      * @return Returns AVCS_ERR_OK if the execution is successful,
48      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
49      * @since 10
50      */
51     virtual int32_t AddTrack(int32_t &trackIndex, const std::shared_ptr<Meta> &trackDesc) = 0;
52 
53     /**
54      * @brief Get the track buffer queue by track index.
55      * Note: This interface can only be called before Start.
56      * @param trackIndex Used to get the track buffer queue by track index.
57      * @return Returns the ptr of AVBufferQueueProducer if the execution is successful,
58      * otherwise returns null.
59      * @since 11
60      */
61     virtual sptr<AVBufferQueueProducer> GetInputBufferQueue(uint32_t trackIndex) = 0;
62 
63     /**
64      * @brief Start the muxer.
65      * Note: This interface is called after AddTrack and before WriteSample.
66      * @return Returns AVCS_ERR_OK if the execution is successful,
67      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
68      * @since 10
69      */
70     virtual int32_t Start() = 0;
71 
72     /**
73      * @brief Write an encoded sample to the muxer.
74      * Note: This interface can only be called after Start and before Stop. The application needs to
75      * make sure that the samples are written to the right tacks. Also, it needs to make sure the samples
76      * for each track are written in chronological order.
77      * @param trackIndex The track index for this sample
78      * @param sample The encoded or demuxer sample, which including data and buffer information
79      * @return Returns AVCS_ERR_OK if the execution is successful,
80      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
81      * @since 10
82      */
83     virtual int32_t WriteSample(uint32_t trackIndex, const std::shared_ptr<AVBuffer> &sample) = 0;
84 
85     /**
86      * @brief Stop the muxer.
87      * Note: Once the muxer stops, it can not be restarted.
88      * @return Returns AVCS_ERR_OK if the execution is successful,
89      * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode}
90      * @since 10
91      */
92     virtual int32_t Stop() = 0;
93 };
94 
95 class __attribute__((visibility("default"))) AVMuxerFactory {
96 public:
97     /**
98      * @brief Create an AVMuxer instance by output file description and format.
99      * @param fd Must be opened with read and write permission. Caller is responsible for closing fd.
100      * @param format The output format is {@link OutputFormat} .
101      * @return Returns a pointer to an AVMuxer instance.
102      * @since 10
103      */
104     static std::shared_ptr<AVMuxer> CreateAVMuxer(int32_t fd, Plugins::OutputFormat format);
105 private:
106     AVMuxerFactory() = default;
107     ~AVMuxerFactory() = default;
108 };
109 } // namespace MediaAVCodec
110 } // namespace OHOS
111 
112 #endif // MEDIA_AVCODEC_AVMUXER_H
113