[0] | 1 | //roard.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | |
---|
[60] | 5 | char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address |
---|
| 6 | |
---|
[0] | 7 | void usage (void) { |
---|
| 8 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
| 9 | |
---|
| 10 | printf("Audio Options:\n\n"); |
---|
| 11 | printf( |
---|
| 12 | " -R --rate RATE - Set server rate\n" |
---|
| 13 | " -B --bits BITS - Set server bits\n" |
---|
| 14 | " -C --chans CHANNELS - Set server channels\n" |
---|
| 15 | ); |
---|
| 16 | |
---|
| 17 | printf("\nDriver Options:\n\n"); |
---|
| 18 | printf(" -d --driver DRV - Set the driver, use '-d list' to get a list (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
| 19 | printf(" -D --device DEV - Set the device\n"); |
---|
| 20 | printf(" -dO OPTS - Set output options\n"); |
---|
| 21 | |
---|
| 22 | printf("\nSource Options:\n\n"); |
---|
| 23 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
| 24 | " -S DEV - Use DEV as input device\n" |
---|
| 25 | " -sO OPTS - Use OPTS as input options\n" |
---|
| 26 | " -sP - Make souce as primary\n" |
---|
| 27 | ); |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | printf("\nServer Options:\n\n"); |
---|
| 31 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
| 32 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
| 33 | " -p --port - TCP Port to bind to\n" |
---|
| 34 | " -b --bind - IP/Hostname to bind to\n" |
---|
| 35 | " -s --sock - Filename for UNIX Domain Socket\n" |
---|
[60] | 36 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: audio)\n" |
---|
| 37 | " You need the permittions to change the GID\n" |
---|
[0] | 38 | ); |
---|
| 39 | // printf("\n Options:\n\n"); |
---|
| 40 | printf("\n"); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int main (int argc, char * argv[]) { |
---|
| 44 | int i; |
---|
| 45 | char * k; |
---|
[60] | 46 | char user_sock[80] = {0}; |
---|
[0] | 47 | struct roar_audio_info sa; |
---|
| 48 | char * driver = NULL; |
---|
| 49 | char * device = NULL; |
---|
| 50 | char * opts = NULL; |
---|
[60] | 51 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[0] | 52 | int port = ROAR_DEFAULT_PORT; |
---|
| 53 | int drvid; |
---|
| 54 | char * s_dev = NULL; |
---|
| 55 | char * s_con = NULL; |
---|
| 56 | char * s_opt = NULL; |
---|
| 57 | int s_prim = 0; |
---|
[60] | 58 | char * sock_grp = "audio"; |
---|
| 59 | struct group * grp; |
---|
[0] | 60 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 61 | struct roar_client * self = NULL; |
---|
[0] | 62 | |
---|
| 63 | g_listen_socket = -1; |
---|
| 64 | g_standby = 0; |
---|
| 65 | |
---|
| 66 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 67 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 68 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 69 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 70 | |
---|
| 71 | g_sa = &sa; |
---|
| 72 | |
---|
[60] | 73 | |
---|
| 74 | if ( getuid() != 0 && getenv("HOME") ) { |
---|
| 75 | snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
| 76 | server = user_sock; |
---|
| 77 | } |
---|
| 78 | |
---|
[63] | 79 | if ( clients_init() == -1 ) { |
---|
| 80 | ROAR_ERR("Can not init clients!"); |
---|
| 81 | return 1; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if ( streams_init() == -1 ) { |
---|
| 85 | ROAR_ERR("Can not init streams!"); |
---|
| 86 | return 1; |
---|
| 87 | } |
---|
| 88 | |
---|
[64] | 89 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 90 | ROAR_ERR("Can not create self client!"); |
---|
| 91 | return 1; |
---|
| 92 | } |
---|
| 93 | |
---|
[0] | 94 | if ( sources_init() == -1 ) { |
---|
| 95 | ROAR_ERR("Can not init sources!"); |
---|
| 96 | return 1; |
---|
| 97 | } |
---|
| 98 | |
---|
[64] | 99 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 100 | ROAR_ERR("Can not init set source client!"); |
---|
| 101 | return 1; |
---|
| 102 | } |
---|
| 103 | |
---|
[0] | 104 | for (i = 1; i < argc; i++) { |
---|
| 105 | k = argv[i]; |
---|
| 106 | |
---|
| 107 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 108 | usage(); |
---|
| 109 | return 0; |
---|
| 110 | |
---|
| 111 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 112 | sa.rate = atoi(argv[++i]); |
---|
| 113 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 114 | sa.bits = atoi(argv[++i]); |
---|
| 115 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 116 | sa.channels = atoi(argv[++i]); |
---|
| 117 | |
---|
| 118 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 119 | driver = argv[++i]; |
---|
| 120 | if ( strcmp(driver, "list") == 0 ) { |
---|
| 121 | print_driverlist(); |
---|
| 122 | } |
---|
| 123 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 124 | device = argv[++i]; |
---|
| 125 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 126 | opts = argv[++i]; |
---|
| 127 | |
---|
| 128 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
| 129 | k = argv[++i]; |
---|
| 130 | if ( sources_add(k, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 131 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, k); |
---|
| 132 | } |
---|
| 133 | s_opt = s_dev = s_con = NULL; |
---|
| 134 | s_prim = 0; |
---|
| 135 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
| 136 | s_dev = argv[++i]; |
---|
| 137 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
| 138 | s_opt = argv[++i]; |
---|
| 139 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
| 140 | s_con = argv[++i]; |
---|
| 141 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
| 142 | s_prim = 1; |
---|
| 143 | |
---|
| 144 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
| 145 | port = atoi(argv[++i]); |
---|
| 146 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 ) { |
---|
| 147 | server = argv[++i]; |
---|
[62] | 148 | } else if ( strcmp(k, "-t") == 0 ) { |
---|
| 149 | server = ROAR_DEFAULT_HOST; |
---|
| 150 | } else if ( strcmp(k, "-u") == 0 ) { |
---|
| 151 | // ignore this case as it is the default behavor. |
---|
[60] | 152 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
| 153 | sock_grp = argv[++i]; |
---|
[0] | 154 | |
---|
| 155 | } else { |
---|
| 156 | usage(); |
---|
| 157 | return 1; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 163 | |
---|
| 164 | if ( (g_listen_socket = roar_socket_listen(ROAR_SOCKET_TYPE_UNKNOWN, server, port)) == -1 ) { |
---|
| 165 | ROAR_ERR("Can not open listen socket!"); |
---|
| 166 | return 1; |
---|
| 167 | } |
---|
| 168 | |
---|
[60] | 169 | if ( *server == '/' ) { |
---|
| 170 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 171 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 172 | } else { |
---|
| 173 | chown(server, -1, grp->gr_gid); |
---|
| 174 | if ( getuid() == 0 ) |
---|
| 175 | chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
[0] | 179 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 180 | ROAR_ERR("Can not init output buffer!"); |
---|
| 181 | return 1; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 185 | ROAR_ERR("Can not open output driver!"); |
---|
| 186 | return 1; |
---|
| 187 | } |
---|
| 188 | |
---|
[44] | 189 | if ( samples_init() == -1 ) { |
---|
| 190 | ROAR_ERR("Can not init samples!"); |
---|
| 191 | return 1; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
[0] | 195 | signal(SIGINT, on_sig_int); |
---|
| 196 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
| 197 | |
---|
| 198 | |
---|
[39] | 199 | clients_set_pid(g_self_client, getpid()); |
---|
| 200 | clients_get(g_self_client, &self); |
---|
[37] | 201 | |
---|
[39] | 202 | if ( self == NULL ) { |
---|
| 203 | ROAR_ERR("Can not get self client!"); |
---|
| 204 | return 1; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | strcpy(self->name, "RoarAudio demon internal"); |
---|
[0] | 208 | // start main loop... |
---|
| 209 | main_loop(drvid, drvinst, &sa); |
---|
| 210 | |
---|
| 211 | // clean up. |
---|
| 212 | clean_quit_prep(); |
---|
| 213 | driver_close(drvinst, drvid); |
---|
| 214 | output_buffer_free(); |
---|
| 215 | |
---|
| 216 | return 0; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | void clean_quit_prep (void) { |
---|
| 220 | close(g_listen_socket); |
---|
[60] | 221 | |
---|
| 222 | if ( *server == '/' ) |
---|
| 223 | unlink(server); |
---|
| 224 | |
---|
| 225 | |
---|
[0] | 226 | sources_free(); |
---|
| 227 | streams_free(); |
---|
| 228 | clients_free(); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | void clean_quit (void) { |
---|
| 232 | clean_quit_prep(); |
---|
| 233 | // driver_close(drvinst, drvid); |
---|
| 234 | // output_buffer_free(); |
---|
| 235 | exit(0); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | //ll |
---|