Changeset 215:517d6bee5d8d in roaraudio


Ignore:
Timestamp:
07/21/08 15:04:11 (16 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

added roar_file_play()

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • include/libroar/file.h

    r208 r215  
    1111 
    1212ssize_t roar_file_send_raw (int out, int in); 
     13ssize_t roar_file_play     (struct roar_connection * con, char * file, int exec); 
    1314 
    1415#endif 
  • libroar/file.c

    r208 r215  
    55#define BUFSIZE 8192 
    66#define BUFMAX  65536 
     7 
     8int 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} 
    723 
    824ssize_t roar_file_send_raw (int out, int in) { 
     
    2541} 
    2642 
     43ssize_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 
    27115//ll 
Note: See TracChangeset for help on using the changeset viewer.