[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]; |
---|
[217] | 29 | #ifdef __linux__ |
---|
| 30 | int cork_new = 1, cork_old; |
---|
| 31 | socklen_t cork_len = sizeof(int); |
---|
| 32 | |
---|
[219] | 33 | if ( getsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, &cork_len) == -1 ) { |
---|
| 34 | cork_old = -1; |
---|
| 35 | } else { |
---|
| 36 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_new, sizeof(int)); |
---|
| 37 | } |
---|
[217] | 38 | #endif |
---|
[208] | 39 | |
---|
| 40 | #ifdef ROAR_HAVE_LINUX_SENDFILE |
---|
| 41 | while ((ret = sendfile(out, in, NULL, BUFMAX)) > 0) |
---|
| 42 | r += ret; |
---|
| 43 | #endif |
---|
| 44 | |
---|
| 45 | // TODO: try mmap here! |
---|
| 46 | |
---|
| 47 | while ((len = read(in, buf, BUFSIZE)) > 0) |
---|
| 48 | r += write(out, buf, len); |
---|
| 49 | |
---|
[217] | 50 | #ifdef __linux__ |
---|
[219] | 51 | if ( cork_old != -1 ) |
---|
| 52 | setsockopt(out, IPPROTO_TCP, TCP_CORK, &cork_old, cork_len); |
---|
[217] | 53 | #endif |
---|
[208] | 54 | return r; |
---|
| 55 | } |
---|
| 56 | |
---|
[215] | 57 | ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) { |
---|
| 58 | int codec = -1; |
---|
| 59 | int in, out = -1; |
---|
| 60 | struct roar_stream s; |
---|
| 61 | ssize_t r = 0; |
---|
| 62 | int len; |
---|
| 63 | char buf[BUFSIZE]; |
---|
| 64 | int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT; |
---|
| 65 | |
---|
| 66 | if ( !con ) |
---|
| 67 | return -1; |
---|
| 68 | |
---|
| 69 | if ( !file ) |
---|
| 70 | return -1; |
---|
| 71 | |
---|
| 72 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
| 73 | return -1; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if ((len = read(in, buf, BUFSIZE)) < 1) { |
---|
| 77 | close(in); |
---|
| 78 | return -1; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | codec = roar_file_codecdetect(buf, len); |
---|
| 82 | |
---|
| 83 | if ( codec == -1 ) { |
---|
| 84 | close(in); |
---|
| 85 | return -1; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if ( exec ) { |
---|
| 89 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
| 90 | close(in); |
---|
| 91 | return -1; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if ( roar_stream_connect(con, &s, ROAR_DIR_PLAY) == -1 ) { |
---|
| 95 | close(in); |
---|
| 96 | return -1; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | if ( roar_stream_exec(con, &s) == -1 ) { |
---|
| 100 | close(in); |
---|
| 101 | return -1; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | shutdown(con->fh, SHUT_RD); |
---|
| 105 | |
---|
| 106 | out = con->fh; |
---|
| 107 | } else { |
---|
| 108 | if ( (out = roar_simple_new_stream(con, rate, channels, bits, codec, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 109 | close(in); |
---|
| 110 | return -1; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | write(out, buf, len); |
---|
| 115 | |
---|
| 116 | r = roar_file_send_raw(out, in); |
---|
| 117 | |
---|
| 118 | close(out); |
---|
| 119 | |
---|
| 120 | if ( exec ) { |
---|
| 121 | con->fh = -1; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | close(in); |
---|
| 125 | |
---|
| 126 | return r; |
---|
| 127 | } |
---|
| 128 | |
---|
[202] | 129 | //ll |
---|