| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2023 by Urs Fleisch |
| 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 <functional> |
| 27 | #include <memory> |
| 28 | |
| 29 | #include "taglib_config.h" |
| 30 | #include "mpegproperties.h" |
| 31 | #include "tbytevector.h" |
| 32 | #include "tpropertymap.h" |
| 33 | #include "mpegfile.h" |
| 34 | #include "id3v2tag.h" |
| 35 | #include "id3v2frame.h" |
| 36 | #include "id3v2framefactory.h" |
| 37 | #ifdef TAGLIB_WITH_VORBIS |
| 38 | #include "flacproperties.h" |
| 39 | #include "flacfile.h" |
| 40 | #endif |
| 41 | #ifdef TAGLIB_WITH_TRUEAUDIO |
| 42 | #include "trueaudiofile.h" |
| 43 | #include "trueaudioproperties.h" |
| 44 | #endif |
| 45 | #ifdef TAGLIB_WITH_RIFF |
| 46 | #include "wavfile.h" |
| 47 | #include "aifffile.h" |
| 48 | #endif |
| 49 | #ifdef TAGLIB_WITH_DSF |
| 50 | #include "dsffile.h" |
| 51 | #include "dsdifffile.h" |
| 52 | #endif |
| 53 | #include <cppunit/extensions/HelperMacros.h> |
| 54 | #include "utils.h" |
| 55 | |
| 56 | using namespace std; |
| 57 | using namespace TagLib; |
| 58 | |
| 59 | namespace |
| 60 | { |
| 61 | |
| 62 | class CustomFrameFactory; |
| 63 | |
| 64 | // Just a silly example of a custom frame holding a number. |
| 65 | class CustomFrame : public ID3v2::Frame |
| 66 | { |
| 67 | friend class CustomFrameFactory; |
| 68 | public: |
| 69 | explicit CustomFrame(unsigned int value = 0) |
| 70 | : Frame("CUST"), m_value(value) {} |
| 71 | CustomFrame(const CustomFrame &) = delete; |
| 72 | CustomFrame &operator=(const CustomFrame &) = delete; |
| 73 | ~CustomFrame() override = default; |
| 74 | String toString() const override { return String::number(m_value); } |
| 75 | PropertyMap asProperties() const override { |
| 76 | return SimplePropertyMap{{"CUSTOM", StringList(String::number(m_value))}}; |
| 77 | } |
| 78 | unsigned int value() const { return m_value; } |
| 79 | |
| 80 | protected: |
| 81 | void parseFields(const ByteVector &data) override { |
| 82 | m_value = data.toUInt(); |
| 83 | } |
| 84 | ByteVector renderFields() const override { |
| 85 | return ByteVector::fromUInt(m_value); |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | CustomFrame(const ByteVector &data, Header *h) |
| 90 | : Frame(h), m_value(fieldData(data).toUInt()) {} |
| 91 | unsigned int m_value; |
| 92 | }; |
| 93 | |
| 94 | // Example for frame factory with support for CustomFrame. |
| 95 | class CustomFrameFactory : public ID3v2::FrameFactory { |
| 96 | public: |
| 97 | CustomFrameFactory(const CustomFrameFactory &) = delete; |
| 98 | CustomFrameFactory &operator=(const CustomFrameFactory &) = delete; |
| 99 | static CustomFrameFactory *instance() { return &factory; } |
| 100 | ID3v2::Frame *createFrameForProperty( |
| 101 | const String &key, const StringList &values) const override { |
| 102 | if(key == "CUSTOM") { |
| 103 | return new CustomFrame(!values.isEmpty() ? values.front().toInt() : 0); |
| 104 | } |
| 105 | return ID3v2::FrameFactory::createFrameForProperty(key, values); |
| 106 | } |
| 107 | |
| 108 | protected: |
| 109 | CustomFrameFactory() = default; |
| 110 | ~CustomFrameFactory() = default; |
| 111 | ID3v2::Frame *createFrame(const ByteVector &data, ID3v2::Frame::Header *header, |
| 112 | const ID3v2::Header *tagHeader) const override { |
| 113 | if(header->frameID() == "CUST") { |
| 114 | return new CustomFrame(data, header); |
| 115 | } |
| 116 | return ID3v2::FrameFactory::createFrame(data, header, tagHeader); |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | static CustomFrameFactory factory; |
| 121 | }; |
| 122 | |
| 123 | CustomFrameFactory CustomFrameFactory::factory; |
| 124 | } // namespace |
| 125 | |
| 126 | class TestId3v2FrameFactory : public CppUnit::TestFixture |
| 127 | { |
| 128 | CPPUNIT_TEST_SUITE(TestId3v2FrameFactory); |
| 129 | CPPUNIT_TEST(testMPEG); |
| 130 | #ifdef TAGLIB_WITH_VORBIS |
| 131 | CPPUNIT_TEST(testFLAC); |
| 132 | #endif |
| 133 | #ifdef TAGLIB_WITH_TRUEAUDIO |
| 134 | CPPUNIT_TEST(testTrueAudio); |
| 135 | #endif |
| 136 | #ifdef TAGLIB_WITH_RIFF |
| 137 | CPPUNIT_TEST(testWAV); |
| 138 | CPPUNIT_TEST(testAIFF); |
| 139 | #endif |
| 140 | #ifdef TAGLIB_WITH_DSF |
| 141 | CPPUNIT_TEST(testDSF); |
| 142 | CPPUNIT_TEST(testDSDIFF); |
| 143 | #endif |
| 144 | CPPUNIT_TEST_SUITE_END(); |
| 145 | |
| 146 | public: |
| 147 | |
| 148 | void testGenericFrameFactory( |
| 149 | const char *fileName, |
| 150 | function<File *(const char *)> createFileWithDefaultFactory, |
| 151 | function<File *(const char *, ID3v2::FrameFactory *factory)> createFileWithFactory, |
| 152 | function<bool(const File &)> hasID3v2Tag, |
| 153 | function<ID3v2::Tag *(File &)> getID3v2Tag, |
| 154 | function<bool(File &)> stripAllTags) |
| 155 | { |
| 156 | { |
| 157 | auto f = std::unique_ptr<File>(createFileWithDefaultFactory(fileName)); |
| 158 | CPPUNIT_ASSERT(f->isValid()); |
| 159 | ID3v2::Tag *tag = getID3v2Tag(*f); |
| 160 | ID3v2::FrameList frames = tag->frameList(); |
| 161 | for(auto it = frames.begin(); it != frames.end(); it = frames.erase(it)) { |
| 162 | tag->removeFrame(*it); |
| 163 | } |
| 164 | tag->setArtist("An artist"); |
| 165 | tag->setTitle("A title"); |
| 166 | f->save(); |
| 167 | } |
| 168 | { |
| 169 | auto f = std::unique_ptr<File>(createFileWithDefaultFactory(fileName)); |
| 170 | CPPUNIT_ASSERT(f->isValid()); |
| 171 | CPPUNIT_ASSERT(hasID3v2Tag(*f)); |
| 172 | ID3v2::Tag *tag = getID3v2Tag(*f); |
| 173 | tag->addFrame(new CustomFrame(1234567890)); |
| 174 | f->save(); |
| 175 | } |
| 176 | { |
| 177 | auto f = std::unique_ptr<File>(createFileWithDefaultFactory(fileName)); |
| 178 | CPPUNIT_ASSERT(f->isValid()); |
| 179 | CPPUNIT_ASSERT(hasID3v2Tag(*f)); |
| 180 | ID3v2::Tag *tag = getID3v2Tag(*f); |
| 181 | const auto &frames = tag->frameList("CUST"); |
| 182 | CPPUNIT_ASSERT(!frames.isEmpty()); |
| 183 | // Without a specialized FrameFactory, you can add custom frames, |
| 184 | // but your cannot parse them. |
| 185 | CPPUNIT_ASSERT(!dynamic_cast<CustomFrame *>(frames.front())); |
| 186 | } |
| 187 | { |
| 188 | auto f = std::unique_ptr<File>(createFileWithFactory(fileName, CustomFrameFactory::instance())); |
| 189 | CPPUNIT_ASSERT(f->isValid()); |
| 190 | CPPUNIT_ASSERT(hasID3v2Tag(*f)); |
| 191 | ID3v2::Tag *tag = getID3v2Tag(*f); |
| 192 | const auto &frames = tag->frameList("CUST"); |
| 193 | CPPUNIT_ASSERT(!frames.isEmpty()); |
| 194 | auto frame = dynamic_cast<CustomFrame *>(frames.front()); |
| 195 | CPPUNIT_ASSERT(frame); |
| 196 | CPPUNIT_ASSERT_EQUAL(1234567890U, frame->value()); |
| 197 | PropertyMap properties = tag->properties(); |
| 198 | CPPUNIT_ASSERT_EQUAL(StringList("1234567890"), |
| 199 | properties.value("CUSTOM")); |
| 200 | CPPUNIT_ASSERT_EQUAL(StringList("An artist"), |
| 201 | properties.value("ARTIST")); |
| 202 | CPPUNIT_ASSERT_EQUAL(StringList("A title"), |
| 203 | properties.value("TITLE")); |
| 204 | stripAllTags(*f); |
| 205 | } |
| 206 | { |
| 207 | auto f = std::unique_ptr<File>(createFileWithFactory(fileName, CustomFrameFactory::instance())); |
| 208 | CPPUNIT_ASSERT(f->isValid()); |
| 209 | CPPUNIT_ASSERT(!hasID3v2Tag(*f)); |
| 210 | ID3v2::Tag *tag = getID3v2Tag(*f); |
| 211 | PropertyMap properties = tag->properties(); |
| 212 | CPPUNIT_ASSERT(properties.isEmpty()); |
| 213 | properties.insert("CUSTOM", StringList("305419896")); |
| 214 | tag->setProperties(properties); |
| 215 | f->save(); |
| 216 | } |
| 217 | { |
| 218 | auto f = std::unique_ptr<File>(createFileWithFactory(fileName, CustomFrameFactory::instance())); |
| 219 | CPPUNIT_ASSERT(f->isValid()); |
| 220 | CPPUNIT_ASSERT(hasID3v2Tag(*f)); |
| 221 | ID3v2::Tag *tag = getID3v2Tag(*f); |
| 222 | PropertyMap properties = tag->properties(); |
| 223 | CPPUNIT_ASSERT_EQUAL(StringList("305419896"), properties.value("CUSTOM")); |
| 224 | const auto &frames = tag->frameList("CUST"); |
| 225 | CPPUNIT_ASSERT(!frames.isEmpty()); |
| 226 | auto frame = dynamic_cast<CustomFrame *>(frames.front()); |
| 227 | CPPUNIT_ASSERT(frame); |
| 228 | CPPUNIT_ASSERT_EQUAL(0x12345678U, frame->value()); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void testMPEG() |
| 233 | { |
| 234 | ScopedFileCopy copy("lame_cbr", ".mp3"); |
| 235 | testGenericFrameFactory( |
| 236 | copy.fileName().c_str(), |
| 237 | [](const char *fileName) { |
| 238 | return new MPEG::File(fileName); |
| 239 | }, |
| 240 | [](const char *fileName, ID3v2::FrameFactory *factory) { |
| 241 | return new MPEG::File(fileName, true, MPEG::Properties::Average, |
| 242 | factory); |
| 243 | }, |
| 244 | [](const File &f) { |
| 245 | return dynamic_cast<const MPEG::File &>(f).hasID3v2Tag(); |
| 246 | }, |
| 247 | [](File &f) { |
| 248 | return dynamic_cast<MPEG::File &>(f).ID3v2Tag(true); |
| 249 | }, |
| 250 | [](File &f) { |
| 251 | return dynamic_cast<MPEG::File &>(f).strip(); |
| 252 | } |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | #ifdef TAGLIB_WITH_VORBIS |
| 257 | void testFLAC() |
| 258 | { |
| 259 | ScopedFileCopy copy("no-tags", ".flac"); |
| 260 | testGenericFrameFactory( |
| 261 | copy.fileName().c_str(), |
| 262 | [](const char *fileName) { |
| 263 | return new FLAC::File(fileName); |
| 264 | }, |
| 265 | [](const char *fileName, ID3v2::FrameFactory *factory) { |
| 266 | return new FLAC::File(fileName, true, FLAC::Properties::Average, |
| 267 | factory); |
| 268 | }, |
| 269 | [](const File &f) { |
| 270 | return dynamic_cast<const FLAC::File &>(f).hasID3v2Tag(); |
| 271 | }, |
| 272 | [](File &f) { |
| 273 | return dynamic_cast<FLAC::File &>(f).ID3v2Tag(true); |
| 274 | }, |
| 275 | [](File &f) { |
| 276 | dynamic_cast<FLAC::File &>(f).strip(); |
| 277 | return f.save(); |
| 278 | } |
| 279 | ); |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | #ifdef TAGLIB_WITH_TRUEAUDIO |
| 284 | void testTrueAudio() |
| 285 | { |
| 286 | ScopedFileCopy copy("empty", ".tta"); |
| 287 | testGenericFrameFactory( |
| 288 | copy.fileName().c_str(), |
| 289 | [](const char *fileName) { |
| 290 | return new TrueAudio::File(fileName); |
| 291 | }, |
| 292 | [](const char *fileName, ID3v2::FrameFactory *factory) { |
| 293 | return new TrueAudio::File(fileName, true, |
| 294 | TrueAudio::Properties::Average, factory); |
| 295 | }, |
| 296 | [](const File &f) { |
| 297 | return dynamic_cast<const TrueAudio::File &>(f).hasID3v2Tag(); |
| 298 | }, |
| 299 | [](File &f) { |
| 300 | return dynamic_cast<TrueAudio::File &>(f).ID3v2Tag(true); |
| 301 | }, |
| 302 | [](File &f) { |
| 303 | dynamic_cast<TrueAudio::File &>(f).strip(); |
| 304 | return f.save(); |
| 305 | } |
| 306 | ); |
| 307 | } |
| 308 | #endif |
| 309 | |
| 310 | #ifdef TAGLIB_WITH_RIFF |
| 311 | void testWAV() |
| 312 | { |
| 313 | ScopedFileCopy copy("empty", ".wav"); |
| 314 | testGenericFrameFactory( |
| 315 | copy.fileName().c_str(), |
| 316 | [](const char *fileName) { |
| 317 | return new RIFF::WAV::File(fileName); |
| 318 | }, |
| 319 | [](const char *fileName, ID3v2::FrameFactory *factory) { |
| 320 | return new RIFF::WAV::File( |
| 321 | fileName, true, RIFF::WAV::Properties::Average, factory); |
| 322 | }, |
| 323 | [](const File &f) { |
| 324 | return dynamic_cast<const RIFF::WAV::File &>(f).hasID3v2Tag(); |
| 325 | }, |
| 326 | [](File &f) { |
| 327 | return dynamic_cast<RIFF::WAV::File &>(f).ID3v2Tag(); |
| 328 | }, |
| 329 | [](File &f) { |
| 330 | dynamic_cast<RIFF::WAV::File &>(f).strip(); |
| 331 | return true; |
| 332 | } |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | void testAIFF() |
| 337 | { |
| 338 | ScopedFileCopy copy("empty", ".aiff"); |
| 339 | testGenericFrameFactory( |
| 340 | copy.fileName().c_str(), |
| 341 | [](const char *fileName) { |
| 342 | return new RIFF::AIFF::File(fileName); |
| 343 | }, |
| 344 | [](const char *fileName, ID3v2::FrameFactory *factory) { |
| 345 | return new RIFF::AIFF::File( |
| 346 | fileName, true, RIFF::AIFF::Properties::Average, factory); |
| 347 | }, |
| 348 | [](const File &f) { |
| 349 | return dynamic_cast<const RIFF::AIFF::File &>(f).hasID3v2Tag(); |
| 350 | }, |
| 351 | [](File &f) { |
| 352 | return dynamic_cast<RIFF::AIFF::File &>(f).tag(); |
| 353 | }, |
| 354 | [](File &f) { |
| 355 | f.setProperties({}); |
| 356 | return f.save(); |
| 357 | } |
| 358 | ); |
| 359 | } |
| 360 | #endif |
| 361 | |
| 362 | #ifdef TAGLIB_WITH_DSF |
| 363 | void testDSF() |
| 364 | { |
| 365 | ScopedFileCopy copy("empty10ms", ".dsf"); |
| 366 | testGenericFrameFactory( |
| 367 | copy.fileName().c_str(), |
| 368 | [](const char *fileName) { |
| 369 | return new DSF::File(fileName); |
| 370 | }, |
| 371 | [](const char *fileName, ID3v2::FrameFactory *factory) { |
| 372 | return new DSF::File( |
| 373 | fileName, true, DSF::Properties::Average, factory); |
| 374 | }, |
| 375 | [](const File &f) { |
| 376 | return !f.tag()->isEmpty(); |
| 377 | }, |
| 378 | [](File &f) { |
| 379 | return dynamic_cast<DSF::File &>(f).tag(); |
| 380 | }, |
| 381 | [](File &f) { |
| 382 | f.setProperties({}); |
| 383 | return f.save(); |
| 384 | } |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | void testDSDIFF() |
| 389 | { |
| 390 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 391 | testGenericFrameFactory( |
| 392 | copy.fileName().c_str(), |
| 393 | [](const char *fileName) { |
| 394 | return new DSDIFF::File(fileName); |
| 395 | }, |
| 396 | [](const char *fileName, ID3v2::FrameFactory *factory) { |
| 397 | return new DSDIFF::File( |
| 398 | fileName, true, DSDIFF::Properties::Average, factory); |
| 399 | }, |
| 400 | [](const File &f) { |
| 401 | return dynamic_cast<const DSDIFF::File &>(f).hasID3v2Tag(); |
| 402 | }, |
| 403 | [](File &f) { |
| 404 | return dynamic_cast<DSDIFF::File &>(f).ID3v2Tag(true); |
| 405 | }, |
| 406 | [](File &f) { |
| 407 | dynamic_cast<DSDIFF::File &>(f).strip(); |
| 408 | return true; |
| 409 | } |
| 410 | ); |
| 411 | } |
| 412 | #endif |
| 413 | |
| 414 | }; |
| 415 | |
| 416 | CPPUNIT_TEST_SUITE_REGISTRATION(TestId3v2FrameFactory); |