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 | #ifndef ROAR_WITHOUT_CF_CMD |
---|
28 | |
---|
29 | #define CMDMAXLEN 1024 |
---|
30 | |
---|
31 | int cf_cmd_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
32 | struct roar_stream_server * info, |
---|
33 | struct roar_codecfilter * filter) { |
---|
34 | int socks[2]; |
---|
35 | int execed = -1; |
---|
36 | char cmd[CMDMAXLEN+1] = {0}; |
---|
37 | char * tmp = NULL; |
---|
38 | char tb[CMDMAXLEN+1]; |
---|
39 | char * o = filter->options; |
---|
40 | int i; |
---|
41 | |
---|
42 | if ( !o ) |
---|
43 | return -1; |
---|
44 | |
---|
45 | for (i = 0; i < CMDMAXLEN && *o != 0; i++, o++) { |
---|
46 | if ( *o == '%' ) { |
---|
47 | // printf("ol: *o='%c' (escape)\n", *o); |
---|
48 | tmp = NULL; |
---|
49 | |
---|
50 | o++; |
---|
51 | if ( *o == 0 ) { |
---|
52 | break; |
---|
53 | } else if ( *o == 'R' ) { |
---|
54 | tmp = tb; |
---|
55 | snprintf(tb, CMDMAXLEN, "%i", g_sa->rate); |
---|
56 | } else if ( *o == 'B' ) { |
---|
57 | tmp = tb; |
---|
58 | snprintf(tb, CMDMAXLEN, "%i", g_sa->bits); |
---|
59 | } else if ( *o == 'C' ) { |
---|
60 | tmp = tb; |
---|
61 | snprintf(tb, CMDMAXLEN, "%i", g_sa->channels); |
---|
62 | } |
---|
63 | |
---|
64 | // printf("*o='%c', tmp=%p\n", *o, tmp); |
---|
65 | |
---|
66 | if ( tmp ) { |
---|
67 | for (; i < CMDMAXLEN && *tmp != 0; i++, tmp++) |
---|
68 | cmd[i] = *tmp; |
---|
69 | i--; |
---|
70 | } |
---|
71 | } else { |
---|
72 | // printf("ol: *o='%c' (copy to cmd[i=%i])\n", *o, i); |
---|
73 | cmd[i] = *o; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | cmd[i+1] = 0; |
---|
78 | |
---|
79 | //printf("cmd='%s'\n", cmd); |
---|
80 | |
---|
81 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
82 | return -1; |
---|
83 | } |
---|
84 | |
---|
85 | if ( lib_run_bg(cmd, ROAR_STREAM(info)->fh, socks[1], ROAR_STDERR, socks, 2) == -1 ) |
---|
86 | return -1; |
---|
87 | |
---|
88 | if ( info->client != -1 ) { |
---|
89 | execed = g_clients[info->client]->execed; |
---|
90 | |
---|
91 | if ( execed != -1 ) { |
---|
92 | if ( g_streams[execed] == info ) { |
---|
93 | g_clients[info->client]->fh = socks[0]; |
---|
94 | } else { |
---|
95 | close(ROAR_STREAM(info)->fh); |
---|
96 | } |
---|
97 | } else { |
---|
98 | close(ROAR_STREAM(info)->fh); |
---|
99 | } |
---|
100 | } else { |
---|
101 | close(ROAR_STREAM(info)->fh); |
---|
102 | } |
---|
103 | |
---|
104 | ROAR_STREAM(info)->fh = socks[0]; |
---|
105 | ROAR_STREAM(info)->info.codec = ROAR_CODEC_DEFAULT; |
---|
106 | close(socks[1]); |
---|
107 | |
---|
108 | info->codecfilter = -1; |
---|
109 | |
---|
110 | return 0; |
---|
111 | } |
---|
112 | |
---|
113 | #endif |
---|
114 | |
---|
115 | //ll |
---|