|
| 1 | +#include <QSignalSpy> |
| 2 | +#include <QDebug> |
| 3 | +#include <QCoreApplication> |
| 4 | +#include <QFile> |
| 5 | + |
| 6 | + |
| 7 | +#include "CodecTests.h" |
| 8 | +#include "AudioClient.h" |
| 9 | +#include "DependencyManager.h" |
| 10 | +#include "NodeList.h" |
| 11 | +#include "plugins/CodecPlugin.h" |
| 12 | +#include "plugins/PluginManager.h" |
| 13 | + |
| 14 | +QTEST_MAIN(CodecTests) |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +void CodecTests::initTestCase() { |
| 20 | + DependencyManager::set<PluginManager>(); |
| 21 | + |
| 22 | + QDir testPath (QCoreApplication::applicationDirPath()); |
| 23 | + QDir interfacePluginPath = testPath; |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + qDebug() << "Our directory is" << testPath; |
| 28 | + |
| 29 | + interfacePluginPath.cdUp(); |
| 30 | + interfacePluginPath.cdUp(); |
| 31 | + interfacePluginPath.cd("interface"); |
| 32 | + interfacePluginPath.cd("plugins"); |
| 33 | + interfacePluginPath.makeAbsolute(); |
| 34 | + |
| 35 | + QString ourPluginPath = testPath.filePath("plugins"); |
| 36 | + |
| 37 | + |
| 38 | + qDebug() << "Interface plugins are at" << interfacePluginPath; |
| 39 | + qDebug() << "Our plugins are at" << ourPluginPath; |
| 40 | + |
| 41 | + |
| 42 | + QFile::link(interfacePluginPath.path(), ourPluginPath); |
| 43 | + |
| 44 | +} |
| 45 | + |
| 46 | +void CodecTests::loadCodecs() { |
| 47 | + const auto& codecPlugins = PluginManager::getInstance()->getCodecPlugins(); |
| 48 | + |
| 49 | + QVERIFY(codecPlugins.size() > 0); |
| 50 | + |
| 51 | + |
| 52 | + for (const auto& plugin : codecPlugins) { |
| 53 | + auto codecName = plugin->getName(); |
| 54 | + qDebug() << "Codec:" << codecName << ", supported=" << plugin->isSupported(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +void CodecTests::testEncoders() { |
| 61 | + const auto& codecPlugins = PluginManager::getInstance()->getCodecPlugins(); |
| 62 | + |
| 63 | + QVERIFY(codecPlugins.size() > 0); |
| 64 | + |
| 65 | + |
| 66 | + for (const auto& plugin : codecPlugins) { |
| 67 | + if (!plugin->isSupported()) { |
| 68 | + qWarning() << "Skipping unsupported plugin" << plugin->getName(); |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + Encoder* encoder = plugin->createEncoder(AudioConstants::SAMPLE_RATE, AudioConstants::STEREO); |
| 73 | + QVERIFY(encoder != nullptr); |
| 74 | + |
| 75 | + QByteArray data(AudioConstants::NETWORK_FRAME_BYTES_STEREO, 0); |
| 76 | + QByteArray encoded; |
| 77 | + |
| 78 | + encoder->encode(data, encoded); |
| 79 | + |
| 80 | + QVERIFY(encoded.size() > 0); |
| 81 | + |
| 82 | + qDebug() << "Codec" << plugin->getName() << "encoded empty buffer of" << data.size() << "bytes into" << encoded.size(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void CodecTests::testDecoders () { |
| 87 | + const auto& codecPlugins = PluginManager::getInstance()->getCodecPlugins(); |
| 88 | + |
| 89 | + QVERIFY(codecPlugins.size() > 0); |
| 90 | + |
| 91 | + |
| 92 | + for (const auto& plugin : codecPlugins) { |
| 93 | + if (!plugin->isSupported()) { |
| 94 | + qWarning() << "Skipping unsupported plugin" << plugin->getName(); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + Encoder* encoder = plugin->createEncoder(AudioConstants::SAMPLE_RATE, AudioConstants::STEREO); |
| 99 | + Decoder* decoder = plugin->createDecoder(AudioConstants::SAMPLE_RATE, AudioConstants::STEREO); |
| 100 | + QVERIFY(encoder != nullptr); |
| 101 | + |
| 102 | + QByteArray data(AudioConstants::NETWORK_FRAME_BYTES_STEREO, 0); |
| 103 | + QByteArray encoded; |
| 104 | + QByteArray decoded; |
| 105 | + QByteArray lost(AudioConstants::NETWORK_FRAME_BYTES_STEREO, 0); |
| 106 | + |
| 107 | + |
| 108 | + encoder->encode(data, encoded); |
| 109 | + decoder->decode(encoded, decoded); |
| 110 | + |
| 111 | + |
| 112 | + QVERIFY(encoded.size() > 0); |
| 113 | + QVERIFY(decoded.size() > 0); |
| 114 | + QVERIFY(decoded.size() == data.size()); |
| 115 | + QVERIFY(lost.size() > 0); |
| 116 | + |
| 117 | + |
| 118 | + qDebug() << "Codec" << plugin->getName() << "encoded empty buffer of" << data.size() << "bytes into" << encoded.size() << "and decoded back into" << decoded.size(); |
| 119 | + |
| 120 | + // This is here mostly for valgrind testing -- we can't really validate anything, but we can see if it crashes. |
| 121 | + decoder->lostFrame(lost); |
| 122 | + QVERIFY(lost.size() > 0); |
| 123 | + qDebug() << "Codec" << plugin->getName() << "decoded a lost frame"; |
| 124 | + } |
| 125 | +} |
0 commit comments