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 AVMUXER_H 17 #define AVMUXER_H 18 19 #include "avsharedmemory.h" 20 #include "media_description.h" 21 #include "av_common.h" 22 #include "avcodec_common.h" 23 24 namespace OHOS { 25 namespace MediaAVCodec { 26 class AVMuxer { 27 public: 28 virtual ~AVMuxer() = default; 29 30 /** 31 * @brief Set the rotation for output video playback. 32 * Note: This interface can only be called before Start. 33 * @param rotation The supported angles are 0, 90, 180, and 270 degrees. 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 SetRotation(int32_t rotation) = 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 trackFormat OH_AVFormat 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 MediaDescription &trackDesc) = 0; 52 53 /** 54 * @brief Start the muxer. 55 * Note: This interface is called after AddTrack and before WriteSample. 56 * @return Returns AVCS_ERR_OK if the execution is successful, 57 * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode} 58 * @since 10 59 */ 60 virtual int32_t Start() = 0; 61 62 /** 63 * @brief Write an encoded sample to the muxer. 64 * Note: This interface can only be called after Start and before Stop. The application needs to 65 * make sure that the samples are written to the right tacks. Also, it needs to make sure the samples 66 * for each track are written in chronological order. 67 * @param trackIndex The track index for this sample 68 * @param sample The encoded or demuxer sample 69 * @param info The buffer information related to this sample {@link AVCodecBufferInfo} 70 * @param flag The buffer flag related to this sample {@link AVCodecBufferFlag} 71 * @return Returns AVCS_ERR_OK if the execution is successful, 72 * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode} 73 * @since 10 74 */ 75 virtual int32_t WriteSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> sample, 76 AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0; 77 78 /** 79 * @brief Stop the muxer. 80 * Note: Once the muxer stops, it can not be restarted. 81 * @return Returns AVCS_ERR_OK if the execution is successful, 82 * otherwise returns a specific error code, refer to {@link AVCodecServiceErrCode} 83 * @since 10 84 */ 85 virtual int32_t Stop() = 0; 86 }; 87 88 class __attribute__((visibility("default"))) AVMuxerFactory { 89 public: 90 /** 91 * @brief Create an AVMuxer instance by output file description and format. 92 * @param fd Must be opened with read and write permission. Caller is responsible for closing fd. 93 * @param format The output format is {@link OutputFormat} . 94 * @return Returns a pointer to an AVMuxer instance. 95 * @since 10 96 */ 97 static std::shared_ptr<AVMuxer> CreateAVMuxer(int32_t fd, OutputFormat format); 98 private: 99 AVMuxerFactory() = default; 100 ~AVMuxerFactory() = default; 101 }; 102 } // namespace MediaAVCodec 103 } // namespace OHOS 104 105 #endif // AVMUXER_H 106