forked from embeye/QEmbeddedEye
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqstreamer.cpp
More file actions
189 lines (143 loc) · 4.85 KB
/
Copy pathqstreamer.cpp
File metadata and controls
189 lines (143 loc) · 4.85 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "qstreamer.h"
#include <string>
#include <sstream>
QStreamer::QStreamer():
clientAddr(""),
camID(-1)
{
}
bool QStreamer::createElements(){
/* Initialisation */
gst_init (NULL, NULL);
loop = g_main_loop_new (NULL, FALSE);
/* Create gstreamer elements */
pipeline = gst_pipeline_new ("video-stream");
source = gst_element_factory_make ("v4l2src", "camera-source");
payLoad = gst_element_factory_make ("rtpjpegpay","rtp jpeg payloader");
sink = gst_element_factory_make ("multiudpsink", "video-output");
if (!pipeline) {
g_printerr ("Pipeline could not be created. Exiting.\n");
return false;
}
if (!source) {
g_printerr ("source could not be created. Exiting.\n");
return false;
}
if (!payLoad) {
g_printerr ("payLoad could not be created. Exiting.\n");
return false;
}
if (!sink) {
g_printerr ("Sink could not be created. Exiting.\n");
return false;
}
return true;
}
void QStreamer::setCameraID(int camID){
if(pipeline == NULL )
this->camID = camID;
else {
GstState state;
gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
if(state == GST_STATE_PLAYING || state == GST_STATE_READY){
gst_element_set_state(pipeline,GST_STATE_NULL);
this->camID = camID;
initialize();
gst_element_set_state(pipeline,state);
}
else if(state == GST_STATE_NULL){
this->camID = camID;
}
}
std::cout<<"camera is:"<<this->camID<<std::endl;
}
void QStreamer::addClient(std::string addr){
if(pipeline == NULL ){
if(!clientAddr.empty())
clientAddr.append(",");
clientAddr.append(addr);
}
else {
GstState state;
gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
if(state == GST_STATE_PLAYING || state == GST_STATE_READY){
gst_element_set_state(pipeline,GST_STATE_NULL);
/* wait until it's changed or failed */
if (gst_element_get_state (pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
g_error ("Failed to go into NULL state");
}
if(!clientAddr.empty())
clientAddr.append(",");
clientAddr.append(addr);
initialize();
gst_element_set_state(pipeline,state);
/* wait until it's changed or failed */
if (gst_element_get_state (pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
g_error ("Failed to go into initial state");
}
}
else if(state == GST_STATE_NULL){
if(!clientAddr.empty())
clientAddr.append(",");
clientAddr.append(addr);
}
}
std::cout<<"Address is:"<<clientAddr<<std::endl;
}
void QStreamer::initialize(){
/* Set up the pipeline */
/* we set the camera device */
std::string v4lsource = "/dev/video";
std::stringstream ss;
ss << camID;
v4lsource.append( ss.str());
std::cout<<"v4lsource is:"<<v4lsource<<std::endl;
g_object_set (G_OBJECT (source), "device", v4lsource.c_str(), NULL);
/* setting udp sink parameters */
g_object_set(G_OBJECT(sink), "clients", clientAddr.c_str(), NULL);
/* set gstreamer bus */
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
bus_watch_id = gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
gst_bin_add_many (GST_BIN (pipeline), source,payLoad, sink, NULL);
GstCaps *caps = NULL;
caps = gst_caps_from_string("image/jpeg,width=960,height=540,videorate=8/1");
gst_element_link_filtered(source,payLoad,caps);
gst_caps_unref(caps);
gst_element_link_many (payLoad, sink, NULL);
}
void QStreamer::startStreaming(){
gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (gst_element_get_state (pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
g_error ("Failed to go into playing state");
}
}
void QStreamer::stopStreaming(){
gst_element_set_state (pipeline, GST_STATE_NULL);
if (gst_element_get_state (pipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE) {
g_error ("Failed to go into stop state");
}
}
gboolean QStreamer:: bus_call(GstBus *bus,GstMessage *msg,gpointer data)
{
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}