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 AVCODEC_MEDIA_MUXER_H 17 #define AVCODEC_MEDIA_MUXER_H 18 19 #include <vector> 20 #include <atomic> 21 #include <thread> 22 #include <mutex> 23 #include <condition_variable> 24 #include "buffer/avbuffer_queue.h" 25 #include "buffer/avbuffer_queue_define.h" 26 #include "plugin/muxer_plugin.h" 27 28 namespace OHOS { 29 namespace Media { 30 class MediaMuxer : public Plugins::Callback { 31 public: 32 MediaMuxer(int32_t appUid, int32_t appPid); 33 virtual ~MediaMuxer(); 34 Status Init(int32_t fd, Plugins::OutputFormat format); 35 Status Init(FILE *file, Plugins::OutputFormat format); 36 Status SetParameter(const std::shared_ptr<Meta> ¶m); 37 Status SetUserMeta(const std::shared_ptr<Meta> &userMeta); 38 Status AddTrack(int32_t &trackIndex, const std::shared_ptr<Meta> &trackDesc); 39 sptr<AVBufferQueueProducer> GetInputBufferQueue(uint32_t trackIndex); 40 Status Start(); 41 Status WriteSample(uint32_t trackIndex, const std::shared_ptr<AVBuffer> &sample); 42 Status Stop(); 43 Status Reset(); 44 void OnEvent(const Plugins::PluginEvent &event) override; 45 46 private: 47 enum class State { 48 UNINITIALIZED, 49 INITIALIZED, 50 STARTED, 51 STOPPED 52 }; 53 54 std::shared_ptr<Plugins::MuxerPlugin> CreatePlugin(Plugins::OutputFormat format); 55 void StartThread(const std::string &name); 56 void StopThread(); 57 void ThreadProcessor(); 58 void OnBufferAvailable(); 59 void ReleaseBuffer(); 60 bool CanAddTrack(const std::string &mimeType); 61 bool CheckKeys(const std::string &mimeType, const std::shared_ptr<Meta> &trackDesc); 62 std::string StateConvert(State state); 63 64 class Track : public IConsumerListener { 65 public: Track()66 Track() {}; ~Track()67 virtual ~Track() {}; 68 std::shared_ptr<AVBuffer> GetBuffer(); 69 void ReleaseBuffer(); 70 void SetBufferAvailableListener(MediaMuxer *listener); 71 void OnBufferAvailable() override; 72 73 public: 74 int32_t trackId_ = -1; 75 std::string mimeType_ = {}; 76 std::shared_ptr<Meta> trackDesc_ = nullptr; 77 sptr<AVBufferQueueProducer> producer_ = nullptr; 78 sptr<AVBufferQueueConsumer> consumer_ = nullptr; 79 std::shared_ptr<AVBufferQueue> bufferQ_ = nullptr; 80 std::shared_ptr<AVBuffer> curBuffer_ = nullptr; 81 82 private: 83 std::atomic<int32_t> bufferAvailableCount_ = 0; 84 MediaMuxer *listener_ = nullptr; 85 }; 86 87 int32_t appUid_ = -1; 88 int32_t appPid_ = -1; 89 Plugins::OutputFormat format_; 90 std::atomic<State> state_ = State::UNINITIALIZED; 91 std::shared_ptr<Plugins::MuxerPlugin> muxer_ = nullptr; 92 std::vector<sptr<Track>> tracks_; 93 std::string threadName_; 94 std::mutex mutex_; 95 std::mutex mutexBufferAvailable_; 96 std::condition_variable condBufferAvailable_; 97 std::atomic<int32_t> bufferAvailableCount_ = 0; 98 std::unique_ptr<std::thread> thread_ = nullptr; 99 bool isThreadExit_ = true; 100 }; 101 } // namespace Media 102 } // namespace OHOS 103 #endif // AVCODEC_MEDIA_MUXER_H