Changeset 3140:2cdc8f58b5da in roaraudio
- Timestamp:
- 01/15/10 12:06:29 (14 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libroaross/libroaross.c
r3139 r3140 66 66 #define _MAX_POINTER 8 67 67 68 // handle type: 69 #define HT_NONE 0 70 #define HT_STREAM 1 71 #define HT_MIXER 2 72 68 73 struct session { 69 74 int refc; … … 76 81 int refc; // refrence counter 77 82 struct session * session; 83 int type; 84 struct roar_stream stream; 85 struct roar_vio_calls stream_vio; 86 int stream_opened; 78 87 }; 79 88 … … 85 94 } _os; 86 95 87 static struct {96 static struct pointer { 88 97 int fh; 89 98 struct handle * handle; … … 155 164 } 156 165 166 static struct handle * _open_handle(struct session * session) { 167 struct handle * handle; 168 169 if ( (handle = roar_mm_malloc(sizeof(struct handle))) == NULL ) 170 return NULL; 171 172 memset(handle, 0, sizeof(struct handle)); 173 174 handle->refc = 1; 175 handle->session = session; 176 session->refc++; // TODO: better warp this 177 handle->type = HT_NONE; 178 roar_stream_new_empty(&(handle->stream)); 179 180 return handle; 181 } 182 183 static void _close_handle(struct handle * handle) { 184 if (handle == NULL) 185 return; 186 187 handle->refc--; 188 189 if ( handle->refc == 0 ) { 190 _close_session(handle->session); 191 192 if ( handle->stream_opened ) 193 roar_vio_close(&(handle->stream_vio)); 194 195 roar_mm_free(handle); 196 } 197 } 198 199 static int _open_stream (struct handle * handle) { 200 return -1; 201 } 202 203 static struct pointer * _get_pointer_by_fh (int fh) { 204 int i; 205 206 for (i = 0; i < _MAX_POINTER; i++) { 207 if ( _ptr[i].fh == fh ) 208 return &(_ptr[i]); 209 } 210 211 return NULL; 212 } 213 214 static struct pointer * _open_pointer(struct handle * handle) { 215 struct pointer * ret = _get_pointer_by_fh(-1); 216 217 if ( ret == NULL ) 218 return NULL; 219 220 if ( (ret->fh = _open_dummy()) == -1 ) 221 return NULL; 222 223 ret->handle = handle; 224 225 return ret; 226 } 227 228 static void _close_pointer(struct pointer * pointer) { 229 if ( pointer == NULL ) 230 return; 231 232 _os.close(pointer->fh); 233 234 pointer->fh = -1; 235 236 _close_handle(pointer->handle); 237 } 157 238 158 239 // ------------------------------------- … … 176 257 177 258 int close(int fd) { 259 struct pointer * pointer; 178 260 _init(); 179 261 262 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { 263 _close_pointer(pointer); 264 return 0; 265 } 266 180 267 return _os.close(fd); 181 268 } 182 269 183 270 ssize_t write(int fd, const void *buf, size_t count) { 271 struct pointer * pointer; 272 184 273 _init(); 185 274 275 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { 276 if ( pointer->handle->type == HT_STREAM ) { 277 if ( pointer->handle->stream_opened == 0 ) { 278 if ( _open_stream(pointer->handle) == -1 ) { 279 errno = EIO; 280 return -1; 281 } 282 } 283 return roar_vio_write(&(pointer->handle->stream_vio), buf, count); 284 } else { 285 errno = EINVAL; 286 return -1; 287 } 288 } 289 186 290 return _os.write(fd, buf, count); 187 291 } 188 292 189 293 ssize_t read(int fd, void *buf, size_t count) { 294 struct pointer * pointer; 295 190 296 _init(); 191 297 298 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { 299 if ( pointer->handle->type == HT_STREAM ) { 300 if ( pointer->handle->stream_opened == 0 ) { 301 if ( _open_stream(pointer->handle) == -1 ) { 302 errno = EIO; 303 return -1; 304 } 305 } 306 return roar_vio_read(&(pointer->handle->stream_vio), buf, count); 307 } else { 308 errno = EINVAL; 309 return -1; 310 } 311 } 312 192 313 return _os.read(fd, buf, count); 193 314 }
Note: See TracChangeset
for help on using the changeset viewer.