1 /* GStreamer
2 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-cdiocddasrc
22 * @see_also: GstCdParanoiaSrc, GstAudioCdSrc
23 *
24 * <refsect2>
25 * <para>
26 * cdiocddasrc reads and extracts raw audio from Audio CDs. It can operate
27 * in one of two modes:
28 * <itemizedlist>
29 * <listitem><para>
30 * treat each track as a separate stream, counting time from the start
31 * of the track to the end of the track and posting EOS at the end of
32 * a track, or
33 * </para></listitem>
34 * <listitem><para>
35 * treat the entire disc as one stream, counting time from the start of
36 * the first track to the end of the last track, posting EOS only at
37 * the end of the last track.
38 * </para></listitem>
39 * </itemizedlist>
40 * </para>
41 * <para>
42 * With a recent-enough version of libcdio, the element will extract
43 * CD-TEXT if this is supported by the CD-drive and CD-TEXT information
44 * is available on the CD. The information will be posted on the bus in
45 * form of a tag message.
46 * </para>
47 * <para>
48 * When opened, the element will also calculate a CDDB disc ID and a
49 * MusicBrainz disc ID, which applications can use to query online
50 * databases for artist/title information. These disc IDs will also be
51 * posted on the bus as part of the tag messages.
52 * </para>
53 * <para>
54 * cdiocddasrc supports the GstUriHandler interface, so applications can use
55 * playbin with cdda://<track-number> URIs for playback (they will have
56 * to connect to playbin's notify::source signal and set the device on the
57 * cd source in the notify callback if they want to set the device property).
58 * Applications should use seeks in "track" format to switch between different
59 * tracks of the same CD (passing a new cdda:// URI to playbin involves opening
60 * and closing the CD device, which is much slower).
61 * </para>
62 * <title>Example launch line</title>
63 * <para>
64 * <programlisting>
65 * gst-launch-1.0 cdiocddasrc track=5 device=/dev/cdrom ! audioconvert ! vorbisenc ! oggmux ! filesink location=track5.ogg
66 * </programlisting>
67 * This pipeline extracts track 5 of the audio CD and encodes it into an
68 * Ogg/Vorbis file.
69 * </para>
70 * </refsect2>
71 */
72
73 #ifdef HAVE_CONFIG_H
74 #include "config.h"
75 #endif
76
77 #include "gstcdio.h"
78 #include "gstcdiocddasrc.h"
79
80 #include <gst/gst.h>
81 #include "gst/gst-i18n-plugin.h"
82
83 #include <sys/types.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <errno.h>
87
88 #define SAMPLES_PER_SECTOR (CDIO_CD_FRAMESIZE_RAW / sizeof (gint16))
89
90 #define DEFAULT_READ_SPEED -1
91
92 enum
93 {
94 PROP_0 = 0,
95 PROP_READ_SPEED
96 };
97
98 G_DEFINE_TYPE (GstCdioCddaSrc, gst_cdio_cdda_src, GST_TYPE_AUDIO_CD_SRC);
99
100 static void gst_cdio_cdda_src_finalize (GObject * obj);
101 static void gst_cdio_cdda_src_set_property (GObject * object, guint prop_id,
102 const GValue * value, GParamSpec * pspec);
103 static void gst_cdio_cdda_src_get_property (GObject * object, guint prop_id,
104 GValue * value, GParamSpec * pspec);
105 static GstBuffer *gst_cdio_cdda_src_read_sector (GstAudioCdSrc * src,
106 gint sector);
107 static gboolean gst_cdio_cdda_src_open (GstAudioCdSrc * src,
108 const gchar * device);
109 static void gst_cdio_cdda_src_close (GstAudioCdSrc * src);
110
111 #if 0
112 static gchar *
113 gst_cdio_cdda_src_get_default_device (GstAudioCdSrc * audiocdsrc)
114 {
115 GstCdioCddaSrc *src;
116 gchar *default_device, *ret;
117
118 src = GST_CDIO_CDDA_SRC (audiocdsrc);
119
120 /* src->cdio may be NULL here */
121 default_device = cdio_get_default_device (src->cdio);
122
123 ret = g_strdup (default_device);
124 free (default_device);
125
126 GST_LOG_OBJECT (src, "returning default device: %s", GST_STR_NULL (ret));
127
128 return ret;
129 }
130
131 static gchar **
132 gst_cdio_cdda_src_probe_devices (GstAudioCdSrc * audiocdsrc)
133 {
134 char **devices, **ret, **d;
135
136 /* FIXME: might return the same hardware device twice, e.g.
137 * as /dev/cdrom and /dev/dvd - gotta do something more sophisticated */
138 devices = cdio_get_devices (DRIVER_DEVICE);
139
140 if (devices == NULL)
141 goto no_devices;
142
143 if (*devices == NULL)
144 goto empty_devices;
145
146 ret = g_strdupv (devices);
147 for (d = devices; *d != NULL; ++d) {
148 GST_DEBUG_OBJECT (audiocdsrc, "device: %s", GST_STR_NULL (*d));
149 free (*d);
150 }
151 free (devices);
152
153 return ret;
154
155 /* ERRORS */
156 no_devices:
157 {
158 GST_DEBUG_OBJECT (audiocdsrc, "no devices found");
159 return NULL;
160 }
161 empty_devices:
162 {
163 GST_DEBUG_OBJECT (audiocdsrc, "empty device list found");
164 free (devices);
165 return NULL;
166 }
167 }
168 #endif
169
170 static GstBuffer *
gst_cdio_cdda_src_read_sector(GstAudioCdSrc * audiocdsrc,gint sector)171 gst_cdio_cdda_src_read_sector (GstAudioCdSrc * audiocdsrc, gint sector)
172 {
173 GstCdioCddaSrc *src;
174 guint8 *data;
175
176 src = GST_CDIO_CDDA_SRC (audiocdsrc);
177
178 data = g_malloc (CDIO_CD_FRAMESIZE_RAW);
179
180 /* can't use pad_alloc because we can't return the GstFlowReturn (FIXME 0.11) */
181 if (cdio_read_audio_sector (src->cdio, data, sector) != 0)
182 goto read_failed;
183
184 if (src->swap_le_be) {
185 gint16 *pcm_data = (gint16 *) data;
186 gint i;
187
188 for (i = 0; i < SAMPLES_PER_SECTOR; ++i)
189 pcm_data[i] = GUINT16_SWAP_LE_BE (pcm_data[i]);
190 }
191
192 return gst_buffer_new_wrapped (data, CDIO_CD_FRAMESIZE_RAW);
193
194 /* ERRORS */
195 read_failed:
196 {
197 GST_WARNING_OBJECT (src, "read at sector %d failed!", sector);
198 GST_ELEMENT_ERROR (src, RESOURCE, READ,
199 (_("Could not read from CD.")),
200 ("cdio_read_audio_sector at %d failed: %s", sector,
201 g_strerror (errno)));
202 g_free (data);
203 return NULL;
204 }
205 }
206
207 static gboolean
gst_cdio_cdda_src_do_detect_drive_endianness(GstCdioCddaSrc * src,gint from,gint to)208 gst_cdio_cdda_src_do_detect_drive_endianness (GstCdioCddaSrc * src, gint from,
209 gint to)
210 {
211 gint16 pcm_data[SAMPLES_PER_SECTOR], last_pcm_ne, last_pcm_oe;
212 gdouble ne_sumd0, ne_sumd1, ne_factor;
213 gdouble oe_sumd0, oe_sumd1, oe_factor;
214 gdouble diff;
215 gint sector;
216 gint i;
217
218 ne_sumd0 = ne_sumd1 = 0.0;
219 oe_sumd0 = oe_sumd1 = 0.0;
220 last_pcm_ne = 0;
221 last_pcm_oe = 0;
222
223 GST_LOG_OBJECT (src, "checking sector %d to %d", from, to);
224
225 for (sector = from; sector < to; ++sector) {
226 if (cdio_read_audio_sector (src->cdio, pcm_data, sector) != 0)
227 goto read_failed;
228
229 /* only evaluate samples for left channel */
230 for (i = 0; i < SAMPLES_PER_SECTOR; i += 2) {
231 gint16 pcm;
232
233 /* Native endianness first */
234 pcm = pcm_data[i];
235 ne_sumd0 += abs (pcm);
236 ne_sumd1 += abs (pcm - last_pcm_ne);
237 last_pcm_ne = pcm;
238
239 /* other endianness next */
240 pcm = GUINT16_SWAP_LE_BE (pcm);
241 oe_sumd0 += abs (pcm);
242 oe_sumd1 += abs (pcm - last_pcm_oe);
243 last_pcm_oe = pcm;
244 }
245
246 }
247
248 ne_factor = (ne_sumd1 / ne_sumd0);
249 oe_factor = (oe_sumd1 / oe_sumd0);
250 diff = ne_factor - oe_factor;
251
252 GST_DEBUG_OBJECT (src, "Native: %.2f, Other: %.2f, diff: %.2f",
253 ne_factor, oe_factor, diff);
254
255 if (diff > 0.5) {
256 GST_INFO_OBJECT (src, "Drive produces samples in other endianness");
257 src->swap_le_be = TRUE;
258 return TRUE;
259 } else if (diff < -0.5) {
260 GST_INFO_OBJECT (src, "Drive produces samples in host endianness");
261 src->swap_le_be = FALSE;
262 return TRUE;
263 } else {
264 GST_INFO_OBJECT (src, "Inconclusive, assuming host endianness");
265 src->swap_le_be = FALSE;
266 return FALSE;
267 }
268
269 /* ERRORS */
270 read_failed:
271 {
272 GST_WARNING_OBJECT (src, "could not read sector %d", sector);
273 src->swap_le_be = FALSE;
274 return FALSE;
275 }
276 }
277
278 static void
gst_cdio_cdda_src_detect_drive_endianness(GstCdioCddaSrc * src,gint first,gint last)279 gst_cdio_cdda_src_detect_drive_endianness (GstCdioCddaSrc * src, gint first,
280 gint last)
281 {
282 gint from, to;
283
284 GST_INFO ("Detecting drive endianness");
285
286 /* try middle of disc first */
287 from = (first + last) / 2;
288 to = MIN (from + 10, last);
289 if (gst_cdio_cdda_src_do_detect_drive_endianness (src, from, to))
290 return;
291
292 /* if that was inconclusive, try other places */
293 from = (first + last) / 4;
294 to = MIN (from + 10, last);
295 if (gst_cdio_cdda_src_do_detect_drive_endianness (src, from, to))
296 return;
297
298 from = (first + last) * 3 / 4;
299 to = MIN (from + 10, last);
300 if (gst_cdio_cdda_src_do_detect_drive_endianness (src, from, to))
301 return;
302
303 /* if that's still inconclusive, we give up and assume host endianness */
304 return;
305 }
306
307 static gboolean
notcdio_track_is_audio_track(const CdIo * p_cdio,track_t i_track)308 notcdio_track_is_audio_track (const CdIo * p_cdio, track_t i_track)
309 {
310 return (cdio_get_track_format (p_cdio, i_track) == TRACK_FORMAT_AUDIO);
311 }
312
313 static gboolean
gst_cdio_cdda_src_open(GstAudioCdSrc * audiocdsrc,const gchar * device)314 gst_cdio_cdda_src_open (GstAudioCdSrc * audiocdsrc, const gchar * device)
315 {
316 GstCdioCddaSrc *src;
317 discmode_t discmode;
318 gint first_track, num_tracks, i;
319 gint first_audio_sector = 0, last_audio_sector = 0;
320 #if LIBCDIO_VERSION_NUM > 83 || LIBCDIO_VERSION_NUM < 76
321 cdtext_t *cdtext;
322 #endif
323
324 src = GST_CDIO_CDDA_SRC (audiocdsrc);
325
326 g_assert (device != NULL);
327 g_assert (src->cdio == NULL);
328
329 GST_LOG_OBJECT (src, "trying to open device %s", device);
330
331 if (!(src->cdio = cdio_open (device, DRIVER_UNKNOWN)))
332 goto open_failed;
333
334 discmode = cdio_get_discmode (src->cdio);
335 GST_LOG_OBJECT (src, "discmode: %d", (gint) discmode);
336
337 if (discmode != CDIO_DISC_MODE_CD_DA && discmode != CDIO_DISC_MODE_CD_MIXED)
338 goto not_audio;
339
340 first_track = cdio_get_first_track_num (src->cdio);
341 num_tracks = cdio_get_num_tracks (src->cdio);
342
343 if (num_tracks <= 0 || first_track < 0)
344 return TRUE; /* base class will generate 'has no tracks' error */
345
346 if (src->read_speed != -1)
347 cdio_set_speed (src->cdio, src->read_speed);
348
349 #if LIBCDIO_VERSION_NUM > 83 || LIBCDIO_VERSION_NUM < 76
350 cdtext = cdio_get_cdtext (src->cdio);
351
352 if (NULL == cdtext)
353 GST_DEBUG_OBJECT (src, "no CD-TEXT on disc");
354 else
355 gst_cdio_add_cdtext_album_tags (GST_OBJECT_CAST (src), cdtext,
356 audiocdsrc->tags);
357 #else
358 gst_cdio_add_cdtext_album_tags (GST_OBJECT_CAST (src), src->cdio,
359 audiocdsrc->tags);
360 #endif
361
362 GST_LOG_OBJECT (src, "%u tracks, first track: %d", num_tracks, first_track);
363
364 for (i = 0; i < num_tracks; ++i) {
365 GstAudioCdSrcTrack track = { 0, };
366 gint len_sectors;
367
368 len_sectors = cdio_get_track_sec_count (src->cdio, i + first_track);
369
370 track.num = i + first_track;
371 track.is_audio = notcdio_track_is_audio_track (src->cdio, i + first_track);
372
373 /* Note: LSN/LBA confusion all around us; in any case, this does
374 * the right thing here (for cddb id calculations etc. as well) */
375 track.start = cdio_get_track_lsn (src->cdio, i + first_track);
376 track.end = track.start + len_sectors - 1; /* -1? */
377
378 if (track.is_audio) {
379 first_audio_sector = MIN (first_audio_sector, track.start);
380 last_audio_sector = MAX (last_audio_sector, track.end);
381 }
382 #if LIBCDIO_VERSION_NUM > 83 || LIBCDIO_VERSION_NUM < 76
383 if (NULL != cdtext)
384 track.tags = gst_cdio_get_cdtext (GST_OBJECT (src), cdtext,
385 i + first_track);
386 #else
387 track.tags = gst_cdio_get_cdtext (GST_OBJECT (src), src->cdio,
388 i + first_track);
389 #endif
390
391 gst_audio_cd_src_add_track (GST_AUDIO_CD_SRC (src), &track);
392 }
393
394 /* Try to detect if we need to byte-order swap the samples coming from the
395 * drive, which might be the case if the CD drive operates in a different
396 * endianness than the host CPU's endianness (happens on e.g. Powerbook G4) */
397 gst_cdio_cdda_src_detect_drive_endianness (src, first_audio_sector,
398 last_audio_sector);
399
400 return TRUE;
401
402 /* ERRORS */
403 open_failed:
404 {
405 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
406 (_("Could not open CD device for reading.")),
407 ("cdio_open() failed: %s", g_strerror (errno)));
408 return FALSE;
409 }
410 not_audio:
411 {
412 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
413 (_("Disc is not an Audio CD.")), ("discmode: %d", (gint) discmode));
414
415 cdio_destroy (src->cdio);
416 src->cdio = NULL;
417 return FALSE;
418 }
419 }
420
421 static void
gst_cdio_cdda_src_close(GstAudioCdSrc * audiocdsrc)422 gst_cdio_cdda_src_close (GstAudioCdSrc * audiocdsrc)
423 {
424 GstCdioCddaSrc *src = GST_CDIO_CDDA_SRC (audiocdsrc);
425
426 if (src->cdio) {
427 cdio_destroy (src->cdio);
428 src->cdio = NULL;
429 }
430 }
431
432 static void
gst_cdio_cdda_src_init(GstCdioCddaSrc * src)433 gst_cdio_cdda_src_init (GstCdioCddaSrc * src)
434 {
435 src->read_speed = DEFAULT_READ_SPEED; /* don't need atomic access here */
436 src->cdio = NULL;
437 }
438
439 static void
gst_cdio_cdda_src_finalize(GObject * obj)440 gst_cdio_cdda_src_finalize (GObject * obj)
441 {
442 GstCdioCddaSrc *src = GST_CDIO_CDDA_SRC (obj);
443
444 if (src->cdio) {
445 cdio_destroy (src->cdio);
446 src->cdio = NULL;
447 }
448
449 G_OBJECT_CLASS (gst_cdio_cdda_src_parent_class)->finalize (obj);
450 }
451
452 static void
gst_cdio_cdda_src_class_init(GstCdioCddaSrcClass * klass)453 gst_cdio_cdda_src_class_init (GstCdioCddaSrcClass * klass)
454 {
455 GstAudioCdSrcClass *audiocdsrc_class = GST_AUDIO_CD_SRC_CLASS (klass);
456 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
457 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
458
459 gobject_class->set_property = gst_cdio_cdda_src_set_property;
460 gobject_class->get_property = gst_cdio_cdda_src_get_property;
461 gobject_class->finalize = gst_cdio_cdda_src_finalize;
462
463 audiocdsrc_class->open = gst_cdio_cdda_src_open;
464 audiocdsrc_class->close = gst_cdio_cdda_src_close;
465 audiocdsrc_class->read_sector = gst_cdio_cdda_src_read_sector;
466 #if 0
467 audiocdsrc_class->probe_devices = gst_cdio_cdda_src_probe_devices;
468 audiocdsrc_class->get_default_device = gst_cdio_cdda_src_get_default_device;
469 #endif
470
471 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_READ_SPEED,
472 g_param_spec_int ("read-speed", "Read speed",
473 "Read from device at the specified speed (-1 = default)", -1, 100,
474 DEFAULT_READ_SPEED, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475
476 gst_element_class_set_static_metadata (element_class,
477 "CD audio source (CDDA)", "Source/File",
478 "Read audio from CD using libcdio",
479 "Tim-Philipp Müller <tim centricular net>");
480 }
481
482 static void
gst_cdio_cdda_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)483 gst_cdio_cdda_src_set_property (GObject * object, guint prop_id,
484 const GValue * value, GParamSpec * pspec)
485 {
486 GstCdioCddaSrc *src = GST_CDIO_CDDA_SRC (object);
487
488 switch (prop_id) {
489 case PROP_READ_SPEED:{
490 gint speed;
491
492 speed = g_value_get_int (value);
493 g_atomic_int_set (&src->read_speed, speed);
494 break;
495 }
496 default:
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 break;
499 }
500 }
501
502 static void
gst_cdio_cdda_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)503 gst_cdio_cdda_src_get_property (GObject * object, guint prop_id,
504 GValue * value, GParamSpec * pspec)
505 {
506 GstCdioCddaSrc *src = GST_CDIO_CDDA_SRC (object);
507
508 switch (prop_id) {
509 case PROP_READ_SPEED:{
510 gint speed;
511
512 speed = g_atomic_int_get (&src->read_speed);
513 g_value_set_int (value, speed);
514 break;
515 }
516 default:
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
518 break;
519 }
520 }
521