• 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 MUXER_ENGINE_IMPL_H
17 #define MUXER_ENGINE_IMPL_H
18 
19 #include <map>
20 #include <atomic>
21 #include <thread>
22 #include <mutex>
23 #include <condition_variable>
24 #include "i_muxer_engine.h"
25 #include "muxer.h"
26 #include "block_queue.h"
27 #include "muxer_buffer_pool.h"
28 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 class MuxerEngineImpl : public IMuxerEngine {
32 public:
33     MuxerEngineImpl(int32_t appUid, int32_t appPid, int32_t fd, OutputFormat format);
34     ~MuxerEngineImpl() override;
35     int32_t Init();
36     int32_t SetRotation(int32_t rotation) override;
37     int32_t AddTrack(int32_t &trackIndex, const MediaDescription &trackDesc) override;
38     int32_t Start() override;
39     int32_t WriteSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> sample,
40         AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
41     int32_t Stop() override;
42     int32_t DumpInfo(int32_t fd) override;
43 
44     enum class State {
45         UNINITIALIZED,
46         INITIALIZED,
47         STARTED,
48         STOPPED
49     };
50 
51     enum TrackMimeType {
52         TRACK_MIME_TYPE_AUDIO = 0,
53         TRACK_MIME_TYPE_VIDEO,
54         TRACK_MIME_TYPE_IMAGE
55     };
56 
57 private:
58     int32_t StartThread(const std::string &name);
59     int32_t StopThread() noexcept;
60     void ThreadProcessor();
61     bool CanAddTrack(const std::string &mimeType);
62     bool CheckKeys(const std::string &mimeType, const MediaDescription &trackDesc);
63     std::string ConvertStateToString(State state);
64     int32_t TranslatePluginStatus(Plugin::Status error);
65     TrackMimeType GetTrackMimeType(const std::string &mime);
66 
67     struct BlockBuffer {
68         uint32_t trackIndex_;
69         std::shared_ptr<uint8_t> buffer_;
70         AVCodecBufferInfo info_;
71         AVCodecBufferFlag flag_;
72     };
73 
74     int32_t appUid_ = -1;
75     int32_t appPid_ = -1;
76     int64_t fd_ = -1;
77     OutputFormat format_;
78     std::atomic<State> state_ = State::UNINITIALIZED;
79     std::shared_ptr<Plugin::Muxer> muxer_ = nullptr;
80     std::map<int32_t, MediaDescription> tracksDesc_;
81     BlockQueue<std::shared_ptr<BlockBuffer>> que_;
82     MuxerBufferPool pool_;
83     std::string threadName_;
84     std::mutex mutex_;
85     std::condition_variable cond_;
86     std::unique_ptr<std::thread> thread_ = nullptr;
87     bool isThreadExit_ = true;
88 };
89 } // namespace MediaAVCodec
90 } // namespace OHOS
91 #endif // MUXER_ENGINE_IMPL_H