audiolayer.cpp 3.09 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
4
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5 6 7 8 9
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
10
 *
11 12 13 14
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
15
 *
16 17
 *  You should have received a copy of the GNU General Public License
 *  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 22 23 24 25 26 27 28 29
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
30 31
 */

32
#include <ctime>
33
#include "audiolayer.h"
34
#include "audio/dcblocker.h"
35
#include "manager.h"
36

37
AudioLayer::AudioLayer()
38 39
    : captureGain_(1.0)
    , playbackGain_(1.0)
40
    , isStarted_(false)
41
    , playbackMode_(NONE)
42
    , urgentRingBuffer_(SIZEBUF, MainBuffer::DEFAULT_ID)
43
    , sampleRate_(Manager::instance().getMainBuffer().getInternalSamplingRate())
44
    , mutex_()
45
    , dcblocker_()
46
    , converter_(sampleRate_)
47
    , lastNotificationTime_(0)
48
{
49
    urgentRingBuffer_.createReadPointer(MainBuffer::DEFAULT_ID);
50
}
51

52
void AudioLayer::flushMain()
Emmanuel Milou's avatar
[#2402]  
Emmanuel Milou committed
53
{
54
    std::lock_guard<std::mutex> lock(mutex_);
55
    // should pass call id
56
    Manager::instance().getMainBuffer().flushAllBuffers();
57 58
}

59
void AudioLayer::flushUrgent()
Emmanuel Milou's avatar
[#2402]  
Emmanuel Milou committed
60
{
61
    std::lock_guard<std::mutex> lock(mutex_);
62
    urgentRingBuffer_.flushAll();
63 64
}

65
void AudioLayer::putUrgent(AudioBuffer& buffer)
Emmanuel Milou's avatar
[#2402]  
Emmanuel Milou committed
66
{
67
    std::lock_guard<std::mutex> lock(mutex_);
Adrien Béraud's avatar
Adrien Béraud committed
68
    urgentRingBuffer_.put(buffer);
69 70
}

71
// Notify (with a beep) an incoming call when there is already a call in progress
72
void AudioLayer::notifyIncomingCall()
73
{
74
    if (!Manager::instance().incomingCallsWaiting())
75
        return;
76

77
    time_t now = time(NULL);
78

79 80 81
    // Notify maximum once every 5 seconds
    if (difftime(now, lastNotificationTime_) < 5)
        return;
82

83
    lastNotificationTime_ = now;
84

85 86 87
    // Enable notification only if more than one call
    if (!Manager::instance().hasCurrentCall())
        return;
88

89 90
    Tone tone("440/160", getSampleRate());
    unsigned int nbSample = tone.getSize();
Adrien Béraud's avatar
Adrien Béraud committed
91
    AudioBuffer buf(nbSample);
92
    tone.getNext(buf, 1.0);
93

94 95
    /* Put the data in the urgent ring buffer */
    flushUrgent();
96
    putUrgent(buf);
97
}