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-04-25 08:05:53 Functions: 6 10 60.0 %

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

Generated by: LCOV version 1.14