[172] | 1 | //codecfilter_cmd.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | |
---|
[220] | 5 | #define CMDMAXLEN 1024 |
---|
| 6 | |
---|
[196] | 7 | int cf_cmd_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 8 | struct roar_stream_server * info, |
---|
| 9 | struct roar_codecfilter * filter) { |
---|
| 10 | int socks[2]; |
---|
[200] | 11 | int execed = -1; |
---|
[220] | 12 | char cmd[CMDMAXLEN+1] = {0}; |
---|
| 13 | char * tmp = NULL; |
---|
| 14 | char tb[CMDMAXLEN+1]; |
---|
| 15 | char * o = filter->options; |
---|
| 16 | int i; |
---|
| 17 | |
---|
| 18 | if ( !o ) |
---|
| 19 | return -1; |
---|
| 20 | |
---|
| 21 | for (i = 0; i < CMDMAXLEN && *o != 0; i++, o++) { |
---|
| 22 | if ( *o == '%' ) { |
---|
| 23 | // printf("ol: *o='%c' (escape)\n", *o); |
---|
| 24 | tmp = NULL; |
---|
| 25 | |
---|
| 26 | o++; |
---|
| 27 | if ( *o == 0 ) { |
---|
| 28 | break; |
---|
| 29 | } else if ( *o == 'R' ) { |
---|
| 30 | tmp = tb; |
---|
| 31 | snprintf(tb, CMDMAXLEN, "%i", g_sa->rate); |
---|
| 32 | } else if ( *o == 'B' ) { |
---|
| 33 | tmp = tb; |
---|
| 34 | snprintf(tb, CMDMAXLEN, "%i", g_sa->bits); |
---|
| 35 | } else if ( *o == 'C' ) { |
---|
| 36 | tmp = tb; |
---|
| 37 | snprintf(tb, CMDMAXLEN, "%i", g_sa->channels); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | // printf("*o='%c', tmp=%p\n", *o, tmp); |
---|
| 41 | |
---|
| 42 | if ( tmp ) { |
---|
| 43 | for (; i < CMDMAXLEN && *tmp != 0; i++, tmp++) |
---|
| 44 | cmd[i] = *tmp; |
---|
| 45 | i--; |
---|
| 46 | } |
---|
| 47 | } else { |
---|
| 48 | // printf("ol: *o='%c' (copy to cmd[i=%i])\n", *o, i); |
---|
| 49 | cmd[i] = *o; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | cmd[i+1] = 0; |
---|
| 54 | |
---|
[223] | 55 | //printf("cmd='%s'\n", cmd); |
---|
[196] | 56 | |
---|
| 57 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
| 58 | return -1; |
---|
| 59 | } |
---|
| 60 | |
---|
[220] | 61 | if ( lib_run_bg(cmd, ((struct roar_stream*)info)->fh, socks[1], ROAR_STDERR, socks, 2) == -1 ) |
---|
[196] | 62 | return -1; |
---|
| 63 | |
---|
[200] | 64 | if ( info->client != -1 ) { |
---|
| 65 | execed = g_clients[info->client]->execed; |
---|
| 66 | |
---|
| 67 | if ( execed != -1 ) { |
---|
| 68 | if ( g_streams[execed] == info ) { |
---|
| 69 | g_clients[info->client]->fh = socks[0]; |
---|
| 70 | } else { |
---|
| 71 | close(((struct roar_stream*)info)->fh); |
---|
| 72 | } |
---|
| 73 | } else { |
---|
| 74 | close(((struct roar_stream*)info)->fh); |
---|
| 75 | } |
---|
| 76 | } else { |
---|
| 77 | close(((struct roar_stream*)info)->fh); |
---|
| 78 | } |
---|
| 79 | |
---|
[196] | 80 | ((struct roar_stream*)info)->fh = socks[0]; |
---|
| 81 | ((struct roar_stream*)info)->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 82 | close(socks[1]); |
---|
| 83 | |
---|
| 84 | info->codecfilter = -1; |
---|
| 85 | |
---|
| 86 | return 0; |
---|
| 87 | } |
---|
| 88 | |
---|
[172] | 89 | //ll |
---|