~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/input/cddaInputStream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  cdda input class based on cdparanoia
 
3
  Copyright (C) 2000  Martin Vogt
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU Library General Public License as published by
 
7
  the Free Software Foundation.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
#ifdef HAVE_CONFIG_H
 
15
#include <config.h>
 
16
#endif
 
17
 
 
18
#ifdef CDDA_PARANOIA
 
19
 
 
20
#include "cddaInputStream.h"
 
21
#include "inputDetector.h"
 
22
 
 
23
 
 
24
void paranoiaCallback(long, int) {
 
25
  //cout << "long:"<<a<<" int:"<<b<<endl;
 
26
}
 
27
 
 
28
CDDAInputStream::CDDAInputStream() {
 
29
  drive = NULL;
 
30
  paranoia=NULL;
 
31
  device=NULL;
 
32
  track=1;
 
33
}
 
34
 
 
35
 
 
36
CDDAInputStream::~CDDAInputStream() {
 
37
  close();
 
38
}
 
39
 
 
40
// here we should encdoe the track Nr. as well
 
41
// eg: /dev/cdrom#1
 
42
int CDDAInputStream::getTrackAndDevice(const char* url) {
 
43
  int matches=0;
 
44
  // dest = "cdda:/dev/cdrom/track01.cda"
 
45
  char* noprotoString=InputDetector::removeProtocol(url);
 
46
  // noprotoString="/dev/cdrom/track01.cda"
 
47
  char* filename=InputDetector::getFilename(noprotoString);
 
48
  // filename="track01.cda"
 
49
  char* filenameNoExt=InputDetector::getWithoutExtension(filename);
 
50
  // filenameNoExt="track01"
 
51
  char* dir=InputDetector::removeExtension(noprotoString,filename);
 
52
  // dir="/dev/cdrom/"
 
53
  device=InputDetector::removeSlash(dir);
 
54
  track=1;
 
55
  if (filenameNoExt != NULL) {
 
56
    matches=sscanf(filenameNoExt,"track%02d",&track);
 
57
  }
 
58
  if (matches == 0) {
 
59
    cout << "no trackNumber found using default"<<endl;
 
60
  } 
 
61
  cout << "device:"<<device<<" track:"<<track<<endl;
 
62
 
 
63
  if (noprotoString != NULL) {
 
64
    delete noprotoString;
 
65
  }
 
66
  if (filename != NULL) {
 
67
    delete filename;
 
68
  }
 
69
  if (filenameNoExt != NULL) {
 
70
    delete filenameNoExt;
 
71
  }
 
72
  if (dir != NULL) {
 
73
    delete dir;
 
74
  }
 
75
  if (device == NULL) {
 
76
    cout << "no device found, using any"<<endl;
 
77
    return false;
 
78
  }
 
79
  return true;
 
80
}
 
81
 
 
82
int CDDAInputStream::open(const char* dest) {
 
83
  if (getTrackAndDevice(dest) == true) {
 
84
    drive = cdda_identify(device, CDDA_MESSAGE_PRINTIT, 0);
 
85
  }
 
86
 
 
87
  if (drive == NULL) {
 
88
    cout << "cdda_identify failed trying to find a device"<<endl;
 
89
    drive=cdda_find_a_cdrom(CDDA_MESSAGE_PRINTIT, 0);
 
90
  }
 
91
  if (drive == NULL) {
 
92
    cout << "nope. nothing found. give up"<<endl;
 
93
    return false;
 
94
  }
 
95
  cout << "cdda_open -s"<<endl;
 
96
  if (cdda_open(drive) != 0) {
 
97
    cout << "cdda_open(drive) failed"<<endl;
 
98
    close();
 
99
    return false;
 
100
  }
 
101
  cout << "cdda_open -e"<<endl;
 
102
 
 
103
  // debug things a bit
 
104
  int trackCount = drive->tracks;
 
105
  for (int i = 1; i <= trackCount; i++) {
 
106
    if (IS_AUDIO(drive, i)) {
 
107
      printf("track%02d.cda\n", i);
 
108
    } else {
 
109
      printf("no audio:%d\n",i);
 
110
    }
 
111
  }
 
112
 
 
113
  paranoia = paranoia_init(drive);
 
114
  if (paranoia == NULL) {
 
115
    cout << "paranoia init failed"<<endl;
 
116
    close();
 
117
    return false;
 
118
  }
 
119
 
 
120
  firstSector=cdda_track_firstsector(drive, track);
 
121
  lastSector=cdda_track_lastsector(drive, track);
 
122
  currentSector=firstSector;
 
123
  // paranoia && drive != NULL -> initialized!
 
124
 
 
125
  int paranoiaLevel = PARANOIA_MODE_FULL ^ PARANOIA_MODE_NEVERSKIP;
 
126
  paranoia_modeset(paranoia, paranoiaLevel);
 
127
  cdda_verbose_set(drive, CDDA_MESSAGE_PRINTIT, CDDA_MESSAGE_PRINTIT);
 
128
  paranoia_seek(paranoia, firstSector, SEEK_SET);
 
129
 
 
130
  return true;
 
131
}
 
