view TOOLS/cache.c @ 4218:3931c41f740a

Added new syncengine thanks to a new previously undocumented feature of the em8300, this might fix playback on both slow and fast machines (more testing needed). This also requires users to get the em8300 driver from cvs until the next version is released (will probably happen this weekend) Added lots of comments, should be pretty easy to understand most of the internals now Added lots of brackets to if's for's while's etc, this is not a cosmetical thing but rather due to the fact I got some very odd bugs with else's since I didn't properly use brackets (and it's the K&R standard to have brackets everywhere) Fixed some bugs that would occur when disabling libmp1e Switched to default to the new naming scheme of device nodes, the driver will slowly switch over to this state, if it can't find devices under the new name it will try the old naming scheme I stopped opening devices in non-blocking mode, it would break the new syncengine which tries to burst data to the device (alot of times meaning it will fill the fifo pretty fast which would previously result in jerkyness on fast machines) The device now sets the initial state of the pts and speed (probably not needed, but assumption is the mother of all fuckups =) Keep the control interface open during the entire duration of the libvo device, we might need this to flush video buffers on seeking (currently not implemented, therefore seeking is broken) This is beta stuff to the driver, I will get some users to test it for me and do my best to fix seeking as soon as possible...
author mswitch
date Thu, 17 Jan 2002 10:33:47 +0000
parents b3385775390d
children
line wrap: on
line source


#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

//int open(const char *pathname, int flags);


#define BUFFSIZE (4*65536)
#define NUM_BUFS (16)

unsigned char *buffer[NUM_BUFS];

unsigned int buf_read=0;
unsigned int buf_write=0;
unsigned int buf_read_pos=0;
unsigned int buf_write_pos=0;
int full_buffers=0;

int main(int argc,char* argv[]){

           fd_set rfds;
           fd_set wfds;
           struct timeval tv;
           int retval;
	   int i;
//	   int empty=1;
	   int can_read=1;
	   int eof=0;
	   int in_fd=0; // stdin
	   
	   if(argc>1) in_fd=open(argv[1],O_RDONLY|O_NDELAY);
	   
	   for(i=0;i<NUM_BUFS;i++) buffer[i]=malloc(BUFFSIZE);

while(1){
           /* Watch stdin (fd 0) to see when it has input. */
           FD_ZERO(&rfds); if(can_read){ FD_SET(in_fd, &rfds);}
           FD_ZERO(&wfds); FD_SET(1, &wfds);
           /* Wait up to five seconds. */
           tv.tv_sec = 1;
           tv.tv_usec = 0;
           retval = select((in_fd<1?1:in_fd)+1, &rfds, &wfds, NULL, &tv);
           /* Don't rely on the value of tv now! */

           if (retval){
	       if(FD_ISSET(in_fd, &rfds) || !full_buffers){
	               fprintf(stderr,"\n%d  r",full_buffers);fflush(stderr);
		   if(full_buffers==NUM_BUFS){
		       // buffer is full!
		       can_read=0;
	               fprintf(stderr,"\n%d  full!\n",full_buffers);fflush(stderr);
		   } else {
		       // we can read input.
		       int len=BUFFSIZE-buf_read_pos;
	               fprintf(stderr,"R");fflush(stderr);
		       len=read(in_fd,buffer[buf_read]+buf_read_pos,len);
	               fprintf(stderr,"(%d)\n",len);fflush(stderr);
		       if(len>0){
		           buf_read_pos+=len;
			   if(buf_read_pos>=BUFFSIZE){
			       // block is full, find next!
			       buf_read=(buf_read+1)%NUM_BUFS;
			       ++full_buffers;
			       buf_read_pos=0;
	                       fprintf(stderr,"+");fflush(stderr);
			   }
		       } else {
		           eof=1;
		       }
		   }
	       }
	       if(FD_ISSET(1, &wfds)){
	               fprintf(stderr,"\n%d  w",full_buffers);fflush(stderr);
		   if(full_buffers==0){
		       if(eof){
		           // flush buffer!
			   int pos=0;
			   int len;
	                   fprintf(stderr,"\nf");fflush(stderr);
			   while((len=buf_read_pos-pos)>0){
			       len=write(1,buffer[buf_write]+pos,len);
	               fprintf(stderr,"(%d)",len);fflush(stderr);
			       if(len<=0) break;
			       pos+=len;
			   }
		           exit(1);
		       }
		       fprintf(stderr," empty");fflush(stderr);
		       //empty=1; // we must fill buffers!
		   } else {
		       // yeah, we can read from the buffer!
		       int len=BUFFSIZE-buf_write_pos;
	               fprintf(stderr,"W");fflush(stderr);
		       len=write(1,buffer[buf_write]+buf_write_pos,len);
	               fprintf(stderr,"(%d)",len);fflush(stderr);
		       if(len>0){
		           buf_write_pos+=len;
			   if(buf_write_pos>=BUFFSIZE){
			       // block is empty, find next!
			       buf_write=(buf_write+1)%NUM_BUFS;
			       --full_buffers;
			       buf_write_pos=0;
	                       fprintf(stderr,"-");fflush(stderr);
			       can_read=1;
			   }
		       }
		   }
	       }
	   } else {
	           fprintf(stderr,".");fflush(stderr);
	   }
}

return 0;
}