1 | //codecfilter_cmd.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | #define CMDMAXLEN 1024 |
---|
28 | |
---|
29 | int cf_cmd_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
30 | struct roar_stream_server * info, |
---|
31 | struct roar_codecfilter * filter) { |
---|
32 | int socks[2]; |
---|
33 | int execed = -1; |
---|
34 | char cmd[CMDMAXLEN+1] = {0}; |
---|
35 | char * tmp = NULL; |
---|
36 | char tb[CMDMAXLEN+1]; |
---|
37 | char * o = filter->options; |
---|
38 | int i; |
---|
39 | |
---|
40 | if ( !o ) |
---|
41 | return -1; |
---|
42 | |
---|
43 | for (i = 0; i < CMDMAXLEN && *o != 0; i++, o++) { |
---|
44 | if ( *o == '%' ) { |
---|
45 | // printf("ol: *o='%c' (escape)\n", *o); |
---|
46 | tmp = NULL; |
---|
47 | |
---|
48 | o++; |
---|
49 | if ( *o == 0 ) { |
---|
50 | break; |
---|
51 | } else if ( *o == 'R' ) { |
---|
52 | tmp = tb; |
---|
53 | snprintf(tb, CMDMAXLEN, "%i", g_sa->rate); |
---|
54 | } else if ( *o == 'B' ) { |
---|
55 | tmp = tb; |
---|
56 | snprintf(tb, CMDMAXLEN, "%i", g_sa->bits); |
---|
57 | } else if ( *o == 'C' ) { |
---|
58 | tmp = tb; |
---|
59 | snprintf(tb, CMDMAXLEN, "%i", g_sa->channels); |
---|
60 | } |
---|
61 | |
---|
62 | // printf("*o='%c', tmp=%p\n", *o, tmp); |
---|
63 | |
---|
64 | if ( tmp ) { |
---|
65 | for (; i < CMDMAXLEN && *tmp != 0; i++, tmp++) |
---|
66 | cmd[i] = *tmp; |
---|
67 | i--; |
---|
68 | } |
---|
69 | } else { |
---|
70 | // printf("ol: *o='%c' (copy to cmd[i=%i])\n", *o, i); |
---|
71 | cmd[i] = *o; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | cmd[i+1] = 0; |
---|
76 | |
---|
77 | //printf("cmd='%s'\n", cmd); |
---|
78 | |
---|
79 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
80 | return -1; |
---|
81 | } |
---|
82 | |
---|
83 | if ( lib_run_bg(cmd, ROAR_STREAM(info)->fh, socks[1], ROAR_STDERR, socks, 2) == -1 ) |
---|
84 | return -1; |
---|
85 | |
---|
86 | if ( info->client != -1 ) { |
---|
87 | execed = g_clients[info->client]->execed; |
---|
88 | |
---|
89 | if ( execed != -1 ) { |
---|
90 | if ( g_streams[execed] == info ) { |
---|
91 | g_clients[info->client]->fh = socks[0]; |
---|
92 | } else { |
---|
93 | close(ROAR_STREAM(info)->fh); |
---|
94 | } |
---|
95 | } else { |
---|
96 | close(ROAR_STREAM(info)->fh); |
---|
97 | } |
---|
98 | } else { |
---|
99 | close(ROAR_STREAM(info)->fh); |
---|
100 | } |
---|
101 | |
---|
102 | ROAR_STREAM(info)->fh = socks[0]; |
---|
103 | ROAR_STREAM(info)->info.codec = ROAR_CODEC_DEFAULT; |
---|
104 | close(socks[1]); |
---|
105 | |
---|
106 | info->codecfilter = -1; |
---|
107 | |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | //ll |
---|