• 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 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> &param);
37     Status AddTrack(int32_t &trackIndex, const std::shared_ptr<Meta> &trackDesc);
38     sptr<AVBufferQueueProducer> GetInputBufferQueue(uint32_t trackIndex);
39     Status Start();
40     Status WriteSample(uint32_t trackIndex, const std::shared_ptr<AVBuffer> &sample);
41     Status Stop();
42     Status Reset();
43     void OnEvent(const Plugins::PluginEvent &event) override;
44 
45 private:
46     enum class State {
47         UNINITIALIZED,
48         INITIALIZED,
49         STARTED,
50         STOPPED
51     };
52 
53     std::shared_ptr<Plugins::MuxerPlugin> CreatePlugin(Plugins::OutputFormat format);
54     void StartThread(const std::string &name);
55     void StopThread();
56     void ThreadProcessor();
57     void OnBufferAvailable();
58     bool CanAddTrack(const std::string &mimeType);
59     bool CheckKeys(const std::string &mimeType, const std::shared_ptr<Meta> &trackDesc);
60     std::string StateConvert(State state);
61 
62     class Track : public IConsumerListener {
63     public:
Track()64         Track() {};
~Track()65         virtual ~Track() {};
66         std::shared_ptr<AVBuffer> GetBuffer();
67         void ReleaseBuffer();
68         void SetBufferAvailableListener(MediaMuxer *listener);
69         void OnBufferAvailable() override;
70 
71     public:
72         int32_t trackId_ = -1;
73         std::string mimeType_ = {};
74         std::shared_ptr<Meta> trackDesc_ = nullptr;
75         sptr<AVBufferQueueProducer> producer_ = nullptr;
76         sptr<AVBufferQueueConsumer> consumer_ = nullptr;
77         std::shared_ptr<AVBufferQueue> bufferQ_ = nullptr;
78         std::shared_ptr<AVBuffer> curBuffer_ = nullptr;
79 
80     private:
81         std::atomic<int32_t> bufferAvailableCount_ = 0;
82         MediaMuxer *listener_ = nullptr;
83     };
84 
85     int32_t appUid_ = -1;
86     int32_t appPid_ = -1;
87     Plugins::OutputFormat format_;
88     std::atomic<State> state_ = State::UNINITIALIZED;
89     std::shared_ptr<Plugins::MuxerPlugin> muxer_ = nullptr;
90     std::vector<sptr<Track>> tracks_;
91     std::string threadName_;
92     std::mutex mutex_;
93     std::mutex mutexBufferAvailable_;
94     std::condition_variable condBufferAvailable_;
95     std::atomic<int32_t> bufferAvailableCount_ = 0;
96     std::unique_ptr<std::thread> thread_ = nullptr;
97     bool isThreadExit_ = true;
98 };
99 } // namespace Media
100 } // namespace OHOS
101 #endif // AVCODEC_MEDIA_MUXER_H