source: roaraudio/libroar/simple.c @ 505:ca6bfa6cfad1

Last change on this file since 505:ca6bfa6cfad1 was 505:ca6bfa6cfad1, checked in by phi, 16 years ago

fixed some bugs in roar_simple_new_stream_obj()

File size: 4.8 KB
Line 
1//simple.c:
2
3#include "libroar.h"
4
5int 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
27int 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
60int 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
65int 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 {
86  return -1;
87 }
88
89 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
90  sprintf(file, "/tmp/.libroar-simple-stream.%i-%i", getpid(), count++);
91 } else {
92  strcpy(file, inet_ntoa(socket_addr.sin_addr));
93 }
94
95 if ( (listen = roar_socket_listen(type, file, port)) == -1 ) {
96  return -1;
97 }
98
99 if ( type == ROAR_SOCKET_TYPE_INET ) {
100  len = sizeof(struct sockaddr_in);
101  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
102
103  if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) {
104   return -1;
105  }
106  port = ROAR_NET2HOST16(socket_addr.sin_port);
107  ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port);
108 }
109
110 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
111  chmod(file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
112
113  grp = getgrnam(ROAR_DEFAULT_SOCKGRP);
114
115  if ( grp )
116   chown(file, -1, grp->gr_gid);
117 }
118
119 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) {
120  return -1;
121 }
122
123 if ( roar_stream_connect(con, s, dir) == -1 ) {
124  return -1;
125 }
126
127 if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) {
128
129  if ( (fh = accept(listen, NULL, NULL)) != -1 ) {
130   if ( dir == ROAR_DIR_PLAY ) {
131    shutdown(fh, SHUT_RD);
132   } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
133    shutdown(fh, SHUT_WR);
134   }
135  }
136   if ( roar_recv_message(con, &mes, NULL) == -1 ) {
137    close(fh);
138    fh = -1;
139   } else if ( mes.cmd != ROAR_CMD_OK ) {
140    close(fh);
141    fh = -1;
142   }
143 }
144
145 close(listen);
146
147 if ( type == ROAR_SOCKET_TYPE_UNIX ) {
148  unlink(file);
149 }
150
151 s->fh = fh;
152
153 return fh;
154}
155
156
157int roar_simple_play_file(char * file, char * server, char * name) {
158 struct roar_connection con;
159
160 if ( roar_simple_connect(&con, server, name) == -1 ) {
161  return -1;
162 }
163
164 return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one.
165}
166
167int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) {
168 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name);
169}
170
171int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) {
172 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name);
173}
174
175int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) {
176 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name);
177}
178
179int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) {
180 return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name);
181}
182
183
184int roar_simple_close(int fh) {
185 return close(fh);
186}
187
188int roar_simple_get_standby (int fh) {
189 struct roar_connection con;
190
191 con.fh = fh;
192
193 return roar_get_standby(&con);
194}
195
196//ll
Note: See TracBrowser for help on using the repository browser.