LCOV - code coverage report
Current view: top level - src/media/audio - tonecontrol.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 35 65 53.8 %
Date: 2024-12-21 08:56:24 Functions: 6 10 60.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             : #ifdef HAVE_CONFIG_H
      19             : #include "config.h"
      20             : #endif
      21             : 
      22             : #include "audio/tonecontrol.h"
      23             : #include "sound/tonelist.h"
      24             : #include "client/ring_signal.h"
      25             : #include "jami/callmanager_interface.h" // for CallSignal
      26             : 
      27             : namespace jami {
      28             : 
      29             : static constexpr unsigned DEFAULT_SAMPLE_RATE = 8000;
      30             : 
      31          38 : ToneControl::ToneControl(const Preferences& preferences)
      32          38 :     : prefs_(preferences)
      33          38 :     , sampleRate_(DEFAULT_SAMPLE_RATE)
      34          38 : {}
      35             : 
      36          38 : ToneControl::~ToneControl() {}
      37             : 
      38             : void
      39          91 : ToneControl::setSampleRate(unsigned rate, AVSampleFormat sampleFormat)
      40             : {
      41          91 :     std::lock_guard lk(mutex_);
      42          91 :     sampleRate_ = rate;
      43          91 :     sampleFormat_ = sampleFormat;
      44          91 :     if (!telephoneTone_)
      45          33 :         telephoneTone_.reset(new TelephoneTone(prefs_.getZoneToneChoice(), rate, sampleFormat));
      46             :     else
      47          58 :         telephoneTone_->setSampleRate(rate, sampleFormat);
      48          91 :     if (!audioFile_) {
      49          91 :         return;
      50             :     }
      51           0 :     auto path = audioFile_->getFilePath();
      52             :     try {
      53           0 :         audioFile_.reset(new AudioFile(path, sampleRate_, sampleFormat));
      54           0 :     } catch (const AudioFileException& e) {
      55           0 :         JAMI_WARN("Audio file error: %s", e.what());
      56           0 :     }
      57          91 : }
      58             : 
      59             : std::shared_ptr<AudioLoop>
      60           0 : ToneControl::getTelephoneTone()
      61             : {
      62           0 :     std::lock_guard lk(mutex_);
      63           0 :     if (telephoneTone_)
      64           0 :         return telephoneTone_->getCurrentTone();
      65           0 :     return nullptr;
      66           0 : }
      67             : 
      68             : std::shared_ptr<AudioLoop>
      69           0 : ToneControl::getTelephoneFile(void)
      70             : {
      71           0 :     std::lock_guard lk(mutex_);
      72           0 :     return audioFile_;
      73           0 : }
      74             : 
      75             : bool
      76          58 : ToneControl::setAudioFile(const std::string& file)
      77             : {
      78          58 :     std::lock_guard lk(mutex_);
      79             : 
      80          58 :     if (audioFile_) {
      81           0 :         emitSignal<libjami::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
      82           0 :         audioFile_.reset();
      83             :     }
      84             : 
      85             :     try {
      86          58 :         audioFile_.reset(new AudioFile(file, sampleRate_, sampleFormat_));
      87          58 :     } catch (const AudioFileException& e) {
      88          58 :         JAMI_WARN("Audio file error: %s", e.what());
      89          58 :     }
      90             : 
      91         116 :     return static_cast<bool>(audioFile_);
      92          58 : }
      93             : 
      94             : void
      95           0 : ToneControl::stopAudioFile()
      96             : {
      97           0 :     std::lock_guard lk(mutex_);
      98             : 
      99           0 :     if (audioFile_) {
     100           0 :         emitSignal<libjami::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
     101           0 :         audioFile_.reset();
     102             :     }
     103           0 : }
     104             : 
     105             : void
     106         535 : ToneControl::stop()
     107             : {
     108         535 :     std::lock_guard lk(mutex_);
     109             : 
     110         535 :     if (telephoneTone_)
     111         535 :         telephoneTone_->setCurrentTone(Tone::ToneId::TONE_NULL);
     112             : 
     113         535 :     if (audioFile_) {
     114           0 :         emitSignal<libjami::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
     115           0 :         audioFile_.reset();
     116             :     }
     117         535 : }
     118             : 
     119             : void
     120         135 : ToneControl::play(Tone::ToneId toneId)
     121             : {
     122         135 :     std::lock_guard lk(mutex_);
     123             : 
     124         135 :     if (telephoneTone_)
     125         135 :         telephoneTone_->setCurrentTone(toneId);
     126         135 : }
     127             : 
     128             : void
     129           0 : ToneControl::seek(double value)
     130             : {
     131           0 :     std::lock_guard lk(mutex_);
     132             : 
     133           0 :     if (audioFile_)
     134           0 :         audioFile_->seek(value);
     135           0 : }
     136             : 
     137             : } // namespace jami

Generated by: LCOV version 1.14