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-11-15 09:04:49 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          93 : ToneControl::setSampleRate(unsigned rate, AVSampleFormat sampleFormat)
      40             : {
      41          93 :     std::lock_guard lk(mutex_);
      42          93 :     sampleRate_ = rate;
      43          93 :     sampleFormat_ = sampleFormat;
      44          93 :     if (!telephoneTone_)
      45          33 :         telephoneTone_.reset(new TelephoneTone(prefs_.getZoneToneChoice(), rate, sampleFormat));
      46             :     else
      47          60 :         telephoneTone_->setSampleRate(rate, sampleFormat);
      48          93 :     if (!audioFile_) {
      49          93 :         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          93 : }
      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          60 : ToneControl::setAudioFile(const std::string& file)
      77             : {
      78          60 :     std::lock_guard lk(mutex_);
      79             : 
      80          60 :     if (audioFile_) {
      81           0 :         emitSignal<libjami::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
      82           0 :         audioFile_.reset();
      83             :     }
      84             : 
      85             :     try {
      86          60 :         audioFile_.reset(new AudioFile(file, sampleRate_, sampleFormat_));
      87          60 :     } catch (const AudioFileException& e) {
      88          60 :         JAMI_WARN("Audio file error: %s", e.what());
      89          60 :     }
      90             : 
      91         120 :     return static_cast<bool>(audioFile_);
      92          60 : }
      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         532 : ToneControl::stop()
     107             : {
     108         532 :     std::lock_guard lk(mutex_);
     109             : 
     110         532 :     if (telephoneTone_)
     111         532 :         telephoneTone_->setCurrentTone(Tone::ToneId::TONE_NULL);
     112             : 
     113         532 :     if (audioFile_) {
     114           0 :         emitSignal<libjami::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
     115           0 :         audioFile_.reset();
     116             :     }
     117         532 : }
     118             : 
     119             : void
     120         136 : ToneControl::play(Tone::ToneId toneId)
     121             : {
     122         136 :     std::lock_guard lk(mutex_);
     123             : 
     124         136 :     if (telephoneTone_)
     125         136 :         telephoneTone_->setCurrentTone(toneId);
     126         136 : }
     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