Makefile: Merge dhahelperwinclean and dhahelperclean targets
[mplayer.git] / stream / stream_vcd.c
blob094cd98fe828b6cc526e222ab1329fc09320fa18
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 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
41 #include "vcd_read_fbsd.h"
42 #elif defined(__APPLE__)
43 #include "vcd_read_darwin.h"
44 #elif defined(__MINGW32__) || defined(__CYGWIN__)
45 #include "vcd_read_win32.h"
46 #elif defined(__OS2__)
47 #include "vcd_read_os2.h"
48 #else
49 #include "vcd_read.h"
50 #endif
52 #include "libmpdemux/demuxer.h"
54 extern char *cdrom_device;
56 static struct stream_priv_s {
57 int track;
58 char* device;
59 } stream_priv_dflts = {
61 NULL
64 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
65 /// URL definition
66 static const m_option_t stream_opts_fields[] = {
67 { "track", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
68 { "device", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
69 /// For url parsing
70 { "hostname", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
71 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
72 { NULL, NULL, 0, 0, 0, 0, NULL }
74 static const struct m_struct_st stream_opts = {
75 "vcd",
76 sizeof(struct stream_priv_s),
77 &stream_priv_dflts,
78 stream_opts_fields
81 static int fill_buffer(stream_t *s, char* buffer, int max_len){
82 if(s->pos > s->end_pos) /// don't past end of current track
83 return 0;
84 return vcd_read(s->priv,buffer);
87 static int seek(stream_t *s,off_t newpos) {
88 s->pos = newpos;
89 vcd_set_msf(s->priv,s->pos/VCD_SECTOR_DATA);
90 return 1;
93 static int control(stream_t *stream, int cmd, void *arg) {
94 struct stream_priv_s *p = stream->priv;
95 switch(cmd) {
96 case STREAM_CTRL_GET_NUM_CHAPTERS:
98 mp_vcd_priv_t *vcd = vcd_read_toc(stream->fd);
99 if (!vcd)
100 break;
101 *(unsigned int *)arg = vcd_end_track(vcd);
102 return STREAM_OK;
104 case STREAM_CTRL_SEEK_TO_CHAPTER:
106 int r;
107 unsigned int track = *(unsigned int *)arg + 1;
108 mp_vcd_priv_t *vcd = vcd_read_toc(stream->fd);
109 if (!vcd)
110 break;
111 r = vcd_seek_to_track(vcd, track);
112 if (r >= 0) {
113 p->track = track;
114 return STREAM_OK;
116 break;
118 case STREAM_CTRL_GET_CURRENT_CHAPTER:
120 *(unsigned int *)arg = p->track - 1;
121 return STREAM_OK;
124 return STREAM_UNSUPPORTED;
127 static void close_s(stream_t *stream) {
128 free(stream->priv);
131 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
132 struct stream_priv_s* p = opts;
133 int ret,ret2,f,sect,tmp;
134 mp_vcd_priv_t* vcd;
135 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
136 int bsize = VCD_SECTOR_SIZE;
137 #endif
138 #if defined(__MINGW32__) || defined(__CYGWIN__)
139 HANDLE hd;
140 char device[] = "\\\\.\\?:";
141 #endif
142 #if defined(__OS2__)
143 char device[] = "X:";
144 HFILE hcd;
145 ULONG ulAction;
146 ULONG rc;
147 #endif
149 if(mode != STREAM_READ
150 #if defined(__MINGW32__) || defined(__CYGWIN__)
151 || GetVersion() > 0x80000000 // Win9x
152 #endif
154 m_struct_free(&stream_opts,opts);
155 return STREAM_UNSUPPORTED;
158 if (!p->device) {
159 if(cdrom_device)
160 p->device = strdup(cdrom_device);
161 else
162 p->device = strdup(DEFAULT_CDROM_DEVICE);
165 #if defined(__MINGW32__) || defined(__CYGWIN__)
166 device[4] = p->device[0];
167 /* open() can't be used for devices so do it the complicated way */
168 hd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
169 OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
170 f = _open_osfhandle((long)hd, _O_RDONLY);
171 #elif defined(__OS2__)
172 device[0] = p->device[0];
173 rc = DosOpen(device, &hcd, &ulAction, 0, FILE_NORMAL,
174 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
175 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
176 NULL);
177 f = rc ? -1 : hcd;
178 #else
179 f=open(p->device,O_RDONLY);
180 #endif
181 if(f<0){
182 mp_tmsg(MSGT_OPEN,MSGL_ERR,"CD-ROM Device '%s' not found.\n",p->device);
183 m_struct_free(&stream_opts,opts);
184 return STREAM_ERROR;
187 vcd = vcd_read_toc(f);
188 if(!vcd) {
189 mp_msg(MSGT_OPEN,MSGL_ERR,"Failed to get cd toc\n");
190 close(f);
191 m_struct_free(&stream_opts,opts);
192 return STREAM_ERROR;
194 ret2=vcd_get_track_end(vcd,p->track);
195 if(ret2<0){
196 mp_msg(MSGT_OPEN, MSGL_ERR, "%s (get)\n",
197 mp_gtext("Error selecting VCD track."));
198 close(f);
199 free(vcd);
200 m_struct_free(&stream_opts,opts);
201 return STREAM_ERROR;
203 ret=vcd_seek_to_track(vcd,p->track);
204 if(ret<0){
205 mp_msg(MSGT_OPEN, MSGL_ERR, "%s (seek)\n",
206 mp_gtext("Error selecting VCD track."));
207 close(f);
208 free(vcd);
209 m_struct_free(&stream_opts,opts);
210 return STREAM_ERROR;
212 /* search forward up to at most 3 seconds to skip leading margin */
213 sect = ret / VCD_SECTOR_DATA;
214 for (tmp = sect; tmp < sect + 3 * 75; tmp++) {
215 char mem[VCD_SECTOR_DATA];
216 //since MPEG packs are block-aligned we stop discarding sectors if they are non-null
217 if (vcd_read(vcd, mem) != VCD_SECTOR_DATA || mem[2] || mem[3])
218 break;
220 mp_msg(MSGT_OPEN, MSGL_DBG2, "%d leading sectors skipped\n", tmp - sect);
221 vcd_set_msf(vcd, tmp);
222 ret = tmp * VCD_SECTOR_DATA;
224 mp_msg(MSGT_OPEN,MSGL_V,"VCD start byte position: 0x%X end: 0x%X\n",ret,ret2);
226 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
227 if (ioctl (f, CDRIOCSETBLOCKSIZE, &bsize) == -1) {
228 mp_msg(MSGT_OPEN,MSGL_WARN,"Error in CDRIOCSETBLOCKSIZE");
230 #endif
232 stream->fd = f;
233 stream->type = STREAMTYPE_VCD;
234 stream->sector_size = VCD_SECTOR_DATA;
235 stream->start_pos=ret;
236 stream->end_pos=ret2;
237 stream->priv = vcd;
239 stream->fill_buffer = fill_buffer;
240 stream->seek = seek;
241 stream->control = control;
242 stream->close = close_s;
243 *file_format = DEMUXER_TYPE_MPEG_PS;
245 m_struct_free(&stream_opts,opts);
246 return STREAM_OK;
249 const stream_info_t stream_info_vcd = {
250 "Video CD",
251 "vcd",
252 "Albeu",
253 "based on the code from ???",
254 open_s,
255 { "vcd", NULL },
256 &stream_opts,
257 1 // Urls are an option string