00001 #include <unistd.h> 00002 #include <fcntl.h> 00003 #include <stdio.h> 00004 #include <stdlib.h> 00005 #include <sys/time.h> 00006 #include <signal.h> 00007 00008 #define BLOCKSIZE (128*1024) 00009 #define MAXPIPEDATASIZE 1024 00010 00011 pid_t pid; 00012 int pipe_player[2]; 00013 char pipe_buffer[ MAXPIPEDATASIZE ]; 00014 char block[BLOCKSIZE]; 00015 00016 void StartPlayer( void ); 00017 void PlayBlock( char *, int ); 00018 void ClosePlayer( void ); 00019 00020 int main( int argc, char ** argv ) 00021 { 00022 int fd, n; 00023 00024 if( argc != 2 ) 00025 { 00026 printf( "Usage: %s <filename>\n", argv[0] ); 00027 return 0; 00028 } 00029 00030 fd = open( argv[1], O_RDONLY ); 00031 if( fd == -1 ) 00032 { 00033 printf( "Could not open file %s for reading\n", argv[1] ); 00034 return 0; 00035 } 00036 00037 StartPlayer( ); 00038 atexit( ClosePlayer ); 00039 00040 printf( "File %s\n", argv[1] ); 00041 while( (n = read( fd, block, BLOCKSIZE )) > 0 ) 00042 { 00043 PlayBlock( block, n ); 00044 } 00045 00046 return 0; 00047 } 00048 00049 void ClosePlayer( void ) 00050 { 00051 kill( pid, SIGTERM ); 00052 } 00053 00054 void PlayBlock( char * pbuf, int size ) 00055 { 00056 int sent; 00057 int n, nwrite; 00058 struct timeval tv1, tv2, tvdiff; 00059 00060 char * buffer = pbuf; 00061 00062 sent = 0; 00063 gettimeofday( &tv1, NULL ); 00064 while( sent < size ) 00065 { 00066 n = size - sent; 00067 if( n > MAXPIPEDATASIZE ) 00068 n = MAXPIPEDATASIZE; 00069 00070 nwrite = write( pipe_player[1], (void*)buffer, n ); 00071 00072 if( nwrite < 0 ) 00073 perror( "write: pipe mpeg player" ); 00074 00075 sent += nwrite; 00076 buffer += nwrite; 00077 } 00078 gettimeofday( &tv2, NULL ); 00079 00080 tvdiff.tv_sec = tv2.tv_sec - tv1.tv_sec; 00081 tvdiff.tv_usec = tv2.tv_usec - tv1.tv_usec; 00082 printf( "%d %ld\n", size, (tvdiff.tv_sec*1000000)+tvdiff.tv_usec ); 00083 fflush( stdout ); 00084 } 00085 00086 void StartPlayer( void ) 00087 { 00088 setbuffer( stdin, pipe_buffer, (size_t)MAXPIPEDATASIZE ); 00089 00090 if( pipe( pipe_player ) < 0 ) 00091 { 00092 perror("pipe(). Failed to create pipe to player."); 00093 return; 00094 } 00095 00096 if( dup2( pipe_player[0], 0 ) == -1 ) 00097 { 00098 perror ("dup2(). Failed to associate pipe to stdin."); 00099 return; 00100 } 00101 setbuffer( stdin, pipe_buffer, (size_t)MAXPIPEDATASIZE ); 00102 00103 if( (pid = fork()) != 0 ) 00104 { 00105 if( pid < 0 ) 00106 { 00107 perror("fork(). Failed to fork process."); 00108 return; 00109 } 00110 } 00111 else 00112 { 00113 #ifndef CLIENT_DEBUG 00114 int null_file; 00115 00116 null_file = open( "/dev/null", O_WRONLY ); 00117 if( null_file != -1 ) 00118 { 00119 dup2( null_file, 1 ); 00120 dup2( null_file, 2 ); 00121 } 00122 #endif 00123 00124 if( execlp( "mplayer","mplayer", "-quiet", "-nocache", "-vo" ,"null" , "-nosound" , "-", NULL ) == -1 ) 00125 { 00126 perror( "execlp(): Failed to execute mpeg player." ); 00127 exit( 1 ); 00128 } 00129 } 00130 }