1 /****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VLC authors and VideoLAN
7 * Authors: Johan Bilien <jobi@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU Lesser General Public License as published by
21 * the Free Software Foundation; either version 2.1 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with this program; if not, write to the Free Software Foundation,
31 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
32 *****************************************************************************/
34 /*****************************************************************************
36 *****************************************************************************/
42 # define INCL_DOSDEVIOCTL
48 #include <sys/types.h>
51 #ifdef HAVE_ARPA_INET_H
52 # include <arpa/inet.h>
55 #include <vlc_common.h>
56 #include <vlc_access.h>
57 #include <vlc_charset.h>
61 #if defined( SYS_BSDI )
63 #elif defined ( __APPLE__ )
64 # include <CoreFoundation/CFBase.h>
65 # include <IOKit/IOKitLib.h>
66 # include <IOKit/storage/IOCDTypes.h>
67 # include <IOKit/storage/IOCDMedia.h>
68 # include <IOKit/storage/IOCDMediaBSDClient.h>
69 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
70 # include <inttypes.h>
71 # include <sys/cdio.h>
72 # include <sys/scsiio.h>
73 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
74 # include <sys/cdio.h>
75 # include <sys/cdrio.h>
76 #elif defined( WIN32 )
78 # include <winioctl.h>
79 #elif defined (__linux__)
80 # include <sys/ioctl.h>
81 # include <linux/cdrom.h>
82 #elif defined( __OS2__ )
88 #include "cdrom_internals.h"
92 /*****************************************************************************
93 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
94 *****************************************************************************/
95 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
100 #if !defined( WIN32 ) && !defined( __OS2__ )
101 struct stat fileinfo;
104 if( !psz_dev ) return NULL;
107 * Initialize structure with default values
109 p_vcddev = malloc( sizeof(*p_vcddev) );
110 if( p_vcddev == NULL )
112 p_vcddev->i_vcdimage_handle = -1;
113 p_vcddev->psz_dev = NULL;
117 * Check if we are dealing with a device or a file (vcd image)
119 #if defined( WIN32 ) || defined( __OS2__ )
120 if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
126 if( vlc_stat( psz_dev, &fileinfo ) < 0 )
132 /* Check if this is a block/char device */
133 if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
139 i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
144 * open the vcd device
148 i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
149 #elif defined( __OS2__ )
150 i_ret = os2_vcd_open( p_this, psz_dev, p_vcddev );
152 p_vcddev->i_device_handle = -1;
153 p_vcddev->i_device_handle = vlc_open( psz_dev, O_RDONLY | O_NONBLOCK );
154 i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
160 p_vcddev->psz_dev = (char *)strdup( psz_dev );
171 /*****************************************************************************
172 * ioctl_Close: Closes an already opened VCD device or file.
173 *****************************************************************************/
174 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
176 free( p_vcddev->psz_dev );
178 if( p_vcddev->i_vcdimage_handle != -1 )
184 CloseVCDImage( p_this, p_vcddev );
193 if( p_vcddev->h_device_handle )
194 CloseHandle( p_vcddev->h_device_handle );
195 #elif defined( __OS2__ )
197 DosClose( p_vcddev->hcd );
199 if( p_vcddev->i_device_handle != -1 )
200 close( p_vcddev->i_device_handle );
205 /*****************************************************************************
206 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
207 * if pp_sectors is not null and return the number of
209 *****************************************************************************/
210 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
215 if( p_vcddev->i_vcdimage_handle != -1 )
221 i_tracks = p_vcddev->i_tracks;
225 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
226 if( *pp_sectors == NULL )
228 memcpy( *pp_sectors, p_vcddev->p_sectors,
229 (i_tracks + 1) * sizeof(**pp_sectors) );
241 #if defined( __APPLE__ )
246 if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
248 msg_Err( p_this, "failed to get the TOC" );
252 i_descriptors = CDTOCGetDescriptorCount( pTOC );
253 i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
257 int i, i_leadout = -1;
258 CDTOCDescriptor *pTrackDescriptors;
261 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
262 if( *pp_sectors == NULL )
264 darwin_freeTOC( pTOC );
268 pTrackDescriptors = pTOC->descriptors;
270 for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
272 track = pTrackDescriptors[i].point;
277 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
280 (*pp_sectors)[i_tracks++] =
281 CDConvertMSFToLBA( pTrackDescriptors[i].p );
284 if( i_leadout == -1 )
286 msg_Err( p_this, "leadout not found" );
288 darwin_freeTOC( pTOC );
292 /* set leadout sector */
293 (*pp_sectors)[i_tracks] =
294 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
297 darwin_freeTOC( pTOC );
299 #elif defined( WIN32 )
300 DWORD dwBytesReturned;
303 if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC,
304 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
305 &dwBytesReturned, NULL ) == 0 )
307 msg_Err( p_this, "could not read TOCHDR" );
311 i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
315 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
316 if( *pp_sectors == NULL )
319 for( int i = 0 ; i <= i_tracks ; i++ )
321 (*pp_sectors)[ i ] = MSF_TO_LBA2(
322 cdrom_toc.TrackData[i].Address[1],
323 cdrom_toc.TrackData[i].Address[2],
324 cdrom_toc.TrackData[i].Address[3] );
325 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
329 #elif defined( __OS2__ )
330 cdrom_get_tochdr_t get_tochdr = {{'C', 'D', '0', '1'}};
331 cdrom_tochdr_t tochdr;
337 rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMAUDIO,
338 CDROMAUDIO_GETAUDIODISK,
339 &get_tochdr, sizeof( get_tochdr ), ¶m_len,
340 &tochdr, sizeof( tochdr ), &data_len );
343 msg_Err( p_this, "could not read TOCHDR" );
347 i_tracks = tochdr.last_track - tochdr.first_track + 1;
351 cdrom_get_track_t get_track = {{'C', 'D', '0', '1'}, };
355 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
356 if( *pp_sectors == NULL )
359 for( i = 0 ; i < i_tracks ; i++ )
361 get_track.track = tochdr.first_track + i;
362 rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMAUDIO,
363 CDROMAUDIO_GETAUDIOTRACK,
364 &get_track, sizeof(get_track), ¶m_len,
365 &track, sizeof(track), &data_len );
368 msg_Err( p_this, "could not read %d track",
373 (*pp_sectors)[ i ] = MSF_TO_LBA2(
377 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
380 /* for lead-out track */
381 (*pp_sectors)[ i ] = MSF_TO_LBA2(
382 tochdr.lead_out.minute,
383 tochdr.lead_out.second,
384 tochdr.lead_out.frame );
385 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
388 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
389 || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
390 struct ioc_toc_header tochdr;
391 struct ioc_read_toc_entry toc_entries;
393 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
396 msg_Err( p_this, "could not read TOCHDR" );
400 i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
406 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
407 if( *pp_sectors == NULL )
410 toc_entries.address_format = CD_LBA_FORMAT;
411 toc_entries.starting_track = 0;
412 toc_entries.data_len = ( i_tracks + 1 ) *
413 sizeof( struct cd_toc_entry );
414 toc_entries.data = (struct cd_toc_entry *)
415 malloc( toc_entries.data_len );
416 if( toc_entries.data == NULL )
423 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
424 &toc_entries ) == -1 )
426 msg_Err( p_this, "could not read the TOC" );
428 free( toc_entries.data );
432 /* Fill the p_sectors structure with the track/sector matches */
433 for( i = 0 ; i <= i_tracks ; i++ )
435 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
436 /* FIXME: is this ok? */
437 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
439 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
444 struct cdrom_tochdr tochdr;
445 struct cdrom_tocentry tocent;
447 /* First we read the TOC header */
448 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
451 msg_Err( p_this, "could not read TOCHDR" );
455 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
461 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
462 if( *pp_sectors == NULL )
465 /* Fill the p_sectors structure with the track/sector matches */
466 for( i = 0 ; i <= i_tracks ; i++ )
468 tocent.cdte_format = CDROM_LBA;
470 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
472 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
475 msg_Err( p_this, "could not read TOCENTRY" );
480 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
489 /****************************************************************************
490 * ioctl_ReadSector: Read VCD or CDDA sectors
491 ****************************************************************************/
492 int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
493 int i_sector, uint8_t *p_buffer, int i_nb, int i_type )
498 if( i_type == VCD_TYPE )
499 p_block = malloc( VCD_SECTOR_SIZE * i_nb );
503 if( p_vcddev->i_vcdimage_handle != -1 )
508 if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
511 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
515 if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
518 msg_Err( p_this, "Could not read sector %d", i_sector );
530 #if defined( __APPLE__ )
531 dk_cd_read_t cd_read;
533 memset( &cd_read, 0, sizeof(cd_read) );
535 cd_read.offset = i_sector * VCD_SECTOR_SIZE;
536 cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
537 kCDSectorAreaSubHeader | kCDSectorAreaUser |
538 kCDSectorAreaAuxiliary;
539 cd_read.sectorType = kCDSectorTypeUnknown;
541 cd_read.buffer = p_block;
542 cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
544 if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
546 msg_Err( p_this, "could not read block %d", i_sector );
550 #elif defined( WIN32 )
551 DWORD dwBytesReturned;
552 RAW_READ_INFO cdrom_raw;
554 /* Initialize CDROM_RAW_READ structure */
555 cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
556 cdrom_raw.SectorCount = i_nb;
557 cdrom_raw.TrackMode = i_type == VCD_TYPE ? XAForm2 : CDDA;
559 if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_RAW_READ,
560 &cdrom_raw, sizeof(RAW_READ_INFO), p_block,
561 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
564 if( i_type == VCD_TYPE )
566 /* Retry in YellowMode2 */
567 cdrom_raw.TrackMode = YellowMode2;
568 if( DeviceIoControl( p_vcddev->h_device_handle,
569 IOCTL_CDROM_RAW_READ, &cdrom_raw,
570 sizeof(RAW_READ_INFO), p_block,
571 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
578 #elif defined( __OS2__ )
579 cdrom_readlong_t readlong = {{'C', 'D', '0', '1'}, };
585 readlong.addr_mode = 0; /* LBA mode */
586 readlong.sectors = i_nb;
587 readlong.start = i_sector;
589 rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMDISK, CDROMDISK_READLONG,
590 &readlong, sizeof( readlong ), ¶m_len,
591 p_block, VCD_SECTOR_SIZE * i_nb, &data_len );
594 msg_Err( p_this, "could not read block %d", i_sector );
598 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
602 memset( &sc, 0, sizeof(sc) );
604 sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
606 sc.cmd[2] = (i_sector >> 24) & 0xff;
607 sc.cmd[3] = (i_sector >> 16) & 0xff;
608 sc.cmd[4] = (i_sector >> 8) & 0xff;
609 sc.cmd[5] = (i_sector >> 0) & 0xff;
610 sc.cmd[6] = (i_nb >> 16) & 0xff;
611 sc.cmd[7] = (i_nb >> 8) & 0xff;
612 sc.cmd[8] = (i_nb ) & 0xff;
613 sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
614 sc.cmd[10] = 0; /* sub channel */
616 sc.databuf = (caddr_t)p_block;
617 sc.datalen = VCD_SECTOR_SIZE * i_nb;
618 sc.senselen = sizeof( sc.sense );
619 sc.flags = SCCMD_READ;
622 i_ret = ioctl( p_vcddev->i_device_handle, SCIOCCOMMAND, &sc );
625 msg_Err( p_this, "SCIOCCOMMAND failed" );
628 if( sc.retsts || sc.error )
630 msg_Err( p_this, "SCSI command failed: status %d error %d",
631 sc.retsts, sc.error );
635 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
636 int i_size = VCD_SECTOR_SIZE;
638 if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
641 msg_Err( p_this, "Could not set block size" );
645 if( lseek( p_vcddev->i_device_handle,
646 i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
648 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
652 if( read( p_vcddev->i_device_handle,
653 p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
655 msg_Err( p_this, "Could not read sector %d", i_sector );
660 for( i = 0; i < i_nb; i++ )
662 int i_dummy = i_sector + i + 2 * CD_FRAMES;
664 #define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
665 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
666 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
667 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
670 if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
671 p_block + i * VCD_SECTOR_SIZE ) == -1 )
673 msg_Err( p_this, "could not read block %i from disc",
685 /* For VCDs, we don't want to keep the header and footer of the
687 if( i_type == VCD_TYPE )
689 for( i = 0; i < i_nb; i++ )
691 memcpy( p_buffer + i * VCD_DATA_SIZE,
692 p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
701 if( i_type == VCD_TYPE )
706 /****************************************************************************
708 ****************************************************************************/
710 /****************************************************************************
711 * OpenVCDImage: try to open a vcd image from a .cue file
712 ****************************************************************************/
713 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
718 char *psz_vcdfile = NULL;
719 char *psz_cuefile = NULL;
720 FILE *cuefile = NULL;
721 int *p_sectors = NULL;
723 bool b_found = false;
725 /* Check if we are dealing with a .cue file */
726 p_pos = strrchr( psz_dev, '.' );
727 if( p_pos && !strcmp( p_pos, ".cue" ) )
729 /* psz_dev must be the cue file. Let's assume there's a .bin
730 * file with the same filename */
731 psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
732 strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
733 strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
734 psz_cuefile = strdup( psz_dev );
738 /* psz_dev must be the actual vcd file. Let's assume there's a .cue
739 * file with the same filename */
742 psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
743 strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
744 strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
748 if( asprintf( &psz_cuefile, "%s.cue", psz_dev ) == -1 )
751 /* If we need to look up the .cue file, then we don't have to look for the vcd */
752 psz_vcdfile = strdup( psz_dev );
755 /* Open the cue file and try to parse it */
756 msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
757 cuefile = vlc_fopen( psz_cuefile, "rt" );
758 if( cuefile == NULL )
760 msg_Dbg( p_this, "could not find .cue file" );
764 msg_Dbg( p_this,"guessing vcd image file: %s", psz_vcdfile );
765 p_vcddev->i_vcdimage_handle = vlc_open( psz_vcdfile,
766 O_RDONLY | O_NONBLOCK | O_BINARY );
768 while( fgets( line, 1024, cuefile ) && !b_found )
770 /* We have a cue file, but no valid vcd file yet */
773 int i_temp = sscanf( line, "FILE \"%1023[^\"]\" %15s", filename, type );
777 msg_Dbg( p_this, "the cue file says the data file is %s", type );
778 if( strcasecmp( type, "BINARY" ) )
779 goto error; /* Error if not binary, otherwise treat as case 1 */
781 if( p_vcddev->i_vcdimage_handle == -1 )
783 msg_Dbg( p_this, "we could not find the data file, but we found a new path" );
785 if( *filename != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
786 || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
788 psz_vcdfile = malloc( strlen(filename) +
789 (p_pos - psz_cuefile + 1) + 1 );
790 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
791 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), filename );
792 } else psz_vcdfile = strdup( filename );
793 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
794 p_vcddev->i_vcdimage_handle = vlc_open( psz_vcdfile,
795 O_RDONLY | O_NONBLOCK | O_BINARY );
803 if( p_vcddev->i_vcdimage_handle == -1)
806 /* Try to parse the i_tracks and p_sectors info so we can just forget
807 * about the cuefile */
810 while( fgets( line, 1024, cuefile ) && i_tracks < INT_MAX-1 )
812 /* look for a TRACK line */
814 if( !sscanf( line, "%9s", psz_dummy ) || strcmp(psz_dummy, "TRACK") )
817 /* look for an INDEX line */
818 while( fgets( line, 1024, cuefile ) )
820 int i_num, i_min, i_sec, i_frame;
822 if( (sscanf( line, "%*9s %2u %2u:%2u:%2u", &i_num,
823 &i_min, &i_sec, &i_frame ) != 4) || (i_num != 1) )
826 int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
830 p_sectors[i_tracks] = MSF_TO_LBA(i_min, i_sec, i_frame);
831 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
832 (int)i_tracks, (int)p_sectors[i_tracks] );
838 /* fill in the last entry */
839 int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
843 p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
845 msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
846 (int)i_tracks, (int)p_sectors[i_tracks] );
847 p_vcddev->i_tracks = ++i_tracks;
848 p_vcddev->p_sectors = p_sectors;
852 if( cuefile ) fclose( cuefile );
860 /****************************************************************************
861 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
862 ****************************************************************************/
863 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
865 VLC_UNUSED( p_this );
866 if( p_vcddev->i_vcdimage_handle != -1 )
867 close( p_vcddev->i_vcdimage_handle );
871 free( p_vcddev->p_sectors );
874 #if defined( __APPLE__ )
875 /****************************************************************************
876 * darwin_getTOC: get the TOC
877 ****************************************************************************/
878 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
884 io_iterator_t iterator;
885 io_registry_entry_t service;
886 CFMutableDictionaryRef properties;
889 /* get the device name */
890 if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
893 psz_devname = p_vcddev->psz_dev;
895 /* unraw the device name */
896 if( *psz_devname == 'r' )
899 /* get port for IOKit communication */
900 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
902 msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
906 /* get service iterator for the device */
907 if( ( ret = IOServiceGetMatchingServices(
908 port, IOBSDNameMatching( port, 0, psz_devname ),
909 &iterator ) ) != KERN_SUCCESS )
911 msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
916 service = IOIteratorNext( iterator );
917 IOObjectRelease( iterator );
919 /* search for kIOCDMediaClass */
920 while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
922 if( ( ret = IORegistryEntryGetParentIterator( service,
923 kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
925 msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
926 IOObjectRelease( service );
930 IOObjectRelease( service );
931 service = IOIteratorNext( iterator );
932 IOObjectRelease( iterator );
937 msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
941 /* create a CF dictionary containing the TOC */
942 if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
943 kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
945 msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
946 IOObjectRelease( service );
950 /* get the TOC from the dictionary */
951 if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
952 CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
957 buf_len = CFDataGetLength( data ) + 1;
958 range = CFRangeMake( 0, buf_len );
960 if( ( pTOC = malloc( buf_len ) ) != NULL )
962 CFDataGetBytes( data, range, (u_char *)pTOC );
967 msg_Err( p_this, "CFDictionaryGetValue failed" );
970 CFRelease( properties );
971 IOObjectRelease( service );
976 /****************************************************************************
977 * darwin_getNumberOfTracks: get number of tracks in TOC
978 ****************************************************************************/
979 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
983 CDTOCDescriptor *pTrackDescriptors = NULL;
985 pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
987 for( i = i_descriptors; i > 0; i-- )
989 track = pTrackDescriptors[i].point;
991 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
999 #endif /* __APPLE__ */
1001 #if defined( WIN32 )
1002 /*****************************************************************************
1003 * win32_vcd_open: open vcd drive
1004 *****************************************************************************
1005 * Use IOCTLs on WinNT/2K/XP.
1006 *****************************************************************************/
1007 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1008 vcddev_t *p_vcddev )
1010 /* Initializations */
1011 p_vcddev->h_device_handle = NULL;
1013 char psz_win32_drive[7];
1015 msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1017 sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1019 p_vcddev->h_device_handle = CreateFileA( psz_win32_drive, GENERIC_READ,
1020 FILE_SHARE_READ | FILE_SHARE_WRITE,
1021 NULL, OPEN_EXISTING,
1022 FILE_FLAG_NO_BUFFERING |
1023 FILE_FLAG_RANDOM_ACCESS, NULL );
1024 return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
1030 /*****************************************************************************
1031 * os2_vcd_open: open vcd drive
1032 *****************************************************************************/
1033 static int os2_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1034 vcddev_t *p_vcddev )
1036 char device[] = "X:";
1043 device[0] = psz_dev[0];
1044 rc = DosOpen( device, &hcd, &i_action, 0, FILE_NORMAL,
1045 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
1046 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
1050 msg_Err( p_this, "could not open the device %s", psz_dev );
1055 p_vcddev->hcd = hcd;
1062 static void astrcat( char **ppsz_dst, char *psz_src )
1064 char *psz_old = *ppsz_dst;
1068 *ppsz_dst = strdup( psz_src );
1072 if( asprintf( ppsz_dst, "%s%s", psz_old, psz_src ) < 0 )
1073 *ppsz_dst = psz_old;
1080 static int CdTextParse( vlc_meta_t ***ppp_tracks, int *pi_tracks,
1081 const uint8_t *p_buffer, int i_buffer )
1083 char *pppsz_info[128][0x10];
1084 int i_track_last = -1;
1088 memset( pppsz_info, 0, sizeof(pppsz_info) );
1090 for( int i = 0; i < (i_buffer-4)/18; i++ )
1092 const uint8_t *p_block = &p_buffer[4 + 18*i];
1093 char psz_text[12+1];
1095 const int i_pack_type = p_block[0];
1096 if( i_pack_type < 0x80 || i_pack_type > 0x8f )
1099 const int i_track_number = (p_block[1] >> 0)&0x7f;
1100 const int i_extension_flag = ( p_block[1] >> 7)& 0x01;
1101 if( i_extension_flag )
1104 //const int i_sequence_number = p_block[2];
1105 //const int i_charater_position = (p_block[3] >> 0) &0x0f;
1106 //const int i_block_number = (p_block[3] >> 4) &0x07;
1107 /* TODO unicode support
1108 * I need a sample */
1109 //const int i_unicode = ( p_block[3] >> 7)&0x01;
1110 //const int i_crc = (p_block[4+12] << 8) | (p_block[4+13] << 0);
1113 memcpy( psz_text, &p_block[4], 12 );
1114 psz_text[12] = '\0';
1117 int i_track = i_track_number;
1118 char *psz_track = &psz_text[0];
1119 while( i_track <= 127 && psz_track < &psz_text[12] )
1121 //fprintf( stderr, "t=%d psz_track=%p end=%p", i_track, psz_track, &psz_text[12] );
1124 astrcat( &pppsz_info[i_track][i_pack_type-0x80], psz_track );
1125 i_track_last = __MAX( i_track_last, i_track );
1129 psz_track += 1 + strlen(psz_track);
1133 if( i_track_last < 0 )
1136 vlc_meta_t **pp_tracks = calloc( i_track_last+1, sizeof(*pp_tracks) );
1140 for( int j = 0; j < 0x10; j++ )
1142 for( int i = 0; i <= i_track_last; i++ )
1145 if( pppsz_info[i][j] )
1146 EnsureUTF8( pppsz_info[i][j] );
1149 const char *psz_default = pppsz_info[0][j];
1150 const char *psz_value = pppsz_info[i][j];
1152 if( !psz_value && !psz_default )
1154 vlc_meta_t *p_track = pp_tracks[i];
1157 p_track = pp_tracks[i] = vlc_meta_New();
1163 case 0x00: /* Album/Title */
1166 vlc_meta_SetAlbum( p_track, psz_value );
1171 vlc_meta_SetTitle( p_track, psz_value );
1173 vlc_meta_SetAlbum( p_track, psz_default );
1176 case 0x01: /* Performer */
1177 vlc_meta_SetArtist( p_track,
1178 psz_value ? psz_value : psz_default );
1180 case 0x05: /* Messages */
1181 vlc_meta_SetDescription( p_track,
1182 psz_value ? psz_value : psz_default );
1184 case 0x07: /* Genre */
1185 vlc_meta_SetGenre( p_track,
1186 psz_value ? psz_value : psz_default );
1188 /* FIXME unsupported:
1198 for( int j = 0; j < 0x10; j++ )
1199 for( int i = 0; i <= i_track_last; i++ )
1200 free( pppsz_info[i][j] );
1202 *ppp_tracks = pp_tracks;
1203 *pi_tracks = i_track_last+1;
1204 return pp_tracks ? 0 : -1;
1207 #if defined( __APPLE__ ) || \
1208 defined( __OS2__ ) || \
1209 defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) || \
1210 defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
1211 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1212 uint8_t **pp_buffer, int *pi_buffer )
1214 VLC_UNUSED( p_object );
1215 VLC_UNUSED( p_vcddev );
1216 VLC_UNUSED( pp_buffer );
1217 VLC_UNUSED( pi_buffer );
1220 #elif defined( WIN32 )
1221 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1222 uint8_t **pp_buffer, int *pi_buffer )
1224 VLC_UNUSED( p_object );
1226 CDROM_READ_TOC_EX TOCEx;
1227 memset(&TOCEx, 0, sizeof(TOCEx));
1228 TOCEx.Format = CDROM_READ_TOC_EX_FORMAT_CDTEXT;
1230 const int i_header_size = __MAX( 4, MINIMUM_CDROM_READ_TOC_EX_SIZE );
1231 uint8_t header[i_header_size];
1233 if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1234 &TOCEx, sizeof(TOCEx), header, i_header_size, &i_read, 0 ) )
1237 const int i_text = 2 + (header[0] << 8) + header[1];
1241 /* Read complete CD-TEXT */
1242 uint8_t *p_text = calloc( 1, i_text );
1244 return VLC_EGENERIC;
1246 if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1247 &TOCEx, sizeof(TOCEx), p_text, i_text, &i_read, 0 ) )
1250 return VLC_EGENERIC;
1254 *pp_buffer = p_text;
1255 *pi_buffer = i_text;
1259 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1260 uint8_t **pp_buffer, int *pi_buffer )
1262 VLC_UNUSED( p_object );
1264 if( p_vcddev->i_device_handle == -1 )
1267 struct cdrom_generic_command gc;
1270 /* Read CD-TEXT size */
1271 memset( header, 0, sizeof(header) );
1272 memset( &gc, 0, sizeof(gc) );
1273 gc.cmd[0] = 0x43; /* Read TOC */
1274 gc.cmd[1] = 0x02; /* MSF */
1275 gc.cmd[2] = 5; /* CD-Text */
1276 gc.cmd[7] = ( sizeof(header) >> 8 ) & 0xff;
1277 gc.cmd[8] = ( sizeof(header) >> 0 ) & 0xff;
1279 gc.buflen = sizeof(header);
1281 gc.data_direction = CGC_DATA_READ;
1284 if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1285 return VLC_EGENERIC;
1287 /* If the size is less than 4 it is an error, if it 4 then
1288 * it means no text data */
1289 const int i_text = 2 + (header[0] << 8) + header[1];
1291 return VLC_EGENERIC;
1293 /* Read complete CD-TEXT */
1294 uint8_t *p_text = calloc( 1, i_text );
1296 return VLC_EGENERIC;
1298 memset( &gc, 0, sizeof(gc) );
1299 gc.cmd[0] = 0x43; /* Read TOC */
1300 gc.cmd[1] = 0x02; /* MSF */
1301 gc.cmd[2] = 5; /* CD-Text */
1302 gc.cmd[7] = ( i_text >> 8 ) & 0xff;
1303 gc.cmd[8] = ( i_text >> 0 ) & 0xff;
1307 gc.data_direction = CGC_DATA_READ;
1310 if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1313 return VLC_EGENERIC;
1317 *pp_buffer = p_text;
1318 *pi_buffer = i_text;
1323 int ioctl_GetCdText( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1324 vlc_meta_t ***ppp_tracks, int *pi_tracks )
1329 if( p_vcddev->i_vcdimage_handle != -1 )
1332 if( CdTextRead( p_object, p_vcddev, &p_text, &i_text ) )
1335 CdTextParse( ppp_tracks, pi_tracks, p_text, i_text );