LCOV - code coverage report
Current view: top level - test/unitTest/media - test_media_player.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 122 122 100.0 %
Date: 2024-12-21 08:56:24 Functions: 15 15 100.0 %

          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             : #include <cmath>
      22             : 
      23             : #include "jami.h"
      24             : #include "manager.h"
      25             : #include "../../test_runner.h"
      26             : #include "client/videomanager.h"
      27             : 
      28             : namespace jami { namespace test {
      29             : 
      30             : class MediaPlayerTest : public CppUnit::TestFixture {
      31             : public:
      32           2 :     static std::string name() { return "media_player"; }
      33             : 
      34             :     void setUp();
      35             :     void tearDown();
      36             : 
      37             : private:
      38             :     void testCreate();
      39             :     void testJPG();
      40             :     void testAudioFile();
      41             :     void testPause();
      42             :     void testSeekWhilePaused();
      43             :     void testSeekWhilePlaying();
      44             : 
      45             :     bool isWithinUsec(int64_t currentTime, int64_t seekTime, int64_t margin);
      46             : 
      47           2 :     CPPUNIT_TEST_SUITE(MediaPlayerTest);
      48           1 :     CPPUNIT_TEST(testCreate);
      49           1 :     CPPUNIT_TEST(testJPG);
      50           1 :     CPPUNIT_TEST(testAudioFile);
      51           1 :     CPPUNIT_TEST(testPause);
      52           1 :     CPPUNIT_TEST(testSeekWhilePaused);
      53           1 :     CPPUNIT_TEST(testSeekWhilePlaying);
      54           4 :     CPPUNIT_TEST_SUITE_END();
      55             : 
      56             :     std::string playerId1_ {};
      57             :     std::string playerId2_ {};
      58             :     int64_t duration_ {};
      59             :     int audio_stream_ {};
      60             :     int video_stream_ {};
      61             :     std::shared_ptr<MediaPlayer> mediaPlayer {};
      62             : 
      63             :     std::mutex mtx;
      64             :     std::unique_lock<std::mutex> lk {mtx};
      65             :     std::condition_variable cv;
      66             : };
      67             : 
      68             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MediaPlayerTest, MediaPlayerTest::name());
      69             : 
      70             : void
      71           6 : MediaPlayerTest::setUp()
      72             : {
      73           6 :     libjami::init(libjami::InitFlag(libjami::LIBJAMI_FLAG_DEBUG | libjami::LIBJAMI_FLAG_CONSOLE_LOG));
      74           6 :     if (not Manager::instance().initialized)
      75           1 :         CPPUNIT_ASSERT(libjami::start("jami-sample.yml"));
      76             : 
      77           6 :     std::map<std::string, std::shared_ptr<libjami::CallbackWrapperBase>> handler;
      78           6 :     handler.insert(libjami::exportable_callback<libjami::MediaPlayerSignal::FileOpened>(
      79           6 :         [=](const std::string& playerId,
      80             :             const std::map<std::string, std::string>& info) {
      81           6 :             duration_ = std::stol(info.at("duration"));
      82           6 :             audio_stream_ = std::stoi(info.at("audio_stream"));
      83           6 :             video_stream_ = std::stoi(info.at("video_stream"));
      84           6 :             playerId2_ = playerId;
      85           6 :             cv.notify_all();
      86           6 :         }));
      87           6 :     libjami::registerSignalHandlers(handler);
      88           6 : }
      89             : 
      90             : void
      91           6 : MediaPlayerTest::tearDown()
      92             : {
      93           6 :     jami::closeMediaPlayer(playerId1_);
      94           6 :     mediaPlayer.reset();
      95           6 :     playerId1_ = {};
      96           6 :     playerId2_ = {};
      97           6 :     libjami::fini();
      98           6 : }
      99             : 
     100             : void
     101           1 : MediaPlayerTest::testCreate()
     102             : {
     103           1 :     JAMI_INFO("Start testCreate");
     104           1 :     playerId1_ = jami::createMediaPlayer("./media/test_video_file.mp4");
     105           1 :     mediaPlayer = jami::getMediaPlayer(playerId1_);
     106           1 :     cv.wait_for(lk, 5s);
     107           1 :     CPPUNIT_ASSERT(playerId1_ == playerId2_);
     108           1 :     CPPUNIT_ASSERT(mediaPlayer->getId() == playerId1_);
     109           1 :     CPPUNIT_ASSERT(mediaPlayer->isInputValid());
     110           1 :     CPPUNIT_ASSERT(audio_stream_ != -1);
     111           1 :     CPPUNIT_ASSERT(video_stream_ != -1);
     112           1 :     CPPUNIT_ASSERT(mediaPlayer->isPaused());
     113           1 :     CPPUNIT_ASSERT(mediaPlayer->getPlayerPosition() == 0);
     114           1 :     JAMI_INFO("End testCreate");
     115           1 : }
     116             : 
     117             : void
     118           1 : MediaPlayerTest::testJPG()
     119             : {
     120           1 :     JAMI_INFO("Start testJpg");
     121           1 :     playerId1_ = jami::createMediaPlayer("./media/jami.jpg");
     122           1 :     mediaPlayer = jami::getMediaPlayer(playerId1_);
     123           1 :     cv.wait_for(lk, 5s);
     124           1 :     CPPUNIT_ASSERT(playerId1_ == playerId2_);
     125           1 :     CPPUNIT_ASSERT(mediaPlayer->getId() == playerId1_);
     126           1 :     CPPUNIT_ASSERT(mediaPlayer->isInputValid());
     127           1 :     CPPUNIT_ASSERT(video_stream_ != -1);
     128           1 :     CPPUNIT_ASSERT(mediaPlayer->isPaused());
     129           1 :     CPPUNIT_ASSERT(mediaPlayer->getPlayerPosition() == 0);
     130           1 :     JAMI_INFO("End testJpg");
     131           1 : }
     132             : 
     133             : void
     134           1 : MediaPlayerTest::testAudioFile()
     135             : {
     136           1 :     JAMI_INFO("Start testAudioFile");
     137           1 :     playerId1_ = jami::createMediaPlayer("./media/test.mp3");
     138           1 :     mediaPlayer = jami::getMediaPlayer(playerId1_);
     139           1 :     cv.wait_for(lk, 5s);
     140           1 :     CPPUNIT_ASSERT(playerId1_ == playerId2_);
     141           1 :     CPPUNIT_ASSERT(mediaPlayer->getId() == playerId1_);
     142           1 :     CPPUNIT_ASSERT(mediaPlayer->isInputValid());
     143           1 :     CPPUNIT_ASSERT(audio_stream_ != -1);
     144           1 :     CPPUNIT_ASSERT(mediaPlayer->isPaused());
     145           1 :     CPPUNIT_ASSERT(mediaPlayer->getPlayerPosition() == 0);
     146           1 :     JAMI_INFO("End testAudioFile");
     147           1 : }
     148             : 
     149             : void
     150           1 : MediaPlayerTest::testPause()
     151             : {
     152           1 :     playerId1_ = jami::createMediaPlayer("./media/test_video_file.mp4");
     153           1 :     mediaPlayer = jami::getMediaPlayer(playerId1_);
     154           1 :     cv.wait_for(lk, 5s);
     155           1 :     JAMI_INFO("Start testPause");
     156             :     // should start paused
     157           1 :     CPPUNIT_ASSERT(mediaPlayer->isPaused());
     158           1 :     mediaPlayer->pause(false);
     159           1 :     CPPUNIT_ASSERT(!mediaPlayer->isPaused());
     160           1 :     JAMI_INFO("End testPause");
     161           1 : }
     162             : 
     163             : bool
     164           9 : MediaPlayerTest::isWithinUsec(int64_t currentTime, int64_t seekTime, int64_t margin)
     165             : {
     166           9 :     return std::abs(currentTime-seekTime) <= margin;
     167             : }
     168             : 
     169             : void
     170           1 : MediaPlayerTest::testSeekWhilePaused()
     171             : {
     172           1 :     JAMI_INFO("Start testSeekWhilePaused");
     173           1 :     playerId1_ = jami::createMediaPlayer("./media/test_video_file.mp4");
     174           1 :     mediaPlayer = jami::getMediaPlayer(playerId1_);
     175           1 :     cv.wait_for(lk, 5s);
     176             : 
     177           1 :     int64_t startTime = mediaPlayer->getPlayerPosition();
     178             : 
     179           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(startTime+100));
     180           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), startTime+100, 1));
     181             : 
     182           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(startTime+1000));
     183           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), startTime+1000, 1));
     184             : 
     185           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(startTime+500));
     186           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), startTime+500, 1));
     187             : 
     188           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(duration_-1));
     189           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), duration_-1, 1));
     190             : 
     191           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(0));
     192           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), 0, 1));
     193             : 
     194           1 :     CPPUNIT_ASSERT(!(mediaPlayer->seekToTime(duration_+1)));
     195           1 :     JAMI_INFO("End testSeekWhilePaused");
     196           1 : }
     197             : 
     198             : void
     199           1 : MediaPlayerTest::testSeekWhilePlaying()
     200             : {
     201           1 :     JAMI_INFO("Start testSeekWhilePlaying");
     202           1 :     playerId1_ = jami::createMediaPlayer("./media/test_video_file.mp4");
     203           1 :     mediaPlayer = jami::getMediaPlayer(playerId1_);
     204           1 :     cv.wait_for(lk, 5s);
     205           1 :     mediaPlayer->pause(false);
     206             : 
     207           1 :     int64_t startTime = mediaPlayer->getPlayerPosition();
     208             : 
     209           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(startTime+10000));
     210           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), startTime+10000, 50));
     211             : 
     212           1 :     startTime = mediaPlayer->getPlayerPosition();
     213           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(startTime-5000));
     214           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), startTime-5000, 50));
     215             : 
     216           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(10000));
     217           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), 10000, 50));
     218             : 
     219           1 :     CPPUNIT_ASSERT(mediaPlayer->seekToTime(0));
     220           1 :     CPPUNIT_ASSERT(isWithinUsec(mediaPlayer->getPlayerPosition(), 0, 50));
     221             : 
     222           1 :     CPPUNIT_ASSERT(!(mediaPlayer->seekToTime(duration_+1)));
     223           1 :     JAMI_INFO("End testSeekWhilePlaying");
     224           1 : }
     225             : 
     226             : }} // namespace jami::test
     227             : 
     228           1 : RING_TEST_RUNNER(jami::test::MediaPlayerTest::name());

Generated by: LCOV version 1.14