-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio.c
299 lines (249 loc) · 6.86 KB
/
audio.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <pulse/pulseaudio.h>
#include <pulse/glib-mainloop.h>
#include <glib.h>
#include <linux/videodev.h>
#include <sys/ioctl.h>
#include <glib/gprintf.h>
#include "fmtx-object.h"
#include "audio.h"
static void pa_connect(FmtxObject *obj);
#define WRITE_FMTX_SYSFS_PILOT(file, val) \
WRITE_FMTX_SYSFS(file, val, "fmtxd fmtx chirping error")
gboolean
idle_timeout_cb(FmtxObject *obj)
{
obj->active = 0;
obj->idle_timeout = 0;
obj->exit_timeout = g_timeout_add(60000, (GSourceFunc)exit_timeout_cb, obj);
return FALSE;
}
static gboolean
pilot_timeout_cb(FmtxObject *obj)
{
if (!obj->active &&
(!obj->mixer_inited || !obj->pa_running) &&
g_str_equal(obj->state, "enabled"))
{
fmtx_enable(obj, FALSE);
g_idle_add(emit_changed, obj);
g_idle_add(emit_info, obj);
obj->active = TRUE;
if(!obj->idle_timeout)
obj->idle_timeout = g_timeout_add_seconds(300,
(GSourceFunc)idle_timeout_cb,
obj);
}
obj->pilot_timeout = 0;
return FALSE;
}
static void
fmtx_set_mute(FmtxObject *obj, int value)
{
struct v4l2_control ctl;
ctl.id = V4L2_CID_AUDIO_MUTE;
ctl.value = value;
if(ioctl(obj->dev_radio, VIDIOC_S_CTRL, &ctl) < 0)
g_fprintf(stderr, "Could not toggle mute on the device\n");
}
int
fmtx_set_frequency(FmtxObject *fmtx, unsigned int frequency)
{
unsigned int f;
struct video_tuner tun;
GError *err = NULL;
if ( fmtx->dev_radio < 0 )
return 1;
if ( fmtx->freq_max < fmtx->freq_min )
return 0;
f = fmtx->freq_min;
while ( f <= fmtx->freq_max )
{
if ( frequency == f)
break;
f += fmtx->freq_step;
}
if (f > fmtx->freq_max)
return 0;
fmtx->frequency = f;
gconf_client_set_int(fmtx->gcclient, "/system/fmtx/frequency", f, &err);
if(err)
g_fprintf(stderr, "Could not set fmtx settings: %s\n", err->message);
if(!g_str_equal(fmtx->state, "enabled"))
return 2;
tun.tuner = 0;
if(ioctl(fmtx->dev_radio, VIDIOCSTUNER, &tun) >= 0 &&
ioctl(fmtx->dev_radio, VIDIOCGTUNER, &tun) >= 0)
{
f = rint((tun.flags & VIDEO_TUNER_LOW ? 16000.0 : 16.0) *
(long double)fmtx->frequency / 1000.0);
if(ioctl(fmtx->dev_radio, VIDIOCSFREQ, &f) >= 0)
return 2;
}
perror("fmtxd Could not set frequency");
return 1;
}
int
fmtx_enable(FmtxObject *fmtx, gboolean enable)
{
int rv;
GError *err = NULL;
if(!g_str_equal(fmtx->state, "n/a"))
{
if(enable)
{
if(!g_str_equal(fmtx->state, "enabled"))
{
if ( fmtx->offline || fmtx->hp_connected )
{
fmtx_set_mute(fmtx, TRUE);
g_free(fmtx->state);
fmtx->state = g_strdup("disabled");
return 0;
}
fmtx_set_mute(fmtx, FALSE);
rv = fmtx_set_frequency(fmtx, fmtx->frequency);
if(rv != 2)
{
fmtx_set_mute(fmtx, TRUE);
return rv;
}
g_free(fmtx->state);
fmtx->state = g_strdup("enabled");
out:
gconf_client_set_bool(fmtx->gcclient, "/system/fmtx/enabled", enable,
&err);
if(err)
g_fprintf(stderr, "Could not save fmtx settings: %s\n", err->message);
fmtx_toggle_pilot(fmtx);
fmtx_set_frequency(fmtx, fmtx->frequency);
return 2;
}
}
else
{
if(!g_str_equal(fmtx->state, "disabled"))
{
fmtx->active = FALSE;
if(fmtx->idle_timeout)
{
g_source_remove(fmtx->idle_timeout);
fmtx->idle_timeout = 0;
}
fmtx_set_mute(fmtx, TRUE);
g_free(fmtx->state);
fmtx->state = g_strdup("disabled");
goto out;
}
fmtx->active = 0;
if(fmtx->idle_timeout)
{
g_source_remove(fmtx->idle_timeout);
fmtx->idle_timeout = 0;
return 2;
}
}
return 2;
}
return 0;
}
void
fmtx_toggle_pilot(FmtxObject *fmtx)
{
if(!g_str_equal(fmtx->state, "enabled") ||
(fmtx->mixer_inited && fmtx->pa_running))
{
if(fmtx->active &&
fmtx->pa_running &&
!fmtx->offline &&
!fmtx->hp_connected &&
!fmtx->call_active)
{
if(fmtx->idle_timeout)
{
g_source_remove(fmtx->idle_timeout);
fmtx->idle_timeout = 0;
}
fmtx->active = 0;
fmtx_enable(fmtx, TRUE);
g_idle_add(emit_changed, fmtx);
g_idle_add(emit_info, fmtx);
}
WRITE_FMTX_SYSFS_PILOT("tone_frequency", "0");
WRITE_FMTX_SYSFS_PILOT("tone_deviation", "0");
WRITE_FMTX_SYSFS_PILOT("tone_off_time", "0");
WRITE_FMTX_SYSFS_PILOT("tone_on_time", "0");
}
else
{
WRITE_FMTX_SYSFS_PILOT("tone_frequency", "1760");
WRITE_FMTX_SYSFS_PILOT("tone_deviation", "6750");
WRITE_FMTX_SYSFS_PILOT("tone_off_time", "2000");
WRITE_FMTX_SYSFS_PILOT("tone_on_time", "50");
if(!fmtx->pilot_timeout)
fmtx->pilot_timeout = g_timeout_add(50000,
(GSourceFunc)pilot_timeout_cb,
fmtx);
}
}
static void
context_sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
void *userdata)
{
if(!eol)
{
((FmtxObject*)userdata)->pa_running = (i->state == PA_SINK_RUNNING);
fmtx_toggle_pilot(userdata);
}
}
static void
context_subscribe_cb(pa_context *c, pa_subscription_event_type_t t,
uint32_t idx, void *userdata)
{
pa_operation *op;
op = pa_context_get_sink_info_by_name(c, "sink.hw0", context_sink_info_cb,
userdata);
pa_operation_unref(op);
}
static void
context_state_cb(pa_context *c, void *userdata)
{
pa_context_state_t state;
pa_operation *op;
state = pa_context_get_state(c);
if(state >= PA_CONTEXT_READY)
{
if( state == PA_CONTEXT_READY)
{
pa_context_set_subscribe_callback(c, context_subscribe_cb, userdata);
op = pa_context_subscribe(c, PA_SUBSCRIPTION_MASK_SINK, 0, userdata);
pa_operation_unref(op);
op = pa_context_get_sink_info_by_name(c, "sink.hw0", context_sink_info_cb,
userdata);
pa_operation_unref(op);
}
else
pa_connect((FmtxObject *)userdata);
}
}
static void
pa_connect(FmtxObject *obj)
{
if(obj->context)
pa_context_unref(obj->context);
obj->context = pa_context_new(obj->api, "fmtx-middleware");
pa_context_set_state_callback(obj->context, context_state_cb, obj);
if(pa_context_connect(obj->context, 0,
PA_CONTEXT_NOFAIL|PA_CONTEXT_NOAUTOSPAWN, 0) < 0)
g_log(NULL, G_LOG_LEVEL_WARNING,
"Failed to connect pa server: %s",
pa_strerror(pa_context_errno(obj->context)));
}
void register_pa(FmtxObject *obj)
{
pa_glib_mainloop *m;
m = pa_glib_mainloop_new(g_main_context_default());
g_assert(m);
obj->api = pa_glib_mainloop_get_api(m);
g_assert(obj->api);
pa_connect(obj);
}