[202] | 1 | //file.c: |
---|
| 2 | |
---|
| 3 | #include "libroar.h" |
---|
| 4 | |
---|
[208] | 5 | #define BUFSIZE 8192 |
---|
| 6 | #define BUFMAX 65536 |
---|
| 7 | |
---|
[215] | 8 | int roar_file_codecdetect(char * buf, int len) { |
---|
| 9 | int codec = -1; |
---|
| 10 | |
---|
| 11 | if ( len > 3 ) { |
---|
| 12 | if ( strncmp(buf, "OggS", 4) == 0 ) { |
---|
| 13 | codec = ROAR_CODEC_OGG_GENERAL; |
---|
| 14 | if ( len > 34 ) { // this is 7 bytes after the end of the header |
---|
| 15 | if ( strncmp(buf+28, "\001vorbis", 7) == 0 ) |
---|
| 16 | codec = ROAR_CODEC_OGG_VORBIS; |
---|
| 17 | } |
---|
| 18 | } |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | return codec; |
---|
| 22 | } |
---|
| 23 | |
---|
[208] | 24 | ssize_t roar_file_send_raw (int out, int in) { |
---|
| 25 | ssize_t r = 0; |
---|
| 26 | ssize_t ret; |
---|
| 27 | int len; |
---|
| 28 | char buf[BUFSIZE]; |
---|
| 29 | |
---|
| 30 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
| 31 | while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0) |
---|
| 32 | r += ret; |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | // TODO: try mmap here! |
---|
| 36 | |
---|
| 37 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 38 | r += write(out, buf, len); |
---|
| 39 | |
---|
| 40 | return r; |
---|
| 41 | } |
---|
| 42 | |
---|
[215] | 43 | ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) { |
---|
| 44 | int codec = -1; |
---|
| 45 | int in, out = -1; |
---|
| 46 | struct roar_stream s; |
---|
| 47 | ssize_t r = 0; |
---|
| 48 | int len; |
---|
| 49 | char buf[BUFSIZE]; |
---|
| 50 | int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT; |
---|
| 51 | |
---|
| 52 | if ( !con ) |
---|
| 53 | return -1; |
---|
| 54 | |
---|
| 55 | if ( !file ) |
---|
| 56 | return -1; |
---|
| 57 | |
---|
| 58 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
| 59 | return -1; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | if ((len = read(in, buf, BUFSIZE)) < 1) { |
---|
| 63 | close(in); |
---|
| 64 | return -1; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | codec = roar_file_codecdetect(buf, len); |
---|
| 68 | |
---|
| 69 | if ( codec == -1 ) { |
---|
| 70 | close(in); |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | if ( exec ) { |
---|
| 75 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
| 76 | close(in); |
---|
| 77 | return -1; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | if ( roar_stream_connect(con, &s, ROAR_DIR_PLAY) == -1 ) { |
---|
| 81 | close(in); |
---|
| 82 | return -1; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if ( roar_stream_exec(con, &s) == -1 ) { |
---|
| 86 | close(in); |
---|
| 87 | return -1; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | shutdown(con->fh, SHUT_RD); |
---|
| 91 | |
---|
| 92 | out = con->fh; |
---|
| 93 | } else { |
---|
| 94 | if ( (out = roar_simple_new_stream(con, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 95 | close(in); |
---|
| 96 | return -1; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | write(out, buf, len); |
---|
| 101 | |
---|
| 102 | r = roar_file_send_raw(out, in); |
---|
| 103 | |
---|
| 104 | close(out); |
---|
| 105 | |
---|
| 106 | if ( exec ) { |
---|
| 107 | con->fh = -1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | close(in); |
---|
| 111 | |
---|
| 112 | return r; |
---|
| 113 | } |
---|
| 114 | |
---|
[202] | 115 | //ll |
---|