1 /*
2  * Copyright (c) 2023-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 FILTERS_MUXER_FILTER_H
17 #define FILTERS_MUXER_FILTER_H
18 
19 #include <cstring>
20 #include <map>
21 
22 #include "filter/filter.h"
23 #include "common/status.h"
24 #include "meta/media_types.h"
25 
26 namespace OHOS {
27 namespace Media {
28 class MediaMuxer;
29 namespace Pipeline {
30 using Plugins::OutputFormat;
31 class MuxerFilter : public Filter, public std::enable_shared_from_this<MuxerFilter> {
32 public:
33     explicit MuxerFilter(std::string name, FilterType type);
34     ~MuxerFilter() override;
35     Status SetOutputParameter(int32_t appUid, int32_t appPid, int32_t fd, int32_t format);
36     Status SetTransCoderMode();
37     int64_t GetCurrentPtsMs();
38     void Init(const std::shared_ptr<EventReceiver> &receiver, const std::shared_ptr<FilterCallback> &callback) override;
39     Status DoPrepare() override;
40     Status DoStart() override;
41     Status DoPause() override;
42     Status DoResume() override;
43     Status DoStop() override;
44     Status DoFlush() override;
45     Status DoRelease() override;
46     void SetParameter(const std::shared_ptr<Meta> &parameter) override;
47     void SetUserMeta(const std::shared_ptr<Meta> &userMeta);
48     void GetParameter(std::shared_ptr<Meta> &parameter) override;
49     Status LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
50     Status UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
51     Status UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
52     FilterType GetFilterType();
53     Status OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta,
54         const std::shared_ptr<FilterLinkCallback> &callback) override;
55     Status OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta,
56         const std::shared_ptr<FilterLinkCallback> &callback) override;
57     Status OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback>& callback) override;
58     void OnBufferFilled(std::shared_ptr<AVBuffer> &inputBuffer, int32_t trackIndex,
59         StreamType streamType, sptr<AVBufferQueueProducer> inputBufferQueue);
60     void OnTransCoderBufferFilled(std::shared_ptr<AVBuffer> &inputBuffer, int32_t trackIndex,
61         StreamType streamType, sptr<AVBufferQueueProducer> inputBufferQueue);
62     void SetFaultEvent(const std::string &errMsg);
63     void SetFaultEvent(const std::string &errMsg, int32_t ret);
64     const std::string &GetContainerFormat(Plugins::OutputFormat format);
65     void SetCallingInfo(int32_t appUid, int32_t appPid, const std::string &bundleName, uint64_t instanceId);
66 
67 private:
68     std::string name_;
69 
70     std::shared_ptr<EventReceiver> eventReceiver_;
71     std::shared_ptr<FilterCallback> filterCallback_;
72 
73     std::shared_ptr<MediaMuxer> mediaMuxer_;
74 
75     int32_t preFilterCount_{0};
76     int32_t startCount_{0};
77     int32_t stopCount_{0};
78     int32_t eosCount_{0};
79     std::map<int32_t, int64_t> bufferPtsMap_;
80     std::map<std::string, int32_t> trackIndexMap_;
81     std::string videoCodecMimeType_;
82     std::string audioCodecMimeType_;
83     std::string metaDataCodecMimeType_;
84     std::string bundleName_;
85     uint64_t instanceId_{0};
86     int32_t appUid_ {0};
87     int32_t appPid_ {0};
88     Plugins::OutputFormat outputFormat_{Plugins::OutputFormat::DEFAULT};
89 
90     int64_t lastVideoPts_{0};
91     int64_t lastAudioPts_{0};
92     bool videoIsEos{false};
93     bool audioIsEos{false};
94     bool isTransCoderMode{false};
95 
96     std::mutex stopMutex_;
97     std::condition_variable stopCondition_;
98 };
99 } // namespace Pipeline
100 } // namespace MEDIA
101 } // namespace OHOS
102 #endif // FILTERS_MUXER_FILTER_H
103