1 /*
2  * Copyright (c) 2021-2021 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 HISTREAMER_PIPELINE_MUXER_FILTER_H
17 #define HISTREAMER_PIPELINE_MUXER_FILTER_H
18 #ifdef RECORDER_SUPPORT
19 #include "pipeline/core/filter_base.h"
20 #include "plugin/core/muxer.h"
21 #include "plugin/core/plugin_info.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Pipeline {
26 class DataSpliter;
27 class MuxerFilter : public FilterBase {
28 public:
29     explicit MuxerFilter(std::string name);
30     ~MuxerFilter() override;
31 
32     void Init(EventReceiver* receiver, FilterCallback* callback) override;
33 
34     bool Negotiate(const std::string& inPort,
35                    const std::shared_ptr<const Plugin::Capability>& upstreamCap,
36                    Plugin::Capability& negotiatedCap,
37                    const Plugin::Meta& upstreamParams,
38                    Plugin::Meta& downstreamParams) override;
39 
40     bool Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta,
41                    Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams) override;
42 
43     ErrorCode SetOutputFormat(std::string containerMime);
44     ErrorCode AddTrack(std::shared_ptr<InPort>& trackPort);
45     ErrorCode SetMaxDuration(uint64_t maxDuration);
46     ErrorCode SetMaxSize(uint64_t maxSize);
47     ErrorCode StartNextSegment();
48     ErrorCode SendEos();
49     ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override;
50     ErrorCode Start() override;
51 private:
52     class MuxerDataSink : public Plugin::DataSinkHelper {
53     public:
54         Plugin::Status WriteAt(int64_t offset, const std::shared_ptr<Plugin::Buffer>& buffer) override;
55         MuxerFilter* muxerFilter_;
56     };
57 
58     struct TrackInfo {
59         int32_t trackId;
60         std::string inPort;
61         bool eos;
62     };
63 
64     int32_t GetTrackIdByInPort(const std::shared_ptr<InPort>& inPort);
65     int32_t UpdateTrackIdOfInPort(const std::shared_ptr<InPort>& inPort, int32_t trackId);
66 
67     bool UpdateAndInitPluginByInfo(const std::shared_ptr<Plugin::PluginInfo>& selectedPluginInfo);
68 
69     ErrorCode ConfigureToStart();
70     ErrorCode AddTrackThenConfigure(const std::pair<std::string, Plugin::Meta>& metaPair);
71 
72     bool AllTracksEos();
73     void UpdateEosState(const std::string& inPort);
74 
75     std::string containerMime_ {};
76     std::vector<TrackInfo> trackInfos_ {};
77     std::shared_ptr<Plugin::Muxer> plugin_ {};
78     std::shared_ptr<Plugin::PluginInfo> targetPluginInfo_ {nullptr};
79     std::shared_ptr<DataSpliter> dataSpliter_{};
80     std::vector<std::pair<std::string, Capability>> capabilityCache_ {};
81     std::vector<std::pair<std::string, Plugin::Meta>> metaCache_ {};
82     bool hasWriteHeader_ {false};
83     std::shared_ptr<MuxerDataSink> muxerDataSink_;
84 
85     OSAL::Mutex pushDataMutex_;
86     bool eos_ {false};
87     std::atomic<int> eosTrackCnt {0};
88 };
89 } // Pipeline
90 } // Media
91 } // OHOS
92 #endif
93 #endif // HISTREAMER_PIPELINE_MUXER_FILTER_H
94 
95