LCOV - code coverage report
Current view: top level - test/unitTest/media/audio - test_audio_frame_resizer.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 48 49 98.0 %
Date: 2024-12-21 08:56:24 Functions: 14 15 93.3 %

          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             : #include "media/audio/audio_frame_resizer.h"
      23             : #include "jami.h"
      24             : #include "media/libav_deps.h"
      25             : #include "media/media_buffer.h"
      26             : 
      27             : #include "../../../test_runner.h"
      28             : 
      29             : #include <stdexcept>
      30             : 
      31             : namespace jami { namespace test {
      32             : 
      33             : class AudioFrameResizerTest : public CppUnit::TestFixture {
      34             : public:
      35           2 :     static std::string name() { return "audio_frame_resizer"; }
      36             : 
      37             : private:
      38             :     void testSameSize();
      39             :     void testBiggerInput();
      40             :     void testBiggerOutput();
      41             :     void testDifferentFormat();
      42             : 
      43             :     void gotFrame(std::shared_ptr<AudioFrame>&& framePtr);
      44             :     std::shared_ptr<AudioFrame> getFrame(int n);
      45             : 
      46           2 :     CPPUNIT_TEST_SUITE(AudioFrameResizerTest);
      47           1 :     CPPUNIT_TEST(testSameSize);
      48           1 :     CPPUNIT_TEST(testBiggerInput);
      49           1 :     CPPUNIT_TEST(testBiggerOutput);
      50           1 :     CPPUNIT_TEST(testDifferentFormat);
      51           4 :     CPPUNIT_TEST_SUITE_END();
      52             : 
      53             :     std::shared_ptr<AudioFrameResizer> q_;
      54             :     AudioFormat format_ = AudioFormat::STEREO();
      55             :     int outputSize_ = 960;
      56             : };
      57             : 
      58             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AudioFrameResizerTest, AudioFrameResizerTest::name());
      59             : 
      60             : void
      61           3 : AudioFrameResizerTest::gotFrame(std::shared_ptr<AudioFrame>&& framePtr)
      62             : {
      63           3 :     CPPUNIT_ASSERT(framePtr && framePtr->pointer());
      64           3 :     CPPUNIT_ASSERT(framePtr->pointer()->nb_samples == outputSize_);
      65           3 : }
      66             : 
      67             : std::shared_ptr<AudioFrame>
      68           4 : AudioFrameResizerTest::getFrame(int n)
      69             : {
      70           4 :     auto frame = std::make_shared<AudioFrame>();
      71           4 :     frame->pointer()->format = format_.sampleFormat;
      72           4 :     frame->pointer()->sample_rate = format_.sample_rate;
      73           4 :     av_channel_layout_default(&frame->pointer()->ch_layout, format_.nb_channels);
      74           4 :     frame->pointer()->nb_samples = n;
      75           4 :     CPPUNIT_ASSERT(av_frame_get_buffer(frame->pointer(), 0) >= 0);
      76           4 :     return frame;
      77           0 : }
      78             : 
      79             : void
      80           1 : AudioFrameResizerTest::testSameSize()
      81             : {
      82             :     // input.nb_samples == output.nb_samples
      83           2 :     q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); }));
      84           1 :     auto in = getFrame(outputSize_);
      85             :     // gotFrame should be called after this
      86           2 :     CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in)));
      87           1 :     CPPUNIT_ASSERT(q_->samples() == 0);
      88           1 : }
      89             : 
      90             : void
      91           1 : AudioFrameResizerTest::testBiggerInput()
      92             : {
      93             :     // input.nb_samples > output.nb_samples
      94           2 :     q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); }));
      95           1 :     auto in = getFrame(outputSize_ + 100);
      96             :     // gotFrame should be called after this
      97           2 :     CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in)));
      98           1 :     CPPUNIT_ASSERT(q_->samples() == 100);
      99           1 : }
     100             : 
     101             : void
     102           1 : AudioFrameResizerTest::testBiggerOutput()
     103             : {
     104             :     // input.nb_samples < output.nb_samples
     105           2 :     q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); }));
     106           1 :     auto in = getFrame(outputSize_ - 100);
     107           2 :     CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in)));
     108           1 :     CPPUNIT_ASSERT(q_->samples() == outputSize_ - 100);
     109             :     // gotFrame should be called after this
     110           2 :     CPPUNIT_ASSERT_NO_THROW(q_->enqueue(std::move(in)));
     111             :     // pushed 2 frames of (outputSize_ - 100) samples and got 1 frame
     112           1 :     CPPUNIT_ASSERT(q_->samples() == outputSize_ - 200);
     113           1 : }
     114             : 
     115             : void
     116           1 : AudioFrameResizerTest::testDifferentFormat()
     117             : {
     118             :     // frame format != q_->format_
     119           1 :     q_.reset(new AudioFrameResizer(format_, outputSize_, [this](std::shared_ptr<AudioFrame>&& f){ gotFrame(std::move(f)); }));
     120           1 :     auto in = getFrame(outputSize_-27);
     121           1 :     q_->enqueue(std::move(in));
     122           1 :     CPPUNIT_ASSERT(q_->samples()==outputSize_-27);
     123           1 :     q_->setFormat(AudioFormat::MONO(), 960);
     124           1 :     CPPUNIT_ASSERT(q_->samples() == 0);
     125           1 : }
     126             : 
     127             : }} // namespace jami::test
     128             : 
     129           1 : RING_TEST_RUNNER(jami::test::AudioFrameResizerTest::name());

Generated by: LCOV version 1.14