-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgstServer.c
155 lines (120 loc) · 3.54 KB
/
gstServer.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
/******************************************************************************************
* gstServer.c *
* Description: This file will setup gstreamer on the server side and stream audio to the *
* client.
* External globals: None *
* Author: James Sleeman *
* This is based on the Gstreamer hello world application which cam be found here: *
* http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-helloworld.html
*****************************************************************************************/
#include <string.h>
#include <gst/gst.h>
#include <glib.h>
#include <linux/limits.h> // not sure which one i need for MAX_PATH
#include <limits.h>
//#define STANDALONE 1
int port;
char ip[16];
GMainLoop *loop;
GstElement *source, *pipeline;
static gboolean 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;
}
#ifdef STANDALONE
int main (int argc, char *argv[])
#else
int gstServer(int port_in, char * ip_in, char * path_in)
#endif
{
char path[100];
port = port_in;
strcpy(ip,ip_in);
strcpy(path,path_in);
GstElement *sink;
GstBus *bus;
/* Initialisation */
gst_init (NULL, NULL);
loop = g_main_loop_new (NULL, FALSE);
/* Create gstreamer elements */
pipeline = gst_pipeline_new ("client");
source = gst_element_factory_make ("filesrc", "file-source");
sink = gst_element_factory_make ("tcpclientsink", "client");
if (!pipeline || !source || !sink)
{
g_printerr ("One element could not be created. Exiting.\n");
return -1;
}
/* Set up the pipeline */
/* we set the input filename to the source element */
#ifdef STANDALONE
g_object_set (G_OBJECT (source), "location", argv[1], NULL);
#else
g_object_set (G_OBJECT (source), "location", path, NULL);
#endif
g_object_set (G_OBJECT (source), "host", ip, NULL);
g_object_set (G_OBJECT (source), "port", port, NULL);
/* we add a message handler */
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
/* we add all elements into the pipeline */
/* source | sink */
gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
/* we link the elements together */
/* source -> sink */
gst_element_link (source, sink);
/* Set the pipeline to "playing" state*/
#ifdef STANDALONE
g_print ("Now playing: %s\n", argv[1]);
#else
g_print ("Now playing: %s\n", path);
#endif
gst_element_set_state (pipeline, GST_STATE_PLAYING);
/* Iterate */
g_print ("Running...\n");
g_main_loop_run (loop);
/* Out of the main loop, clean up nicely */
g_print ("Returned, stopping playback\n");
gst_element_set_state (pipeline, GST_STATE_NULL);
g_print ("Deleting pipline\n");
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}
void killGst()
{
g_main_loop_quit(loop);
}
void playGst()
{
gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
/*void stopGst()
{
gst_element_set_state(pipeline, GST_STATE_STOPPED);
}*/
void setPathGst(char * path)
{
g_object_set (G_OBJECT (source), "location", path, NULL);
}