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:
|
692
|
27 case STREAMTYPE_STREAM:
|
1
|
28 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
|
|
29 case STREAMTYPE_VCD:
|
|
30 #ifdef VCD_CACHE
|
|
31 len=vcd_cache_read(s->fd,s->buffer);break;
|
|
32 #else
|
|
33 len=vcd_read(s->fd,s->buffer);break;
|
|
34 #endif
|
|
35 default: len=0;
|
|
36 }
|
|
37 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
|
|
38 s->buf_pos=0;
|
|
39 s->buf_len=len;
|
|
40 s->pos+=len;
|
|
41 // printf("[%d]",len);fflush(stdout);
|
|
42 return len;
|
|
43 }
|
|
44
|
578
|
45 int stream_seek_long(stream_t *s,unsigned int pos){
|
1
|
46 unsigned int newpos;
|
|
47
|
578
|
48 // if(verbose>=3) printf("seek to 0x%X\n",pos);
|
1
|
49
|
|
50 if(verbose>=3){
|
|
51 printf("s->pos=%X newpos=%X new_bufpos=%X buflen=%X \n",
|
578
|
52 (unsigned int)s->pos,newpos,pos,s->buf_len);
|
1
|
53 }
|
|
54
|
|
55 s->buf_pos=s->buf_len=0;
|
|
56
|
|
57 switch(s->type){
|
|
58 case STREAMTYPE_FILE:
|
692
|
59 case STREAMTYPE_STREAM:
|
492
|
60 newpos=pos&(~(STREAM_BUFFER_SIZE-1));break;
|
1
|
61 case STREAMTYPE_VCD:
|
|
62 newpos=(pos/VCD_SECTOR_DATA)*VCD_SECTOR_DATA;break;
|
|
63 }
|
|
64
|
|
65 pos-=newpos;
|
|
66
|
|
67 if(newpos==0 || newpos!=s->pos){
|
|
68 switch(s->type){
|
|
69 case STREAMTYPE_FILE:
|
692
|
70 s->pos=newpos; // real seek
|
1
|
71 if(lseek(s->fd,s->pos,SEEK_SET)<0) s->eof=1;
|
|
72 break;
|
|
73 case STREAMTYPE_VCD:
|
692
|
74 s->pos=newpos; // real seek
|
1
|
75 #ifdef VCD_CACHE
|
|
76 vcd_cache_seek(s->pos/VCD_SECTOR_DATA);
|
|
77 #else
|
|
78 vcd_set_msf(s->pos/VCD_SECTOR_DATA);
|
|
79 #endif
|
|
80 break;
|
692
|
81 case STREAMTYPE_STREAM:
|
|
82 //s->pos=newpos; // real seek
|
|
83 if(newpos<s->pos){
|
|
84 printf("Cannot seek backward in linear streams!\n");
|
|
85 return 1;
|
|
86 }
|
|
87 while(s->pos<newpos){
|
|
88 if(stream_fill_buffer(s)<=0) break; // EOF
|
|
89 }
|
|
90 break;
|
1
|
91 }
|
|
92 // putchar('.');fflush(stdout);
|
|
93 //} else {
|
|
94 // putchar('%');fflush(stdout);
|
|
95 }
|
|
96
|
|
97 stream_fill_buffer(s);
|
|
98 if(pos>=0 && pos<s->buf_len){
|
|
99 s->buf_pos=pos; // byte position in sector
|
|
100 return 1;
|
|
101 }
|
578
|
102 if(verbose) printf("stream_seek: WARNING! Can't seek to 0x%X !\n",pos+newpos);
|
1
|
103 return 0;
|
|
104 }
|
|
105
|
|
106
|
|
107 void stream_reset(stream_t *s){
|
|
108 if(s->eof){
|
|
109 s->pos=0; //ftell(f);
|
|
110 // s->buf_pos=s->buf_len=0;
|
|
111 s->eof=0;
|
|
112 }
|
|
113 //stream_seek(s,0);
|
|
114 }
|
|
115
|
|
116 stream_t* new_stream(int fd,int type){
|
|
117 stream_t *s=malloc(sizeof(stream_t));
|
|
118 s->fd=fd;
|
|
119 s->type=type;
|
180
|
120 s->buf_pos=s->buf_len=0;
|
598
|
121 s->start_pos=s->end_pos=0;
|
1
|
122 stream_reset(s);
|
|
123 return s;
|
|
124 }
|
|
125
|
|
126 void free_stream(stream_t *s){
|
|
127 free(s);
|
|
128 }
|
|
129
|
|
130
|