LCOV - code coverage report
Current view: top level - test/unitTest/media - test_media_frame.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 56 56 100.0 %
Date: 2024-04-25 08:05:53 Functions: 9 9 100.0 %

          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             : extern "C" {
      26             : #include <libavutil/frame.h>
      27             : #include <libavutil/pixfmt.h>
      28             : }
      29             : 
      30             : #include "jami.h"
      31             : #include "videomanager_interface.h"
      32             : #include "media/audio/audio_format.h"
      33             : 
      34             : #include "../../test_runner.h"
      35             : 
      36             : namespace jami { namespace test {
      37             : 
      38             : class MediaFrameTest : public CppUnit::TestFixture {
      39             : public:
      40           2 :     static std::string name() { return "media_frame"; }
      41             : 
      42             :     void setUp();
      43             :     void tearDown();
      44             : 
      45             : private:
      46             :     void testCopy();
      47             :     void testMix();
      48             : 
      49           2 :     CPPUNIT_TEST_SUITE(MediaFrameTest);
      50           1 :     CPPUNIT_TEST(testCopy);
      51           1 :     CPPUNIT_TEST(testMix);
      52           4 :     CPPUNIT_TEST_SUITE_END();
      53             : };
      54             : 
      55             : 
      56             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MediaFrameTest, MediaFrameTest::name());
      57             : 
      58             : void
      59           2 : MediaFrameTest::setUp()
      60             : {
      61           2 :     libjami::init(libjami::InitFlag(libjami::LIBJAMI_FLAG_DEBUG | libjami::LIBJAMI_FLAG_CONSOLE_LOG));
      62           2 : }
      63             : 
      64             : void
      65           2 : MediaFrameTest::tearDown()
      66             : {
      67           2 :     libjami::fini();
      68           2 : }
      69             : 
      70             : void
      71           1 : MediaFrameTest::testCopy()
      72             : {
      73             :     // test allocation
      74           1 :     libjami::VideoFrame v1;
      75           1 :     v1.reserve(AV_PIX_FMT_YUV420P, 100, 100);
      76           1 :     v1.pointer()->data[0][0] = 42;
      77           1 :     CPPUNIT_ASSERT(v1.pointer());
      78             : 
      79             :     // test frame referencing (different pointers, but same data)
      80           1 :     libjami::VideoFrame v2;
      81           1 :     v2.copyFrom(v1);
      82           1 :     CPPUNIT_ASSERT(v1.format() == v2.format());
      83           1 :     CPPUNIT_ASSERT(v1.width() == v2.width());
      84           1 :     CPPUNIT_ASSERT(v1.height() == v2.height());
      85           1 :     CPPUNIT_ASSERT(v1.pointer() != v2.pointer());
      86           1 :     CPPUNIT_ASSERT(v1.pointer()->data[0][0] == 42);
      87           1 :     CPPUNIT_ASSERT(v2.pointer()->data[0][0] == 42);
      88           1 : }
      89             : 
      90             : void
      91           1 : MediaFrameTest::testMix()
      92             : {
      93           1 :     const AudioFormat& format = AudioFormat::STEREO();
      94           1 :     const int nbSamples = format.sample_rate / 50;
      95           1 :     auto a1 = std::make_unique<libjami::AudioFrame>(format, nbSamples);
      96           1 :     auto d1 = reinterpret_cast<int16_t*>(a1->pointer()->extended_data[0]);
      97           1 :     d1[0] = 0;
      98           1 :     d1[1] = 1;
      99           1 :     d1[2] = 3;
     100           1 :     d1[3] = -2;
     101           1 :     d1[4] = 5;
     102           1 :     d1[5] = std::numeric_limits<int16_t>::min();
     103           1 :     d1[6] = std::numeric_limits<int16_t>::max();
     104           1 :     auto a2 = std::make_unique<libjami::AudioFrame>(format, nbSamples);
     105           1 :     auto d2 = reinterpret_cast<int16_t*>(a2->pointer()->extended_data[0]);
     106           1 :     d2[0] = 0;
     107           1 :     d2[1] = 3;
     108           1 :     d2[2] = -1;
     109           1 :     d2[3] = 3;
     110           1 :     d2[4] = -6;
     111           1 :     d2[5] = -101;
     112           1 :     d2[6] = 101;
     113           1 :     a2->mix(*a1);
     114           1 :     CPPUNIT_ASSERT(d2[0] == 0);
     115           1 :     CPPUNIT_ASSERT(d2[1] == 4);
     116           1 :     CPPUNIT_ASSERT(d2[2] == 2);
     117           1 :     CPPUNIT_ASSERT(d2[3] == 1);
     118           1 :     CPPUNIT_ASSERT(d2[4] == -1);
     119           1 :     CPPUNIT_ASSERT(d2[5] == std::numeric_limits<int16_t>::min());
     120           1 :     CPPUNIT_ASSERT(d2[6] == std::numeric_limits<int16_t>::max());
     121           1 : }
     122             : 
     123             : }} // namespace jami::test
     124             : 
     125           1 : RING_TEST_RUNNER(jami::test::MediaFrameTest::name());

Generated by: LCOV version 1.14