1 | //simple.c: |
---|
2 | |
---|
3 | #include "libroar.h" |
---|
4 | |
---|
5 | int roar_simple_connect (struct roar_connection * con, char * server, char * name) { |
---|
6 | |
---|
7 | ROAR_DBG("roar_simple_connect(*): trying to connect..."); |
---|
8 | |
---|
9 | if ( roar_connect(con, server) == -1 ) { |
---|
10 | ROAR_DBG("roar_simple_connect(*): roar_connect() faild!"); |
---|
11 | return -1; |
---|
12 | } |
---|
13 | |
---|
14 | if ( roar_identify(con, name) == -1 ) { |
---|
15 | ROAR_DBG("roar_simple_connect(*): roar_identify() faild!"); |
---|
16 | return -1; |
---|
17 | } |
---|
18 | |
---|
19 | if ( roar_auth(con) == -1 ) { |
---|
20 | ROAR_DBG("roar_simple_connect(*): roar_auth() faild!"); |
---|
21 | return -1; |
---|
22 | } |
---|
23 | |
---|
24 | return 0; |
---|
25 | } |
---|
26 | |
---|
27 | int roar_simple_stream(int rate, int channels, int bits, int codec, char * server, int dir, char * name) { |
---|
28 | struct roar_connection con; |
---|
29 | struct roar_stream s; |
---|
30 | |
---|
31 | if ( roar_simple_connect(&con, server, name) == -1 ) { |
---|
32 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
33 | return -1; |
---|
34 | } |
---|
35 | |
---|
36 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
37 | roar_disconnect(&con); |
---|
38 | return -1; |
---|
39 | } |
---|
40 | |
---|
41 | if ( roar_stream_connect(&con, &s, dir) == -1 ) { |
---|
42 | roar_disconnect(&con); |
---|
43 | return -1; |
---|
44 | } |
---|
45 | |
---|
46 | if ( roar_stream_exec(&con, &s) == -1 ) { |
---|
47 | roar_disconnect(&con); |
---|
48 | return -1; |
---|
49 | } |
---|
50 | |
---|
51 | if ( dir == ROAR_DIR_PLAY ) { |
---|
52 | shutdown(con.fh, SHUT_RD); |
---|
53 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
54 | shutdown(con.fh, SHUT_WR); |
---|
55 | } |
---|
56 | |
---|
57 | return con.fh; |
---|
58 | } |
---|
59 | |
---|
60 | int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) { |
---|
61 | struct roar_stream s; |
---|
62 | return roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir); |
---|
63 | } |
---|
64 | |
---|
65 | int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) { |
---|
66 | struct roar_message mes; |
---|
67 | char file[80]; |
---|
68 | int fh = -1, listen; |
---|
69 | static int count = 0; |
---|
70 | struct group * grp = NULL; |
---|
71 | int type = ROAR_SOCKET_TYPE_UNIX; |
---|
72 | int port = 0; |
---|
73 | int opt = 1; |
---|
74 | struct sockaddr_in socket_addr; |
---|
75 | socklen_t len = sizeof(struct sockaddr_in); |
---|
76 | |
---|
77 | if ( getsockname(con->fh, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
78 | return -1; |
---|
79 | } |
---|
80 | |
---|
81 | if ( socket_addr.sin_family == AF_INET ) { |
---|
82 | type = ROAR_SOCKET_TYPE_INET; |
---|
83 | } else if ( socket_addr.sin_family == AF_UNIX ) { |
---|
84 | type = ROAR_SOCKET_TYPE_UNIX; |
---|
85 | } else if ( socket_addr.sin_family == AF_DECnet ) { |
---|
86 | type = ROAR_SOCKET_TYPE_DECNET; |
---|
87 | } else { |
---|
88 | return -1; |
---|
89 | } |
---|
90 | |
---|
91 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
92 | sprintf(file, "/tmp/.libroar-simple-stream.%i-%i", getpid(), count++); |
---|
93 | } else if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
94 | if ( roar_socket_get_local_nodename() ) { |
---|
95 | sprintf(file, "%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++); |
---|
96 | } else { |
---|
97 | return -1; |
---|
98 | } |
---|
99 | } else { |
---|
100 | strcpy(file, inet_ntoa(socket_addr.sin_addr)); |
---|
101 | } |
---|
102 | |
---|
103 | if ( (listen = roar_socket_listen(type, file, port)) == -1 ) { |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | if ( type == ROAR_SOCKET_TYPE_INET ) { |
---|
108 | len = sizeof(struct sockaddr_in); |
---|
109 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
110 | |
---|
111 | if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
112 | return -1; |
---|
113 | } |
---|
114 | port = ROAR_NET2HOST16(socket_addr.sin_port); |
---|
115 | ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port); |
---|
116 | } else if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
117 | chmod(file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
118 | |
---|
119 | grp = getgrnam(ROAR_DEFAULT_SOCKGRP); |
---|
120 | |
---|
121 | if ( grp ) |
---|
122 | chown(file, -1, grp->gr_gid); |
---|
123 | } else if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
124 | len = sizeof(struct sockaddr_in); |
---|
125 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
126 | } |
---|
127 | |
---|
128 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | if ( roar_stream_connect(con, s, dir) == -1 ) { |
---|
133 | return -1; |
---|
134 | } |
---|
135 | |
---|
136 | if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) { |
---|
137 | |
---|
138 | if ( (fh = accept(listen, NULL, NULL)) != -1 ) { |
---|
139 | if ( dir == ROAR_DIR_PLAY ) { |
---|
140 | shutdown(fh, SHUT_RD); |
---|
141 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
142 | shutdown(fh, SHUT_WR); |
---|
143 | } |
---|
144 | } |
---|
145 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
146 | close(fh); |
---|
147 | fh = -1; |
---|
148 | } else if ( mes.cmd != ROAR_CMD_OK ) { |
---|
149 | close(fh); |
---|
150 | fh = -1; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | close(listen); |
---|
155 | |
---|
156 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
157 | unlink(file); |
---|
158 | } |
---|
159 | |
---|
160 | s->fh = fh; |
---|
161 | |
---|
162 | return fh; |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | int roar_simple_play_file(char * file, char * server, char * name) { |
---|
167 | struct roar_connection con; |
---|
168 | |
---|
169 | if ( roar_simple_connect(&con, server, name) == -1 ) { |
---|
170 | return -1; |
---|
171 | } |
---|
172 | |
---|
173 | return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one. |
---|
174 | } |
---|
175 | |
---|
176 | int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
177 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name); |
---|
178 | } |
---|
179 | |
---|
180 | int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
181 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name); |
---|
182 | } |
---|
183 | |
---|
184 | int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
185 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name); |
---|
186 | } |
---|
187 | |
---|
188 | int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
189 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | int roar_simple_close(int fh) { |
---|
194 | return close(fh); |
---|
195 | } |
---|
196 | |
---|
197 | int roar_simple_get_standby (int fh) { |
---|
198 | struct roar_connection con; |
---|
199 | |
---|
200 | con.fh = fh; |
---|
201 | |
---|
202 | return roar_get_standby(&con); |
---|
203 | } |
---|
204 | |
---|
205 | //ll |
---|