core: Clean up move-to-next-file logic
[mplayer.git] / stream / stream_vcd.c
blob4202a9786d8278edd4bcef70ed52b5b6b34b21bf
2 #include "config.h"
4 #ifdef WIN32
5 #include <windows.h>
6 #endif
8 #include "mp_msg.h"
9 #include "stream.h"
10 #include "help_mp.h"
11 #include "m_option.h"
12 #include "m_struct.h"
14 #include <fcntl.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #ifndef WIN32
18 #include <sys/ioctl.h>
19 #endif
20 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
23 #include "vcd_read_fbsd.h"
24 #elif defined(__APPLE__)
25 #include "vcd_read_darwin.h"
26 #elif defined(WIN32)
27 #include "vcd_read_win32.h"
28 #else
29 #include "vcd_read.h"
30 #endif
32 #include "libmpdemux/demuxer.h"
34 extern char *cdrom_device;
36 static struct stream_priv_s {
37 int track;
38 char* device;
39 } stream_priv_dflts = {
41 NULL
44 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
45 /// URL definition
46 static const m_option_t stream_opts_fields[] = {
47 { "track", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
48 { "device", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
49 /// For url parsing
50 { "hostname", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
51 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
52 { NULL, NULL, 0, 0, 0, 0, NULL }
54 static const struct m_struct_st stream_opts = {
55 "vcd",
56 sizeof(struct stream_priv_s),
57 &stream_priv_dflts,
58 stream_opts_fields
61 static int fill_buffer(stream_t *s, char* buffer, int max_len){
62 if(s->pos > s->end_pos) /// don't past end of current track
63 return 0;
64 return vcd_read(s->priv,buffer);
67 static int seek(stream_t *s,off_t newpos) {
68 s->pos = newpos;
69 vcd_set_msf(s->priv,s->pos/VCD_SECTOR_DATA);
70 return 1;
73 static void close_s(stream_t *stream) {
74 free(stream->priv);
77 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
78 struct stream_priv_s* p = (struct stream_priv_s*)opts;
79 int ret,ret2,f,sect,tmp;
80 mp_vcd_priv_t* vcd;
81 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
82 int bsize = VCD_SECTOR_SIZE;
83 #endif
84 #ifdef WIN32
85 HANDLE hd;
86 char device[] = "\\\\.\\?:";
87 #endif
89 if(mode != STREAM_READ
90 #ifdef WIN32
91 || GetVersion() > 0x80000000 // Win9x
92 #endif
93 ) {
94 m_struct_free(&stream_opts,opts);
95 return STREAM_UNSUPPORTED;
98 if (!p->device) {
99 if(cdrom_device)
100 p->device = strdup(cdrom_device);
101 else
102 p->device = strdup(DEFAULT_CDROM_DEVICE);
105 #ifdef WIN32
106 device[4] = p->device[0];
107 /* open() can't be used for devices so do it the complicated way */
108 hd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
109 OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
110 f = _open_osfhandle((long)hd, _O_RDONLY);
111 #else
112 f=open(p->device,O_RDONLY);
113 #endif
114 if(f<0){
115 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CdDevNotfound,p->device);
116 m_struct_free(&stream_opts,opts);
117 return STREAM_ERROR;
120 vcd = vcd_read_toc(f);
121 if(!vcd) {
122 mp_msg(MSGT_OPEN,MSGL_ERR,"Failed to get cd toc\n");
123 close(f);
124 m_struct_free(&stream_opts,opts);
125 return STREAM_ERROR;
127 ret2=vcd_get_track_end(vcd,p->track);
128 if(ret2<0){
129 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (get)\n");
130 close(f);
131 free(vcd);
132 m_struct_free(&stream_opts,opts);
133 return STREAM_ERROR;
135 ret=vcd_seek_to_track(vcd,p->track);
136 if(ret<0){
137 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (seek)\n");
138 close(f);
139 free(vcd);
140 m_struct_free(&stream_opts,opts);
141 return STREAM_ERROR;
143 /* search forward up to at most 3 seconds to skip leading margin */
144 sect = ret / VCD_SECTOR_DATA;
145 for (tmp = sect; tmp < sect + 3 * 75; tmp++) {
146 char mem[VCD_SECTOR_DATA];
147 //since MPEG packs are block-aligned we stop discarding sectors if they are non-null
148 if (vcd_read(vcd, mem) != VCD_SECTOR_DATA || mem[2] || mem[3])
149 break;
151 mp_msg(MSGT_OPEN, MSGL_DBG2, "%d leading sectors skipped\n", tmp - sect);
152 vcd_set_msf(vcd, tmp);
153 ret = tmp * VCD_SECTOR_DATA;
155 mp_msg(MSGT_OPEN,MSGL_V,"VCD start byte position: 0x%X end: 0x%X\n",ret,ret2);
157 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
158 if (ioctl (f, CDRIOCSETBLOCKSIZE, &bsize) == -1) {
159 mp_msg(MSGT_OPEN,MSGL_WARN,"Error in CDRIOCSETBLOCKSIZE");
161 #endif
163 stream->fd = f;
164 stream->type = STREAMTYPE_VCD;
165 stream->sector_size = VCD_SECTOR_DATA;
166 stream->start_pos=ret;
167 stream->end_pos=ret2;
168 stream->priv = vcd;
170 stream->fill_buffer = fill_buffer;
171 stream->seek = seek;
172 stream->close = close_s;
173 *file_format = DEMUXER_TYPE_MPEG_PS;
175 m_struct_free(&stream_opts,opts);
176 return STREAM_OK;
179 const stream_info_t stream_info_vcd = {
180 "Video CD",
181 "vcd",
182 "Albeu",
183 "based on the code from ???",
184 open_s,
185 { "vcd", NULL },
186 &stream_opts,
187 1 // Urls are an option string