#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>
Go to the source code of this file.
Defines | |
#define | BLOCKSIZE (128*1024) |
#define | MAXPIPEDATASIZE 1024 |
Functions | |
void | StartPlayer (void) |
void | PlayBlock (char *, int) |
void | ClosePlayer (void) |
int | main (int argc, char **argv) |
Variables | |
pid_t | pid |
int | pipe_player [2] |
char | pipe_buffer [MAXPIPEDATASIZE] |
char | block [BLOCKSIZE] |
#define BLOCKSIZE (128*1024) |
Definition at line 8 of file movietime.c.
#define MAXPIPEDATASIZE 1024 |
Definition at line 9 of file movietime.c.
void ClosePlayer | ( | void | ) |
Definition at line 49 of file movietime.c.
00050 { 00051 kill( pid, SIGTERM ); 00052 }
int main | ( | int | argc, | |
char ** | argv | |||
) |
Definition at line 20 of file movietime.c.
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 }
void PlayBlock | ( | char * | pbuf, | |
int | size | |||
) |
Definition at line 54 of file movietime.c.
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 }
void StartPlayer | ( | void | ) |
Definition at line 86 of file movietime.c.
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 }
char block[BLOCKSIZE] |
Definition at line 14 of file movietime.c.
pid_t pid |
Definition at line 11 of file movietime.c.
char pipe_buffer[MAXPIPEDATASIZE] |
Definition at line 13 of file movietime.c.
int pipe_player[2] |
Definition at line 12 of file movietime.c.