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 : #include "dtmf.h" 19 : 20 : namespace jami { 21 : 22 33 : DTMF::DTMF(unsigned int sampleRate, AVSampleFormat sampleFormat) 23 33 : : currentTone_(0) 24 33 : , newTone_(0) 25 33 : , dtmfgenerator_(sampleRate, sampleFormat) 26 33 : {} 27 : 28 : void 29 0 : DTMF::startTone(char code) 30 : { 31 0 : newTone_ = code; 32 0 : } 33 : 34 : bool 35 0 : DTMF::generateDTMF(AVFrame* buffer) 36 : { 37 : try { 38 0 : if (currentTone_ != 0) { 39 : // Currently generating a DTMF tone 40 0 : if (currentTone_ == newTone_) { 41 : // Continue generating the same tone 42 0 : dtmfgenerator_.getNextSamples(buffer); 43 0 : return true; 44 0 : } else if (newTone_ != 0) { 45 : // New tone requested 46 0 : dtmfgenerator_.getSamples(buffer, newTone_); 47 0 : currentTone_ = newTone_; 48 0 : return true; 49 : } else { 50 : // Stop requested 51 0 : currentTone_ = newTone_; 52 0 : return false; 53 : } 54 : } else { 55 : // Not generating any DTMF tone 56 0 : if (newTone_) { 57 : // Requested to generate a DTMF tone 58 0 : dtmfgenerator_.getSamples(buffer, newTone_); 59 0 : currentTone_ = newTone_; 60 0 : return true; 61 : } else 62 0 : return false; 63 : } 64 0 : } catch (const DTMFException& e) { 65 : // invalid key 66 0 : return false; 67 0 : } 68 : } 69 : 70 : } // namespace jami