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 virtual Status Init(int32_t fd, Plugins::OutputFormat format); 35 virtual Status Init(FILE *file, Plugins::OutputFormat format); 36 virtual Status SetParameter(const std::shared_ptr<Meta> ¶m); 37 virtual Status SetUserMeta(const std::shared_ptr<Meta> &userMeta); 38 virtual Status AddTrack(int32_t &trackIndex, const std::shared_ptr<Meta> &trackDesc); 39 virtual sptr<AVBufferQueueProducer> GetInputBufferQueue(uint32_t trackIndex); 40 virtual Status Start(); 41 virtual Status WriteSample(uint32_t trackIndex, const std::shared_ptr<AVBuffer> &sample); 42 virtual Status Stop(); 43 virtual 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 bool CheckKeysExt(const std::string &mimeType, const std::shared_ptr<Meta> &trackDesc, 63 Plugins::OutputFormat format); 64 std::string StateConvert(State state); 65 void PrintWriteCount(); 66 67 class Track : public IConsumerListener { 68 public: Track()69 Track() {}; ~Track()70 virtual ~Track() {}; 71 std::shared_ptr<AVBuffer> GetBuffer(); 72 void ReleaseBuffer(); 73 void SetBufferAvailableListener(MediaMuxer *listener); 74 void OnBufferAvailable() override; 75 76 public: 77 int32_t trackId_ = -1; 78 std::string mimeType_ = {}; 79 std::shared_ptr<Meta> trackDesc_ = nullptr; 80 sptr<AVBufferQueueProducer> producer_ = nullptr; 81 sptr<AVBufferQueueConsumer> consumer_ = nullptr; 82 std::shared_ptr<AVBufferQueue> bufferQ_ = nullptr; 83 std::shared_ptr<AVBuffer> curBuffer_ = nullptr; 84 uint64_t writeCount_ = 0; 85 86 private: 87 std::atomic<int32_t> bufferAvailableCount_ = 0; 88 MediaMuxer *listener_ = nullptr; 89 }; 90 91 int32_t appUid_ = -1; 92 int32_t appPid_ = -1; 93 Plugins::OutputFormat format_; 94 std::atomic<State> state_ = State::UNINITIALIZED; 95 std::shared_ptr<Plugins::MuxerPlugin> muxer_ = nullptr; 96 std::vector<sptr<Track>> tracks_; 97 std::string threadName_; 98 std::mutex mutex_; 99 std::mutex mutexBufferAvailable_; 100 std::condition_variable condBufferAvailable_; 101 std::atomic<int32_t> bufferAvailableCount_ = 0; 102 std::unique_ptr<std::thread> thread_ = nullptr; 103 bool isThreadExit_ = true; 104 }; 105 } // namespace Media 106 } // namespace OHOS 107 #endif // AVCODEC_MEDIA_MUXER_H