Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 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/jami_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 87 : ToneControl::setSampleRate(unsigned rate, AVSampleFormat sampleFormat)
40 : {
41 87 : std::lock_guard lk(mutex_);
42 87 : sampleRate_ = rate;
43 87 : sampleFormat_ = sampleFormat;
44 87 : if (!telephoneTone_)
45 32 : telephoneTone_.reset(new TelephoneTone(prefs_.getZoneToneChoice(), rate, sampleFormat));
46 : else
47 55 : telephoneTone_->setSampleRate(rate, sampleFormat);
48 87 : if (!audioFile_) {
49 87 : 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_WARNING("Audio file error: {}", e.what());
56 0 : }
57 87 : }
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 55 : ToneControl::setAudioFile(const std::string& file)
77 : {
78 55 : std::lock_guard lk(mutex_);
79 :
80 55 : if (audioFile_) {
81 0 : emitSignal<libjami::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
82 0 : audioFile_.reset();
83 : }
84 :
85 : try {
86 55 : audioFile_.reset(new AudioFile(file, sampleRate_, sampleFormat_));
87 55 : } catch (const AudioFileException& e) {
88 220 : JAMI_WARNING("Audio file error: {}", e.what());
89 55 : }
90 :
91 110 : return static_cast<bool>(audioFile_);
92 55 : }
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 496 : ToneControl::stop()
107 : {
108 496 : std::lock_guard lk(mutex_);
109 :
110 496 : if (telephoneTone_)
111 496 : telephoneTone_->setCurrentTone(Tone::ToneId::TONE_NULL);
112 :
113 496 : if (audioFile_) {
114 0 : emitSignal<libjami::CallSignal::RecordPlaybackStopped>(audioFile_->getFilePath());
115 0 : audioFile_.reset();
116 : }
117 496 : }
118 :
119 : void
120 122 : ToneControl::play(Tone::ToneId toneId)
121 : {
122 122 : std::lock_guard lk(mutex_);
123 :
124 122 : if (telephoneTone_)
125 122 : telephoneTone_->setCurrentTone(toneId);
126 122 : }
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
|