132
 
 
133
 
 
134
void CDDAInputStream::close() {
 
135
  if (isOpen() == false) {
 
136
    return;
 
137
  }
 
138
  cdda_close(drive);
 
139
  drive=NULL;
 
140
  if (paranoia != NULL) {
 
141
    paranoia_free(paranoia);
 
142
    paranoia = 0;
 
143
  }
 
144
  if (device != NULL) {
 
145
    delete device;
 
146
    device=NULL;
 
147
  }
 
148
}
 
149
 
 
150
 
 
151
int CDDAInputStream::isOpen() {
 
152
  return (drive != NULL); 
 
153
}
 
154
 
 
155
 
 
156
int CDDAInputStream::eof() {
 
157
  if (isOpen()==false) {
 
158
    return true;
 
159
  }
 
160
  if (currentSector >= lastSector) {
 
161
    return true;  
 
162
  }
 
163
  return false;
 
164
}
 
165
 
 
166
 
 
167
int CDDAInputStream::read(char* dest,int len) {
 
168
  if (len != 2*CD_FRAMESIZE_RAW) {
 
169
    cout << "len must be 2*CD_FRAMESIZE_RAW"<<endl;
 
170
    exit(0);
 
171
  }
 
172
  int16_t * buf = paranoia_read(paranoia, paranoiaCallback);
 
173
  currentSector++;
 
174
  if (buf == NULL) {
 
175
    cout << "paranoia_read failed"<<endl;
 
176
    close();
 
177
    return 0;
 
178
  }
 
179
  memcpy(dest,buf,sizeof(int16_t)*CD_FRAMESIZE_RAW);
 
180
  return CD_FRAMESIZE_RAW;  
 
181
}
 
182
 
 
183
 
 
184
int CDDAInputStream::seek(long bytePos) {
 
185
  int byteLength=getByteLength();
 
186
  float ratio=(float)bytePos/(float)(byteLength+1);
 
187
  float wantSector=ratio*(float)((lastSector-firstSector));
 
188
  if (isOpen()) {
 
189
    currentSector=(int)wantSector;
 
190
    cout << "paranoia_seek:"<<currentSector<<endl;
 
191
    paranoia_seek(paranoia, currentSector, SEEK_SET);
 
192
  }
 
193
  return true;  
 
194
}
 
195
 
 
196
void CDDAInputStream::clear() {
 
197
  cout << "direct virtual call CDDAInputStream::clear:"<<endl;
 
198
}
 
199
 
 
200
 
 
201
long CDDAInputStream::getByteLength() {
 
202
  int sectors=lastSector-firstSector;
 
203
  int bytes=sectors*CD_FRAMESIZE_RAW*sizeof(int16_t);
 
204
  cout << "getByteLength:"<<bytes<<endl;
 
205
  return bytes;
 
206
}
 
207
 
 
208
 
 
209
long CDDAInputStream::getBytePosition() {
 
210
  int readSectors=currentSector-firstSector;
 
211
  int bytes=readSectors*CD_FRAMESIZE_RAW*sizeof(int16_t);
 
212
  return bytes;
 
213
}
 
214
 
 
215
#endif
 
216
//CDDA_PARANOIA
 
217
 
 
218
 
 
219
 
 
220
 
 
221