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