• 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 GST_MSG_PROCESSOR_H
17 #define GST_MSG_PROCESSOR_H
18 
19 #include <mutex>
20 #include <condition_variable>
21 #include <string>
22 #include <vector>
23 #include <unordered_map>
24 #include <gst/gst.h>
25 #include "inner_msg_define.h"
26 #include "task_queue.h"
27 #include "gst_msg_converter.h"
28 #include "nocopyable.h"
29 
30 namespace OHOS {
31 namespace Media {
32 struct TickCallbackInfo;
33 
34 class GstMsgProcessor : public NoCopyable {
35 public:
36     GstMsgProcessor(GstBus &gstBus, const InnerMsgNotifier &notifier,
37         const std::shared_ptr<IGstMsgConverter> &converter = nullptr);
38     ~GstMsgProcessor();
39 
40     int32_t Init();
41 
42     /**
43      * Add the gst msg filter except the error msg.
44      * The filter string will be compared with the gst msg src's name.
45      * If matched, the msg will be notified, or the msg will be ignored.
46      */
47     void AddMsgFilter(const std::string &filter);
48     void FlushBegin();
49     void FlushEnd();
50     void Reset() noexcept;
51     void AddTickSource(int32_t type, uint32_t interval);
52     void RemoveTickSourceByType(int32_t type);
53     void RemoveTickSourceAll();
54 
55 private:
56     int32_t DoInit();
57     static gboolean MainLoopRunDone(GstMsgProcessor *thiz);
58     static gboolean TickCallback(TickCallbackInfo *tickCbInfo);
59     static void FreeTickType(TickCallbackInfo *tickCbInfo);
60     static gboolean BusCallback(const GstBus *bus, GstMessage *msg, GstMsgProcessor *thiz);
61     void ProcessGstMessage(GstMessage &msg);
62     void DoReset();
63 
64     GstBus *gstBus_ = nullptr;
65     GMainLoop *mainLoop_ = nullptr;
66     GMainContext *context_ = nullptr;
67     GSource *busSource_ = nullptr;
68     InnerMsgNotifier notifier_;
69     TaskQueue guardTask_;
70     std::mutex mutex_;
71     std::condition_variable cond_;
72     bool needWaiting_ = true;
73     std::shared_ptr<IGstMsgConverter> msgConverter_;
74     std::vector<std::string> filters_;
75     std::unordered_map<int32_t, GSource *> tickSource_;
76 };
77 
78 struct TickCallbackInfo {
79     int32_t type;
80     GstMsgProcessor *msgProcessor;
81 };
82 } // namespace Media
83 } // namespace OHOS
84 #endif // GST_MSG_PROCESSOR_H