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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
|
/* GStreamer Audio CD Source Base Class
* Copyright (C) 2005 Tim-Philipp Müller <tim centricular net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library 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.
*/
/* TODO:
*
* - in ::start(), we want to post a tags message with an array or a list
* of tagslists of all tracks, so that applications know at least the
* number of tracks and all track durations immediately without having
* to do any querying. We have to decide what type and name to use for
* this array of track taglists.
*
* - FIX cddb discid calculation algorithm for mixed mode CDs - do we use
* offsets and duration of ALL tracks (data + audio) for the CDDB ID
* calculation, or only audio tracks?
*
* - Do we really need properties for the TOC bias/offset stuff? Wouldn't
* environment variables make much more sense? Do we need this at all
* (does it only affect ancient hardware?)
*/
/**
* SECTION:gstaudiocdsrc
* @short_description: Base class for Audio CD sources
*
* <refsect2>
* <para>
* Provides a base class for CD digital audio (CDDA) sources, which handles
* things like seeking, querying, discid calculation, tags, and buffer
* timestamping.
* </para>
* <title>Using GstAudioCdSrc-based elements in applications</title>
* <para>
* GstAudioCdSrc registers two #GstFormat<!-- -->s of its own, namely
* the "track" format and the "sector" format. Applications will usually
* only find the "track" format interesting. You can retrieve that #GstFormat
* for use in seek events or queries with gst_format_get_by_nick("track").
* </para>
* <para>
* In order to query the number of tracks, for example, an application would
* set the CDDA source element to READY or PAUSED state and then query the
* the number of tracks via gst_element_query_duration() using the track
* format acquired above. Applications can query the currently playing track
* in the same way.
* </para>
* <para>
* Alternatively, applications may retrieve the currently playing track and
* the total number of tracks from the taglist that will posted on the bus
* whenever the CD is opened or the currently playing track changes. The
* taglist will contain GST_TAG_TRACK_NUMBER and GST_TAG_TRACK_COUNT tags.
* </para>
* <para>
* Applications playing back CD audio using playbin and cdda://n URIs should
* issue a seek command in track format to change between tracks, rather than
* setting a new cdda://n+1 URI on playbin (as setting a new URI on playbin
* involves closing and re-opening the CD device, which is much much slower).
* </para>
* <title>Tags and meta-information</title>
* <para>
* CDDA sources will automatically emit a number of tags, details about which
* can be found in the libgsttag documentation. Those tags are:
* #GST_TAG_CDDA_CDDB_DISCID, #GST_TAG_CDDA_CDDB_DISCID_FULL,
* #GST_TAG_CDDA_MUSICBRAINZ_DISCID, #GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL,
* among others.
* </para>
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h> /* for strtol */
#include <gst/tag/tag.h>
#include <gst/audio/audio.h>
#include "gstaudiocdsrc.h"
#include "gst/gst-i18n-plugin.h"
GST_DEBUG_CATEGORY_STATIC (gst_audio_cd_src_debug);
#define GST_CAT_DEFAULT gst_audio_cd_src_debug
#define DEFAULT_DEVICE "/dev/cdrom"
#define CD_FRAMESIZE_RAW (2352)
#define SECTORS_PER_SECOND (75)
#define SECTORS_PER_MINUTE (75*60)
#define SAMPLES_PER_SECTOR (CD_FRAMESIZE_RAW >> 2)
#define TIME_INTERVAL_FROM_SECTORS(sectors) ((SAMPLES_PER_SECTOR * sectors * GST_SECOND) / 44100)
#define SECTORS_FROM_TIME_INTERVAL(dtime) (dtime * 44100 / (SAMPLES_PER_SECTOR * GST_SECOND))
enum
{
ARG_0,
ARG_MODE,
ARG_DEVICE,
ARG_TRACK,
ARG_TOC_OFFSET,
ARG_TOC_BIAS
};
static void gst_audio_cd_src_uri_handler_init (gpointer g_iface,
gpointer iface_data);
static void gst_audio_cd_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_audio_cd_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_audio_cd_src_finalize (GObject * obj);
static gboolean gst_audio_cd_src_query (GstBaseSrc * src, GstQuery * query);
static gboolean gst_audio_cd_src_handle_event (GstBaseSrc * basesrc,
GstEvent * event);
static gboolean gst_audio_cd_src_do_seek (GstBaseSrc * basesrc,
GstSegment * segment);
static gboolean gst_audio_cd_src_start (GstBaseSrc * basesrc);
static gboolean gst_audio_cd_src_stop (GstBaseSrc * basesrc);
static GstFlowReturn gst_audio_cd_src_create (GstPushSrc * pushsrc,
GstBuffer ** buf);
static gboolean gst_audio_cd_src_is_seekable (GstBaseSrc * basesrc);
static void gst_audio_cd_src_update_duration (GstAudioCdSrc * src);
static void gst_audio_cd_src_set_index (GstElement * src, GstIndex * index);
static GstIndex *gst_audio_cd_src_get_index (GstElement * src);
#define gst_audio_cd_src_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstAudioCdSrc, gst_audio_cd_src, GST_TYPE_PUSH_SRC,
G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
gst_audio_cd_src_uri_handler_init));
#define SRC_CAPS \
"audio/x-raw, " \
"format = (string) " GST_AUDIO_NE(S16) ", " \
"rate = (int) 44100, " \
"channels = (int) 2" \
static GstStaticPadTemplate gst_audio_cd_src_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (SRC_CAPS)
);
/* our two formats */
static GstFormat track_format;
static GstFormat sector_format;
GType
gst_audio_cd_src_mode_get_type (void)
{
static GType mode_type; /* 0 */
static const GEnumValue modes[] = {
{GST_AUDIO_CD_SRC_MODE_NORMAL, "Stream consists of a single track",
"normal"},
{GST_AUDIO_CD_SRC_MODE_CONTINUOUS, "Stream consists of the whole disc",
"continuous"},
{0, NULL, NULL}
};
if (mode_type == 0)
mode_type = g_enum_register_static ("GstAudioCdSrcMode", modes);
return mode_type;
}
static void
gst_audio_cd_src_class_init (GstAudioCdSrcClass * klass)
{
GstElementClass *element_class;
GstPushSrcClass *pushsrc_class;
GstBaseSrcClass *basesrc_class;
GObjectClass *gobject_class;
gobject_class = (GObjectClass *) klass;
element_class = (GstElementClass *) klass;
basesrc_class = (GstBaseSrcClass *) klass;
pushsrc_class = (GstPushSrcClass *) klass;
GST_DEBUG_CATEGORY_INIT (gst_audio_cd_src_debug, "audiocdsrc", 0,
"Audio CD source base class");
/* our very own formats */
track_format = gst_format_register ("track", "CD track");
sector_format = gst_format_register ("sector", "CD sector");
/* register CDDA tags */
gst_tag_register_musicbrainz_tags ();
#if 0
///// FIXME: what type to use here? ///////
gst_tag_register (GST_TAG_CDDA_TRACK_TAGS, GST_TAG_FLAG_META, GST_TYPE_TAG_LIST, "track-tags", "CDDA taglist for one track", gst_tag_merge_use_first); ///////////// FIXME: right function??? ///////
#endif
gobject_class->set_property = gst_audio_cd_src_set_property;
gobject_class->get_property = gst_audio_cd_src_get_property;
gobject_class->finalize = gst_audio_cd_src_finalize;
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
g_param_spec_string ("device", "Device", "CD device location",
NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MODE,
g_param_spec_enum ("mode", "Mode", "Mode", GST_TYPE_AUDIO_CD_SRC_MODE,
GST_AUDIO_CD_SRC_MODE_NORMAL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TRACK,
g_param_spec_uint ("track", "Track", "Track", 1, 99, 1,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
#if 0
/* Do we really need this toc adjustment stuff as properties? does the user
* have a chance to set it in practice, e.g. when using sound-juicer, rb,
* totem, whatever? Shouldn't we rather use environment variables
* for this? (tpm) */
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOC_OFFSET,
g_param_spec_int ("toc-offset", "Table of contents offset",
"Add <n> sectors to the values reported", G_MININT, G_MAXINT, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOC_BIAS,
g_param_spec_boolean ("toc-bias", "Table of contents bias",
"Assume that the beginning offset of track 1 as reported in the TOC "
"will be addressed as LBA 0. Necessary for some Toshiba drives to "
"get track boundaries", FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
#endif
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_audio_cd_src_src_template));
element_class->set_index = GST_DEBUG_FUNCPTR (gst_audio_cd_src_set_index);
element_class->get_index = GST_DEBUG_FUNCPTR (gst_audio_cd_src_get_index);
basesrc_class->start = GST_DEBUG_FUNCPTR (gst_audio_cd_src_start);
basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_audio_cd_src_stop);
basesrc_class->query = GST_DEBUG_FUNCPTR (gst_audio_cd_src_query);
basesrc_class->event = GST_DEBUG_FUNCPTR (gst_audio_cd_src_handle_event);
basesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_audio_cd_src_do_seek);
basesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_audio_cd_src_is_seekable);
pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_audio_cd_src_create);
}
static void
gst_audio_cd_src_init (GstAudioCdSrc * src)
{
/* we're not live and we operate in time */
gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
gst_base_src_set_live (GST_BASE_SRC (src), FALSE);
src->device = NULL;
src->mode = GST_AUDIO_CD_SRC_MODE_NORMAL;
src->uri_track = -1;
}
static void
gst_audio_cd_src_finalize (GObject * obj)
{
GstAudioCdSrc *cddasrc = GST_AUDIO_CD_SRC (obj);
g_free (cddasrc->uri);
g_free (cddasrc->device);
if (cddasrc->index)
gst_object_unref (cddasrc->index);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
static void
gst_audio_cd_src_set_device (GstAudioCdSrc * src, const gchar * device)
{
if (src->device)
g_free (src->device);
src->device = NULL;
if (!device)
return;
/* skip multiple slashes */
while (*device == '/' && *(device + 1) == '/')
device++;
#ifdef __sun
/*
* On Solaris, /dev/rdsk is used for accessing the CD device, but some
* applications pass in /dev/dsk, so correct.
*/
if (strncmp (device, "/dev/dsk", 8) == 0) {
gchar *rdsk_value;
rdsk_value = g_strdup_printf ("/dev/rdsk%s", device + 8);
src->device = g_strdup (rdsk_value);
g_free (rdsk_value);
} else {
#endif
src->device = g_strdup (device);
#ifdef __sun
}
#endif
}
static void
gst_audio_cd_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (object);
GST_OBJECT_LOCK (src);
switch (prop_id) {
case ARG_MODE:{
src->mode = g_value_get_enum (value);
break;
}
case ARG_DEVICE:{
const gchar *dev = g_value_get_string (value);
gst_audio_cd_src_set_device (src, dev);
break;
}
case ARG_TRACK:{
guint track = g_value_get_uint (value);
if (src->num_tracks > 0 && track > src->num_tracks) {
g_warning ("Invalid track %u", track);
} else if (track > 0 && src->tracks != NULL) {
src->cur_sector = src->tracks[track - 1].start;
src->uri_track = track;
} else {
src->uri_track = track; /* seek will be done in start() */
}
break;
}
case ARG_TOC_OFFSET:{
src->toc_offset = g_value_get_int (value);
break;
}
case ARG_TOC_BIAS:{
src->toc_bias = g_value_get_boolean (value);
break;
}
default:{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
GST_OBJECT_UNLOCK (src);
}
static void
gst_audio_cd_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstAudioCdSrcClass *klass = GST_AUDIO_CD_SRC_GET_CLASS (object);
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (object);
GST_OBJECT_LOCK (src);
switch (prop_id) {
case ARG_MODE:
g_value_set_enum (value, src->mode);
break;
case ARG_DEVICE:{
if (src->device == NULL && klass->get_default_device != NULL) {
gchar *d = klass->get_default_device (src);
if (d != NULL) {
g_value_set_string (value, DEFAULT_DEVICE);
g_free (d);
break;
}
}
if (src->device == NULL)
g_value_set_string (value, DEFAULT_DEVICE);
else
g_value_set_string (value, src->device);
break;
}
case ARG_TRACK:{
if (src->num_tracks <= 0 && src->uri_track > 0) {
g_value_set_uint (value, src->uri_track);
} else {
g_value_set_uint (value, src->cur_track + 1);
}
break;
}
case ARG_TOC_OFFSET:
g_value_set_int (value, src->toc_offset);
break;
case ARG_TOC_BIAS:
g_value_set_boolean (value, src->toc_bias);
break;
default:{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
GST_OBJECT_UNLOCK (src);
}
static gint
gst_audio_cd_src_get_track_from_sector (GstAudioCdSrc * src, gint sector)
{
gint i;
for (i = 0; i < src->num_tracks; ++i) {
if (sector >= src->tracks[i].start && sector <= src->tracks[i].end)
return i;
}
return -1;
}
static gboolean
gst_audio_cd_src_convert (GstAudioCdSrc * src, GstFormat src_format,
gint64 src_val, GstFormat dest_format, gint64 * dest_val)
{
gboolean started;
GST_LOG_OBJECT (src, "converting value %" G_GINT64_FORMAT " from %s into %s",
src_val, gst_format_get_name (src_format),
gst_format_get_name (dest_format));
if (src_format == dest_format) {
*dest_val = src_val;
return TRUE;
}
started = GST_OBJECT_FLAG_IS_SET (GST_BASE_SRC (src), GST_BASE_SRC_STARTED);
if (src_format == track_format) {
if (!started)
goto not_started;
if (src_val < 0 || src_val >= src->num_tracks) {
GST_DEBUG_OBJECT (src, "track number %d out of bounds", (gint) src_val);
goto wrong_value;
}
src_format = GST_FORMAT_DEFAULT;
src_val = src->tracks[src_val].start * SAMPLES_PER_SECTOR;
} else if (src_format == sector_format) {
src_format = GST_FORMAT_DEFAULT;
src_val = src_val * SAMPLES_PER_SECTOR;
}
if (src_format == dest_format) {
*dest_val = src_val;
goto done;
}
switch (src_format) {
case GST_FORMAT_BYTES:
/* convert to samples (4 bytes per sample) */
src_val = src_val >> 2;
/* fallthrough */
case GST_FORMAT_DEFAULT:{
switch (dest_format) {
case GST_FORMAT_BYTES:{
if (src_val < 0) {
GST_DEBUG_OBJECT (src, "sample source value negative");
goto wrong_value;
}
*dest_val = src_val << 2; /* 4 bytes per sample */
break;
}
case GST_FORMAT_TIME:{
*dest_val = gst_util_uint64_scale_int (src_val, GST_SECOND, 44100);
break;
}
default:{
gint64 sector = src_val / SAMPLES_PER_SECTOR;
if (dest_format == sector_format) {
*dest_val = sector;
} else if (dest_format == track_format) {
if (!started)
goto not_started;
*dest_val = gst_audio_cd_src_get_track_from_sector (src, sector);
} else {
goto unknown_format;
}
break;
}
}
break;
}
case GST_FORMAT_TIME:{
gint64 sample_offset;
if (src_val == GST_CLOCK_TIME_NONE) {
GST_DEBUG_OBJECT (src, "source time value invalid");
goto wrong_value;
}
sample_offset = gst_util_uint64_scale_int (src_val, 44100, GST_SECOND);
switch (dest_format) {
case GST_FORMAT_BYTES:{
*dest_val = sample_offset << 2; /* 4 bytes per sample */
break;
}
case GST_FORMAT_DEFAULT:{
*dest_val = sample_offset;
break;
}
default:{
gint64 sector = sample_offset / SAMPLES_PER_SECTOR;
if (dest_format == sector_format) {
*dest_val = sector;
} else if (dest_format == track_format) {
if (!started)
goto not_started;
*dest_val = gst_audio_cd_src_get_track_from_sector (src, sector);
} else {
goto unknown_format;
}
break;
}
}
break;
}
default:{
goto unknown_format;
}
}
done:
{
GST_LOG_OBJECT (src, "returning %" G_GINT64_FORMAT, *dest_val);
return TRUE;
}
unknown_format:
{
GST_DEBUG_OBJECT (src, "conversion failed: %s", "unsupported format");
return FALSE;
}
wrong_value:
{
GST_DEBUG_OBJECT (src, "conversion failed: %s",
"source value not within allowed range");
return FALSE;
}
not_started:
{
GST_DEBUG_OBJECT (src, "conversion failed: %s",
"cannot do this conversion, device not open");
return FALSE;
}
}
static gboolean
gst_audio_cd_src_query (GstBaseSrc * basesrc, GstQuery * query)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (basesrc);
gboolean started;
started = GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED);
GST_LOG_OBJECT (src, "handling %s query",
gst_query_type_get_name (GST_QUERY_TYPE (query)));
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_DURATION:{
GstFormat dest_format;
gint64 dest_val;
guint sectors;
gst_query_parse_duration (query, &dest_format, NULL);
if (!started)
return FALSE;
g_assert (src->tracks != NULL);
if (dest_format == track_format) {
GST_LOG_OBJECT (src, "duration: %d tracks", src->num_tracks);
gst_query_set_duration (query, track_format, src->num_tracks);
return TRUE;
}
if (src->cur_track < 0 || src->cur_track >= src->num_tracks)
return FALSE;
if (src->mode == GST_AUDIO_CD_SRC_MODE_NORMAL) {
sectors = src->tracks[src->cur_track].end -
src->tracks[src->cur_track].start + 1;
} else {
sectors = src->tracks[src->num_tracks - 1].end -
src->tracks[0].start + 1;
}
/* ... and convert into final format */
if (!gst_audio_cd_src_convert (src, sector_format, sectors,
dest_format, &dest_val)) {
return FALSE;
}
gst_query_set_duration (query, dest_format, dest_val);
GST_LOG ("duration: %u sectors, %" G_GINT64_FORMAT " in format %s",
sectors, dest_val, gst_format_get_name (dest_format));
break;
}
case GST_QUERY_POSITION:{
GstFormat dest_format;
gint64 pos_sector;
gint64 dest_val;
gst_query_parse_position (query, &dest_format, NULL);
if (!started)
return FALSE;
g_assert (src->tracks != NULL);
if (dest_format == track_format) {
GST_LOG_OBJECT (src, "position: track %d", src->cur_track);
gst_query_set_position (query, track_format, src->cur_track);
return TRUE;
}
if (src->cur_track < 0 || src->cur_track >= src->num_tracks)
return FALSE;
if (src->mode == GST_AUDIO_CD_SRC_MODE_NORMAL) {
pos_sector = src->cur_sector - src->tracks[src->cur_track].start;
} else {
pos_sector = src->cur_sector - src->tracks[0].start;
}
if (!gst_audio_cd_src_convert (src, sector_format, pos_sector,
dest_format, &dest_val)) {
return FALSE;
}
gst_query_set_position (query, dest_format, dest_val);
GST_LOG ("position: sector %u, %" G_GINT64_FORMAT " in format %s",
(guint) pos_sector, dest_val, gst_format_get_name (dest_format));
break;
}
case GST_QUERY_CONVERT:{
GstFormat src_format, dest_format;
gint64 src_val, dest_val;
gst_query_parse_convert (query, &src_format, &src_val, &dest_format,
NULL);
if (!gst_audio_cd_src_convert (src, src_format, src_val, dest_format,
&dest_val)) {
return FALSE;
}
gst_query_set_convert (query, src_format, src_val, dest_format, dest_val);
break;
}
default:{
GST_DEBUG_OBJECT (src, "unhandled query, chaining up to parent class");
return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
}
}
return TRUE;
}
static gboolean
gst_audio_cd_src_is_seekable (GstBaseSrc * basesrc)
{
return TRUE;
}
static gboolean
gst_audio_cd_src_do_seek (GstBaseSrc * basesrc, GstSegment * segment)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (basesrc);
gint64 seek_sector;
GST_DEBUG_OBJECT (src, "segment %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop));
if (!gst_audio_cd_src_convert (src, GST_FORMAT_TIME, segment->start,
sector_format, &seek_sector)) {
GST_WARNING_OBJECT (src, "conversion failed");
return FALSE;
}
/* we should only really be called when open */
g_assert (src->cur_track >= 0 && src->cur_track < src->num_tracks);
switch (src->mode) {
case GST_AUDIO_CD_SRC_MODE_NORMAL:
seek_sector += src->tracks[src->cur_track].start;
break;
case GST_AUDIO_CD_SRC_MODE_CONTINUOUS:
seek_sector += src->tracks[0].start;
break;
default:
g_return_val_if_reached (FALSE);
}
src->cur_sector = (gint) seek_sector;
GST_DEBUG_OBJECT (src, "seek'd to sector %d", src->cur_sector);
return TRUE;
}
static gboolean
gst_audio_cd_src_handle_track_seek (GstAudioCdSrc * src, gdouble rate,
GstSeekFlags flags, GstSeekType start_type, gint64 start,
GstSeekType stop_type, gint64 stop)
{
GstBaseSrc *basesrc = GST_BASE_SRC (src);
GstEvent *event;
if ((flags & GST_SEEK_FLAG_SEGMENT) == GST_SEEK_FLAG_SEGMENT) {
gint64 start_time = -1;
gint64 stop_time = -1;
if (src->mode != GST_AUDIO_CD_SRC_MODE_CONTINUOUS) {
GST_DEBUG_OBJECT (src, "segment seek in track format is only "
"supported in CONTINUOUS mode, not in mode %d", src->mode);
return FALSE;
}
switch (start_type) {
case GST_SEEK_TYPE_SET:
if (!gst_audio_cd_src_convert (src, track_format, start,
GST_FORMAT_TIME, &start_time)) {
GST_DEBUG_OBJECT (src, "cannot convert track %d to time",
(gint) start);
return FALSE;
}
break;
case GST_SEEK_TYPE_END:
if (!gst_audio_cd_src_convert (src, track_format,
src->num_tracks - start - 1, GST_FORMAT_TIME, &start_time)) {
GST_DEBUG_OBJECT (src, "cannot convert track %d to time",
(gint) start);
return FALSE;
}
start_type = GST_SEEK_TYPE_SET;
break;
case GST_SEEK_TYPE_NONE:
start_time = -1;
break;
default:
g_return_val_if_reached (FALSE);
}
switch (stop_type) {
case GST_SEEK_TYPE_SET:
if (!gst_audio_cd_src_convert (src, track_format, stop,
GST_FORMAT_TIME, &stop_time)) {
GST_DEBUG_OBJECT (src, "cannot convert track %d to time",
(gint) stop);
return FALSE;
}
break;
case GST_SEEK_TYPE_END:
if (!gst_audio_cd_src_convert (src, track_format,
src->num_tracks - stop - 1, GST_FORMAT_TIME, &stop_time)) {
GST_DEBUG_OBJECT (src, "cannot convert track %d to time",
(gint) stop);
return FALSE;
}
stop_type = GST_SEEK_TYPE_SET;
break;
case GST_SEEK_TYPE_NONE:
stop_time = -1;
break;
default:
g_return_val_if_reached (FALSE);
}
GST_LOG_OBJECT (src, "seek segment %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
GST_TIME_ARGS (start_time), GST_TIME_ARGS (stop_time));
/* send fake segment seek event in TIME format to
* base class, which will hopefully handle the rest */
event = gst_event_new_seek (rate, GST_FORMAT_TIME, flags, start_type,
start_time, stop_type, stop_time);
return GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
}
/* not a segment seek */
if (start_type == GST_SEEK_TYPE_NONE) {
GST_LOG_OBJECT (src, "start seek type is NONE, nothing to do");
return TRUE;
}
if (stop_type != GST_SEEK_TYPE_NONE) {
GST_WARNING_OBJECT (src, "ignoring stop seek type (expected NONE)");
}
if (start < 0 || start >= src->num_tracks) {
GST_DEBUG_OBJECT (src, "invalid track %" G_GINT64_FORMAT, start);
return FALSE;
}
GST_DEBUG_OBJECT (src, "seeking to track %" G_GINT64_FORMAT, start + 1);
src->cur_sector = src->tracks[start].start;
GST_DEBUG_OBJECT (src, "starting at sector %d", src->cur_sector);
if (src->cur_track != start) {
src->cur_track = (gint) start;
src->uri_track = -1;
src->prev_track = -1;
gst_audio_cd_src_update_duration (src);
} else {
GST_DEBUG_OBJECT (src, "is current track, just seeking back to start");
}
/* send fake segment seek event in TIME format to
* base class (so we get a newsegment etc.) */
event = gst_event_new_seek (rate, GST_FORMAT_TIME, flags,
GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1);
return GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
}
static gboolean
gst_audio_cd_src_handle_event (GstBaseSrc * basesrc, GstEvent * event)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (basesrc);
gboolean ret = FALSE;
GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEEK:{
GstSeekType start_type, stop_type;
GstSeekFlags flags;
GstFormat format;
gdouble rate;
gint64 start, stop;
if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED)) {
GST_DEBUG_OBJECT (src, "seek failed: device not open");
break;
}
gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
&stop_type, &stop);
if (format == sector_format) {
GST_DEBUG_OBJECT (src, "seek in sector format not supported");
break;
}
if (format == track_format) {
ret = gst_audio_cd_src_handle_track_seek (src, rate, flags,
start_type, start, stop_type, stop);
} else {
GST_LOG_OBJECT (src, "let base class handle seek in %s format",
gst_format_get_name (format));
event = gst_event_ref (event);
ret = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
}
break;
}
default:{
GST_LOG_OBJECT (src, "let base class handle event");
ret = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
break;
}
}
return ret;
}
static GstURIType
gst_audio_cd_src_uri_get_type (GType type)
{
return GST_URI_SRC;
}
static const gchar *const *
gst_audio_cd_src_uri_get_protocols (GType type)
{
static const gchar *protocols[] = { "cdda", NULL };
return protocols;
}
static gchar *
gst_audio_cd_src_uri_get_uri (GstURIHandler * handler)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (handler);
GST_OBJECT_LOCK (src);
/* FIXME: can we get rid of all that here and just return a copy of the
* existing URI perhaps? */
g_free (src->uri);
if (GST_OBJECT_FLAG_IS_SET (GST_BASE_SRC (src), GST_BASE_SRC_STARTED)) {
src->uri =
g_strdup_printf ("cdda://%s#%d", src->device,
(src->uri_track > 0) ? src->uri_track : 1);
} else {
src->uri = g_strdup ("cdda://1");
}
GST_OBJECT_UNLOCK (src);
return g_strdup (src->uri);
}
/* Note: gst_element_make_from_uri() might call us with just 'cdda://' as
* URI and expects us to return TRUE then (and this might be in any state) */
/* We accept URIs of the format cdda://(device#track)|(track) */
static gboolean
gst_audio_cd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
GError ** error)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (handler);
const gchar *location;
gchar *track_number;
GST_OBJECT_LOCK (src);
location = uri + 7;
track_number = g_strrstr (location, "#");
src->uri_track = 0;
/* FIXME 0.11: ignore URI fragments that look like device paths for
* the benefit of rhythmbox and possibly other applications.
*/
if (track_number && track_number[1] != '/') {
gchar *device, *nuri = g_strdup (uri);
track_number = nuri + (track_number - uri);
*track_number = '\0';
device = gst_uri_get_location (nuri);
gst_audio_cd_src_set_device (src, device);
g_free (device);
src->uri_track = strtol (track_number + 1, NULL, 10);
g_free (nuri);
} else {
if (*location == '\0')
src->uri_track = 1;
else
src->uri_track = strtol (location, NULL, 10);
}
if (src->uri_track < 1)
goto failed;
if (src->num_tracks > 0
&& src->tracks != NULL && src->uri_track > src->num_tracks)
goto failed;
if (src->uri_track > 0 && src->tracks != NULL) {
GST_OBJECT_UNLOCK (src);
gst_pad_send_event (GST_BASE_SRC_PAD (src),
gst_event_new_seek (1.0, track_format, GST_SEEK_FLAG_FLUSH,
GST_SEEK_TYPE_SET, src->uri_track - 1, GST_SEEK_TYPE_NONE, -1));
} else {
/* seek will be done in start() */
GST_OBJECT_UNLOCK (src);
}
GST_LOG_OBJECT (handler, "successfully handled uri '%s'", uri);
return TRUE;
failed:
{
GST_OBJECT_UNLOCK (src);
GST_DEBUG_OBJECT (src, "cannot handle URI '%s'", uri);
g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
"Could not handle CDDA URI");
return FALSE;
}
}
static void
gst_audio_cd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
{
GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
iface->get_type = gst_audio_cd_src_uri_get_type;
iface->get_uri = gst_audio_cd_src_uri_get_uri;
iface->set_uri = gst_audio_cd_src_uri_set_uri;
iface->get_protocols = gst_audio_cd_src_uri_get_protocols;
}
/**
* gst_audio_cd_src_add_track:
* @src: a #GstAudioCdSrc
* @track: address of #GstAudioCdSrcTrack to add
*
* CDDA sources use this function from their start vfunc to announce the
* available data and audio tracks to the base source class. The caller
* should allocate @track on the stack, the base source will do a shallow
* copy of the structure (and take ownership of the taglist if there is one).
*
* Returns: FALSE on error, otherwise TRUE.
*/
gboolean
gst_audio_cd_src_add_track (GstAudioCdSrc * src, GstAudioCdSrcTrack * track)
{
g_return_val_if_fail (GST_IS_AUDIO_CD_SRC (src), FALSE);
g_return_val_if_fail (track != NULL, FALSE);
g_return_val_if_fail (track->num > 0, FALSE);
GST_DEBUG_OBJECT (src, "adding track %2u (%2u) [%6u-%6u] [%5s], tags: %"
GST_PTR_FORMAT, src->num_tracks + 1, track->num, track->start,
track->end, (track->is_audio) ? "AUDIO" : "DATA ", track->tags);
if (src->num_tracks > 0) {
guint end_of_previous_track = src->tracks[src->num_tracks - 1].end;
if (track->start <= end_of_previous_track) {
GST_WARNING ("track %2u overlaps with previous tracks", track->num);
return FALSE;
}
}
GST_OBJECT_LOCK (src);
++src->num_tracks;
src->tracks = g_renew (GstAudioCdSrcTrack, src->tracks, src->num_tracks);
src->tracks[src->num_tracks - 1] = *track;
GST_OBJECT_UNLOCK (src);
return TRUE;
}
static void
gst_audio_cd_src_update_duration (GstAudioCdSrc * src)
{
GstBaseSrc *basesrc;
gint64 dur;
basesrc = GST_BASE_SRC (src);
if (!gst_pad_query_duration (GST_BASE_SRC_PAD (src), GST_FORMAT_TIME, &dur)) {
dur = GST_CLOCK_TIME_NONE;
}
basesrc->segment.duration = dur;
gst_element_post_message (GST_ELEMENT (src),
gst_message_new_duration (GST_OBJECT (src), GST_FORMAT_TIME, -1));
GST_LOG_OBJECT (src, "duration updated to %" GST_TIME_FORMAT,
GST_TIME_ARGS (dur));
}
#define CD_MSF_OFFSET 150
/* the cddb hash function */
static guint
cddb_sum (gint n)
{
guint ret;
ret = 0;
while (n > 0) {
ret += (n % 10);
n /= 10;
}
return ret;
}
static void
gst_audio_cd_src_calculate_musicbrainz_discid (GstAudioCdSrc * src)
{
GString *s;
GChecksum *sha;
guchar digest[20];
gchar *ptr;
gchar tmp[9];
gulong i;
guint leadout_sector;
gsize digest_len;
s = g_string_new (NULL);
leadout_sector = src->tracks[src->num_tracks - 1].end + 1 + CD_MSF_OFFSET;
/* generate SHA digest */
sha = g_checksum_new (G_CHECKSUM_SHA1);
g_snprintf (tmp, sizeof (tmp), "%02X", src->tracks[0].num);
g_string_append_printf (s, "%02X", src->tracks[0].num);
g_checksum_update (sha, (guchar *) tmp, 2);
g_snprintf (tmp, sizeof (tmp), "%02X", src->tracks[src->num_tracks - 1].num);
g_string_append_printf (s, " %02X", src->tracks[src->num_tracks - 1].num);
g_checksum_update (sha, (guchar *) tmp, 2);
g_snprintf (tmp, sizeof (tmp), "%08X", leadout_sector);
g_string_append_printf (s, " %08X", leadout_sector);
g_checksum_update (sha, (guchar *) tmp, 8);
for (i = 0; i < 99; i++) {
if (i < src->num_tracks) {
guint frame_offset = src->tracks[i].start + CD_MSF_OFFSET;
g_snprintf (tmp, sizeof (tmp), "%08X", frame_offset);
g_string_append_printf (s, " %08X", frame_offset);
g_checksum_update (sha, (guchar *) tmp, 8);
} else {
g_checksum_update (sha, (guchar *) "00000000", 8);
}
}
digest_len = 20;
g_checksum_get_digest (sha, (guint8 *) & digest, &digest_len);
/* re-encode to base64 */
ptr = g_base64_encode (digest, digest_len);
g_checksum_free (sha);
i = strlen (ptr);
g_assert (i < sizeof (src->mb_discid) + 1);
memcpy (src->mb_discid, ptr, i);
src->mb_discid[i] = '\0';
free (ptr);
/* Replace '/', '+' and '=' by '_', '.' and '-' as specified on
* http://musicbrainz.org/doc/DiscIDCalculation
*/
for (ptr = src->mb_discid; *ptr != '\0'; ptr++) {
if (*ptr == '/')
*ptr = '_';
else if (*ptr == '+')
*ptr = '.';
else if (*ptr == '=')
*ptr = '-';
}
GST_DEBUG_OBJECT (src, "musicbrainz-discid = %s", src->mb_discid);
GST_DEBUG_OBJECT (src, "musicbrainz-discid-full = %s", s->str);
gst_tag_list_add (src->tags, GST_TAG_MERGE_REPLACE,
GST_TAG_CDDA_MUSICBRAINZ_DISCID, src->mb_discid,
GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL, s->str, NULL);
g_string_free (s, TRUE);
}
static void
lba_to_msf (guint sector, guint * p_m, guint * p_s, guint * p_f, guint * p_secs)
{
guint m, s, f;
m = sector / SECTORS_PER_MINUTE;
sector = sector % SECTORS_PER_MINUTE;
s = sector / SECTORS_PER_SECOND;
f = sector % SECTORS_PER_SECOND;
if (p_m)
*p_m = m;
if (p_s)
*p_s = s;
if (p_f)
*p_f = f;
if (p_secs)
*p_secs = s + (m * 60);
}
static void
gst_audio_cd_src_calculate_cddb_id (GstAudioCdSrc * src)
{
GString *s;
guint first_sector = 0, last_sector = 0;
guint start_secs, end_secs, secs, len_secs;
guint total_secs, num_audio_tracks;
guint id, t, i;
id = 0;
total_secs = 0;
num_audio_tracks = 0;
/* FIXME: do we use offsets and duration of ALL tracks (data + audio)
* for the CDDB ID calculation, or only audio tracks? */
for (i = 0; i < src->num_tracks; ++i) {
if (1) { /* src->tracks[i].is_audio) { */
if (num_audio_tracks == 0) {
first_sector = src->tracks[i].start + CD_MSF_OFFSET;
}
last_sector = src->tracks[i].end + CD_MSF_OFFSET + 1;
++num_audio_tracks;
lba_to_msf (src->tracks[i].start + CD_MSF_OFFSET, NULL, NULL, NULL,
&secs);
len_secs = (src->tracks[i].end - src->tracks[i].start + 1) / 75;
GST_DEBUG_OBJECT (src, "track %02u: lsn %6u (%02u:%02u), "
"length: %u seconds (%02u:%02u)",
num_audio_tracks, src->tracks[i].start + CD_MSF_OFFSET,
secs / 60, secs % 60, len_secs, len_secs / 60, len_secs % 60);
id += cddb_sum (secs);
total_secs += len_secs;
}
}
/* first_sector = src->tracks[0].start + CD_MSF_OFFSET; */
lba_to_msf (first_sector, NULL, NULL, NULL, &start_secs);
/* last_sector = src->tracks[src->num_tracks-1].end + CD_MSF_OFFSET; */
lba_to_msf (last_sector, NULL, NULL, NULL, &end_secs);
GST_DEBUG_OBJECT (src, "first_sector = %u = %u secs (%02u:%02u)",
first_sector, start_secs, start_secs / 60, start_secs % 60);
GST_DEBUG_OBJECT (src, "last_sector = %u = %u secs (%02u:%02u)",
last_sector, end_secs, end_secs / 60, end_secs % 60);
t = end_secs - start_secs;
GST_DEBUG_OBJECT (src, "total length = %u secs (%02u:%02u), added title "
"lengths = %u seconds (%02u:%02u)", t, t / 60, t % 60, total_secs,
total_secs / 60, total_secs % 60);
src->discid = ((id % 0xff) << 24 | t << 8 | num_audio_tracks);
s = g_string_new (NULL);
g_string_append_printf (s, "%08x", src->discid);
gst_tag_list_add (src->tags, GST_TAG_MERGE_REPLACE,
GST_TAG_CDDA_CDDB_DISCID, s->str, NULL);
g_string_append_printf (s, " %u", src->num_tracks);
for (i = 0; i < src->num_tracks; ++i) {
g_string_append_printf (s, " %u", src->tracks[i].start + CD_MSF_OFFSET);
}
g_string_append_printf (s, " %u", t);
gst_tag_list_add (src->tags, GST_TAG_MERGE_REPLACE,
GST_TAG_CDDA_CDDB_DISCID_FULL, s->str, NULL);
GST_DEBUG_OBJECT (src, "cddb discid = %s", s->str);
g_string_free (s, TRUE);
}
static void
gst_audio_cd_src_add_tags (GstAudioCdSrc * src)
{
gint i;
/* fill in details for each track */
for (i = 0; i < src->num_tracks; ++i) {
gint64 duration;
guint num_sectors;
if (src->tracks[i].tags == NULL)
src->tracks[i].tags = gst_tag_list_new_empty ();
num_sectors = src->tracks[i].end - src->tracks[i].start + 1;
gst_audio_cd_src_convert (src, sector_format, num_sectors,
GST_FORMAT_TIME, &duration);
gst_tag_list_add (src->tracks[i].tags,
GST_TAG_MERGE_REPLACE,
GST_TAG_TRACK_NUMBER, i + 1,
GST_TAG_TRACK_COUNT, src->num_tracks, GST_TAG_DURATION, duration, NULL);
}
/* now fill in per-album tags and include each track's tags
* in the album tags, so that interested parties can retrieve
* the relevant details for each track in one go */
/* /////////////////////////////// FIXME should we rather insert num_tracks
* tags by the name of 'track-tags' and have the caller use
* gst_tag_list_get_value_index() rather than use tag names incl.
* the track number ?? *////////////////////////////////////////
gst_tag_list_add (src->tags, GST_TAG_MERGE_REPLACE,
GST_TAG_TRACK_COUNT, src->num_tracks, NULL);
#if 0
for (i = 0; i < src->num_tracks; ++i) {
gst_tag_list_add (src->tags, GST_TAG_MERGE_APPEND,
GST_TAG_CDDA_TRACK_TAGS, src->tracks[i].tags, NULL);
}
#endif
GST_DEBUG ("src->tags = %" GST_PTR_FORMAT, src->tags);
}
static void
gst_audio_cd_src_add_index_associations (GstAudioCdSrc * src)
{
gint i;
for (i = 0; i < src->num_tracks; i++) {
gint64 sector;
sector = src->tracks[i].start;
gst_index_add_association (src->index, src->index_id, GST_ASSOCIATION_FLAG_KEY_UNIT, track_format, i, /* here we count from 0 */
sector_format, sector,
GST_FORMAT_TIME,
(gint64) (((CD_FRAMESIZE_RAW >> 2) * sector * GST_SECOND) / 44100),
GST_FORMAT_BYTES, (gint64) (sector << 2), GST_FORMAT_DEFAULT,
(gint64) ((CD_FRAMESIZE_RAW >> 2) * sector), NULL);
}
}
static void
gst_audio_cd_src_set_index (GstElement * element, GstIndex * index)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (element);
GstIndex *old;
GST_OBJECT_LOCK (element);
old = src->index;
if (old == index) {
GST_OBJECT_UNLOCK (element);
return;
}
if (index)
gst_object_ref (index);
src->index = index;
GST_OBJECT_UNLOCK (element);
if (old)
gst_object_unref (old);
if (index) {
gst_index_get_writer_id (index, GST_OBJECT (src), &src->index_id);
gst_index_add_format (index, src->index_id, track_format);
gst_index_add_format (index, src->index_id, sector_format);
}
}
static GstIndex *
gst_audio_cd_src_get_index (GstElement * element)
{
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (element);
GstIndex *index;
GST_OBJECT_LOCK (element);
if ((index = src->index))
gst_object_ref (index);
GST_OBJECT_UNLOCK (element);
return index;
}
static gint
gst_audio_cd_src_track_sort_func (gconstpointer a, gconstpointer b,
gpointer foo)
{
GstAudioCdSrcTrack *track_a = ((GstAudioCdSrcTrack *) a);
GstAudioCdSrcTrack *track_b = ((GstAudioCdSrcTrack *) b);
/* sort data tracks to the end, and audio tracks by track number */
if (track_a->is_audio == track_b->is_audio)
return (gint) track_a->num - (gint) track_b->num;
if (track_a->is_audio) {
return -1;
} else {
return 1;
}
}
static gboolean
gst_audio_cd_src_start (GstBaseSrc * basesrc)
{
GstAudioCdSrcClass *klass = GST_AUDIO_CD_SRC_GET_CLASS (basesrc);
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (basesrc);
gboolean ret;
gchar *device = NULL;
src->discid = 0;
src->mb_discid[0] = '\0';
g_assert (klass->open != NULL);
if (src->device != NULL) {
device = g_strdup (src->device);
} else if (klass->get_default_device != NULL) {
device = klass->get_default_device (src);
}
if (device == NULL)
device = g_strdup (DEFAULT_DEVICE);
GST_LOG_OBJECT (basesrc, "opening device %s", device);
src->tags = gst_tag_list_new_empty ();
ret = klass->open (src, device);
g_free (device);
device = NULL;
if (!ret) {
GST_DEBUG_OBJECT (basesrc, "failed to open device");
/* subclass (should have) posted an error message with the details */
gst_audio_cd_src_stop (basesrc);
return FALSE;
}
if (src->num_tracks == 0 || src->tracks == NULL) {
GST_DEBUG_OBJECT (src, "no tracks");
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
(_("This CD has no audio tracks")), (NULL));
gst_audio_cd_src_stop (basesrc);
return FALSE;
}
/* need to calculate disc IDs before we ditch the data tracks */
gst_audio_cd_src_calculate_cddb_id (src);
gst_audio_cd_src_calculate_musicbrainz_discid (src);
#if 0
/* adjust sector offsets if necessary */
if (src->toc_bias) {
src->toc_offset -= src->tracks[0].start;
}
for (i = 0; i < src->num_tracks; ++i) {
src->tracks[i].start += src->toc_offset;
src->tracks[i].end += src->toc_offset;
}
#endif
/* now that we calculated the various disc IDs,
* sort the data tracks to end and ignore them */
src->num_all_tracks = src->num_tracks;
g_qsort_with_data (src->tracks, src->num_tracks,
sizeof (GstAudioCdSrcTrack), gst_audio_cd_src_track_sort_func, NULL);
while (src->num_tracks > 0 && !src->tracks[src->num_tracks - 1].is_audio)
--src->num_tracks;
if (src->num_tracks == 0) {
GST_DEBUG_OBJECT (src, "no audio tracks");
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
(_("This CD has no audio tracks")), (NULL));
gst_audio_cd_src_stop (basesrc);
return FALSE;
}
gst_audio_cd_src_add_tags (src);
if (src->index && GST_INDEX_IS_WRITABLE (src->index))
gst_audio_cd_src_add_index_associations (src);
src->cur_track = 0;
src->prev_track = -1;
if (src->uri_track > 0 && src->uri_track <= src->num_tracks) {
GST_LOG_OBJECT (src, "seek to track %d", src->uri_track);
src->cur_track = src->uri_track - 1;
src->uri_track = -1;
src->mode = GST_AUDIO_CD_SRC_MODE_NORMAL;
}
src->cur_sector = src->tracks[src->cur_track].start;
GST_LOG_OBJECT (src, "starting at sector %d", src->cur_sector);
gst_audio_cd_src_update_duration (src);
return TRUE;
}
static void
gst_audio_cd_src_clear_tracks (GstAudioCdSrc * src)
{
if (src->tracks != NULL) {
gint i;
for (i = 0; i < src->num_all_tracks; ++i) {
if (src->tracks[i].tags)
gst_tag_list_free (src->tracks[i].tags);
}
g_free (src->tracks);
src->tracks = NULL;
}
src->num_tracks = 0;
src->num_all_tracks = 0;
}
static gboolean
gst_audio_cd_src_stop (GstBaseSrc * basesrc)
{
GstAudioCdSrcClass *klass = GST_AUDIO_CD_SRC_GET_CLASS (basesrc);
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (basesrc);
g_assert (klass->close != NULL);
klass->close (src);
gst_audio_cd_src_clear_tracks (src);
if (src->tags) {
gst_tag_list_free (src->tags);
src->tags = NULL;
}
src->prev_track = -1;
src->cur_track = -1;
return TRUE;
}
static GstFlowReturn
gst_audio_cd_src_create (GstPushSrc * pushsrc, GstBuffer ** buffer)
{
GstAudioCdSrcClass *klass = GST_AUDIO_CD_SRC_GET_CLASS (pushsrc);
GstAudioCdSrc *src = GST_AUDIO_CD_SRC (pushsrc);
GstBuffer *buf;
gboolean eos;
GstClockTime position = GST_CLOCK_TIME_NONE;
GstClockTime duration = GST_CLOCK_TIME_NONE;
gint64 qry_position;
g_assert (klass->read_sector != NULL);
switch (src->mode) {
case GST_AUDIO_CD_SRC_MODE_NORMAL:
eos = (src->cur_sector > src->tracks[src->cur_track].end);
break;
case GST_AUDIO_CD_SRC_MODE_CONTINUOUS:
eos = (src->cur_sector > src->tracks[src->num_tracks - 1].end);
src->cur_track = gst_audio_cd_src_get_track_from_sector (src,
src->cur_sector);
break;
default:
g_return_val_if_reached (GST_FLOW_ERROR);
}
if (eos) {
src->prev_track = -1;
GST_DEBUG_OBJECT (src, "EOS at sector %d, cur_track=%d, mode=%d",
src->cur_sector, src->cur_track, src->mode);
/* base class will send EOS for us */
return GST_FLOW_EOS;
}
if (src->prev_track != src->cur_track) {
GstTagList *tags;
tags = gst_tag_list_merge (src->tags, src->tracks[src->cur_track].tags,
GST_TAG_MERGE_REPLACE);
GST_LOG_OBJECT (src, "announcing tags: %" GST_PTR_FORMAT, tags);
gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tags));
src->prev_track = src->cur_track;
gst_audio_cd_src_update_duration (src);
g_object_notify (G_OBJECT (src), "track");
}
GST_LOG_OBJECT (src, "asking for sector %u", src->cur_sector);
buf = klass->read_sector (src, src->cur_sector);
if (buf == NULL) {
GST_WARNING_OBJECT (src, "failed to read sector %u", src->cur_sector);
return GST_FLOW_ERROR;
}
if (gst_pad_query_position (GST_BASE_SRC_PAD (src), GST_FORMAT_TIME,
&qry_position)) {
gint64 next_ts = 0;
position = (GstClockTime) qry_position;
++src->cur_sector;
if (gst_pad_query_position (GST_BASE_SRC_PAD (src), GST_FORMAT_TIME,
&next_ts)) {
duration = (GstClockTime) (next_ts - qry_position);
}
--src->cur_sector;
}
/* fallback duration: 4 bytes per sample, 44100 samples per second */
if (duration == GST_CLOCK_TIME_NONE) {
duration = gst_util_uint64_scale_int (gst_buffer_get_size (buf) >> 2,
GST_SECOND, 44100);
}
GST_BUFFER_TIMESTAMP (buf) = position;
GST_BUFFER_DURATION (buf) = duration;
GST_LOG_OBJECT (src, "pushing sector %d with timestamp %" GST_TIME_FORMAT,
src->cur_sector, GST_TIME_ARGS (position));
++src->cur_sector;
*buffer = buf;
return GST_FLOW_OK;
}
|