Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include <cppunit/TestAssert.h> 19 : #include <cppunit/TestFixture.h> 20 : #include <cppunit/extensions/HelperMacros.h> 21 : 22 : extern "C" { 23 : #include <libavutil/frame.h> 24 : #include <libavutil/pixfmt.h> 25 : } 26 : 27 : #include "jami.h" 28 : #include "videomanager_interface.h" 29 : #include "media/audio/audio_format.h" 30 : 31 : #include "../../test_runner.h" 32 : 33 : namespace jami { namespace test { 34 : 35 : class MediaFrameTest : public CppUnit::TestFixture { 36 : public: 37 2 : static std::string name() { return "media_frame"; } 38 : 39 : void setUp(); 40 : void tearDown(); 41 : 42 : private: 43 : void testCopy(); 44 : void testMix(); 45 : 46 2 : CPPUNIT_TEST_SUITE(MediaFrameTest); 47 1 : CPPUNIT_TEST(testCopy); 48 1 : CPPUNIT_TEST(testMix); 49 4 : CPPUNIT_TEST_SUITE_END(); 50 : }; 51 : 52 : 53 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MediaFrameTest, MediaFrameTest::name()); 54 : 55 : void 56 2 : MediaFrameTest::setUp() 57 : { 58 2 : libjami::init(libjami::InitFlag(libjami::LIBJAMI_FLAG_DEBUG | libjami::LIBJAMI_FLAG_CONSOLE_LOG)); 59 2 : } 60 : 61 : void 62 2 : MediaFrameTest::tearDown() 63 : { 64 2 : libjami::fini(); 65 2 : } 66 : 67 : void 68 1 : MediaFrameTest::testCopy() 69 : { 70 : // test allocation 71 1 : libjami::VideoFrame v1; 72 1 : v1.reserve(AV_PIX_FMT_YUV420P, 100, 100); 73 1 : v1.pointer()->data[0][0] = 42; 74 1 : CPPUNIT_ASSERT(v1.pointer()); 75 : 76 : // test frame referencing (different pointers, but same data) 77 1 : libjami::VideoFrame v2; 78 1 : v2.copyFrom(v1); 79 1 : CPPUNIT_ASSERT(v1.format() == v2.format()); 80 1 : CPPUNIT_ASSERT(v1.width() == v2.width()); 81 1 : CPPUNIT_ASSERT(v1.height() == v2.height()); 82 1 : CPPUNIT_ASSERT(v1.pointer() != v2.pointer()); 83 1 : CPPUNIT_ASSERT(v1.pointer()->data[0][0] == 42); 84 1 : CPPUNIT_ASSERT(v2.pointer()->data[0][0] == 42); 85 1 : } 86 : 87 : void 88 1 : MediaFrameTest::testMix() 89 : { 90 1 : const AudioFormat& format = AudioFormat::STEREO(); 91 1 : const int nbSamples = format.sample_rate / 50; 92 1 : auto a1 = std::make_unique<libjami::AudioFrame>(format, nbSamples); 93 1 : auto d1 = reinterpret_cast<int16_t*>(a1->pointer()->extended_data[0]); 94 1 : d1[0] = 0; 95 1 : d1[1] = 1; 96 1 : d1[2] = 3; 97 1 : d1[3] = -2; 98 1 : d1[4] = 5; 99 1 : d1[5] = std::numeric_limits<int16_t>::min(); 100 1 : d1[6] = std::numeric_limits<int16_t>::max(); 101 1 : auto a2 = std::make_unique<libjami::AudioFrame>(format, nbSamples); 102 1 : auto d2 = reinterpret_cast<int16_t*>(a2->pointer()->extended_data[0]); 103 1 : d2[0] = 0; 104 1 : d2[1] = 3; 105 1 : d2[2] = -1; 106 1 : d2[3] = 3; 107 1 : d2[4] = -6; 108 1 : d2[5] = -101; 109 1 : d2[6] = 101; 110 1 : a2->mix(*a1); 111 1 : CPPUNIT_ASSERT(d2[0] == 0); 112 1 : CPPUNIT_ASSERT(d2[1] == 4); 113 1 : CPPUNIT_ASSERT(d2[2] == 2); 114 1 : CPPUNIT_ASSERT(d2[3] == 1); 115 1 : CPPUNIT_ASSERT(d2[4] == -1); 116 1 : CPPUNIT_ASSERT(d2[5] == std::numeric_limits<int16_t>::min()); 117 1 : CPPUNIT_ASSERT(d2[6] == std::numeric_limits<int16_t>::max()); 118 1 : } 119 : 120 : }} // namespace jami::test 121 : 122 1 : RING_TEST_RUNNER(jami::test::MediaFrameTest::name());