Debian Codegraph @

tests/test_mp4.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2008 by Lukas Lalinsky
3 email : [email protected]
4 ***************************************************************************/
5
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
10 * *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 USA *
20 * *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
25
26 #include <string>
27 #include <cstdio>
28
29 #include "tbytevectorlist.h"
30 #include "tbytevectorstream.h"
31 #include "tpropertymap.h"
32 #include "tag.h"
33 #include "mp4tag.h"
34 #include "mp4atom.h"
35 #include "mp4file.h"
36 #include "mp4itemfactory.h"
37 #include "mp4chapterholder.h"
38 #include "plainfile.h"
39 #include <cppunit/extensions/HelperMacros.h>
40 #include "utils.h"
41
42 using namespace std;
43 using namespace TagLib;
44
45 namespace
46 {
47
48 class CustomItemFactory : public MP4::ItemFactory {
49 public:
50 CustomItemFactory(const CustomItemFactory &) = delete;
51 CustomItemFactory &operator=(const CustomItemFactory &) = delete;
52 static CustomItemFactory *instance() { return &factory; }
53 protected:
54 CustomItemFactory() = default;
55 ~CustomItemFactory() = default;
56 NameHandlerMap nameHandlerMap() const override
57 {
58 return MP4::ItemFactory::nameHandlerMap()
59 .insert("tsti", ItemHandlerType::Int)
60 .insert("tstt", ItemHandlerType::Text);
61 }
62
63 Map<ByteVector, String> namePropertyMap() const override
64 {
65 return MP4::ItemFactory::namePropertyMap()
66 .insert("tsti", "TESTINTEGER");
67 }
68 private:
69 static CustomItemFactory factory;
70 };
71
72 CustomItemFactory CustomItemFactory::factory;
73
74 class MockChapterList : public MP4::ChapterHolder {
75 public:
76 static const MP4::ChapterList mockChapters;
77
78 bool read(TagLib::File *)
79 {
80 chapterList = mockChapters;
81 ++readCount;
82 return true;
83 }
84
85 bool write(TagLib::File *)
86 {
87 ++writeCount;
88 return true;
89 }
90
91 int readCount = 0;
92 int writeCount = 0;
93 };
94
95 const MP4::ChapterList MockChapterList::mockChapters = {
96 MP4::Chapter("Mock", 123)
97 };
98
99 class MockChapterFile : public PlainFile {
100 public:
101 explicit MockChapterFile(FileName name) : PlainFile(name)
102 {
103 }
104
105 MP4::ChapterList chapters()
106 {
107 return getChaptersLazy(chapterList, this);
108 }
109
110 void setChapters(const MP4::ChapterList& chapters)
111 {
112 setChaptersLazy(chapterList, chapters);
113 }
114
115 bool save() override
116 {
117 return MP4::saveChaptersIfModified(chapterList, this);
118 }
119
120 std::unique_ptr<MockChapterList> chapterList;
121 };
122
123 } // namespace
124
125 class TestMP4 : public CppUnit::TestFixture
126 {
127 CPPUNIT_TEST_SUITE(TestMP4);
128 CPPUNIT_TEST(testPropertiesAAC);
129 CPPUNIT_TEST(testPropertiesAACWithoutBitrate);
130 CPPUNIT_TEST(testPropertiesALAC);
131 CPPUNIT_TEST(testPropertiesALACWithoutBitrate);
132 CPPUNIT_TEST(testPropertiesAACWithoutLength);
133 CPPUNIT_TEST(testPropertiesM4V);
134 CPPUNIT_TEST(testFreeForm);
135 CPPUNIT_TEST(testCheckValid);
136 CPPUNIT_TEST(testHasTag);
137 CPPUNIT_TEST(testIsEmpty);
138 CPPUNIT_TEST(testUpdateStco);
139 CPPUNIT_TEST(testSaveExisingWhenIlstIsLast);
140 CPPUNIT_TEST(test64BitAtom);
141 CPPUNIT_TEST(testGnre);
142 CPPUNIT_TEST(testCovrRead);
143 CPPUNIT_TEST(testCovrWrite);
144 CPPUNIT_TEST(testCovrRead2);
145 CPPUNIT_TEST(testProperties);
146 CPPUNIT_TEST(testPropertiesAllSupported);
147 CPPUNIT_TEST(testPropertiesMovement);
148 CPPUNIT_TEST(testFuzzedFile);
149 CPPUNIT_TEST(testRepeatedSave);
150 CPPUNIT_TEST(testWithZeroLengthAtom);
151 CPPUNIT_TEST(testEmptyValuesRemoveItems);
152 CPPUNIT_TEST(testRemoveMetadata);
153 CPPUNIT_TEST(testNonFullMetaAtom);
154 CPPUNIT_TEST(testItemFactory);
155 CPPUNIT_TEST(testNonPrintableAtom);
156 CPPUNIT_TEST(testChapterListWrite);
157 CPPUNIT_TEST(testChapterListRemove);
158 CPPUNIT_TEST(testChapterListWithExistingTags);
159 CPPUNIT_TEST(testChapterListReadEmpty);
160 CPPUNIT_TEST(testQTChapterListWrite);
161 CPPUNIT_TEST(testQTChapterListRemove);
162 CPPUNIT_TEST(testQTChapterListWithExistingTags);
163 CPPUNIT_TEST(testQTChapterListReadEmpty);
164 CPPUNIT_TEST(testQTChapterListOverwrite);
165 CPPUNIT_TEST(testQTChapterListTimestampPrecision);
166 CPPUNIT_TEST(testQTChapterListNonZeroFirstChapter);
167 CPPUNIT_TEST(testQTChapterListNoOrphanedMdat);
168 CPPUNIT_TEST(testQTChapterListSharedMdatPreservesAudio);
169 CPPUNIT_TEST(testQTChapterListUnicodeTitles);
170 CPPUNIT_TEST(testChapterListUnicodeTitles);
171 CPPUNIT_TEST(testQTChapterListEmptyTitleStripped);
172 CPPUNIT_TEST(testQTChapterListSingleEmptyTitleNotStripped);
173 CPPUNIT_TEST(testNeroAndQTChaptersAreIndependent);
174 CPPUNIT_TEST(testNeroChaptersAloneWhenNoQT);
175 CPPUNIT_TEST(testLazyReadingAndWritingChapters);
176 CPPUNIT_TEST_SUITE_END();
177
178 public:
179
180 void testPropertiesAAC()
181 {
182 MP4::File f(TEST_FILE_PATH_C("has-tags.m4a"));
183 CPPUNIT_ASSERT(f.audioProperties());
184 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
185 CPPUNIT_ASSERT_EQUAL(3708, f.audioProperties()->lengthInMilliseconds());
186 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->bitrate());
187 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
188 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
189 CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
190 CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted());
191 CPPUNIT_ASSERT_EQUAL(MP4::Properties::AAC, f.audioProperties()->codec());
192 }
193
194 void testPropertiesAACWithoutBitrate()
195 {
196 ByteVector aacData = PlainFile(TEST_FILE_PATH_C("has-tags.m4a")).readAll();
197 CPPUNIT_ASSERT_GREATER(1960U, aacData.size());
198 CPPUNIT_ASSERT_EQUAL(ByteVector("mp4a"), aacData.mid(1890, 4));
199 // Set the bitrate to zero
200 for (int offset = 1956; offset < 1960; ++offset) {
201 aacData[offset] = 0;
202 }
203 ByteVectorStream aacStream(aacData);
204 MP4::File f(&aacStream);
205 CPPUNIT_ASSERT(f.audioProperties());
206 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
207 CPPUNIT_ASSERT_EQUAL(3708, f.audioProperties()->lengthInMilliseconds());
208 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->bitrate());
209 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
210 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
211 CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
212 CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted());
213 CPPUNIT_ASSERT_EQUAL(MP4::Properties::AAC, f.audioProperties()->codec());
214 }
215
216 void testPropertiesALAC()
217 {
218 MP4::File f(TEST_FILE_PATH_C("empty_alac.m4a"));
219 CPPUNIT_ASSERT(f.audioProperties());
220 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
221 CPPUNIT_ASSERT_EQUAL(3705, f.audioProperties()->lengthInMilliseconds());
222 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->bitrate());
223 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
224 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
225 CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
226 CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted());
227 CPPUNIT_ASSERT_EQUAL(MP4::Properties::ALAC, f.audioProperties()->codec());
228 }
229
230 void testPropertiesALACWithoutBitrate()
231 {
232 ByteVector alacData = PlainFile(TEST_FILE_PATH_C("empty_alac.m4a")).readAll();
233 CPPUNIT_ASSERT_GREATER(474U, alacData.size());
234 CPPUNIT_ASSERT_EQUAL(ByteVector("alac"), alacData.mid(446, 4));
235 // Set the bitrate to zero
236 for (int offset = 470; offset < 474; ++offset) {
237 alacData[offset] = 0;
238 }
239 ByteVectorStream alacStream(alacData);
240 MP4::File f(&alacStream);
241 CPPUNIT_ASSERT(f.audioProperties());
242 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
243 CPPUNIT_ASSERT_EQUAL(3705, f.audioProperties()->lengthInMilliseconds());
244 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->bitrate());
245 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
246 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
247 CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
248 CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted());
249 CPPUNIT_ASSERT_EQUAL(MP4::Properties::ALAC, f.audioProperties()->codec());
250 }
251
252 void testPropertiesAACWithoutLength()
253 {
254 ByteVector m4aData = PlainFile(TEST_FILE_PATH_C("no-tags.m4a")).readAll();
255 CPPUNIT_ASSERT_EQUAL(2898U, m4aData.size());
256 CPPUNIT_ASSERT_EQUAL(ByteVector("mdhd"), m4aData.mid(1749, 4));
257 // Set the length to zero
258 for (int offset = 1769; offset < 1773; ++offset) {
259 m4aData[offset] = 0;
260 }
261 ByteVectorStream m4aStream(m4aData);
262 MP4::File f(&m4aStream);
263 CPPUNIT_ASSERT(f.audioProperties());
264 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
265 CPPUNIT_ASSERT_EQUAL(3707, f.audioProperties()->lengthInMilliseconds());
266 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->bitrate());
267 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
268 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
269 CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
270 CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted());
271 CPPUNIT_ASSERT_EQUAL(MP4::Properties::AAC, f.audioProperties()->codec());
272 }
273
274 void testPropertiesM4V()
275 {
276 MP4::File f(TEST_FILE_PATH_C("blank_video.m4v"));
277 CPPUNIT_ASSERT(f.audioProperties());
278 CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
279 CPPUNIT_ASSERT_EQUAL(975, f.audioProperties()->lengthInMilliseconds());
280 CPPUNIT_ASSERT_EQUAL(96, f.audioProperties()->bitrate());
281 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
282 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
283 CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
284 CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted());
285 CPPUNIT_ASSERT_EQUAL(MP4::Properties::AAC, f.audioProperties()->codec());
286 }
287
288 void testCheckValid()
289 {
290 MP4::File f(TEST_FILE_PATH_C("empty.aiff"));
291 CPPUNIT_ASSERT(!f.isValid());
292 }
293
294 void testHasTag()
295 {
296 {
297 MP4::File f(TEST_FILE_PATH_C("has-tags.m4a"));
298 CPPUNIT_ASSERT(f.isValid());
299 CPPUNIT_ASSERT(f.hasMP4Tag());
300 }
301
302 ScopedFileCopy copy("no-tags", ".m4a");
303
304 {
305 MP4::File f(copy.fileName().c_str());
306 CPPUNIT_ASSERT(f.isValid());
307 CPPUNIT_ASSERT(!f.hasMP4Tag());
308 f.tag()->setTitle("TITLE");
309 f.save();
310 }
311 {
312 MP4::File f(copy.fileName().c_str());
313 CPPUNIT_ASSERT(f.isValid());
314 CPPUNIT_ASSERT(f.hasMP4Tag());
315 }
316 }
317
318 void testIsEmpty()
319 {
320 MP4::Tag t1;
321 CPPUNIT_ASSERT(t1.isEmpty());
322 t1.setArtist("Foo");
323 CPPUNIT_ASSERT(!t1.isEmpty());
324
325 MP4::Tag t2;
326 t2.setItem("foo", "bar");
327 CPPUNIT_ASSERT(!t2.isEmpty());
328 }
329
330 void testUpdateStco()
331 {
332 ScopedFileCopy copy("no-tags", ".3g2");
333 string filename = copy.fileName();
334
335 ByteVectorList data1;
336
337 {
338 MP4::File f(filename.c_str());
339 f.tag()->setArtist(ByteVector(3000, 'x'));
340
341 MP4::Atoms a(&f);
342 MP4::Atom *stco = a.find("moov")->findall("stco", true)[0];
343 f.seek(stco->offset() + 12);
344 ByteVector data = f.readBlock(stco->length() - 12);
345 unsigned int count = data.mid(0, 4).toUInt();
346 int pos = 4;
347 while (count--) {
348 unsigned int offset = data.mid(pos, 4).toUInt();
349 f.seek(offset);
350 data1.append(f.readBlock(20));
351 pos += 4;
352 }
353
354 f.save();
355 }
356
357 {
358 MP4::File f(filename.c_str());
359
360 MP4::Atoms a(&f);
361 MP4::Atom *stco = a.find("moov")->findall("stco", true)[0];
362 f.seek(stco->offset() + 12);
363 ByteVector data = f.readBlock(stco->length() - 12);
364 unsigned int count = data.mid(0, 4).toUInt();
365 int pos = 4, i = 0;
366 while (count--) {
367 unsigned int offset = data.mid(pos, 4).toUInt();
368 f.seek(offset);
369 CPPUNIT_ASSERT_EQUAL(data1[i], f.readBlock(20));
370 pos += 4;
371 i++;
372 }
373 }
374 }
375
376 void testFreeForm()
377 {
378 ScopedFileCopy copy("has-tags", ".m4a");
379 string filename = copy.fileName();
380
381 {
382 MP4::File f(filename.c_str());
383 CPPUNIT_ASSERT(f.tag()->contains("----:com.apple.iTunes:iTunNORM"));
384 f.tag()->setItem("----:org.kde.TagLib:Foo", StringList("Bar"));
385 f.save();
386 }
387 {
388 MP4::File f(filename.c_str());
389 CPPUNIT_ASSERT(f.tag()->contains("----:org.kde.TagLib:Foo"));
390 CPPUNIT_ASSERT_EQUAL(String("Bar"),
391 f.tag()->item("----:org.kde.TagLib:Foo").toStringList().front());
392 f.save();
393 }
394 }
395
396 void testSaveExisingWhenIlstIsLast()
397 {
398 ScopedFileCopy copy("ilst-is-last", ".m4a");
399 string filename = copy.fileName();
400
401 {
402 MP4::File f(filename.c_str());
403 CPPUNIT_ASSERT_EQUAL(String("82,164"),
404 f.tag()->item("----:com.apple.iTunes:replaygain_track_minmax").toStringList().front());
405 CPPUNIT_ASSERT_EQUAL(String("Pearl Jam"), f.tag()->artist());
406 f.tag()->setComment("foo");
407 f.save();
408 }
409 {
410 MP4::File f(filename.c_str());
411 CPPUNIT_ASSERT_EQUAL(String("82,164"),
412 f.tag()->item("----:com.apple.iTunes:replaygain_track_minmax").toStringList().front());
413 CPPUNIT_ASSERT_EQUAL(String("Pearl Jam"), f.tag()->artist());
414 CPPUNIT_ASSERT_EQUAL(String("foo"), f.tag()->comment());
415 }
416 }
417
418 void test64BitAtom()
419 {
420 ScopedFileCopy copy("64bit", ".mp4");
421 string filename = copy.fileName();
422
423 {
424 MP4::File f(filename.c_str());
425 CPPUNIT_ASSERT_EQUAL(true, f.tag()->itemMap()["cpil"].toBool());
426
427 MP4::Atoms atoms(&f);
428 MP4::Atom *moov = atoms.atoms()[0];
429 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(77), moov->length());
430
431 f.tag()->setItem("pgap", true);
432 f.save();
433 }
434 {
435 MP4::File f(filename.c_str());
436 CPPUNIT_ASSERT_EQUAL(true, f.tag()->item("cpil").toBool());
437 CPPUNIT_ASSERT_EQUAL(true, f.tag()->item("pgap").toBool());
438
439 MP4::Atoms atoms(&f);
440 MP4::Atom *moov = atoms.atoms()[0];
441 // original size + 'pgap' size + padding
442 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(77 + 25 + 974), moov->length());
443 }
444 }
445
446 void testGnre()
447 {
448 MP4::File f(TEST_FILE_PATH_C("gnre.m4a"));
449 CPPUNIT_ASSERT_EQUAL(TagLib::String("Ska"), f.tag()->genre());
450 }
451
452 void testCovrRead()
453 {
454 MP4::File f(TEST_FILE_PATH_C("has-tags.m4a"));
455 CPPUNIT_ASSERT(f.tag()->contains("covr"));
456 MP4::CoverArtList l = f.tag()->item("covr").toCoverArtList();
457 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), l.size());
458 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::PNG, l[0].format());
459 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(79), l[0].data().size());
460 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::JPEG, l[1].format());
461 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(287), l[1].data().size());
462 }
463
464 void testCovrWrite()
465 {
466 ScopedFileCopy copy("has-tags", ".m4a");
467 string filename = copy.fileName();
468
469 {
470 MP4::File f(filename.c_str());
471 CPPUNIT_ASSERT(f.tag()->contains("covr"));
472 MP4::CoverArtList l = f.tag()->item("covr").toCoverArtList();
473 l.append(MP4::CoverArt(MP4::CoverArt::PNG, "foo"));
474 f.tag()->setItem("covr", l);
475 f.save();
476 }
477 {
478 MP4::File f(filename.c_str());
479 CPPUNIT_ASSERT(f.tag()->contains("covr"));
480 MP4::CoverArtList l = f.tag()->item("covr").toCoverArtList();
481 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), l.size());
482 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::PNG, l[0].format());
483 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(79), l[0].data().size());
484 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::JPEG, l[1].format());
485 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(287), l[1].data().size());
486 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::PNG, l[2].format());
487 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), l[2].data().size());
488 }
489 }
490
491 void testCovrRead2()
492 {
493 MP4::File f(TEST_FILE_PATH_C("covr-junk.m4a"));
494 CPPUNIT_ASSERT(f.tag()->contains("covr"));
495 MP4::CoverArtList l = f.tag()->item("covr").toCoverArtList();
496 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), l.size());
497 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::PNG, l[0].format());
498 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(79), l[0].data().size());
499 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::JPEG, l[1].format());
500 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(287), l[1].data().size());
501 }
502
503 void testProperties()
504 {
505 MP4::File f(TEST_FILE_PATH_C("has-tags.m4a"));
506
507 PropertyMap tags = f.properties();
508
509 CPPUNIT_ASSERT_EQUAL(StringList("Test Artist"), tags["ARTIST"]);
510
511 tags["TRACKNUMBER"] = StringList("2/4");
512 tags["DISCNUMBER"] = StringList("3/5");
513 tags["BPM"] = StringList("123");
514 tags["ARTIST"] = StringList("Foo Bar");
515 tags["COMPILATION"] = StringList("1");
516 tags["REMIXEDBY"] = StringList("Remixed by");
517 f.setProperties(tags);
518
519 tags = f.properties();
520
521 CPPUNIT_ASSERT(f.tag()->contains("trkn"));
522 CPPUNIT_ASSERT_EQUAL(2, f.tag()->item("trkn").toIntPair().first);
523 CPPUNIT_ASSERT_EQUAL(4, f.tag()->item("trkn").toIntPair().second);
524 CPPUNIT_ASSERT_EQUAL(StringList("2/4"), tags["TRACKNUMBER"]);
525
526 CPPUNIT_ASSERT(f.tag()->contains("disk"));
527 CPPUNIT_ASSERT_EQUAL(3, f.tag()->item("disk").toIntPair().first);
528 CPPUNIT_ASSERT_EQUAL(5, f.tag()->item("disk").toIntPair().second);
529 CPPUNIT_ASSERT_EQUAL(StringList("3/5"), tags["DISCNUMBER"]);
530
531 CPPUNIT_ASSERT(f.tag()->contains("tmpo"));
532 CPPUNIT_ASSERT_EQUAL(123, f.tag()->item("tmpo").toInt());
533 CPPUNIT_ASSERT_EQUAL(StringList("123"), tags["BPM"]);
534
535 CPPUNIT_ASSERT(f.tag()->contains("\251ART"));
536 CPPUNIT_ASSERT_EQUAL(StringList("Foo Bar"), f.tag()->item("\251ART").toStringList());
537 CPPUNIT_ASSERT_EQUAL(StringList("Foo Bar"), tags["ARTIST"]);
538
539 CPPUNIT_ASSERT(f.tag()->contains("cpil"));
540 CPPUNIT_ASSERT_EQUAL(true, f.tag()->item("cpil").toBool());
541 CPPUNIT_ASSERT_EQUAL(StringList("1"), tags["COMPILATION"]);
542
543 CPPUNIT_ASSERT(f.tag()->contains("----:com.apple.iTunes:REMIXEDBY"));
544 CPPUNIT_ASSERT_EQUAL(StringList("Remixed by"),
545 f.tag()->item("----:com.apple.iTunes:REMIXEDBY").toStringList());
546 CPPUNIT_ASSERT_EQUAL(StringList("Remixed by"), tags["REMIXEDBY"]);
547
548 tags["COMPILATION"] = StringList("0");
549 f.setProperties(tags);
550
551 tags = f.properties();
552
553 CPPUNIT_ASSERT(f.tag()->contains("cpil"));
554 CPPUNIT_ASSERT_EQUAL(false, f.tag()->item("cpil").toBool());
555 CPPUNIT_ASSERT_EQUAL(StringList("0"), tags["COMPILATION"]);
556
557 // Empty properties do not result in access violations
558 // when converting integers
559 tags["TRACKNUMBER"] = StringList();
560 tags["DISCNUMBER"] = StringList();
561 tags["BPM"] = StringList();
562 tags["COMPILATION"] = StringList();
563 f.setProperties(tags);
564 }
565
566 void testPropertiesAllSupported()
567 {
568 PropertyMap tags;
569 tags["ALBUM"] = StringList("Album");
570 tags["ALBUMARTIST"] = StringList("Album Artist");
571 tags["ALBUMARTISTSORT"] = StringList("Album Artist Sort");
572 tags["ALBUMSORT"] = StringList("Album Sort");
573 tags["ARTIST"] = StringList("Artist");
574 tags["ARTISTS"] = StringList("Artists");
575 tags["ARTISTSORT"] = StringList("Artist Sort");
576 tags["ASIN"] = StringList("ASIN");
577 tags["BARCODE"] = StringList("Barcode");
578 tags["BPM"] = StringList("123");
579 tags["CATALOGNUMBER"] = StringList("Catalog Number");
580 tags["COMMENT"] = StringList("Comment");
581 tags["COMPILATION"] = StringList("1");
582 tags["COMPOSER"] = StringList("Composer");
583 tags["COMPOSERSORT"] = StringList("Composer Sort");
584 tags["CONDUCTOR"] = StringList("Conductor");
585 tags["COPYRIGHT"] = StringList("2021 Copyright");
586 tags["DATE"] = StringList("2021-01-03 12:29:23");
587 tags["DISCNUMBER"] = StringList("3/5");
588 tags["DISCSUBTITLE"] = StringList("Disc Subtitle");
589 tags["DJMIXER"] = StringList("DJ Mixer");
590 tags["ENCODEDBY"] = StringList("Encoded by");
591 tags["ENCODING"] = StringList("Encoding");
592 tags["ENGINEER"] = StringList("Engineer");
593 tags["GAPLESSPLAYBACK"] = StringList("1");
594 tags["GENRE"] = StringList("Genre");
595 tags["GROUPING"] = StringList("Grouping");
596 tags["ISRC"] = StringList("UKAAA0500001");
597 tags["LABEL"] = StringList("Label");
598 tags["LANGUAGE"] = StringList("eng");
599 tags["LICENSE"] = StringList("License");
600 tags["LYRICIST"] = StringList("Lyricist");
601 tags["LYRICS"] = StringList("Lyrics");
602 tags["MEDIA"] = StringList("Media");
603 tags["MIXER"] = StringList("Mixer");
604 tags["MOOD"] = StringList("Mood");
605 tags["MOVEMENTCOUNT"] = StringList("3");
606 tags["MOVEMENTNAME"] = StringList("Movement Name");
607 tags["MOVEMENTNUMBER"] = StringList("2");
608 tags["MUSICBRAINZ_ALBUMARTISTID"] = StringList("MusicBrainz_AlbumartistID");
609 tags["MUSICBRAINZ_ALBUMID"] = StringList("MusicBrainz_AlbumID");
610 tags["MUSICBRAINZ_ARTISTID"] = StringList("MusicBrainz_ArtistID");
611 tags["MUSICBRAINZ_RELEASEGROUPID"] = StringList("MusicBrainz_ReleasegroupID");
612 tags["MUSICBRAINZ_RELEASETRACKID"] = StringList("MusicBrainz_ReleasetrackID");
613 tags["MUSICBRAINZ_TRACKID"] = StringList("MusicBrainz_TrackID");
614 tags["MUSICBRAINZ_WORKID"] = StringList("MusicBrainz_WorkID");
615 tags["ORIGINALDATE"] = StringList("2021-01-03 13:52:19");
616 tags["OWNER"] = StringList("Owner");
617 tags["PODCAST"] = StringList("1");
618 tags["PODCASTCATEGORY"] = StringList("Podcast Category");
619 tags["PODCASTDESC"] = StringList("Podcast Description");
620 tags["PODCASTID"] = StringList("Podcast ID");
621 tags["PODCASTURL"] = StringList("Podcast URL");
622 tags["PRODUCER"] = StringList("Producer");
623 tags["RELEASECOUNTRY"] = StringList("Release Country");
624 tags["RELEASESTATUS"] = StringList("Release Status");
625 tags["RELEASETYPE"] = StringList("Release Type");
626 tags["REMIXER"] = StringList("Remixer");
627 tags["SCRIPT"] = StringList("Script");
628 tags["SHOWSORT"] = StringList("Show Sort");
629 tags["SHOWWORKMOVEMENT"] = StringList("1");
630 tags["SUBTITLE"] = StringList("Subtitle");
631 tags["TITLE"] = StringList("Title");
632 tags["TITLESORT"] = StringList("Title Sort");
633 tags["TRACKNUMBER"] = StringList("2/4");
634 tags["TVEPISODE"] = StringList("3");
635 tags["TVEPISODEID"] = StringList("TV Episode ID");
636 tags["TVNETWORK"] = StringList("TV Network");
637 tags["TVSEASON"] = StringList("2");
638 tags["TVSHOW"] = StringList("TV Show");
639 tags["WORK"] = StringList("Work");
640
641 ScopedFileCopy copy("no-tags", ".m4a");
642 {
643 MP4::File f(copy.fileName().c_str());
644 PropertyMap properties = f.properties();
645 CPPUNIT_ASSERT(properties.isEmpty());
646 f.setProperties(tags);
647 f.save();
648 }
649 {
650 const MP4::File f(copy.fileName().c_str());
651 PropertyMap properties = f.properties();
652 if (tags != properties) {
653 CPPUNIT_ASSERT_EQUAL(tags.toString(), properties.toString());
654 }
655 CPPUNIT_ASSERT(tags == properties);
656 }
657 }
658
659 void testPropertiesMovement()
660 {
661 MP4::File f(TEST_FILE_PATH_C("has-tags.m4a"));
662
663 PropertyMap tags = f.properties();
664
665 tags["WORK"] = StringList("Foo");
666 tags["MOVEMENTNAME"] = StringList("Bar");
667 tags["MOVEMENTNUMBER"] = StringList("2");
668 tags["MOVEMENTCOUNT"] = StringList("3");
669 tags["SHOWWORKMOVEMENT"] = StringList("1");
670 f.setProperties(tags);
671
672 tags = f.properties();
673
674 CPPUNIT_ASSERT(f.tag()->contains("\251wrk"));
675 CPPUNIT_ASSERT_EQUAL(StringList("Foo"), f.tag()->item("\251wrk").toStringList());
676 CPPUNIT_ASSERT_EQUAL(StringList("Foo"), tags["WORK"]);
677
678 CPPUNIT_ASSERT(f.tag()->contains("\251mvn"));
679 CPPUNIT_ASSERT_EQUAL(StringList("Bar"), f.tag()->item("\251mvn").toStringList());
680 CPPUNIT_ASSERT_EQUAL(StringList("Bar"), tags["MOVEMENTNAME"]);
681
682 CPPUNIT_ASSERT(f.tag()->contains("\251mvi"));
683 CPPUNIT_ASSERT_EQUAL(2, f.tag()->item("\251mvi").toInt());
684 CPPUNIT_ASSERT_EQUAL(StringList("2"), tags["MOVEMENTNUMBER"]);
685
686 CPPUNIT_ASSERT(f.tag()->contains("\251mvc"));
687 CPPUNIT_ASSERT_EQUAL(3, f.tag()->item("\251mvc").toInt());
688 CPPUNIT_ASSERT_EQUAL(StringList("3"), tags["MOVEMENTCOUNT"]);
689
690 CPPUNIT_ASSERT(f.tag()->contains("shwm"));
691 CPPUNIT_ASSERT_EQUAL(true, f.tag()->item("shwm").toBool());
692 CPPUNIT_ASSERT_EQUAL(StringList("1"), tags["SHOWWORKMOVEMENT"]);
693
694 tags["SHOWWORKMOVEMENT"] = StringList("0");
695 f.setProperties(tags);
696
697 tags = f.properties();
698
699 CPPUNIT_ASSERT(f.tag()->contains("shwm"));
700 CPPUNIT_ASSERT_EQUAL(false, f.tag()->item("shwm").toBool());
701 CPPUNIT_ASSERT_EQUAL(StringList("0"), tags["SHOWWORKMOVEMENT"]);
702
703 tags["WORK"] = StringList();
704 tags["MOVEMENTNAME"] = StringList();
705 tags["MOVEMENTNUMBER"] = StringList();
706 tags["MOVEMENTCOUNT"] = StringList();
707 tags["SHOWWORKMOVEMENT"] = StringList();
708 f.setProperties(tags);
709 }
710
711 void testFuzzedFile()
712 {
713 MP4::File f(TEST_FILE_PATH_C("infloop.m4a"));
714 CPPUNIT_ASSERT(f.isValid());
715 }
716
717 void testRepeatedSave()
718 {
719 ScopedFileCopy copy("no-tags", ".m4a");
720
721 MP4::File f(copy.fileName().c_str());
722 f.tag()->setTitle("0123456789");
723 f.save();
724 f.save();
725 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2862), f.find("0123456789"));
726 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(-1), f.find("0123456789", 2863));
727 }
728
729 void testWithZeroLengthAtom()
730 {
731 MP4::File f(TEST_FILE_PATH_C("zero-length-mdat.m4a"));
732 CPPUNIT_ASSERT(f.isValid());
733 CPPUNIT_ASSERT_EQUAL(1115, f.audioProperties()->lengthInMilliseconds());
734 CPPUNIT_ASSERT_EQUAL(22050, f.audioProperties()->sampleRate());
735 }
736
737 void testEmptyValuesRemoveItems()
738 {
739 const MP4::File f(TEST_FILE_PATH_C("has-tags.m4a"));
740 MP4::Tag *tag = f.tag();
741 const String testTitle("Title");
742 const String testArtist("Artist");
743 const String testAlbum("Album");
744 const String testComment("Comment");
745 const String testGenre("Genre");
746 const String nullString;
747 constexpr unsigned int testYear = 2020;
748 constexpr unsigned int testTrack = 1;
749 constexpr unsigned int zeroUInt = 0;
750
751 tag->setTitle(testTitle);
752 CPPUNIT_ASSERT_EQUAL(testTitle, tag->title());
753 CPPUNIT_ASSERT(tag->contains("\251nam"));
754 tag->setArtist(testArtist);
755 CPPUNIT_ASSERT_EQUAL(testArtist, tag->artist());
756 CPPUNIT_ASSERT(tag->contains("\251ART"));
757 tag->setAlbum(testAlbum);
758 CPPUNIT_ASSERT_EQUAL(testAlbum, tag->album());
759 CPPUNIT_ASSERT(tag->contains("\251alb"));
760 tag->setComment(testComment);
761 CPPUNIT_ASSERT_EQUAL(testComment, tag->comment());
762 CPPUNIT_ASSERT(tag->contains("\251cmt"));
763 tag->setGenre(testGenre);
764 CPPUNIT_ASSERT_EQUAL(testGenre, tag->genre());
765 CPPUNIT_ASSERT(tag->contains("\251gen"));
766 tag->setYear(testYear);
767 CPPUNIT_ASSERT_EQUAL(testYear, tag->year());
768 CPPUNIT_ASSERT(tag->contains("\251day"));
769 tag->setTrack(testTrack);
770 CPPUNIT_ASSERT_EQUAL(testTrack, tag->track());
771 CPPUNIT_ASSERT(tag->contains("trkn"));
772
773 tag->setTitle(nullString);
774 CPPUNIT_ASSERT_EQUAL(nullString, tag->title());
775 CPPUNIT_ASSERT(!tag->contains("\251nam"));
776 tag->setArtist(nullString);
777 CPPUNIT_ASSERT_EQUAL(nullString, tag->artist());
778 CPPUNIT_ASSERT(!tag->contains("\251ART"));
779 tag->setAlbum(nullString);
780 CPPUNIT_ASSERT_EQUAL(nullString, tag->album());
781 CPPUNIT_ASSERT(!tag->contains("\251alb"));
782 tag->setComment(nullString);
783 CPPUNIT_ASSERT_EQUAL(nullString, tag->comment());
784 CPPUNIT_ASSERT(!tag->contains("\251cmt"));
785 tag->setGenre(nullString);
786 CPPUNIT_ASSERT_EQUAL(nullString, tag->genre());
787 CPPUNIT_ASSERT(!tag->contains("\251gen"));
788 tag->setYear(zeroUInt);
789 CPPUNIT_ASSERT_EQUAL(zeroUInt, tag->year());
790 CPPUNIT_ASSERT(!tag->contains("\251day"));
791 tag->setTrack(zeroUInt);
792 CPPUNIT_ASSERT_EQUAL(zeroUInt, tag->track());
793 CPPUNIT_ASSERT(!tag->contains("trkn"));
794 }
795
796 void testRemoveMetadata()
797 {
798 ScopedFileCopy copy("no-tags", ".m4a");
799
800 {
801 MP4::File f(copy.fileName().c_str());
802 CPPUNIT_ASSERT(f.isValid());
803 CPPUNIT_ASSERT(!f.hasMP4Tag());
804 MP4::Tag *tag = f.tag();
805 CPPUNIT_ASSERT(tag->isEmpty());
806 tag->setTitle("TITLE");
807 f.save();
808 }
809 {
810 MP4::File f(copy.fileName().c_str());
811 CPPUNIT_ASSERT(f.isValid());
812 CPPUNIT_ASSERT(f.hasMP4Tag());
813 CPPUNIT_ASSERT(!f.tag()->isEmpty());
814 f.strip();
815 }
816 {
817 MP4::File f(copy.fileName().c_str());
818 CPPUNIT_ASSERT(f.isValid());
819 CPPUNIT_ASSERT(!f.hasMP4Tag());
820 CPPUNIT_ASSERT(f.tag()->isEmpty());
821 CPPUNIT_ASSERT(fileEqual(
822 copy.fileName(),
823 testFilePath("no-tags.m4a")));
824 }
825 }
826
827 void testNonFullMetaAtom()
828 {
829 {
830 MP4::File f(TEST_FILE_PATH_C("non-full-meta.m4a"));
831 CPPUNIT_ASSERT(f.isValid());
832 CPPUNIT_ASSERT(f.hasMP4Tag());
833
834 CPPUNIT_ASSERT(f.tag()->contains("covr"));
835 MP4::CoverArtList l = f.tag()->item("covr").toCoverArtList();
836 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), l.size());
837 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::PNG, l[0].format());
838 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(79), l[0].data().size());
839 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::JPEG, l[1].format());
840 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(287), l[1].data().size());
841
842 PropertyMap properties = f.properties();
843 CPPUNIT_ASSERT_EQUAL(StringList("Test Artist!!!!"), properties["ARTIST"]);
844 CPPUNIT_ASSERT_EQUAL(StringList("FAAC 1.24"), properties["ENCODING"]);
845 }
846 }
847
848 void testItemFactory()
849 {
850 ScopedFileCopy copy("no-tags", ".m4a");
851 {
852 MP4::File f(copy.fileName().c_str());
853 CPPUNIT_ASSERT(f.isValid());
854 CPPUNIT_ASSERT(!f.hasMP4Tag());
855 MP4::Tag *tag = f.tag();
856 tag->setItem("tsti", MP4::Item(123));
857 tag->setItem("tstt", MP4::Item(StringList("Test text")));
858 f.save();
859 }
860 {
861 MP4::File f(copy.fileName().c_str());
862 CPPUNIT_ASSERT(f.isValid());
863 CPPUNIT_ASSERT(f.hasMP4Tag());
864 MP4::Tag *tag = f.tag();
865 // Without a custom item factory, only custom text atoms with four
866 // letter names are possible.
867 MP4::Item item = tag->item("tsti");
868 CPPUNIT_ASSERT(!item.isValid());
869 CPPUNIT_ASSERT(item.toInt() != 123);
870 item = tag->item("tstt");
871 CPPUNIT_ASSERT(item.isValid());
872 CPPUNIT_ASSERT_EQUAL(StringList("Test text"), item.toStringList());
873 f.strip();
874 }
875 {
876 MP4::File f(copy.fileName().c_str(),
877 true, MP4::Properties::Average, CustomItemFactory::instance());
878 CPPUNIT_ASSERT(f.isValid());
879 CPPUNIT_ASSERT(!f.hasMP4Tag());
880 MP4::Tag *tag = f.tag();
881 tag->setItem("tsti", MP4::Item(123));
882 tag->setItem("tstt", MP4::Item(StringList("Test text")));
883 tag->setItem("trkn", MP4::Item(2, 10));
884 tag->setItem("rate", MP4::Item(80));
885 tag->setItem("plID", MP4::Item(1540934238LL));
886 tag->setItem("rtng", MP4::Item(static_cast<unsigned char>(2)));
887 f.save();
888 }
889 {
890 MP4::File f(copy.fileName().c_str(),
891 true, MP4::Properties::Average, CustomItemFactory::instance());
892 CPPUNIT_ASSERT(f.isValid());
893 CPPUNIT_ASSERT(f.hasMP4Tag());
894 MP4::Tag *tag = f.tag();
895 MP4::Item item = tag->item("tsti");
896 CPPUNIT_ASSERT(item.isValid());
897 CPPUNIT_ASSERT_EQUAL(123, item.toInt());
898 item = tag->item("tstt");
899 CPPUNIT_ASSERT(item.isValid());
900 CPPUNIT_ASSERT_EQUAL(StringList("Test text"), item.toStringList());
901 item = tag->item("trkn");
902 CPPUNIT_ASSERT(item.isValid());
903 CPPUNIT_ASSERT_EQUAL(2, item.toIntPair().first);
904 CPPUNIT_ASSERT_EQUAL(10, item.toIntPair().second);
905 CPPUNIT_ASSERT_EQUAL(80, tag->item("rate").toInt());
906 CPPUNIT_ASSERT_EQUAL(1540934238LL, tag->item("plID").toLongLong());
907 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned char>(2), tag->item("rtng").toByte());
908 PropertyMap properties = tag->properties();
909 CPPUNIT_ASSERT_EQUAL(StringList("123"), properties.value("TESTINTEGER"));
910 CPPUNIT_ASSERT_EQUAL(StringList("2/10"), properties.value("TRACKNUMBER"));
911 properties["TESTINTEGER"] = StringList("456");
912 tag->setProperties(properties);
913 f.save();
914 }
915 {
916 MP4::File f(copy.fileName().c_str(),
917 true, MP4::Properties::Average, CustomItemFactory::instance());
918 CPPUNIT_ASSERT(f.isValid());
919 CPPUNIT_ASSERT(f.hasMP4Tag());
920 MP4::Tag *tag = f.tag();
921 MP4::Item item = tag->item("tsti");
922 CPPUNIT_ASSERT(item.isValid());
923 CPPUNIT_ASSERT_EQUAL(456, item.toInt());
924 PropertyMap properties = tag->properties();
925 CPPUNIT_ASSERT_EQUAL(StringList("456"), properties.value("TESTINTEGER"));
926 }
927 }
928
929 void testNonPrintableAtom()
930 {
931 ScopedFileCopy copy("nonprintable-atom-type", ".m4a");
932 {
933 MP4::File f(copy.fileName().c_str());
934 CPPUNIT_ASSERT(f.isValid());
935 CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
936 CPPUNIT_ASSERT_EQUAL(32000, f.audioProperties()->sampleRate());
937 f.tag()->setTitle("TITLE");
938 f.save();
939 }
940 {
941 MP4::File f(copy.fileName().c_str());
942 CPPUNIT_ASSERT(f.isValid());
943 CPPUNIT_ASSERT(f.hasMP4Tag());
944 CPPUNIT_ASSERT_EQUAL(String("TITLE"), f.tag()->title());
945 }
946 }
947
948 void testChapterListWrite()
949 {
950 ScopedFileCopy copy("no-tags", ".m4a");
951 string filename = copy.fileName();
952
953 // File should have no chapters initially
954 {
955 MP4::File f(filename.c_str());
956 MP4::ChapterList chapters = f.neroChapters();
957 CPPUNIT_ASSERT(chapters.isEmpty());
958 }
959
960 // Write chapters
961 {
962 MP4::File f(filename.c_str());
963 f.setNeroChapters(MP4::ChapterList{
964 MP4::Chapter("Introduction", 0),
965 MP4::Chapter("Main Content", 30000LL),
966 MP4::Chapter("Conclusion", 60000LL)
967 });
968 CPPUNIT_ASSERT(f.save());
969 }
970
971 // Read back and verify
972 {
973 MP4::File f(filename.c_str());
974 MP4::ChapterList chapters = f.neroChapters();
975 CPPUNIT_ASSERT_EQUAL(3U, chapters.size());
976 CPPUNIT_ASSERT_EQUAL(0LL, chapters[0].startTime());
977 CPPUNIT_ASSERT_EQUAL(String("Introduction"), chapters[0].title());
978 CPPUNIT_ASSERT_EQUAL(30000LL, chapters[1].startTime());
979 CPPUNIT_ASSERT_EQUAL(String("Main Content"), chapters[1].title());
980 CPPUNIT_ASSERT_EQUAL(60000LL, chapters[2].startTime());
981 CPPUNIT_ASSERT_EQUAL(String("Conclusion"), chapters[2].title());
982
983 // Overwrite with different chapters
984 f.setNeroChapters(MP4::ChapterList{
985 MP4::Chapter("Part One", 0)
986 });
987 CPPUNIT_ASSERT(f.save());
988 }
989
990 // Verify overwrite
991 {
992 MP4::File f(filename.c_str());
993 MP4::ChapterList chapters = f.neroChapters();
994 CPPUNIT_ASSERT_EQUAL(1U, chapters.size());
995 CPPUNIT_ASSERT_EQUAL(String("Part One"), chapters[0].title());
996 }
997 }
998
999 void testChapterListRemove()
1000 {
1001 ScopedFileCopy copy("no-tags", ".m4a");
1002 string filename = copy.fileName();
1003
1004 // Write chapters
1005 {
1006 MP4::File f(filename.c_str());
1007 f.setNeroChapters(MP4::ChapterList{
1008 MP4::Chapter("Chapter 1", 0)
1009 });
1010 CPPUNIT_ASSERT(f.save());
1011 }
1012
1013 // Verify written
1014 {
1015 MP4::File f(filename.c_str());
1016 MP4::ChapterList chapters = f.neroChapters();
1017 CPPUNIT_ASSERT_EQUAL(1U, chapters.size());
1018
1019 // Remove chapters
1020 f.setNeroChapters(MP4::ChapterList());
1021 CPPUNIT_ASSERT(f.save());
1022 }
1023
1024 // Verify removed
1025 {
1026 MP4::File f(filename.c_str());
1027 MP4::ChapterList chapters = f.neroChapters();
1028 CPPUNIT_ASSERT(chapters.isEmpty());
1029
1030 // Remove from file with no chapters should also succeed
1031 f.setNeroChapters(MP4::ChapterList());
1032 CPPUNIT_ASSERT(f.save());
1033 }
1034 }
1035
1036 void testChapterListWithExistingTags()
1037 {
1038 ScopedFileCopy copy("has-tags", ".m4a");
1039 string filename = copy.fileName();
1040
1041 // File has existing tags -- verify they survive chapter operations
1042 String originalArtist;
1043 {
1044 MP4::File f(filename.c_str());
1045 CPPUNIT_ASSERT(f.isValid());
1046 originalArtist = f.tag()->artist();
1047 CPPUNIT_ASSERT(!originalArtist.isEmpty());
1048
1049 // Write chapters
1050 f.setNeroChapters(MP4::ChapterList{
1051 MP4::Chapter("Intro", 0),
1052 MP4::Chapter("Verse", 10000LL)});
1053 CPPUNIT_ASSERT(f.save());
1054 }
1055
1056 // Verify chapters are written AND existing tags are preserved
1057 {
1058 MP4::File f(filename.c_str());
1059 CPPUNIT_ASSERT(f.isValid());
1060 MP4::ChapterList chapters = f.neroChapters();
1061 CPPUNIT_ASSERT_EQUAL(2U, chapters.size());
1062 CPPUNIT_ASSERT_EQUAL(String("Intro"), chapters[0].title());
1063 CPPUNIT_ASSERT_EQUAL(String("Verse"), chapters[1].title());
1064 CPPUNIT_ASSERT_EQUAL(originalArtist, f.tag()->artist());
1065
1066 // Remove chapters and verify tags still survive
1067 f.setNeroChapters(MP4::ChapterList());
1068 CPPUNIT_ASSERT(f.save());
1069 }
1070
1071 {
1072 MP4::File f(filename.c_str());
1073 CPPUNIT_ASSERT(f.isValid());
1074 CPPUNIT_ASSERT_EQUAL(originalArtist, f.tag()->artist());
1075 CPPUNIT_ASSERT(f.neroChapters().isEmpty());
1076 }
1077 }
1078
1079 void testChapterListReadEmpty()
1080 {
1081 // Reading from a file with no chpl atom should return empty list
1082 ScopedFileCopy copy("no-tags", ".m4a");
1083 string filename = copy.fileName();
1084
1085 {
1086 MP4::File f(filename.c_str());
1087 CPPUNIT_ASSERT(f.neroChapters().isEmpty());
1088 }
1089 }
1090
1091 void testQTChapterListWrite()
1092 {
1093 ScopedFileCopy copy("no-tags", ".m4a");
1094 string filename = copy.fileName();
1095
1096 // File should have no QT chapters initially
1097 {
1098 MP4::File f(filename.c_str());
1099 MP4::ChapterList chapters = f.qtChapters();
1100 CPPUNIT_ASSERT(chapters.isEmpty());
1101 }
1102
1103 // Write chapters (times in 100-nanosecond units)
1104 {
1105 MP4::File f(filename.c_str());
1106 f.setQtChapters(MP4::ChapterList{
1107 MP4::Chapter("Intro", 0),
1108 MP4::Chapter("Verse", 15000LL),
1109 MP4::Chapter("Outro", 30000LL)
1110 });
1111 CPPUNIT_ASSERT(f.save());
1112 }
1113
1114 // Read back and verify
1115 {
1116 MP4::File f(filename.c_str());
1117 MP4::ChapterList chapters = f.qtChapters();
1118 CPPUNIT_ASSERT_EQUAL(3U, chapters.size());
1119 CPPUNIT_ASSERT_EQUAL(0LL, chapters[0].startTime());
1120 CPPUNIT_ASSERT_EQUAL(String("Intro"), chapters[0].title());
1121 CPPUNIT_ASSERT_EQUAL(15000LL, chapters[1].startTime());
1122 CPPUNIT_ASSERT_EQUAL(String("Verse"), chapters[1].title());
1123 CPPUNIT_ASSERT_EQUAL(30000LL, chapters[2].startTime());
1124 CPPUNIT_ASSERT_EQUAL(String("Outro"), chapters[2].title());
1125 }
1126 }
1127
1128 void testQTChapterListRemove()
1129 {
1130 ScopedFileCopy copy("no-tags", ".m4a");
1131 string filename = copy.fileName();
1132
1133 // Write chapters first
1134 {
1135 MP4::File f(filename.c_str());
1136 f.setQtChapters(MP4::ChapterList{
1137 MP4::Chapter("Chapter 1", 0),
1138 MP4::Chapter("Chapter 2", 10000LL)
1139 });
1140 CPPUNIT_ASSERT(f.save());
1141 }
1142
1143 // Verify written
1144 {
1145 MP4::File f(filename.c_str());
1146 MP4::ChapterList chapters = f.qtChapters();
1147 CPPUNIT_ASSERT_EQUAL(2U, chapters.size());
1148
1149 // Remove chapters
1150 f.setQtChapters(MP4::ChapterList());
1151 CPPUNIT_ASSERT(f.save());
1152 }
1153
1154 // Verify removed
1155 {
1156 MP4::File f(filename.c_str());
1157 MP4::ChapterList chapters = f.qtChapters();
1158 CPPUNIT_ASSERT(chapters.isEmpty());
1159
1160 // Remove from file with no chapters should also succeed
1161 f.setQtChapters(MP4::ChapterList());
1162 CPPUNIT_ASSERT(f.save());
1163 }
1164 }
1165
1166 void testQTChapterListWithExistingTags()
1167 {
1168 ScopedFileCopy copy("has-tags", ".m4a");
1169 string filename = copy.fileName();
1170
1171 // File has existing tags -- verify they survive chapter operations
1172 String originalArtist;
1173 {
1174 MP4::File f(filename.c_str());
1175 CPPUNIT_ASSERT(f.isValid());
1176 originalArtist = f.tag()->artist();
1177 CPPUNIT_ASSERT(!originalArtist.isEmpty());
1178
1179 // Write chapters
1180 f.setQtChapters(MP4::ChapterList{
1181 MP4::Chapter("Intro", 0),
1182 MP4::Chapter("Verse", 10000LL)
1183 });
1184 CPPUNIT_ASSERT(f.save());
1185 }
1186
1187 // Verify chapters are written AND existing tags are preserved
1188 {
1189 MP4::File f(filename.c_str());
1190 MP4::ChapterList chapters = f.qtChapters();
1191 CPPUNIT_ASSERT_EQUAL(2U, chapters.size());
1192 CPPUNIT_ASSERT_EQUAL(String("Intro"), chapters[0].title());
1193 CPPUNIT_ASSERT_EQUAL(String("Verse"), chapters[1].title());
1194
1195 CPPUNIT_ASSERT(f.isValid());
1196 CPPUNIT_ASSERT_EQUAL(originalArtist, f.tag()->artist());
1197
1198 // Remove chapters and verify tags still survive
1199 f.setQtChapters(MP4::ChapterList());
1200 CPPUNIT_ASSERT(f.save());
1201 }
1202
1203 {
1204 MP4::File f(filename.c_str());
1205 CPPUNIT_ASSERT(f.isValid());
1206 CPPUNIT_ASSERT_EQUAL(originalArtist, f.tag()->artist());
1207 CPPUNIT_ASSERT(f.qtChapters().isEmpty());
1208 }
1209 }
1210
1211 void testQTChapterListReadEmpty()
1212 {
1213 // Reading from a file with no chapter track should return empty list
1214 ScopedFileCopy copy("no-tags", ".m4a");
1215 string filename = copy.fileName();
1216
1217 {
1218 MP4::File f(filename.c_str());
1219 CPPUNIT_ASSERT(f.qtChapters().isEmpty());
1220 }
1221 }
1222
1223 void testQTChapterListOverwrite()
1224 {
1225 ScopedFileCopy copy("no-tags", ".m4a");
1226 string filename = copy.fileName();
1227
1228 // Write initial chapters
1229 {
1230 MP4::File f(filename.c_str());
1231 f.setQtChapters(MP4::ChapterList{
1232 MP4::Chapter("Old1", 0),
1233 MP4::Chapter("Old2", 5000LL)
1234 });
1235 CPPUNIT_ASSERT(f.save());
1236 }
1237
1238 // Verify initial
1239 {
1240 MP4::File f(filename.c_str());
1241 MP4::ChapterList chapters = f.qtChapters();
1242 CPPUNIT_ASSERT_EQUAL(2U, chapters.size());
1243 }
1244
1245 // Overwrite with different chapters
1246 {
1247 MP4::File f(filename.c_str());
1248 f.setQtChapters(MP4::ChapterList{
1249 MP4::Chapter("New1", 0),
1250 MP4::Chapter("New2", 10000LL),
1251 MP4::Chapter("New3", 20000LL)
1252 });
1253 CPPUNIT_ASSERT(f.save());
1254 }
1255
1256 // Verify overwrite
1257 {
1258 MP4::File f(filename.c_str());
1259 MP4::ChapterList chapters = f.qtChapters();
1260 CPPUNIT_ASSERT_EQUAL(3U, chapters.size());
1261 CPPUNIT_ASSERT_EQUAL(String("New1"), chapters[0].title());
1262 CPPUNIT_ASSERT_EQUAL(String("New2"), chapters[1].title());
1263 CPPUNIT_ASSERT_EQUAL(String("New3"), chapters[2].title());
1264 }
1265 }
1266
1267 void testQTChapterListTimestampPrecision()
1268 {
1269 ScopedFileCopy copy("no-tags", ".m4a");
1270 string filename = copy.fileName();
1271
1272 // Write chapters at precise times
1273 {
1274 MP4::File f(filename.c_str());
1275 f.setQtChapters(MP4::ChapterList{
1276 MP4::Chapter("Start", 0),
1277 MP4::Chapter("Precise", 1500LL)
1278 });
1279 CPPUNIT_ASSERT(f.save());
1280 }
1281
1282 // Read back and verify timestamps
1283 {
1284 MP4::File f(filename.c_str());
1285 MP4::ChapterList chapters = f.qtChapters();
1286 CPPUNIT_ASSERT_EQUAL(2U, chapters.size());
1287 CPPUNIT_ASSERT_EQUAL(0LL, chapters[0].startTime());
1288 CPPUNIT_ASSERT_EQUAL(1500LL, chapters[1].startTime());
1289 }
1290 }
1291
1292 void testQTChapterListNonZeroFirstChapter()
1293 {
1294 ScopedFileCopy copy("no-tags", ".m4a");
1295 string filename = copy.fileName();
1296
1297 // Write chapters where first chapter is NOT at time 0
1298 {
1299 MP4::File f(filename.c_str());
1300 f.setQtChapters(MP4::ChapterList{
1301 MP4::Chapter("One", 10000LL),
1302 MP4::Chapter("Two", 20000LL),
1303 MP4::Chapter("Three", 30000LL)
1304 });
1305 CPPUNIT_ASSERT(f.save());
1306 }
1307
1308 // Read back -- dummy chapter at time 0 should be stripped
1309 {
1310 MP4::File f(filename.c_str());
1311 MP4::ChapterList chapters = f.qtChapters();
1312 CPPUNIT_ASSERT_EQUAL(3U, chapters.size());
1313 CPPUNIT_ASSERT_EQUAL(10000LL, chapters[0].startTime());
1314 CPPUNIT_ASSERT_EQUAL(20000LL, chapters[1].startTime());
1315 CPPUNIT_ASSERT_EQUAL(30000LL, chapters[2].startTime());
1316 CPPUNIT_ASSERT_EQUAL(String("One"), chapters[0].title());
1317 CPPUNIT_ASSERT_EQUAL(String("Two"), chapters[1].title());
1318 CPPUNIT_ASSERT_EQUAL(String("Three"), chapters[2].title());
1319 }
1320 }
1321
1322 // Regression test for the orphaned-mdat bug reported in PR #1325 by ufleisch.
1323 // Each add/remove cycle must leave the file's mdat count unchanged. Before
1324 // the fix, the chapter mdat appended by write() was never removed, so three
1325 // cycles produced originalCount + 3 mdat atoms.
1326 void testQTChapterListNoOrphanedMdat()
1327 {
1328 ScopedFileCopy copy("no-tags", ".m4a");
1329 string filename = copy.fileName();
1330
1331 // Count top-level mdat atoms using TagLib's own atom parser.
1332 auto countMdatTagLib = [&]() -> int {
1333 PlainFile pf(filename.c_str());
1334 MP4::Atoms atoms(&pf);
1335 int count = 0;
1336 for(const auto *atom : atoms.atoms())
1337 if(atom->name() == "mdat")
1338 ++count;
1339 return count;
1340 };
1341
1342 const int baseMdatTagLib = countMdatTagLib();
1343
1344 // Three add/remove cycles (the scenario ufleisch demonstrated).
1345 for(int cycle = 0; cycle < 3; ++cycle) {
1346 {
1347 MP4::File f(filename.c_str());
1348 f.setQtChapters(MP4::ChapterList{
1349 MP4::Chapter("Chapter 1", 0),
1350 MP4::Chapter("Chapter 2", 10000LL)
1351 });
1352 CPPUNIT_ASSERT(f.save());
1353 }
1354 {
1355 MP4::File f(filename.c_str());
1356 f.setQtChapters(MP4::ChapterList());
1357 CPPUNIT_ASSERT(f.save());
1358 }
1359 }
1360
1361 // No orphaned mdat atoms should remain.
1362 CPPUNIT_ASSERT_EQUAL(baseMdatTagLib, countMdatTagLib());
1363 }
1364
1365 // Regression test for the data-loss bug reported in PR #1343 by ufleisch.
1366 // Audiobook-style files co-locate chapter text samples inside the main
1367 // audio mdat. In that case the chapter track's stco[0] does NOT mark a
1368 // dedicated chapter mdat -- it points into the shared audio mdat, and
1369 // naively deleting "the mdat at stco[0] - 8" destroys the audio payload.
1370 //
1371 // Simulate that layout by writing a chapter track, then rewriting its
1372 // stco[0] to point at the start of the primary audio mdat. Removing the
1373 // chapter track must leave the audio mdat fully intact.
1374 void testQTChapterListSharedMdatPreservesAudio()
1375 {
1376 ScopedFileCopy copy("no-tags", ".m4a");
1377 string filename = copy.fileName();
1378
1379 struct MdatInfo { offset_t offset; offset_t length; };
1380 auto findFirstMdat = [&]() -> MdatInfo {
1381 PlainFile pf(filename.c_str());
1382 MP4::Atoms atoms(&pf);
1383 for(const auto *atom : atoms.atoms())
1384 if(atom->name() == "mdat")
1385 return {atom->offset(), atom->length()};
1386 return {-1, 0};
1387 };
1388
1389 const MdatInfo audioMdat = findFirstMdat();
1390 CPPUNIT_ASSERT(audioMdat.offset >= 0);
1391 CPPUNIT_ASSERT(audioMdat.length > 16);
1392
1393 // Capture the audio mdat bytes so we can confirm byte-for-byte preservation.
1394 ByteVector originalAudioMdat;
1395 {
1396 PlainFile pf(filename.c_str());
1397 pf.seek(audioMdat.offset);
1398 originalAudioMdat = pf.readBlock(audioMdat.length);
1399 }
1400
1401 // Add a chapter track. write() appends its own mdat for the chapter text
1402 // at EOF; we'll relocate stco[0] below to simulate the shared-mdat case.
1403 {
1404 MP4::File f(filename.c_str());
1405 f.setQtChapters(MP4::ChapterList{
1406 MP4::Chapter("Chapter 1", 0),
1407 MP4::Chapter("Chapter 2", 1000LL)
1408 });
1409 CPPUNIT_ASSERT(f.save());
1410 }
1411
1412 // Rewrite the chapter track's stco[0] to point inside the audio mdat's
1413 // data, so findMdatContaining() will identify the audio mdat as the
1414 // candidate target. Choosing audioMdat.offset + 8 (the data start) is
1415 // the worst case: without the shared-mdat guard, the old code would
1416 // treat the audio mdat header as the chapter mdat header and wipe it.
1417 {
1418 PlainFile pf(filename.c_str());
1419 MP4::Atoms atoms(&pf);
1420 const MP4::Atom *moov = atoms.find("moov");
1421 CPPUNIT_ASSERT(moov);
1422 const MP4::AtomList traks = moov->findall("trak");
1423 CPPUNIT_ASSERT(traks.size() >= 2);
1424 // The chapter trak is the most recently added -- find the one whose
1425 // hdlr handler_type is "text".
1426 MP4::Atom *chapterTrak = nullptr;
1427 for(auto *t : traks) {
1428 MP4::Atom *hdlr = t->find("mdia", "hdlr");
1429 if(!hdlr) continue;
1430 pf.seek(hdlr->offset());
1431 if(ByteVector d = pf.readBlock(hdlr->length()); d.containsAt("text", 16)) {
1432 chapterTrak = t;
1433 break;
1434 }
1435 }
1436 CPPUNIT_ASSERT(chapterTrak);
1437 MP4::Atom *stco = chapterTrak->find("mdia", "minf", "stbl", "stco");
1438 CPPUNIT_ASSERT(stco);
1439 // stco payload: full-box header(4) + entry_count(4) + offsets[]
1440 pf.seek(stco->offset() + 16);
1441 pf.writeBlock(ByteVector::fromUInt(
1442 static_cast<unsigned int>(audioMdat.offset + 8)));
1443 }
1444
1445 // Trigger the chapter-removal path with the crafted stco[0].
1446 {
1447 MP4::File f(filename.c_str());
1448 f.setQtChapters(MP4::ChapterList());
1449 CPPUNIT_ASSERT(f.save());
1450 }
1451
1452 // The audio mdat must survive with its contents byte-identical.
1453 const MdatInfo afterMdat = findFirstMdat();
1454 CPPUNIT_ASSERT(afterMdat.offset >= 0);
1455 CPPUNIT_ASSERT_EQUAL(audioMdat.length, afterMdat.length);
1456 {
1457 PlainFile pf(filename.c_str());
1458 pf.seek(afterMdat.offset);
1459 const ByteVector afterBytes = pf.readBlock(afterMdat.length);
1460 CPPUNIT_ASSERT(afterBytes == originalAudioMdat);
1461 }
1462 }
1463
1464 // Unicode titles (CJK, Latin with diacritics, Cyrillic) survive the
1465 // write -> save -> open -> read round-trip through the QT chapter track.
1466 // This exercises the text-sample serialisation in mp4qtchapterlist.cpp.
1467 void testQTChapterListUnicodeTitles()
1468 {
1469 ScopedFileCopy copy("no-tags", ".m4a");
1470 string filename = copy.fileName();
1471
1472 // UTF-8: 日本語, Über, Привет
1473 const String japanese("\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e", String::UTF8);
1474 const String german("\xc3\x9c" "ber", String::UTF8);
1475 const String russian("\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82", String::UTF8);
1476
1477 {
1478 MP4::File f(filename.c_str());
1479 f.setQtChapters(MP4::ChapterList{
1480 MP4::Chapter(japanese, 0),
1481 MP4::Chapter(german, 15000LL),
1482 MP4::Chapter(russian, 30000LL)
1483 });
1484 CPPUNIT_ASSERT(f.save());
1485 }
1486
1487 {
1488 MP4::File f(filename.c_str());
1489 MP4::ChapterList chapters = f.qtChapters();
1490 CPPUNIT_ASSERT_EQUAL(3U, chapters.size());
1491 CPPUNIT_ASSERT_EQUAL(japanese, chapters[0].title());
1492 CPPUNIT_ASSERT_EQUAL(german, chapters[1].title());
1493 CPPUNIT_ASSERT_EQUAL(russian, chapters[2].title());
1494 }
1495 }
1496
1497 // Unicode titles survive the write -> save -> open -> read round-trip
1498 // through the Nero chpl atom, which uses a different serialisation path
1499 // (length-prefixed UTF-8 inside udta/chpl).
1500 void testChapterListUnicodeTitles()
1501 {
1502 ScopedFileCopy copy("no-tags", ".m4a");
1503 string filename = copy.fileName();
1504
1505 // UTF-8: 日本語, Über, Привет
1506 const String japanese("\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e", String::UTF8);
1507 const String german("\xc3\x9c" "ber", String::UTF8);
1508 const String russian("\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82", String::UTF8);
1509
1510 {
1511 MP4::File f(filename.c_str());
1512 f.setNeroChapters(MP4::ChapterList{
1513 MP4::Chapter(japanese, 0),
1514 MP4::Chapter(german, 15000LL),
1515 MP4::Chapter(russian, 30000LL)
1516 });
1517 CPPUNIT_ASSERT(f.save());
1518 }
1519
1520 {
1521 MP4::File f(filename.c_str());
1522 MP4::ChapterList chapters = f.neroChapters();
1523 CPPUNIT_ASSERT_EQUAL(3U, chapters.size());
1524 CPPUNIT_ASSERT_EQUAL(japanese, chapters[0].title());
1525 CPPUNIT_ASSERT_EQUAL(german, chapters[1].title());
1526 CPPUNIT_ASSERT_EQUAL(russian, chapters[2].title());
1527 }
1528 }
1529
1530 // When a multi-chapter list begins with an empty-titled chapter at time 0,
1531 // that entry matches the QT dummy-marker pattern and must be stripped on
1532 // read-back. This test documents the stripping behaviour so a regression
1533 // is immediately detectable.
1534 void testQTChapterListEmptyTitleStripped()
1535 {
1536 ScopedFileCopy copy("no-tags", ".m4a");
1537 string filename = copy.fileName();
1538
1539 {
1540 MP4::File f(filename.c_str());
1541 // First entry has an empty title at t=0. write() sees the list already
1542 // starts at t=0 so no dummy is prepended; the empty entry is written
1543 // as-is. read() must strip it because size > 1 && startTime()==0 &&
1544 // title().isEmpty().
1545 f.setQtChapters(MP4::ChapterList{
1546 MP4::Chapter("", 0),
1547 MP4::Chapter("Chapter 1", 5000LL),
1548 MP4::Chapter("Chapter 2", 10000LL)
1549 });
1550 CPPUNIT_ASSERT(f.save());
1551 }
1552
1553 {
1554 MP4::File f(filename.c_str());
1555 MP4::ChapterList chapters = f.qtChapters();
1556 // The empty t=0 entry is stripped; only the two real chapters remain.
1557 CPPUNIT_ASSERT_EQUAL(2U, chapters.size());
1558 CPPUNIT_ASSERT_EQUAL(5000LL, chapters[0].startTime());
1559 CPPUNIT_ASSERT_EQUAL(String("Chapter 1"), chapters[0].title());
1560 CPPUNIT_ASSERT_EQUAL(10000LL, chapters[1].startTime());
1561 CPPUNIT_ASSERT_EQUAL(String("Chapter 2"), chapters[1].title());
1562 }
1563 }
1564
1565 // A single chapter with an empty title at time 0 must NOT be stripped.
1566 // The stripping rule applies only when size > 1 -- a file with exactly one
1567 // chapter is valid and its t=0 marker is not a dummy.
1568 void testQTChapterListSingleEmptyTitleNotStripped()
1569 {
1570 ScopedFileCopy copy("no-tags", ".m4a");
1571 string filename = copy.fileName();
1572
1573 {
1574 MP4::File f(filename.c_str());
1575 f.setQtChapters(MP4::ChapterList{
1576 MP4::Chapter("", 0)
1577 });
1578 CPPUNIT_ASSERT(f.save());
1579 }
1580
1581 {
1582 MP4::File f(filename.c_str());
1583 MP4::ChapterList chapters = f.qtChapters();
1584 CPPUNIT_ASSERT_EQUAL(1U, chapters.size());
1585 CPPUNIT_ASSERT_EQUAL(0LL, chapters[0].startTime());
1586 CPPUNIT_ASSERT_EQUAL(String(""), chapters[0].title());
1587 }
1588 }
1589
1590 // Both Nero (chpl) and QT chapter tracks can coexist in the same file.
1591 // Writing one format must not disturb the other, and removing one must
1592 // leave the other intact -- this validates the saveChaptersIfModified lazy
1593 // save contract in mp4file.cpp.
1594 void testNeroAndQTChaptersAreIndependent()
1595 {
1596 ScopedFileCopy copy("no-tags", ".m4a");
1597 string filename = copy.fileName();
1598
1599 // Write both formats in a single save.
1600 {
1601 MP4::File f(filename.c_str());
1602 f.setNeroChapters(MP4::ChapterList{
1603 MP4::Chapter("Nero 1", 0),
1604 MP4::Chapter("Nero 2", 10000LL)
1605 });
1606 f.setQtChapters(MP4::ChapterList{
1607 MP4::Chapter("QT 1", 0),
1608 MP4::Chapter("QT 2", 20000LL)
1609 });
1610 CPPUNIT_ASSERT(f.save());
1611 }
1612
1613 // Verify both are present and distinct.
1614 {
1615 MP4::File f(filename.c_str());
1616 const MP4::ChapterList nero = f.neroChapters();
1617 const MP4::ChapterList qt = f.qtChapters();
1618
1619 CPPUNIT_ASSERT_EQUAL(2U, nero.size());
1620 CPPUNIT_ASSERT_EQUAL(String("Nero 1"), nero[0].title());
1621 CPPUNIT_ASSERT_EQUAL(String("Nero 2"), nero[1].title());
1622
1623 CPPUNIT_ASSERT_EQUAL(2U, qt.size());
1624 CPPUNIT_ASSERT_EQUAL(String("QT 1"), qt[0].title());
1625 CPPUNIT_ASSERT_EQUAL(String("QT 2"), qt[1].title());
1626
1627 // Remove only the QT track.
1628 f.setQtChapters(MP4::ChapterList());
1629 CPPUNIT_ASSERT(f.save());
1630 }
1631
1632 // QT removed; Nero chapters must be fully intact.
1633 {
1634 MP4::File f(filename.c_str());
1635 CPPUNIT_ASSERT(f.qtChapters().isEmpty());
1636
1637 const MP4::ChapterList nero = f.neroChapters();
1638 CPPUNIT_ASSERT_EQUAL(2U, nero.size());
1639 CPPUNIT_ASSERT_EQUAL(String("Nero 1"), nero[0].title());
1640 CPPUNIT_ASSERT_EQUAL(String("Nero 2"), nero[1].title());
1641 }
1642 }
1643
1644 // Writing only Nero chapters must not accidentally create a QT chapter track,
1645 // and writing only QT chapters must not accidentally create a Nero chpl atom.
1646 void testNeroChaptersAloneWhenNoQT()
1647 {
1648 // Nero only -- QT track must remain absent.
1649 {
1650 ScopedFileCopy copy("no-tags", ".m4a");
1651 string filename = copy.fileName();
1652
1653 {
1654 MP4::File f(filename.c_str());
1655 f.setNeroChapters(MP4::ChapterList{
1656 MP4::Chapter("Nero Only", 0)
1657 });
1658 CPPUNIT_ASSERT(f.save());
1659 }
1660
1661 {
1662 MP4::File f(filename.c_str());
1663 CPPUNIT_ASSERT_EQUAL(1U, f.neroChapters().size());
1664 CPPUNIT_ASSERT(f.qtChapters().isEmpty());
1665 }
1666 }
1667
1668 // QT only -- Nero chpl atom must remain absent.
1669 {
1670 ScopedFileCopy copy("no-tags", ".m4a");
1671 string filename = copy.fileName();
1672
1673 {
1674 MP4::File f(filename.c_str());
1675 f.setQtChapters(MP4::ChapterList{
1676 MP4::Chapter("QT Only", 0)
1677 });
1678 CPPUNIT_ASSERT(f.save());
1679 }
1680
1681 {
1682 MP4::File f(filename.c_str());
1683 CPPUNIT_ASSERT_EQUAL(1U, f.qtChapters().size());
1684 CPPUNIT_ASSERT(f.neroChapters().isEmpty());
1685 }
1686 }
1687 }
1688
1689 void testLazyReadingAndWritingChapters()
1690 {
1691 // No reads or writes if chapters are not used
1692 {
1693 MockChapterFile f(TEST_FILE_PATH_C("no-tags.m4a"));
1694 f.save();
1695 CPPUNIT_ASSERT(!f.chapterList);
1696 }
1697 // Do not read if already read, do not write if not modified
1698 {
1699 MockChapterFile f(TEST_FILE_PATH_C("no-tags.m4a"));
1700 auto chapters = f.chapters();
1701 CPPUNIT_ASSERT(chapters == MockChapterList::mockChapters);
1702 CPPUNIT_ASSERT(f.chapterList);
1703 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->readCount);
1704 chapters = f.chapters();
1705 CPPUNIT_ASSERT(chapters == MockChapterList::mockChapters);
1706 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->readCount);
1707 f.save();
1708 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->readCount);
1709 CPPUNIT_ASSERT_EQUAL(0, f.chapterList->writeCount);
1710 }
1711 // Do not write if not modified
1712 {
1713 MockChapterFile f(TEST_FILE_PATH_C("no-tags.m4a"));
1714 auto chapters = f.chapters();
1715 CPPUNIT_ASSERT(chapters == MockChapterList::mockChapters);
1716 CPPUNIT_ASSERT(f.chapterList);
1717 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->readCount);
1718 f.setChapters(MockChapterList::mockChapters);
1719 f.save();
1720 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->readCount);
1721 CPPUNIT_ASSERT_EQUAL(0, f.chapterList->writeCount);
1722 }
1723 // Write if set without being read before
1724 {
1725 MockChapterFile f(TEST_FILE_PATH_C("no-tags.m4a"));
1726 f.setChapters(MP4::ChapterList());
1727 f.save();
1728 CPPUNIT_ASSERT(f.chapterList);
1729 CPPUNIT_ASSERT_EQUAL(0, f.chapterList->readCount);
1730 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->writeCount);
1731 }
1732 // Do write if modified
1733 {
1734 MockChapterFile f(TEST_FILE_PATH_C("no-tags.m4a"));
1735 CPPUNIT_ASSERT(f.chapters() == MockChapterList::mockChapters);
1736 CPPUNIT_ASSERT(f.chapterList);
1737 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->readCount);
1738 const auto chapters1 = MP4::ChapterList({
1739 MP4::Chapter("Chapter 1", 0),
1740 });
1741 f.setChapters(chapters1);
1742 CPPUNIT_ASSERT(f.chapters() == chapters1);
1743 f.save();
1744 CPPUNIT_ASSERT(f.chapters() == chapters1);
1745 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->readCount);
1746 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->writeCount);
1747 f.setChapters(chapters1);
1748 f.save();
1749 CPPUNIT_ASSERT_EQUAL(1, f.chapterList->writeCount);
1750 auto chapters2 = MP4::ChapterList({
1751 MP4::Chapter("Chapter 1", 0),
1752 MP4::Chapter("Chapter 2", 1),
1753 });
1754 f.setChapters(chapters2);
1755 CPPUNIT_ASSERT(f.chapters() == chapters2);
1756 f.save();
1757 CPPUNIT_ASSERT(f.chapters() == chapters2);
1758 CPPUNIT_ASSERT_EQUAL(2, f.chapterList->writeCount);
1759 chapters2 = MP4::ChapterList({
1760 MP4::Chapter("Chapter 1", 0),
1761 MP4::Chapter("Chapter 2", 2),
1762 });
1763 f.setChapters(chapters2);
1764 f.save();
1765 CPPUNIT_ASSERT_EQUAL(3, f.chapterList->writeCount);
1766 f.setChapters(chapters2);
1767 CPPUNIT_ASSERT(f.chapters() == chapters2);
1768 f.save();
1769 CPPUNIT_ASSERT(f.chapters() == chapters2);
1770 CPPUNIT_ASSERT_EQUAL(3, f.chapterList->writeCount);
1771 const auto chapters3 = MP4::ChapterList({
1772 MP4::Chapter("Chapter 1", 0),
1773 MP4::Chapter("Chapter 3", 2),
1774 });
1775 f.setChapters(chapters3);
1776 CPPUNIT_ASSERT(f.chapters() == chapters3);
1777 f.save();
1778 CPPUNIT_ASSERT(f.chapters() == chapters3);
1779 CPPUNIT_ASSERT_EQUAL(4, f.chapterList->writeCount);
1780 f.setChapters(MP4::ChapterList());
1781 CPPUNIT_ASSERT(f.chapters().isEmpty());
1782 f.save();
1783 CPPUNIT_ASSERT(f.chapters().isEmpty());
1784 CPPUNIT_ASSERT_EQUAL(5, f.chapterList->writeCount);
1785 }
1786 }
1787
1788 };
1789
1790 CPPUNIT_TEST_SUITE_REGISTRATION(TestMP4);