Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Philippe Gorley <philippe.gorley@savoirfairelinux.com> 5 : * 6 : * This program is free software; you can redistribute it and/or modify 7 : * it under the terms of the GNU General Public License as published by 8 : * the Free Software Foundation; either version 3 of the License, or 9 : * (at your option) any later version. 10 : * 11 : * This program is distributed in the hope that it will be useful, 12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : * GNU General Public License for more details. 15 : * 16 : * You should have received a copy of the GNU General Public License 17 : * along with this program; if not, write to the Free Software 18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 : */ 20 : 21 : #include <cppunit/TestAssert.h> 22 : #include <cppunit/TestFixture.h> 23 : #include <cppunit/extensions/HelperMacros.h> 24 : 25 : #include "media/audio/audio_frame_resizer.h" 26 : #include "jami.h" 27 : #include "media/libav_deps.h" 28 : #include "media/media_buffer.h" 29 : 30 : #include "../../../test_runner.h" 31 : 32 : #include <stdexcept> 33 : 34 : namespace jami { namespace test { 35 : 36 : class AudioFrameResizerTest : public CppUnit::TestFixture { 37 : public: 38 2 : static std::string name() { return "audio_frame_resizer"; } 39 : 40 : private: 41 : void testSameSize(); 42 : void testBiggerInput(); 43 : void testBiggerOutput(); 44 : void testDifferentFormat(); 45 : 46 : void gotFrame(std::shared_ptr<AudioFrame>&& framePtr); 47 : std::shared_ptr<AudioFrame> getFrame(int n); 48 : 49 2 : CPPUNIT_TEST_SUITE(AudioFrameResizerTest); 50 1 : CPPUNIT_TEST(testSameSize); 51 1 : CPPUNIT_TEST(testBiggerInput); 52 1 : CPPUNIT_TEST(testBiggerOutput); 53 1 : CPPUNIT_TEST(testDifferentFormat); 54 4 : CPPUNIT_TEST_SUITE_END(); 55 : 56 : std::shared_ptr<AudioFrameResizer> q_; 57 : AudioFormat format_ = AudioFormat::STEREO(); 58 : int outputSize_ = 960; 59 : }; 60 : 61 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AudioFrameResizerTest, AudioFrameResizerTest::name()); 62 : 63 : void 64 3 : AudioFrameResizerTest::gotFrame(std::shared_ptr<AudioFrame>&& framePtr) 65 : { 66 3 : CPPUNIT_ASSERT(framePtr && framePtr->pointer()); 67 3 : CPPUNIT_ASSERT(framePtr->pointer()->nb_samples == outputSize_); 68 3 : } 69 : 70 : std::shared_ptr<AudioFrame> 71 4 : AudioFrameResizerTest::getFrame(int n) 72 : { 73 4 : auto frame = std::make_shared<AudioFrame>(); 74 4 : frame->pointer()->format = format_.sampleFormat; 75 4 : frame->pointer()->sample_rate = format_.sample_rate; 76 4 : av_channel_layout_default(&frame->pointer()->ch_layout, format_.nb_channels); 77 4 : frame->pointer()->nb_samples = n; 78 4 : CPPUNIT_ASSERT(av_frame_get_buffer(frame->pointer(), 0) >= 0); 79 4 : return frame; 80 0 : } 81 : 82 : void 83 1 : AudioFrameResizerTest::testSameSize() 84 : { 85 : // input.nb_samples == output.nb_samples 86 2 : q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); })); 87 1 : auto in = getFrame(outputSize_); 88 : // gotFrame should be called after this 89 2 : CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in))); 90 1 : CPPUNIT_ASSERT(q_->samples() == 0); 91 1 : } 92 : 93 : void 94 1 : AudioFrameResizerTest::testBiggerInput() 95 : { 96 : // input.nb_samples > output.nb_samples 97 2 : q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); })); 98 1 : auto in = getFrame(outputSize_ + 100); 99 : // gotFrame should be called after this 100 2 : CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in))); 101 1 : CPPUNIT_ASSERT(q_->samples() == 100); 102 1 : } 103 : 104 : void 105 1 : AudioFrameResizerTest::testBiggerOutput() 106 : { 107 : // input.nb_samples < output.nb_samples 108 2 : q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); })); 109 1 : auto in = getFrame(outputSize_ - 100); 110 2 : CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in))); 111 1 : CPPUNIT_ASSERT(q_->samples() == outputSize_ - 100); 112 : // gotFrame should be called after this 113 2 : CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in))); 114 : // pushed 2 frames of (outputSize_ - 100) samples and got 1 frame 115 1 : CPPUNIT_ASSERT(q_->samples() == outputSize_ - 200); 116 1 : } 117 : 118 : void 119 1 : AudioFrameResizerTest::testDifferentFormat() 120 : { 121 : // frame format != q_->format_ 122 1 : q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); })); 123 1 : auto in = getFrame(outputSize_-27); 124 1 : q_->enqueue(std::move(in)); 125 1 : CPPUNIT_ASSERT(q_->samples()==outputSize_-27); 126 1 : q_->setFormat(AudioFormat::MONO(), 960); 127 1 : CPPUNIT_ASSERT(q_->samples() == 0); 128 1 : } 129 : 130 : }} // namespace jami::test 131 : 132 1 : RING_TEST_RUNNER(jami::test::AudioFrameResizerTest::name());