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 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
* Libbrasero-burn
* Copyright (C) Philippe Rouquier 2005-2009 <bonfire-app@wanadoo.fr>
*
* Libbrasero-burn is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Libbrasero-burn authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Libbrasero-burn. This permission is above and beyond the permissions granted
* by the GPL license by which Libbrasero-burn is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Libbrasero-burn 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 General Public License
* along with this program; if not, write to:
* The Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <gmodule.h>
#include <gst/gst.h>
#include "burn-basics.h"
#include "brasero-medium.h"
#include "brasero-tags.h"
#include "burn-job.h"
#include "brasero-plugin-registration.h"
#include "burn-normalize.h"
#define BRASERO_TYPE_TRANSCODE (brasero_transcode_get_type ())
#define BRASERO_TRANSCODE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), BRASERO_TYPE_TRANSCODE, BraseroTranscode))
#define BRASERO_TRANSCODE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), BRASERO_TYPE_TRANSCODE, BraseroTranscodeClass))
#define BRASERO_IS_TRANSCODE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), BRASERO_TYPE_TRANSCODE))
#define BRASERO_IS_TRANSCODE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), BRASERO_TYPE_TRANSCODE))
#define BRASERO_TRANSCODE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), BRASERO_TYPE_TRANSCODE, BraseroTranscodeClass))
BRASERO_PLUGIN_BOILERPLATE (BraseroTranscode, brasero_transcode, BRASERO_TYPE_JOB, BraseroJob);
static gboolean brasero_transcode_bus_messages (GstBus *bus,
GstMessage *msg,
BraseroTranscode *transcode);
static void brasero_transcode_new_decoded_pad_cb (GstElement *decode,
GstPad *pad,
gboolean arg2,
BraseroTranscode *transcode);
struct BraseroTranscodePrivate {
GstElement *pipeline;
GstElement *convert;
GstElement *source;
GstElement *decode;
GstElement *sink;
/* element to link decode to */
GstElement *link;
gint pad_size;
gint pad_fd;
gint pad_id;
gint64 size;
gint64 pos;
gulong probe;
gint64 segment_start;
gint64 segment_end;
guint set_active_state:1;
guint mp3_size_pipeline:1;
};
typedef struct BraseroTranscodePrivate BraseroTranscodePrivate;
#define BRASERO_TRANSCODE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BRASERO_TYPE_TRANSCODE, BraseroTranscodePrivate))
static GObjectClass *parent_class = NULL;
static gboolean
brasero_transcode_buffer_handler (GstPad *pad,
GstBuffer *buffer,
BraseroTranscode *self)
{
BraseroTranscodePrivate *priv;
GstPad *peer;
gint64 size;
priv = BRASERO_TRANSCODE_PRIVATE (self);
size = GST_BUFFER_SIZE (buffer);
if (priv->segment_start <= 0 && priv->segment_end <= 0)
return TRUE;
/* what we do here is more or less what gstreamer does when seeking:
* it reads and process from 0 to the seek position (I tried).
* It even forwards the data before the seek position to the sink (which
* is a problem in our case as it would be written) */
if (priv->size > priv->segment_end) {
priv->size += size;
return FALSE;
}
if (priv->size + size > priv->segment_end) {
GstBuffer *new_buffer;
int data_size;
/* the entire the buffer is not interesting for us */
/* create a new buffer and push it on the pad:
* NOTE: we're going to receive it ... */
data_size = priv->segment_end - priv->size;
new_buffer = gst_buffer_new_and_alloc (data_size);
memcpy (GST_BUFFER_DATA (new_buffer), GST_BUFFER_DATA (buffer), data_size);
/* Recursive: the following calls ourselves BEFORE we finish */
peer = gst_pad_get_peer (pad);
gst_pad_push (peer, new_buffer);
priv->size += size - data_size;
/* post an EOS event to stop pipeline */
gst_pad_push_event (peer, gst_event_new_eos ());
gst_object_unref (peer);
return FALSE;
}
/* see if the buffer is in the segment */
if (priv->size < priv->segment_start) {
GstBuffer *new_buffer;
gint data_size;
/* see if all the buffer is interesting for us */
if (priv->size + size < priv->segment_start) {
priv->size += size;
return FALSE;
}
/* create a new buffer and push it on the pad:
* NOTE: we're going to receive it ... */
data_size = priv->size + size - priv->segment_start;
new_buffer = gst_buffer_new_and_alloc (data_size);
memcpy (GST_BUFFER_DATA (new_buffer),
GST_BUFFER_DATA (buffer) +
GST_BUFFER_SIZE (buffer) -
data_size,
data_size);
GST_BUFFER_TIMESTAMP (new_buffer) = GST_BUFFER_TIMESTAMP (buffer) + data_size;
/* move forward by the size of bytes we dropped */
priv->size += size - data_size;
/* this is recursive the following calls ourselves
* BEFORE we finish */
peer = gst_pad_get_peer (pad);
gst_pad_push (peer, new_buffer);
gst_object_unref (peer);
return FALSE;
}
priv->size += size;
priv->pos += size;
return TRUE;
}
static BraseroBurnResult
brasero_transcode_set_boundaries (BraseroTranscode *transcode)
{
BraseroTranscodePrivate *priv;
BraseroTrack *track;
gint64 start;
gint64 end;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
/* we need to reach the song start and set a possible end; this is only
* needed when it is decoding a song. Otherwise*/
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
start = brasero_track_stream_get_start (BRASERO_TRACK_STREAM (track));
end = brasero_track_stream_get_end (BRASERO_TRACK_STREAM (track));
priv->segment_start = BRASERO_DURATION_TO_BYTES (start);
priv->segment_end = BRASERO_DURATION_TO_BYTES (end);
BRASERO_JOB_LOG (transcode, "settings track boundaries time = %lli %lli / bytes = %lli %lli",
start, end,
priv->segment_start, priv->segment_end);
return BRASERO_BURN_OK;
}
static void
brasero_transcode_send_volume_event (BraseroTranscode *transcode)
{
BraseroTranscodePrivate *priv;
gdouble track_peak = 0.0;
gdouble track_gain = 0.0;
GstTagList *tag_list;
BraseroTrack *track;
GstEvent *event;
GValue *value;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
BRASERO_JOB_LOG (transcode, "Sending audio levels tags");
if (brasero_track_tag_lookup (track, BRASERO_TRACK_PEAK_VALUE, &value) == BRASERO_BURN_OK)
track_peak = g_value_get_double (value);
if (brasero_track_tag_lookup (track, BRASERO_TRACK_GAIN_VALUE, &value) == BRASERO_BURN_OK)
track_gain = g_value_get_double (value);
/* it's possible we fail */
tag_list = gst_tag_list_new ();
gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
GST_TAG_TRACK_GAIN, track_gain,
GST_TAG_TRACK_PEAK, track_peak,
NULL);
/* NOTE: that event is goind downstream */
event = gst_event_new_tag (tag_list);
if (!gst_element_send_event (priv->convert, event))
BRASERO_JOB_LOG (transcode, "Couldn't send tags to rgvolume");
BRASERO_JOB_LOG (transcode, "Set volume level %lf %lf", track_gain, track_peak);
}
static GstElement *
brasero_transcode_create_volume (BraseroTranscode *transcode,
BraseroTrack *track)
{
GstElement *volume = NULL;
/* see if we need a volume object */
if (brasero_track_tag_lookup (track, BRASERO_TRACK_PEAK_VALUE, NULL) == BRASERO_BURN_OK
|| brasero_track_tag_lookup (track, BRASERO_TRACK_GAIN_VALUE, NULL) == BRASERO_BURN_OK) {
BRASERO_JOB_LOG (transcode, "Found audio levels tags");
volume = gst_element_factory_make ("rgvolume", NULL);
if (volume)
g_object_set (volume,
"album-mode", FALSE,
NULL);
else
BRASERO_JOB_LOG (transcode, "rgvolume object couldn't be created");
}
return volume;
}
static gboolean
brasero_transcode_create_pipeline_size_mp3 (BraseroTranscode *transcode,
GstElement *pipeline,
GstElement *source,
GError **error)
{
BraseroTranscodePrivate *priv;
GstElement *parse;
GstElement *sink;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
BRASERO_JOB_LOG (transcode, "Creating specific pipeline for MP3s");
parse = gst_element_factory_make ("mp3parse", NULL);
if (!parse) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Mp3parse\"");
g_object_unref (pipeline);
return FALSE;
}
gst_bin_add (GST_BIN (pipeline), parse);
sink = gst_element_factory_make ("fakesink", NULL);
if (!sink) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Fakesink\"");
g_object_unref (pipeline);
return FALSE;
}
gst_bin_add (GST_BIN (pipeline), sink);
/* Link */
if (!gst_element_link_many (source, parse, sink, NULL)) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("Impossible to link plugin pads"));
BRASERO_JOB_LOG (transcode, "Impossible to link elements");
g_object_unref (pipeline);
return FALSE;
}
priv->convert = NULL;
priv->sink = sink;
priv->source = source;
priv->pipeline = pipeline;
/* Get it going */
gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
return TRUE;
}
static void
brasero_transcode_error_on_pad_linking (BraseroTranscode *self,
const gchar *function_name)
{
BraseroTranscodePrivate *priv;
GstMessage *message;
GstBus *bus;
priv = BRASERO_TRANSCODE_PRIVATE (self);
BRASERO_JOB_LOG (self, "Error on pad linking");
message = gst_message_new_error (GST_OBJECT (priv->pipeline),
g_error_new (BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
/* Translators: This message is sent
* when brasero could not link together
* two gstreamer plugins so that one
* sends its data to the second for further
* processing. This data transmission is
* done through a pad. Maybe this is a bit
* too technical and should be removed? */
_("Impossible to link plugin pads")),
function_name);
bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
gst_bus_post (bus, message);
g_object_unref (bus);
}
static void
brasero_transcode_wavparse_pad_added_cb (GstElement *wavparse,
GstPad *new_pad,
gpointer user_data)
{
GstPad *pad = NULL;
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (user_data);
pad = gst_element_get_static_pad (priv->sink, "sink");
if (!pad)
goto error;
if (gst_pad_link (new_pad, pad) != GST_PAD_LINK_OK)
goto error;
gst_element_set_state (priv->sink, GST_STATE_PLAYING);
return;
error:
if (pad)
gst_object_unref (pad);
brasero_transcode_error_on_pad_linking (BRASERO_TRANSCODE (user_data), "Sent by brasero_transcode_wavparse_pad_added_cb");
}
static gboolean
brasero_transcode_create_pipeline (BraseroTranscode *transcode,
GError **error)
{
gchar *uri;
gboolean keep_dts;
GstElement *decode;
GstElement *source;
GstBus *bus = NULL;
GstCaps *filtercaps;
GValue *value = NULL;
GstElement *pipeline;
GstElement *sink = NULL;
BraseroJobAction action;
GstElement *filter = NULL;
GstElement *volume = NULL;
GstElement *convert = NULL;
BraseroTrack *track = NULL;
GstElement *resample = NULL;
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
BRASERO_JOB_LOG (transcode, "Creating new pipeline");
priv->set_active_state = 0;
/* free the possible current pipeline and create a new one */
if (priv->pipeline) {
gst_element_set_state (priv->pipeline, GST_STATE_NULL);
gst_object_unref (G_OBJECT (priv->pipeline));
priv->link = NULL;
priv->sink = NULL;
priv->source = NULL;
priv->convert = NULL;
priv->pipeline = NULL;
}
/* create three types of pipeline according to the needs: (possibly adding grvolume)
* - filesrc ! decodebin ! audioconvert ! fakesink (find size) and filesrc!mp3parse!fakesink for mp3s
* - filesrc ! decodebin ! audioresample ! audioconvert ! audio/x-raw-int,rate=44100,width=16,depth=16,endianness=4321,signed ! filesink
* - filesrc ! decodebin ! audioresample ! audioconvert ! audio/x-raw-int,rate=44100,width=16,depth=16,endianness=4321,signed ! fdsink
*/
pipeline = gst_pipeline_new (NULL);
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus,
(GstBusFunc) brasero_transcode_bus_messages,
transcode);
gst_object_unref (bus);
/* source */
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
uri = brasero_track_stream_get_source (BRASERO_TRACK_STREAM (track), TRUE);
source = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
g_free (uri);
if (source == NULL) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
/* Translators: %s is the name of the GstElement that
* could not be created */
_("%s element could not be created"),
"\"Source\"");
goto error;
}
gst_bin_add (GST_BIN (pipeline), source);
g_object_set (source,
"typefind", FALSE,
NULL);
/* sink */
brasero_job_get_action (BRASERO_JOB (transcode), &action);
switch (action) {
case BRASERO_JOB_ACTION_SIZE:
if (priv->mp3_size_pipeline)
return brasero_transcode_create_pipeline_size_mp3 (transcode, pipeline, source, error);
sink = gst_element_factory_make ("fakesink", NULL);
break;
case BRASERO_JOB_ACTION_IMAGE:
volume = brasero_transcode_create_volume (transcode, track);
if (brasero_job_get_fd_out (BRASERO_JOB (transcode), NULL) != BRASERO_BURN_OK) {
gchar *output;
brasero_job_get_image_output (BRASERO_JOB (transcode),
&output,
NULL);
sink = gst_element_factory_make ("filesink", NULL);
g_object_set (sink,
"location", output,
NULL);
g_free (output);
}
else {
int fd;
brasero_job_get_fd_out (BRASERO_JOB (transcode), &fd);
sink = gst_element_factory_make ("fdsink", NULL);
g_object_set (sink,
"fd", fd,
NULL);
}
break;
default:
goto error;
}
if (!sink) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Sink\"");
goto error;
}
gst_bin_add (GST_BIN (pipeline), sink);
g_object_set (sink,
"sync", FALSE,
NULL);
brasero_job_tag_lookup (BRASERO_JOB (transcode),
BRASERO_SESSION_STREAM_AUDIO_FORMAT,
&value);
if (value)
keep_dts = (g_value_get_int (value) & BRASERO_AUDIO_FORMAT_DTS) != 0;
else
keep_dts = FALSE;
if (keep_dts
&& action == BRASERO_JOB_ACTION_IMAGE
&& (brasero_track_stream_get_format (BRASERO_TRACK_STREAM (track)) & BRASERO_AUDIO_FORMAT_DTS) != 0) {
GstElement *wavparse;
GstPad *sinkpad;
BRASERO_JOB_LOG (transcode, "DTS wav pipeline");
/* FIXME: volume normalization won't work here. We'd need to
* reencode it afterwards otherwise. */
/* This is a special case. This is DTS wav. So we only decode wav. */
wavparse = gst_element_factory_make ("wavparse", NULL);
if (wavparse == NULL) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Wavparse\"");
goto error;
}
gst_bin_add (GST_BIN (pipeline), wavparse);
if (!gst_element_link (source, wavparse)) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("Impossible to link plugin pads"));
goto error;
}
g_signal_connect (wavparse,
"pad-added",
G_CALLBACK (brasero_transcode_wavparse_pad_added_cb),
transcode);
/* This is an ugly workaround for the lack of accuracy with
* gstreamer. Yet this is unfortunately a necessary evil. */
priv->pos = 0;
priv->size = 0;
sinkpad = gst_element_get_pad (sink, "sink");
priv->probe = gst_pad_add_buffer_probe (sinkpad,
G_CALLBACK (brasero_transcode_buffer_handler),
transcode);
gst_object_unref (sinkpad);
priv->link = NULL;
priv->sink = sink;
priv->decode = NULL;
priv->source = source;
priv->convert = NULL;
priv->pipeline = pipeline;
gst_element_set_state (pipeline, GST_STATE_PLAYING);
return TRUE;
}
/* audioconvert */
convert = gst_element_factory_make ("audioconvert", NULL);
if (convert == NULL) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Audioconvert\"");
goto error;
}
gst_bin_add (GST_BIN (pipeline), convert);
if (action == BRASERO_JOB_ACTION_IMAGE) {
BraseroStreamFormat session_format;
BraseroTrackType *output_type;
output_type = brasero_track_type_new ();
brasero_job_get_output_type (BRASERO_JOB (transcode), output_type);
session_format = brasero_track_type_get_stream_format (output_type);
brasero_track_type_free (output_type);
/* audioresample */
resample = gst_element_factory_make ("audioresample", NULL);
if (resample == NULL) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Audioresample\"");
goto error;
}
gst_bin_add (GST_BIN (pipeline), resample);
/* filter */
filter = gst_element_factory_make ("capsfilter", NULL);
if (!filter) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Filter\"");
goto error;
}
gst_bin_add (GST_BIN (pipeline), filter);
filtercaps = gst_caps_new_full (gst_structure_new ("audio/x-raw-int",
"channels", G_TYPE_INT, 2,
"width", G_TYPE_INT, 16,
"depth", G_TYPE_INT, 16,
/* NOTE: we use little endianness only for libburn which requires little */
"endianness", G_TYPE_INT, (session_format & BRASERO_AUDIO_FORMAT_RAW_LITTLE_ENDIAN) != 0 ? 1234:4321,
"rate", G_TYPE_INT, 44100,
"signed", G_TYPE_BOOLEAN, TRUE,
NULL),
NULL);
g_object_set (GST_OBJECT (filter), "caps", filtercaps, NULL);
gst_caps_unref (filtercaps);
}
/* decode */
decode = gst_element_factory_make ("decodebin", NULL);
if (decode == NULL) {
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("%s element could not be created"),
"\"Decodebin\"");
goto error;
}
gst_bin_add (GST_BIN (pipeline), decode);
if (action == BRASERO_JOB_ACTION_IMAGE) {
GstPad *sinkpad;
gboolean res;
if (!gst_element_link (source, decode)) {
BRASERO_JOB_LOG (transcode, "Impossible to link plugin pads");
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("Impossible to link plugin pads"));
goto error;
}
priv->link = resample;
g_signal_connect (G_OBJECT (decode),
"new-decoded-pad",
G_CALLBACK (brasero_transcode_new_decoded_pad_cb),
transcode);
if (volume) {
gst_bin_add (GST_BIN (pipeline), volume);
res = gst_element_link_many (resample,
volume,
convert,
filter,
sink,
NULL);
}
else
res = gst_element_link_many (resample,
convert,
filter,
sink,
NULL);
if (!res) {
BRASERO_JOB_LOG (transcode, "Impossible to link plugin pads");
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("Impossible to link plugin pads"));
goto error;
}
/* This is an ugly workaround for the lack of accuracy with
* gstreamer. Yet this is unfortunately a necessary evil. */
priv->pos = 0;
priv->size = 0;
sinkpad = gst_element_get_pad (sink, "sink");
priv->probe = gst_pad_add_buffer_probe (sinkpad,
G_CALLBACK (brasero_transcode_buffer_handler),
transcode);
gst_object_unref (sinkpad);
}
else {
if (!gst_element_link (source, decode)
|| !gst_element_link (convert, sink)) {
BRASERO_JOB_LOG (transcode, "Impossible to link plugin pads");
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("Impossible to link plugin pads"));
goto error;
}
priv->link = convert;
g_signal_connect (G_OBJECT (decode),
"new-decoded-pad",
G_CALLBACK (brasero_transcode_new_decoded_pad_cb),
transcode);
}
priv->sink = sink;
priv->decode = decode;
priv->source = source;
priv->convert = convert;
priv->pipeline = pipeline;
gst_element_set_state (pipeline, GST_STATE_PLAYING);
return TRUE;
error:
if (error && (*error))
BRASERO_JOB_LOG (transcode,
"can't create object : %s \n",
(*error)->message);
gst_object_unref (GST_OBJECT (pipeline));
return FALSE;
}
static void
brasero_transcode_set_track_size (BraseroTranscode *transcode,
gint64 duration)
{
gchar *uri;
BraseroTrack *track;
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
brasero_track_stream_set_boundaries (BRASERO_TRACK_STREAM (track), -1, duration, -1);
duration += brasero_track_stream_get_gap (BRASERO_TRACK_STREAM (track));
/* if transcoding on the fly we should add some length just to make
* sure we won't be too short (gstreamer duration discrepancy) */
brasero_job_set_output_size_for_current_track (BRASERO_JOB (transcode),
BRASERO_DURATION_TO_SECTORS (duration),
BRASERO_DURATION_TO_BYTES (duration));
uri = brasero_track_stream_get_source (BRASERO_TRACK_STREAM (track), FALSE);
BRASERO_JOB_LOG (transcode,
"Song %s"
"\nsectors %" G_GINT64_FORMAT
"\ntime %" G_GINT64_FORMAT,
uri,
BRASERO_DURATION_TO_SECTORS (duration),
duration);
g_free (uri);
}
/**
* These functions are to deal with siblings
*/
static BraseroBurnResult
brasero_transcode_create_sibling_size (BraseroTranscode *transcode,
BraseroTrack *src,
GError **error)
{
BraseroTrack *dest;
guint64 duration;
/* it means the same file uri is in the selection and was already
* checked. Simply get the values for the length and other information
* and copy them. */
/* NOTE: no need to copy the length since if they are sibling that means
* that they have the same length */
brasero_track_stream_get_length (BRASERO_TRACK_STREAM (src), &duration);
brasero_job_set_output_size_for_current_track (BRASERO_JOB (transcode),
BRASERO_DURATION_TO_SECTORS (duration),
BRASERO_DURATION_TO_BYTES (duration));
/* copy the info we are missing */
brasero_job_get_current_track (BRASERO_JOB (transcode), &dest);
brasero_track_tag_copy_missing (dest, src);
return BRASERO_BURN_OK;
}
static BraseroBurnResult
brasero_transcode_create_sibling_image (BraseroTranscode *transcode,
BraseroTrack *src,
GError **error)
{
BraseroTrackStream *dest;
BraseroTrack *track;
guint64 length = 0;
gchar *path_dest;
gchar *path_src;
/* it means the file is already in the selection. Simply create a
* symlink pointing to first file in the selection with the same uri */
path_src = brasero_track_stream_get_source (BRASERO_TRACK_STREAM (src), FALSE);
brasero_job_get_audio_output (BRASERO_JOB (transcode), &path_dest);
if (symlink (path_src, path_dest) == -1) {
int errsv = errno;
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
/* Translators: the %s is the error message from errno */
_("An internal error occurred (%s)"),
g_strerror (errsv));
goto error;
}
dest = brasero_track_stream_new ();
brasero_track_stream_set_source (dest, path_dest);
/* FIXME: what if input had metadata ?*/
brasero_track_stream_set_format (dest, BRASERO_AUDIO_FORMAT_RAW);
/* NOTE: there is no gap and start = 0 since these tracks are the result
* of the transformation of previous ones */
brasero_track_stream_get_length (BRASERO_TRACK_STREAM (src), &length);
brasero_track_stream_set_boundaries (dest, 0, length, 0);
/* copy all infos but from the current track */
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
brasero_track_tag_copy_missing (BRASERO_TRACK (dest), track);
brasero_job_add_track (BRASERO_JOB (transcode), BRASERO_TRACK (dest));
/* It's good practice to unref the track afterwards as we don't need it
* anymore. BraseroTaskCtx refs it. */
g_object_unref (dest);
g_free (path_src);
g_free (path_dest);
return BRASERO_BURN_NOT_RUNNING;
error:
g_free (path_src);
g_free (path_dest);
return BRASERO_BURN_ERR;
}
static BraseroTrack *
brasero_transcode_search_for_sibling (BraseroTranscode *transcode)
{
BraseroJobAction action;
GSList *iter, *songs;
BraseroTrack *track;
gint64 start;
gint64 end;
gchar *uri;
brasero_job_get_action (BRASERO_JOB (transcode), &action);
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
start = brasero_track_stream_get_start (BRASERO_TRACK_STREAM (track));
end = brasero_track_stream_get_end (BRASERO_TRACK_STREAM (track));
uri = brasero_track_stream_get_source (BRASERO_TRACK_STREAM (track), TRUE);
brasero_job_get_done_tracks (BRASERO_JOB (transcode), &songs);
for (iter = songs; iter; iter = iter->next) {
gchar *iter_uri;
gint64 iter_end;
gint64 iter_start;
BraseroTrack *iter_track;
iter_track = iter->data;
iter_uri = brasero_track_stream_get_source (BRASERO_TRACK_STREAM (iter_track), TRUE);
if (strcmp (iter_uri, uri))
continue;
iter_end = brasero_track_stream_get_end (BRASERO_TRACK_STREAM (iter_track));
if (!iter_end)
continue;
if (iter_end != end)
continue;
iter_start = brasero_track_stream_get_start (BRASERO_TRACK_STREAM (track));
if (iter_start == start) {
g_free (uri);
return iter_track;
}
}
g_free (uri);
return NULL;
}
static BraseroBurnResult
brasero_transcode_has_track_sibling (BraseroTranscode *transcode,
GError **error)
{
BraseroJobAction action;
BraseroTrack *sibling = NULL;
BraseroBurnResult result = BRASERO_BURN_OK;
if (brasero_job_get_fd_out (BRASERO_JOB (transcode), NULL) == BRASERO_BURN_OK)
return BRASERO_BURN_OK;
sibling = brasero_transcode_search_for_sibling (transcode);
if (!sibling)
return BRASERO_BURN_OK;
BRASERO_JOB_LOG (transcode, "found sibling: skipping");
brasero_job_get_action (BRASERO_JOB (transcode), &action);
if (action == BRASERO_JOB_ACTION_IMAGE)
result = brasero_transcode_create_sibling_image (transcode,
sibling,
error);
else if (action == BRASERO_JOB_ACTION_SIZE)
result = brasero_transcode_create_sibling_size (transcode,
sibling,
error);
return result;
}
static BraseroBurnResult
brasero_transcode_start (BraseroJob *job,
GError **error)
{
BraseroTranscode *transcode;
BraseroBurnResult result;
BraseroJobAction action;
transcode = BRASERO_TRANSCODE (job);
brasero_job_get_action (job, &action);
brasero_job_set_use_average_rate (job, TRUE);
if (action == BRASERO_JOB_ACTION_SIZE) {
BraseroTrack *track;
/* see if the track size was already set since then no need to
* carry on with a lengthy get size and the library will do it
* itself. */
brasero_job_get_current_track (job, &track);
if (brasero_track_stream_get_end (BRASERO_TRACK_STREAM (track)) > 0)
return BRASERO_BURN_NOT_SUPPORTED;
if (!brasero_transcode_create_pipeline (transcode, error))
return BRASERO_BURN_ERR;
brasero_job_set_current_action (job,
BRASERO_BURN_ACTION_GETTING_SIZE,
NULL,
TRUE);
brasero_job_start_progress (job, FALSE);
return BRASERO_BURN_OK;
}
if (action == BRASERO_JOB_ACTION_IMAGE) {
/* Look for a sibling to avoid transcoding twice. In this case
* though start and end of this track must be inside start and
* end of the previous track. Of course if we are piping that
* operation is simply impossible. */
if (brasero_job_get_fd_out (job, NULL) != BRASERO_BURN_OK) {
result = brasero_transcode_has_track_sibling (BRASERO_TRANSCODE (job), error);
if (result != BRASERO_BURN_OK)
return result;
}
brasero_transcode_set_boundaries (transcode);
if (!brasero_transcode_create_pipeline (transcode, error))
return BRASERO_BURN_ERR;
}
else
BRASERO_JOB_NOT_SUPPORTED (transcode);
return BRASERO_BURN_OK;
}
static void
brasero_transcode_stop_pipeline (BraseroTranscode *transcode)
{
BraseroTranscodePrivate *priv;
GstPad *sinkpad;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
if (!priv->pipeline)
return;
sinkpad = gst_element_get_pad (priv->sink, "sink");
if (priv->probe)
gst_pad_remove_buffer_probe (sinkpad, priv->probe);
gst_object_unref (sinkpad);
gst_element_set_state (priv->pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (priv->pipeline));
priv->link = NULL;
priv->sink = NULL;
priv->source = NULL;
priv->convert = NULL;
priv->pipeline = NULL;
priv->set_active_state = 0;
}
static BraseroBurnResult
brasero_transcode_stop (BraseroJob *job,
GError **error)
{
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (job);
priv->mp3_size_pipeline = 0;
if (priv->pad_id) {
g_source_remove (priv->pad_id);
priv->pad_id = 0;
}
brasero_transcode_stop_pipeline (BRASERO_TRANSCODE (job));
return BRASERO_BURN_OK;
}
/* we must make sure that the track size is a multiple
* of 2352 to be burnt by cdrecord with on the fly */
static gint64
brasero_transcode_pad_real (BraseroTranscode *transcode,
int fd,
gint64 bytes2write,
GError **error)
{
const int buffer_size = 512;
char buffer [buffer_size];
gint64 b_written;
gint64 size;
b_written = 0;
bzero (buffer, sizeof (buffer));
for (; bytes2write; bytes2write -= b_written) {
size = bytes2write > buffer_size ? buffer_size : bytes2write;
b_written = write (fd, buffer, (int) size);
BRASERO_JOB_LOG (transcode,
"written %" G_GINT64_FORMAT " bytes for padding",
b_written);
/* we should not handle EINTR and EAGAIN as errors */
if (b_written < 0) {
if (errno == EINTR || errno == EAGAIN) {
BRASERO_JOB_LOG (transcode, "got EINTR / EAGAIN, retrying");
/* we'll try later again */
return bytes2write;
}
}
if (size != b_written) {
int errsv = errno;
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
/* Translators: %s is the string error from errno */
_("Error while padding file (%s)"),
g_strerror (errsv));
return -1;
}
}
return 0;
}
static void
brasero_transcode_push_track (BraseroTranscode *transcode)
{
guint64 length = 0;
gchar *output = NULL;
BraseroTrack *src = NULL;
BraseroTrackStream *track;
brasero_job_get_audio_output (BRASERO_JOB (transcode), &output);
brasero_job_get_current_track (BRASERO_JOB (transcode), &src);
brasero_track_stream_get_length (BRASERO_TRACK_STREAM (src), &length);
track = brasero_track_stream_new ();
brasero_track_stream_set_source (track, output);
g_free (output);
/* FIXME: what if input had metadata ?*/
brasero_track_stream_set_format (track, BRASERO_AUDIO_FORMAT_RAW);
brasero_track_stream_set_boundaries (track, 0, length, 0);
brasero_track_tag_copy_missing (BRASERO_TRACK (track), src);
brasero_job_add_track (BRASERO_JOB (transcode), BRASERO_TRACK (track));
/* It's good practice to unref the track afterwards as we don't need it
* anymore. BraseroTaskCtx refs it. */
g_object_unref (track);
brasero_job_finished_track (BRASERO_JOB (transcode));
}
static gboolean
brasero_transcode_pad_idle (BraseroTranscode *transcode)
{
gint64 bytes2write;
GError *error = NULL;
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
bytes2write = brasero_transcode_pad_real (transcode,
priv->pad_fd,
priv->pad_size,
&error);
if (bytes2write == -1) {
priv->pad_id = 0;
brasero_job_error (BRASERO_JOB (transcode), error);
return FALSE;
}
if (bytes2write) {
priv->pad_size = bytes2write;
return TRUE;
}
/* we are finished with padding */
priv->pad_id = 0;
close (priv->pad_fd);
priv->pad_fd = -1;
/* set the next song or finish */
brasero_transcode_push_track (transcode);
return FALSE;
}
static gboolean
brasero_transcode_pad (BraseroTranscode *transcode, int fd, GError **error)
{
guint64 length = 0;
gint64 bytes2write = 0;
BraseroTrack *track = NULL;
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
if (priv->pos < 0)
return TRUE;
/* Padding is important for two reasons:
* - first if didn't output enough bytes compared to what we should have
* - second we must output a multiple of 2352 to respect sector
* boundaries */
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
brasero_track_stream_get_length (BRASERO_TRACK_STREAM (track), &length);
if (priv->pos < BRASERO_DURATION_TO_BYTES (length)) {
gint64 b_written = 0;
/* Check bytes boundary for length */
b_written = BRASERO_DURATION_TO_BYTES (length);
b_written += (b_written % 2352) ? 2352 - (b_written % 2352):0;
bytes2write = b_written - priv->pos;
BRASERO_JOB_LOG (transcode,
"wrote %lli bytes (= %lli ns) out of %lli (= %lli ns)"
"\n=> padding %lli bytes",
priv->pos,
BRASERO_BYTES_TO_DURATION (priv->pos),
BRASERO_DURATION_TO_BYTES (length),
length,
bytes2write);
}
else {
gint64 b_written = 0;
/* wrote more or the exact amount of bytes. Check bytes boundary */
b_written = priv->pos;
bytes2write = (b_written % 2352) ? 2352 - (b_written % 2352):0;
BRASERO_JOB_LOG (transcode,
"wrote %lli bytes (= %lli ns)"
"\n=> padding %lli bytes",
b_written,
priv->pos,
bytes2write);
}
if (!bytes2write)
return TRUE;
bytes2write = brasero_transcode_pad_real (transcode,
fd,
bytes2write,
error);
if (bytes2write == -1)
return TRUE;
if (bytes2write) {
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
/* when writing to a pipe it can happen that its buffer is full
* because cdrecord is not fast enough. Therefore we couldn't
* write/pad it and we'll have to wait for the pipe to become
* available again */
priv->pad_fd = fd;
priv->pad_size = bytes2write;
priv->pad_id = g_timeout_add (50,
(GSourceFunc) brasero_transcode_pad_idle,
transcode);
return FALSE;
}
return TRUE;
}
static gboolean
brasero_transcode_pad_pipe (BraseroTranscode *transcode, GError **error)
{
int fd;
gboolean result;
brasero_job_get_fd_out (BRASERO_JOB (transcode), &fd);
fd = dup (fd);
result = brasero_transcode_pad (transcode, fd, error);
if (result)
close (fd);
return result;
}
static gboolean
brasero_transcode_pad_file (BraseroTranscode *transcode, GError **error)
{
int fd;
gchar *output;
gboolean result;
output = NULL;
brasero_job_get_audio_output (BRASERO_JOB (transcode), &output);
fd = open (output, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRGRP | S_IROTH);
g_free (output);
if (fd == -1) {
int errsv = errno;
g_set_error (error,
BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
/* Translators: %s is the string error from errno */
_("Error while padding file (%s)"),
g_strerror (errsv));
return FALSE;
}
result = brasero_transcode_pad (transcode, fd, error);
if (result)
close (fd);
return result;
}
static gboolean
brasero_transcode_is_mp3 (BraseroTranscode *transcode)
{
BraseroTranscodePrivate *priv;
GstElement *typefind;
GstCaps *caps = NULL;
const gchar *mime;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
/* find the type of the file */
typefind = gst_bin_get_by_name (GST_BIN (priv->decode), "typefind");
g_object_get (typefind, "caps", &caps, NULL);
if (!caps) {
gst_object_unref (typefind);
return TRUE;
}
if (caps && gst_caps_get_size (caps) > 0) {
mime = gst_structure_get_name (gst_caps_get_structure (caps, 0));
gst_object_unref (typefind);
if (mime && !strcmp (mime, "application/x-id3"))
return TRUE;
if (!strcmp (mime, "audio/mpeg"))
return TRUE;
}
else
gst_object_unref (typefind);
return FALSE;
}
static gint64
brasero_transcode_get_duration (BraseroTranscode *transcode)
{
gint64 duration = -1;
BraseroTranscodePrivate *priv;
GstFormat format = GST_FORMAT_TIME;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
/* This part is specific to MP3s */
if (priv->mp3_size_pipeline) {
/* This is the most reliable way to get the duration for mp3
* read them till the end and get the position. */
gst_element_query_position (priv->pipeline,
&format,
&duration);
}
/* This is for any sort of files */
if (duration == -1 || duration == 0)
gst_element_query_duration (priv->pipeline,
&format,
&duration);
BRASERO_JOB_LOG (transcode, "got duration %"G_GINT64_FORMAT, duration);
if (duration == -1 || duration == 0)
brasero_job_error (BRASERO_JOB (transcode),
g_error_new (BRASERO_BURN_ERROR,
BRASERO_BURN_ERROR_GENERAL,
_("Error while getting duration")));
return duration;
}
static gboolean
brasero_transcode_song_end_reached (BraseroTranscode *transcode)
{
GError *error = NULL;
BraseroJobAction action;
brasero_job_get_action (BRASERO_JOB (transcode), &action);
if (action == BRASERO_JOB_ACTION_SIZE) {
gint64 duration;
/* this is when we need to write infs:
* - when asked to create infs
* - when decoding to a file */
duration = brasero_transcode_get_duration (transcode);
if (duration == -1)
return FALSE;
brasero_transcode_set_track_size (transcode, duration);
brasero_job_finished_track (BRASERO_JOB (transcode));
return TRUE;
}
if (action == BRASERO_JOB_ACTION_IMAGE) {
gboolean result;
/* pad file so it is a multiple of 2352 (= 1 sector) */
if (brasero_job_get_fd_out (BRASERO_JOB (transcode), NULL) == BRASERO_BURN_OK)
result = brasero_transcode_pad_pipe (transcode, &error);
else
result = brasero_transcode_pad_file (transcode, &error);
if (error) {
brasero_job_error (BRASERO_JOB (transcode), error);
return FALSE;
}
if (!result) {
brasero_transcode_stop_pipeline (transcode);
return FALSE;
}
}
brasero_transcode_push_track (transcode);
return TRUE;
}
static void
foreach_tag (const GstTagList *list,
const gchar *tag,
BraseroTranscode *transcode)
{
BraseroTrack *track;
BraseroJobAction action;
brasero_job_get_action (BRASERO_JOB (transcode), &action);
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
BRASERO_JOB_LOG (transcode, "Retrieving tags");
if (!strcmp (tag, GST_TAG_TITLE)) {
if (!brasero_track_tag_lookup_string (track, BRASERO_TRACK_STREAM_TITLE_TAG)) {
gchar *title = NULL;
gst_tag_list_get_string (list, tag, &title);
brasero_track_tag_add_string (track,
BRASERO_TRACK_STREAM_TITLE_TAG,
title);
g_free (title);
}
}
else if (!strcmp (tag, GST_TAG_ARTIST)) {
if (!brasero_track_tag_lookup_string (track, BRASERO_TRACK_STREAM_ARTIST_TAG)) {
gchar *artist = NULL;
gst_tag_list_get_string (list, tag, &artist);
brasero_track_tag_add_string (track,
BRASERO_TRACK_STREAM_ARTIST_TAG,
artist);
g_free (artist);
}
}
else if (!strcmp (tag, GST_TAG_ISRC)) {
if (!brasero_track_tag_lookup_int (track, BRASERO_TRACK_STREAM_ISRC_TAG)) {
gchar *isrc = NULL;
gst_tag_list_get_string (list, tag, &isrc);
brasero_track_tag_add_int (track,
BRASERO_TRACK_STREAM_ISRC_TAG,
(int) g_ascii_strtoull (isrc, NULL, 10));
}
}
else if (!strcmp (tag, GST_TAG_PERFORMER)) {
if (!brasero_track_tag_lookup_string (track, BRASERO_TRACK_STREAM_ARTIST_TAG)) {
gchar *artist = NULL;
gst_tag_list_get_string (list, tag, &artist);
brasero_track_tag_add_string (track,
BRASERO_TRACK_STREAM_ARTIST_TAG,
artist);
g_free (artist);
}
}
else if (action == BRASERO_JOB_ACTION_SIZE
&& !strcmp (tag, GST_TAG_DURATION)) {
guint64 duration;
/* this is only useful when we try to have the size */
gst_tag_list_get_uint64 (list, tag, &duration);
brasero_track_stream_set_boundaries (BRASERO_TRACK_STREAM (track), 0, duration, -1);
}
}
/* NOTE: the return value is whether or not we should stop the bus callback */
static gboolean
brasero_transcode_active_state (BraseroTranscode *transcode)
{
BraseroTranscodePrivate *priv;
gchar *name, *string, *uri;
BraseroJobAction action;
BraseroTrack *track;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
if (priv->set_active_state)
return TRUE;
priv->set_active_state = TRUE;
brasero_job_get_current_track (BRASERO_JOB (transcode), &track);
uri = brasero_track_stream_get_source (BRASERO_TRACK_STREAM (track), FALSE);
brasero_job_get_action (BRASERO_JOB (transcode), &action);
if (action == BRASERO_JOB_ACTION_SIZE) {
BRASERO_JOB_LOG (transcode,
"Analysing Track %s",
uri);
if (priv->mp3_size_pipeline) {
gchar *escaped_basename;
/* Run the pipeline till the end */
escaped_basename = g_path_get_basename (uri);
name = g_uri_unescape_string (escaped_basename, NULL);
g_free (escaped_basename);
string = g_strdup_printf (_("Analysing \"%s\""), name);
g_free (name);
brasero_job_set_current_action (BRASERO_JOB (transcode),
BRASERO_BURN_ACTION_ANALYSING,
string,
TRUE);
g_free (string);
brasero_job_start_progress (BRASERO_JOB (transcode), FALSE);
g_free (uri);
return TRUE;
}
if (brasero_transcode_is_mp3 (transcode)) {
GError *error = NULL;
/* Rebuild another pipeline which is specific to MP3s. */
priv->mp3_size_pipeline = TRUE;
brasero_transcode_stop_pipeline (transcode);
if (!brasero_transcode_create_pipeline (transcode, &error))
brasero_job_error (BRASERO_JOB (transcode), error);
}
else
brasero_transcode_song_end_reached (transcode);
g_free (uri);
return FALSE;
}
else {
gchar *escaped_basename;
escaped_basename = g_path_get_basename (uri);
name = g_uri_unescape_string (escaped_basename, NULL);
g_free (escaped_basename);
string = g_strdup_printf (_("Transcoding \"%s\""), name);
g_free (name);
brasero_job_set_current_action (BRASERO_JOB (transcode),
BRASERO_BURN_ACTION_TRANSCODING,
string,
TRUE);
g_free (string);
brasero_job_start_progress (BRASERO_JOB (transcode), FALSE);
if (brasero_job_get_fd_out (BRASERO_JOB (transcode), NULL) != BRASERO_BURN_OK) {
gchar *dest = NULL;
brasero_job_get_audio_output (BRASERO_JOB (transcode), &dest);
BRASERO_JOB_LOG (transcode,
"start decoding %s to %s",
uri,
dest);
g_free (dest);
}
else
BRASERO_JOB_LOG (transcode,
"start piping %s",
uri)
}
g_free (uri);
return TRUE;
}
static gboolean
brasero_transcode_bus_messages (GstBus *bus,
GstMessage *msg,
BraseroTranscode *transcode)
{
BraseroTranscodePrivate *priv;
GstTagList *tags = NULL;
GError *error = NULL;
GstState state;
gchar *debug;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_TAG:
/* we use the information to write an .inf file
* for the time being just store the information */
gst_message_parse_tag (msg, &tags);
gst_tag_list_foreach (tags, (GstTagForeachFunc) foreach_tag, transcode);
gst_tag_list_free (tags);
return TRUE;
case GST_MESSAGE_ERROR:
gst_message_parse_error (msg, &error, &debug);
BRASERO_JOB_LOG (transcode, debug);
g_free (debug);
brasero_job_error (BRASERO_JOB (transcode), error);
return FALSE;
case GST_MESSAGE_EOS:
brasero_transcode_song_end_reached (transcode);
return FALSE;
case GST_MESSAGE_STATE_CHANGED: {
GstStateChangeReturn result;
result = gst_element_get_state (priv->pipeline,
&state,
NULL,
1);
if (result != GST_STATE_CHANGE_SUCCESS)
return TRUE;
if (state == GST_STATE_PLAYING)
return brasero_transcode_active_state (transcode);
break;
}
default:
return TRUE;
}
return TRUE;
}
static void
brasero_transcode_new_decoded_pad_cb (GstElement *decode,
GstPad *pad,
gboolean arg2,
BraseroTranscode *transcode)
{
GstCaps *caps;
GstStructure *structure;
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (transcode);
BRASERO_JOB_LOG (transcode, "New pad");
/* make sure we only have audio */
caps = gst_pad_get_caps (pad);
if (!caps)
return;
structure = gst_caps_get_structure (caps, 0);
if (structure) {
if (g_strrstr (gst_structure_get_name (structure), "audio")) {
GstPad *sink;
GstElement *queue;
GstPadLinkReturn res;
/* before linking pads (before any data reach grvolume), send tags */
brasero_transcode_send_volume_event (transcode);
/* This is necessary in case there is a video stream
* (see brasero-metadata.c). we need to queue to avoid
* a deadlock. */
queue = gst_element_factory_make ("queue", NULL);
gst_bin_add (GST_BIN (priv->pipeline), queue);
if (!gst_element_link (queue, priv->link)) {
brasero_transcode_error_on_pad_linking (transcode, "Sent by brasero_transcode_new_decoded_pad_cb");
goto end;
}
sink = gst_element_get_pad (queue, "sink");
if (GST_PAD_IS_LINKED (sink)) {
brasero_transcode_error_on_pad_linking (transcode, "Sent by brasero_transcode_new_decoded_pad_cb");
goto end;
}
res = gst_pad_link (pad, sink);
if (res == GST_PAD_LINK_OK)
gst_element_set_state (queue, GST_STATE_PLAYING);
else
brasero_transcode_error_on_pad_linking (transcode, "Sent by brasero_transcode_new_decoded_pad_cb");
gst_object_unref (sink);
}
/* For video streams add a fakesink (see brasero-metadata.c) */
else if (g_strrstr (gst_structure_get_name (structure), "video")) {
GstPad *sink;
GstElement *fakesink;
GstPadLinkReturn res;
BRASERO_JOB_LOG (transcode, "Adding a fakesink for video stream");
fakesink = gst_element_factory_make ("fakesink", NULL);
if (!fakesink) {
brasero_transcode_error_on_pad_linking (transcode, "Sent by brasero_transcode_new_decoded_pad_cb");
goto end;
}
sink = gst_element_get_static_pad (fakesink, "sink");
if (!sink) {
brasero_transcode_error_on_pad_linking (transcode, "Sent by brasero_transcode_new_decoded_pad_cb");
gst_object_unref (fakesink);
goto end;
}
gst_bin_add (GST_BIN (priv->pipeline), fakesink);
res = gst_pad_link (pad, sink);
if (res == GST_PAD_LINK_OK)
gst_element_set_state (fakesink, GST_STATE_PLAYING);
else
brasero_transcode_error_on_pad_linking (transcode, "Sent by brasero_transcode_new_decoded_pad_cb");
gst_object_unref (sink);
}
}
end:
gst_caps_unref (caps);
}
static BraseroBurnResult
brasero_transcode_clock_tick (BraseroJob *job)
{
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (job);
if (!priv->pipeline)
return BRASERO_BURN_ERR;
brasero_job_set_written_track (job, priv->pos);
return BRASERO_BURN_OK;
}
static void
brasero_transcode_class_init (BraseroTranscodeClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
BraseroJobClass *job_class = BRASERO_JOB_CLASS (klass);
g_type_class_add_private (klass, sizeof (BraseroTranscodePrivate));
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = brasero_transcode_finalize;
job_class->start = brasero_transcode_start;
job_class->clock_tick = brasero_transcode_clock_tick;
job_class->stop = brasero_transcode_stop;
}
static void
brasero_transcode_init (BraseroTranscode *obj)
{ }
static void
brasero_transcode_finalize (GObject *object)
{
BraseroTranscodePrivate *priv;
priv = BRASERO_TRANSCODE_PRIVATE (object);
if (priv->pad_id) {
g_source_remove (priv->pad_id);
priv->pad_id = 0;
}
brasero_transcode_stop_pipeline (BRASERO_TRANSCODE (object));
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
brasero_transcode_export_caps (BraseroPlugin *plugin)
{
GSList *input;
GSList *output;
brasero_plugin_define (plugin,
"transcode",
_("Converts any song file into a format suitable for audio CDs"),
"Philippe Rouquier",
1);
output = brasero_caps_audio_new (BRASERO_PLUGIN_IO_ACCEPT_FILE|
BRASERO_PLUGIN_IO_ACCEPT_PIPE,
BRASERO_AUDIO_FORMAT_RAW|
BRASERO_AUDIO_FORMAT_RAW_LITTLE_ENDIAN|
BRASERO_METADATA_INFO);
input = brasero_caps_audio_new (BRASERO_PLUGIN_IO_ACCEPT_FILE,
BRASERO_AUDIO_FORMAT_UNDEFINED|
BRASERO_METADATA_INFO);
brasero_plugin_link_caps (plugin, output, input);
g_slist_free (input);
input = brasero_caps_audio_new (BRASERO_PLUGIN_IO_ACCEPT_FILE,
BRASERO_AUDIO_FORMAT_DTS|
BRASERO_METADATA_INFO);
brasero_plugin_link_caps (plugin, output, input);
g_slist_free (output);
g_slist_free (input);
output = brasero_caps_audio_new (BRASERO_PLUGIN_IO_ACCEPT_FILE|
BRASERO_PLUGIN_IO_ACCEPT_PIPE,
BRASERO_AUDIO_FORMAT_RAW|
BRASERO_AUDIO_FORMAT_RAW_LITTLE_ENDIAN);
input = brasero_caps_audio_new (BRASERO_PLUGIN_IO_ACCEPT_FILE,
BRASERO_AUDIO_FORMAT_UNDEFINED);
brasero_plugin_link_caps (plugin, output, input);
g_slist_free (input);
input = brasero_caps_audio_new (BRASERO_PLUGIN_IO_ACCEPT_FILE,
BRASERO_AUDIO_FORMAT_DTS);
brasero_plugin_link_caps (plugin, output, input);
g_slist_free (output);
g_slist_free (input);
}
|