578
|
1
|
|
2 #include <stdio.h>
|
|
3 #include <stdlib.h>
|
|
4
|
|
5 #include <sys/ioctl.h>
|
|
6 #include <unistd.h>
|
|
7
|
|
8 //#include <sys/types.h>
|
|
9 //#include <sys/stat.h>
|
|
10 //#include <fcntl.h>
|
|
11
|
|
12 #include <linux/cdrom.h>
|
|
13
|
|
14 #include "stream.h"
|
|
15
|
|
16 extern int verbose; // defined in mplayer.c
|
1
|
17
|
|
18 #include "vcd_read.c"
|
|
19
|
|
20 //=================== STREAMER =========================
|
|
21
|
|
22 int stream_fill_buffer(stream_t *s){
|
|
23 int len;
|
|
24 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
|
|
25 switch(s->type){
|
|
26 case STREAMTYPE_FILE:
|
|
27 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
|
|
28 case STREAMTYPE_VCD:
|
|
29 #ifdef VCD_CACHE
|
|
30 len=vcd_cache_read(s->fd,s->buffer);break;
|
|
31 #else
|
|
32 len=vcd_read(s->fd,s->buffer);break;
|
|
33 #endif
|
|
34 default: len=0;
|
|
35 }
|
|
36 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
|
|
37 s->buf_pos=0;
|
|
38 s->buf_len=len;
|
|
39 s->pos+=len;
|
|
40 // printf("[%d]",len);fflush(stdout);
|
|
41 return len;
|
|
42 }
|
|
43
|
578
|
44 int stream_seek_long(stream_t *s,unsigned int pos){
|
1
|
45 unsigned int newpos;
|
|
46
|
578
|
47 // if(verbose>=3) printf("seek to 0x%X\n",pos);
|
1
|
48
|
|
49 if(verbose>=3){
|
|
50 printf("s->pos=%X newpos=%X new_bufpos=%X buflen=%X \n",
|
578
|
51 (unsigned int)s->pos,newpos,pos,s->buf_len);
|
1
|
52 }
|
|
53
|
|
54 s->buf_pos=s->buf_len=0;
|
|
55
|
|
56 switch(s->type){
|
|
57 case STREAMTYPE_FILE:
|
492
|
58 newpos=pos&(~(STREAM_BUFFER_SIZE-1));break;
|
1
|
59 case STREAMTYPE_VCD:
|
|
60 newpos=(pos/VCD_SECTOR_DATA)*VCD_SECTOR_DATA;break;
|
|
61 }
|
|
62
|
|
63 pos-=newpos;
|
|
64
|
|
65 if(newpos==0 || newpos!=s->pos){
|
|
66 s->pos=newpos; // real seek
|
|
67 switch(s->type){
|
|
68 case STREAMTYPE_FILE:
|
|
69 if(lseek(s->fd,s->pos,SEEK_SET)<0) s->eof=1;
|
|
70 break;
|
|
71 case STREAMTYPE_VCD:
|
|
72 #ifdef VCD_CACHE
|
|
73 vcd_cache_seek(s->pos/VCD_SECTOR_DATA);
|
|
74 #else
|
|
75 vcd_set_msf(s->pos/VCD_SECTOR_DATA);
|
|
76 #endif
|
|
77 break;
|
|
78 }
|
|
79 // putchar('.');fflush(stdout);
|
|
80 //} else {
|
|
81 // putchar('%');fflush(stdout);
|
|
82 }
|
|
83
|
|
84 stream_fill_buffer(s);
|
|
85 if(pos>=0 && pos<s->buf_len){
|
|
86 s->buf_pos=pos; // byte position in sector
|
|
87 return 1;
|
|
88 }
|
578
|
89 if(verbose) printf("stream_seek: WARNING! Can't seek to 0x%X !\n",pos+newpos);
|
1
|
90 return 0;
|
|
91 }
|
|
92
|
|
93
|
|
94 void stream_reset(stream_t *s){
|
|
95 if(s->eof){
|
|
96 s->pos=0; //ftell(f);
|
|
97 // s->buf_pos=s->buf_len=0;
|
|
98 s->eof=0;
|
|
99 }
|
|
100 //stream_seek(s,0);
|
|
101 }
|
|
102
|
|
103 stream_t* new_stream(int fd,int type){
|
|
104 stream_t *s=malloc(sizeof(stream_t));
|
|
105 s->fd=fd;
|
|
106 s->type=type;
|
180
|
107 s->buf_pos=s->buf_len=0;
|
598
|
108 s->start_pos=s->end_pos=0;
|
1
|
109 stream_reset(s);
|
|
110 return s;
|
|
111 }
|
|
112
|
|
113 void free_stream(stream_t *s){
|
|
114 free(s);
|
|
115 }
|
|
116
|
|
117
|