File: mb_linux.cpp

package info (click to toggle)
mkcue 1-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 528 kB
  • ctags: 252
  • sloc: sh: 2,618; cpp: 1,747; makefile: 83
file content (185 lines) | stat: -rw-r--r-- 4,453 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* --------------------------------------------------------------------------

   MusicBrainz -- The Internet music metadatabase

   Copyright (C) 2000 Robert Kaye
   Copyright (C) 1999 Marc E E van Woerkom
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id: mb_linux.cpp,v 1.4 2004/04/26 20:10:44 robert Exp $

----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>

#include "mb.h"
#include "diskid.h"
#include "config.h"


#define XA_INTERVAL ((60 + 90 + 2) * CD_FRAMES)


MUSICBRAINZ_DEVICE DEFAULT_DEVICE = "/dev/cdrom";


int ReadTOCHeader(int fd, 
                  int& first, 
                  int& last)
{
   struct cdrom_tochdr th;
   struct cdrom_multisession ms;

   int ret = ioctl(fd,
                   CDROMREADTOCHDR,
                   &th);

   if (!ret)
   {
      // Hide the last track if this is a multisession disc (note that
      // currently only dual-session discs with one track in the second
      // session are handled correctly).
      ms.addr_format = CDROM_LBA;
      ret = ioctl(fd,
                  CDROMMULTISESSION,
                  &ms);

      first = th.cdth_trk0;
      last = th.cdth_trk1;
      if (ms.xa_flag)
         last--;
   }

   return ret;
}


int ReadTOCEntry(int fd, 
                 int track, 
                 int& lba)
{
   struct cdrom_tocentry te;
   struct cdrom_multisession ms;
   int ret = 0;

   if (track == CDROM_LEADOUT)
   {
      // For a multisession disc, the location of the single-session lead-out
      // track must be calculated based on where the last session begins.
      ms.addr_format = CDROM_LBA;
      ret = ioctl(fd,
                  CDROMMULTISESSION,
                  &ms);

      if (ms.xa_flag)
      {
         lba = ms.addr.lba - XA_INTERVAL;
         return ret;
      }
   }

   if (!ret)
   {
      te.cdte_track = track;
      te.cdte_format = CDROM_LBA;

      ret = ioctl(fd, 
                  CDROMREADTOCENTRY, 
                  &te);

      assert(te.cdte_format == CDROM_LBA);

      lba = te.cdte_addr.lba;
   }

   return ret;
}


bool DiskId::ReadTOC(MUSICBRAINZ_DEVICE device, 
                     MUSICBRAINZ_CDINFO& cdinfo)
{
   int  fd;
   int  first;
   int  last;
   int  lba;
   int  i;

   if (device == NULL)
   {
       device = DEFAULT_DEVICE;
   }

   fd = open(device, O_RDONLY | O_NONBLOCK);
   if (fd < 0)
   {
       char err[256];
       sprintf(err,"Cannot open '%s'", device);
       ReportError(err);
       return false;
   }

   // Initialize cdinfo to all zeroes.
   memset(&cdinfo, 0, sizeof(MUSICBRAINZ_CDINFO));

   // Find the number of the first track (usually 1) and the last track.
   if (ReadTOCHeader(fd, first, last))
   {
      ReportError("Cannot read table of contents.");
      close(fd);
      return false;
   }

   // Do some basic error checking.
   if (last==0)
   {
      ReportError("This disk has no tracks.");
      close(fd);	
      return false;
   }

   // Get the logical block address (lba) for the end of the audio data.
   // The "LEADOUT" track is the track beyond the final audio track
   // so we're looking for the block address of the LEADOUT track.
   ReadTOCEntry(fd, 
                CDROM_LEADOUT, 
                lba);

   cdinfo.FrameOffset[0] = lba + 150;

   // Now, for every track, find out the block address where it starts.
   for (i = first; i <= last; i++)
   {
      ReadTOCEntry(fd, i, lba);
      cdinfo.FrameOffset[i] = lba + 150;
   }

   cdinfo.FirstTrack = first;
   cdinfo.LastTrack = last;

   close(fd);

   return true;
}