-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgst_multicast_receiver.hpp
141 lines (107 loc) · 3.65 KB
/
gst_multicast_receiver.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef GST_MULTICAST_RECEIVER_CPP
#define GST_MULTICAST_RECEIVER_CPP
#include <gstreamer-1.0/gst/gst.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <thread>
#include <string>
#include "config.hpp"
#define GST_CHECK(ptr, msg) { \
if(not ptr){ \
std::cerr << msg; \
return GST_FLOW_ERROR; \
} \
}
struct GST_Struct{
GstElement *appsink, *gst_pipeline;
GstSample* sample;
GstBuffer* buffer;
GstMapInfo map;
bool frame_initalized;
cv::Size frame_shape;
cv::Mat frame;
GST_Struct(){
appsink = nullptr;
gst_pipeline = nullptr;
sample = nullptr;
buffer = nullptr;
frame_initalized = false;
gst_init(nullptr, nullptr);
}
void StartPipeline(const char* pipeline){
gst_pipeline = gst_parse_launch(pipeline, nullptr);
g_assert(gst_pipeline);
gst_element_set_state(gst_pipeline, GST_STATE_PLAYING);
}
GstFlowReturn SetFrameInfo(){
GstStructure* info {nullptr};
GstCaps* caps {nullptr};
caps = gst_sample_get_caps (sample);
GST_CHECK(caps, "get caps is null");
info = gst_caps_get_structure (caps, 0);
GST_CHECK(info, "get info is null");
gst_structure_get_int (info, "width", &frame_shape.width);
gst_structure_get_int (info, "height", &frame_shape.height);
return GST_FLOW_OK;
}
void ReadGSTMap2Mat(){
gst_buffer_map(buffer, &map, GST_MAP_READ);
frame = cv::Mat(frame_shape, CV_8UC3, (char *)map.data, cv::Mat::AUTO_STEP);
}
static GstFlowReturn Callback(GstElement* appsink, GST_Struct* gst_self){
g_signal_emit_by_name (appsink, "pull-sample", &gst_self->sample, NULL);
gst_self->buffer = gst_sample_get_buffer (gst_self->sample);
GST_CHECK(gst_self->buffer, "get buffer is null");
if(not gst_self->frame_initalized)
{
if(gst_self->SetFrameInfo() != GST_FLOW_OK)
return GST_FLOW_ERROR;
gst_self->frame_initalized = true;
}
gst_self->ReadGSTMap2Mat();
gst_buffer_unmap(gst_self->buffer, &gst_self->map);
gst_sample_unref(gst_self->sample);
return GST_FLOW_OK;
}
void ConnectAppsink(const char* appsink_str){
appsink = gst_bin_get_by_name(GST_BIN(gst_pipeline), (const gchar*)(appsink_str));
if (not appsink){
std::cerr << "GST appsink is NULL !\n";
exit(0);
}
}
void StartCallBack(){
g_object_set (appsink, "emit-signals", TRUE, NULL);
g_signal_connect(appsink, "new-sample", G_CALLBACK(Callback), this);
}
const cv::Mat* GetFrameFromConnection(){
while (frame.empty()){
std::cerr << "Frame is not exist !\n";
std::this_thread::sleep_for(GST_PARAMS::SLEEP_TIME);
}
return &frame;
}
~GST_Struct(){
if(gst_pipeline) {
gst_element_set_state (gst_pipeline, GST_STATE_NULL);
gst_object_unref (gst_pipeline);
}
if(appsink) gst_object_unref (appsink);
}
};
class UDPMulticastReceiver{
friend struct GST_Struct;
inline static GST_Struct gst_stream;
public:
UDPMulticastReceiver() = default;
static void StartListen(const char* pipeline){
gst_stream.StartPipeline(pipeline);
gst_stream.ConnectAppsink(GST_PARAMS::appsink_name.c_str());
gst_stream.StartCallBack();
}
static cv::Mat GetFrame(){
return gst_stream.GetFrameFromConnection()->clone();
}
~UDPMulticastReceiver() = default;
};
#endif // GST_MULTICAST_RECEIVER_CPP