cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / stream / stream_vcd.c
blob5d2eecc27b86d6707f62c2c2a467fdd44c1ae654
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #if defined(__MINGW32__) || defined(__CYGWIN__)
22 #include <windows.h>
23 #endif
25 #include "osdep/osdep.h"
27 #include "mp_msg.h"
28 #include "stream.h"
29 #include "m_option.h"
30 #include "m_struct.h"
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
36 #include <sys/ioctl.h>
37 #endif
38 #include <errno.h>
40 #include "talloc.h"
42 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
43 #include "vcd_read_fbsd.h"
44 #elif defined(__APPLE__)
45 #include "vcd_read_darwin.h"
46 #elif defined(__MINGW32__) || defined(__CYGWIN__)
47 #include "vcd_read_win32.h"
48 #elif defined(__OS2__)
49 #include "vcd_read_os2.h"
50 #else
51 #include "vcd_read.h"
52 #endif
54 #include "libmpdemux/demuxer.h"
56 extern char *cdrom_device;
58 static struct stream_priv_s {
59 int track;
60 char* device;
61 } stream_priv_dflts = {
63 NULL
66 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
67 /// URL definition
68 static const m_option_t stream_opts_fields[] = {
69 { "track", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
70 { "device", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
71 /// For url parsing
72 { "hostname", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
73 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
74 { NULL, NULL, 0, 0, 0, 0, NULL }
76 static const struct m_struct_st stream_opts = {
77 "vcd",
78 sizeof(struct stream_priv_s),
79 &stream_priv_dflts,
80 stream_opts_fields
83 static int fill_buffer(stream_t *s, char* buffer, int max_len){
84 if(s->pos > s->end_pos) /// don't past end of current track
85 return 0;
86 return vcd_read(s->priv,buffer);
89 static int seek(stream_t *s,off_t newpos) {
90 s->pos = newpos;
91 vcd_set_msf(s->priv,s->pos/VCD_SECTOR_DATA);
92 return 1;
95 static int control(stream_t *stream, int cmd, void *arg) {
96 struct stream_priv_s *p = stream->priv;
97 switch(cmd) {
98 case STREAM_CTRL_GET_NUM_CHAPTERS:
100 mp_vcd_priv_t *vcd = vcd_read_toc(stream->fd);
101 if (!vcd)
102 break;
103 *(unsigned int *)arg = vcd_end_track(vcd);
104 return STREAM_OK;
106 case STREAM_CTRL_SEEK_TO_CHAPTER:
108 int r;
109 unsigned int track = *(unsigned int *)arg + 1;
110 mp_vcd_priv_t *vcd = vcd_read_toc(stream->fd);
111 if (!vcd)
112 break;
113 r = vcd_seek_to_track(vcd, track);
114 if (r >= 0) {
115 p->track = track;
116 return STREAM_OK;
118 break;
120 case STREAM_CTRL_GET_CURRENT_CHAPTER:
122 *(unsigned int *)arg = p->track - 1;
123 return STREAM_OK;
126 return STREAM_UNSUPPORTED;
129 static void close_s(stream_t *stream) {
130 free(stream->priv);
133 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
134 struct stream_priv_s* p = opts;
135 int ret,ret2,f,sect,tmp;
136 mp_vcd_priv_t* vcd;
137 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
138 int bsize = VCD_SECTOR_SIZE;
139 #endif
140 #if defined(__MINGW32__) || defined(__CYGWIN__)
141 HANDLE hd;
142 char device[] = "\\\\.\\?:";
143 #endif
144 #if defined(__OS2__)
145 char device[] = "X:";
146 HFILE hcd;
147 ULONG ulAction;
148 ULONG rc;
149 #endif
151 if(mode != STREAM_READ
152 #if defined(__MINGW32__) || defined(__CYGWIN__)
153 || GetVersion() > 0x80000000 // Win9x
154 #endif
156 m_struct_free(&stream_opts,opts);
157 return STREAM_UNSUPPORTED;
160 if (!p->device) {
161 if(cdrom_device)
162 p->device = talloc_strdup(NULL, cdrom_device);
163 else
164 p->device = talloc_strdup(NULL, DEFAULT_CDROM_DEVICE);
167 #if defined(__MINGW32__) || defined(__CYGWIN__)
168 device[4] = p->device[0];
169 /* open() can't be used for devices so do it the complicated way */
170 hd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
171 OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
172 f = _open_osfhandle((long)hd, _O_RDONLY);
173 #elif defined(__OS2__)
174 device[0] = p->device[0];
175 rc = DosOpen(device, &hcd, &ulAction, 0, FILE_NORMAL,
176 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
177 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
178 NULL);
179 f = rc ? -1 : hcd;
180 #else
181 f=open(p->device,O_RDONLY);
182 #endif
183 if(f<0){
184 mp_tmsg(MSGT_OPEN,MSGL_ERR,"CD-ROM Device '%s' not found.\n",p->device);
185 m_struct_free(&stream_opts,opts);
186 return STREAM_ERROR;
189 vcd = vcd_read_toc(f);
190 if(!vcd) {
191 mp_msg(MSGT_OPEN,MSGL_ERR,"Failed to get cd toc\n");
192 close(f);
193 m_struct_free(&stream_opts,opts);
194 return STREAM_ERROR;
196 ret2=vcd_get_track_end(vcd,p->track);
197 if(ret2<0){
198 mp_msg(MSGT_OPEN, MSGL_ERR, "%s (get)\n",
199 mp_gtext("Error selecting VCD track."));
200 close(f);
201 free(vcd);
202 m_struct_free(&stream_opts,opts);
203 return STREAM_ERROR;
205 ret=vcd_seek_to_track(vcd,p->track);
206 if(ret<0){
207 mp_msg(MSGT_OPEN, MSGL_ERR, "%s (seek)\n",
208 mp_gtext("Error selecting VCD track."));
209 close(f);
210 free(vcd);
211 m_struct_free(&stream_opts,opts);
212 return STREAM_ERROR;
214 /* search forward up to at most 3 seconds to skip leading margin */
215 sect = ret / VCD_SECTOR_DATA;
216 for (tmp = sect; tmp < sect + 3 * 75; tmp++) {
217 char mem[VCD_SECTOR_DATA];
218 //since MPEG packs are block-aligned we stop discarding sectors if they are non-null
219 if (vcd_read(vcd, mem) != VCD_SECTOR_DATA || mem[2] || mem[3])
220 break;
222 mp_msg(MSGT_OPEN, MSGL_DBG2, "%d leading sectors skipped\n", tmp - sect);
223 vcd_set_msf(vcd, tmp);
224 ret = tmp * VCD_SECTOR_DATA;
226 mp_msg(MSGT_OPEN,MSGL_V,"VCD start byte position: 0x%X end: 0x%X\n",ret,ret2);
228 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
229 if (ioctl (f, CDRIOCSETBLOCKSIZE, &bsize) == -1) {
230 mp_msg(MSGT_OPEN,MSGL_WARN,"Error in CDRIOCSETBLOCKSIZE");
232 #endif
234 stream->fd = f;
235 stream->type = STREAMTYPE_VCD;
236 stream->sector_size = VCD_SECTOR_DATA;
237 stream->start_pos=ret;
238 stream->end_pos=ret2;
239 stream->priv = vcd;
241 stream->fill_buffer = fill_buffer;
242 stream->seek = seek;
243 stream->control = control;
244 stream->close = close_s;
245 *file_format = DEMUXER_TYPE_MPEG_PS;
247 m_struct_free(&stream_opts,opts);
248 return STREAM_OK;
251 const stream_info_t stream_info_vcd = {
252 "Video CD",
253 "vcd",
254 "Albeu",
255 "based on the code from ???",
256 open_s,
257 { "vcd", NULL },
258 &stream_opts,
259 1 // Urls are an option string