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
223
224
|
/*
* mur.sat
*
* Somewhen in the year 2012, mur.at will have a nano satellite launched
* into a low earth orbit (310 km above the surface of our planet). The
* satellite itself is a TubeSat personal satellite kit, developed and
* launched by interorbital systems. mur.sat is a joint venture of mur.at,
* ESC im Labor and realraum.
*
* Please visit the project hompage at sat.mur.at for further information.
*
*
* Copyright (C) 2011 Christian Pointner <equinox@mur.at>
*
* This file is part of mur.sat.
*
* mur.sat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* mur.sat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mur.sat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <gst/gst.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
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;
}
struct thread_param {
GstElement* element;
GMainLoop* loop;
};
static GThread *reader_thread;
static gpointer reader_thread_func(gpointer data)
{
struct thread_param* param = (struct thread_param *)data;
GMainLoop* loop = param->loop;
GstElement* element = param->element;
free(param);
printf("reader thread started\n");
struct termios tio;
tcgetattr(fileno(stdin), &tio);
tio.c_lflag &= ~ICANON;
tcsetattr(fileno(stdin), TCSANOW, &tio);
int code = -1;
for(;;) {
char sign;
int ret = read(0, &sign, 1);
if(!ret) break;
if(ret < 0) {
printf("read error: %s", strerror(errno));
break;
}
GstStructure* st;
GstEvent* ev;
if(sign != 's') {
if(code != -1) {
st = gst_structure_new ("dtmf-event", "type", G_TYPE_INT, 1, "number", G_TYPE_INT, code, "start", G_TYPE_BOOLEAN, FALSE, NULL);
ev = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, st);
gst_element_send_event(element, ev);
}
code = -1;
sign=tolower(sign);
switch(sign) {
case '0': code = 0; break;
case '1': code = 1; break;
case '2': code = 2; break;
case '3': code = 3; break;
case '4': code = 4; break;
case '5': code = 5; break;
case '6': code = 6; break;
case '7': code = 7; break;
case '8': code = 8; break;
case '9': code = 9; break;
case '*': code = 10; break;
case '#': code = 11; break;
case 'a': code = 12; break;
case 'b': code = 13; break;
case 'c': code = 14; break;
case 'd': code = 15; break;
default: continue;
}
st = gst_structure_new ("dtmf-event", "type", G_TYPE_INT, 1, "number", G_TYPE_INT, code, "volume", G_TYPE_INT, 25, "start", G_TYPE_BOOLEAN, TRUE, NULL);
ev = gst_event_new_custom(GST_EVENT_CUSTOM_UPSTREAM, st);
gst_element_send_event(element, ev);
} else if(code != -1) {
st = gst_structure_new ("dtmf-event", "type", G_TYPE_INT, 1, "number", G_TYPE_INT, code, "start", G_TYPE_BOOLEAN, FALSE, NULL);
ev = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, st);
gst_element_send_event(element, ev);
code = -1;
}
}
g_main_loop_quit(loop);
return NULL;
}
int init_code_reader(GMainLoop *loop, GstElement* element)
{
g_assert(!reader_thread);
struct thread_param* p = malloc(sizeof(struct thread_param));
if(!p)
return -1;
p->element = element;
p->loop = loop;
reader_thread = g_thread_create(reader_thread_func, p, TRUE, NULL);
if(!reader_thread)
return -1;
return 0;
}
int main (int argc, char *argv[])
{
GMainLoop *loop;
GstElement *pipeline, *source, *demuxer, *decoder, *conv, *sink;
GstBus *bus;
/* Initialisation */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* Create gstreamer elements */
pipeline = gst_pipeline_new ("gst-dtmf");
source = gst_element_factory_make ("dtmfsrc", "source");
sink = gst_element_factory_make ("autoaudiosink", "output");
if (!pipeline || !source || !sink) {
g_printerr ("One element could not be created. Exiting.\n");
return -1;
}
g_object_set(G_OBJECT (source), "interval", 10, NULL);
/* Set up the pipeline */
/* 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 */
/* file-source | ogg-demuxer | vorbis-decoder | converter | alsa-output */
gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
gst_element_link_many (source, sink, NULL);
/* Set the pipeline to "playing" state*/
gst_element_set_state (pipeline, GST_STATE_PLAYING);
init_code_reader(loop, pipeline);
/* 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 pipeline\n");
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}
|