forked from mitchslade/Museum-Audio-Distribution-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMulticastStreamingTestingServer.c
222 lines (186 loc) · 6.5 KB
/
MulticastStreamingTestingServer.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*26-02-2015 Multiple Streaming Testing Server*/
/*By Lee Jackson - Developed from the GStreamer Online Tutorials*/
/*Includes Addition of Emergency Alarm Option*/
#include <string.h>
#include <gst/gst.h>
#define DEVICELIMIT 9999
#define HOSTIP1 "192.168.1.93"
#define HOSTIP2 "192.168.1.94"
typedef struct _CustomData {
GstElement *pipeline;
GstElement *audio_sink;
GstElement *file_src;
GMainLoop *loop;
gboolean playing; /* Playing or Paused */
gdouble rate; /* Current playback rate (can be negative) */
} CustomData;
CustomData data[DEVICELIMIT];
int track = 0;
/*Event to build pipeline and start playing*/
int buildPipeline (char* FilePath, char* HostName, int DeviceID)
{
int ret;
if (data[DeviceID].pipeline != NULL)
{
gst_element_set_state (data[DeviceID].pipeline, GST_STATE_NULL);
gst_object_unref (data[DeviceID].audio_sink);
gst_object_unref(data[DeviceID].file_src);
gst_object_unref (data[DeviceID].pipeline);
}
data[DeviceID].pipeline = gst_parse_launch ("filesrc name=file-src ! decodebin ! audioconvert ! audio/x-raw-int,channels=1,depth=16,width=16,rate=44100 ! rtpL16pay ! udpsink name=udp-sink ", NULL);
/*Get udp sink and file source elements*/
data[DeviceID].audio_sink = gst_bin_get_by_name (GST_BIN(data[DeviceID].pipeline), "udp-sink");
data[DeviceID].file_src = gst_bin_get_by_name (GST_BIN(data[DeviceID].pipeline), "file-src");
/*Configure UDP Sink*/
g_object_set (G_OBJECT (data[DeviceID].audio_sink), "host", HostName, NULL);
g_object_set (G_OBJECT (data[DeviceID].audio_sink), "port", 5000, NULL);
/*Set File Source*/
g_object_set (G_OBJECT (data[DeviceID].file_src), "location", FilePath, NULL);
/* Start playing */
ret = gst_element_set_state (data[DeviceID].pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (data[DeviceID].pipeline);
return -1;
}
data[DeviceID].playing = TRUE;
data[DeviceID].rate = 1.0;
}
void togglePlayPause(char* Host, int DeviceID)
{
data[DeviceID].playing = !data[DeviceID].playing;
gst_element_set_state (data[DeviceID].pipeline, data[DeviceID].playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);
g_print ("Setting state to %s\n", data[DeviceID].playing ? "PLAYING" : "PAUSE");
}
/* Send seek event to change rate */
static void send_seek_event (CustomData *data) {
gint64 position;
GstFormat format = GST_FORMAT_TIME;
GstEvent *seek_event;
/* Obtain the current position, needed for the seek event */
if (!gst_element_query_position (data->pipeline, &format, &position)) {
g_printerr ("Unable to retrieve current position.\n");
return;
}
/* Create the seek event */
if (data->rate > 0) {
seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET, -1);
} else {
seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, position);
}
if (data->audio_sink == NULL) {
/* If we have not done so, obtain the sink through which we will send the seek events */
data->audio_sink = gst_bin_get_by_name (GST_BIN(data->pipeline), "udp-sink");
}
/* Send the event */
gst_element_send_event (data->audio_sink, seek_event);
g_print ("Current rate: %g\n", data->rate);
}
/* Process keyboard input */
static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData data[]) {
gchar *str = NULL;
int ret;
if (g_io_channel_read_line (source, &str, NULL, NULL, NULL) != G_IO_STATUS_NORMAL) {
return TRUE;
}
switch (g_ascii_tolower (str[0])) {
case 'p':
togglePlayPause(HOSTIP1, 1);
break;
case 'q':
togglePlayPause(HOSTIP2, 2);
break;
case 'u':
if (g_ascii_isupper (str[0])) {
data[1].rate *= 1.5;
} else {
data[1].rate /= 1.5;
}
send_seek_event (&data[1]);
break;
case 's':
if (g_ascii_isupper (str[0])) {
data[2].rate *= 1.5;
} else {
data[2].rate /= 1.5;
}
send_seek_event (&data[2]);
break;
case 'd':
data->rate *= -1.0;
send_seek_event (&data[1]);
break;
case 'n':
switch(track){
case 0:
ret = buildPipeline("/home/lee/EmbeddedProject/TestSong3.ogg", HOSTIP1, 1);
track++;
break;
case 1:
ret = buildPipeline("/home/lee/EmbeddedProject/TestSong.ogg", HOSTIP1, 1);
track = 0;
break;
}
break;
case 'f;':
ret = buildPipeline("/home/lee/EmbeddedProject/Alarm.ogg", HOSTIP1, 1);
ret = buildPipeline("/home/lee/EmbeddedProject/Alarm.ogg", HOSTIP2, 2);
break;
default:
break;
}
g_free (str);
return TRUE;
}
int main(int argc, char *argv[]) {
GstElement *sink;
GstStateChangeReturn ret;
GIOChannel *io_stdin;
GstPad *pad;
int i;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Initialize our data structure */
memset (&data, 0, sizeof (data));
/* Print usage map */
g_print (
"USAGE: Choose one of the following options, then press enter:\n"
" 'P' to toggle between PAUSE and PLAY(Device 1)\n"
" 'Q' to toggle between PAUSE and PLAY (Device 2)\n"
" 'U' to increase playback speed, 'u' to decrease playback speed (Device 1)\n"
" 'S' to increase playback speed, 's' to decrease playback speed (Device 2)\n"
" 'D' to toggle playback direction\n"
" 'N' to move to next frame (in the current direction, better in PAUSE)\n"
);
/* Call the Pipeline Build Function for both devices*/
ret = buildPipeline("/home/lee/EmbeddedProject/TestSong.ogg", HOSTIP1, 1);
ret = buildPipeline("/home/lee/EmbeddedProject/TestSong2.ogg", HOSTIP2, 2);
/* Add a keyboard watch so we get notified of keystrokes */
#ifdef _WIN32
io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
#else
io_stdin = g_io_channel_unix_new (fileno (stdin));
#endif
g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
/* Create a GLib Main Loop and set it to run */
/*Loop for all Devices*/
for(i=0; i<=DEVICELIMIT; i++)
{
data[i].loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (data[i].loop);
}
/* Free resources */
/*Loop for all devices*/
for(i=0; i<=DEVICELIMIT; i++)
{
g_main_loop_unref (data[i].loop);
g_io_channel_unref (io_stdin);
gst_element_set_state (data[i].pipeline, GST_STATE_NULL);
if (data[i].audio_sink != NULL)
gst_object_unref (data[i].audio_sink);
gst_object_unref (data[i].pipeline);
}
return 0;
}