• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 AVMETA_BUFFER_BLOCKER_H
17 #define AVMETA_BUFFER_BLOCKER_H
18 
19 #include <vector>
20 #include <mutex>
21 #include <functional>
22 #include <memory>
23 #include <gst/gst.h>
24 #include "nocopyable.h"
25 
26 namespace OHOS {
27 namespace Media {
28 /**
29  * Utility for easy to process the work that block buffer on one element's pads.
30  * Only available for avmeta_meta_collector.
31  */
32 class AVMetaBufferBlocker : public std::enable_shared_from_this<AVMetaBufferBlocker>, public NoCopyable {
33 public:
34     using BufferRecievedNotifier = std::function<void(void)>;
35     // direction == true means block srcpads's buffer
36     AVMetaBufferBlocker(GstElement &elem, bool direction, BufferRecievedNotifier notifier);
37     ~AVMetaBufferBlocker();
38 
39     void Init();
40     bool IsRemoved();
41     bool IsBufferDetected();
42     void Remove();
43     void Hide();
44     std::string GetElemName();
45 
46 private:
47     static GstPadProbeReturn BlockCallback(GstPad *pad, GstPadProbeInfo *info, gpointer usrdata);
48     static void PadAdded(GstElement *elem, GstPad *pad, gpointer userData);
49     GstPadProbeReturn OnBlockCallback(GstPad &pad, GstPadProbeInfo &info);
50     void OnPadAdded(GstElement &elem, GstPad &pad);
51     bool CheckUpStreamBlocked(GstPad &pad);
52     void AddPadProbe(GstPad &pad, GstPadProbeType type);
53     bool CheckBufferDetected(GstPadProbeInfo &info);
54 
55     struct PadInfo {
56         GstPad *pad;
57         gulong probeId;
58         bool detected;
59         bool blocked;
60     };
61 
62     std::mutex mutex_;
63     bool isHidden_ = false;
64     bool isRemoved_ = false;
65     GstPadProbeReturn probeRet_ = GST_PAD_PROBE_PASS;
66     std::vector<PadInfo> padInfos_;
67     GstElement &elem_;
68     gulong signalId_ = 0;
69     bool direction_;
70     BufferRecievedNotifier notifier_;
71 };
72 } // namespace Media
73 } // namespace OHOS
74 #endif // AVMETA_BUFFER_BLOCKER_H
75