audiolayer.h 6.66 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
jpbl's avatar
jpbl committed
3
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
Julien Bonjean's avatar
Julien Bonjean committed
4
 *  Author:  Jerome Oufella <jerome.oufella@savoirfairelinux.com>
5
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
6
 *  Authro: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
jpbl's avatar
jpbl committed
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
10
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
11
 *  (at your option) any later version.
Julien Bonjean's avatar
Julien Bonjean committed
12
 *
jpbl's avatar
jpbl committed
13 14 15 16
 *  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.
Julien Bonjean's avatar
Julien Bonjean committed
17
 *
jpbl's avatar
jpbl committed
18 19
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
20
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
21 22 23 24 25 26 27 28 29 30 31
 *
 *  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.
jpbl's avatar
jpbl committed
32 33
 */

34 35
#ifndef AUDIO_LAYER_H_
#define AUDIO_LAYER_H_
jpbl's avatar
jpbl committed
36

37
#include <mutex>
38
#include <sys/time.h>
39
#include <vector>
40
#include "ringbuffer.h"
41
#include "dcblocker.h"
42
#include "samplerateconverter.h"
43
#include "noncopyable.h"
44

45 46
/**
 * @file  audiolayer.h
Julien Bonjean's avatar
Julien Bonjean committed
47
 * @brief Main sound class. Manages the data transfers between the application and the hardware.
48 49
 */

50
class MainBuffer;
51
class AudioPreference;
52

53 54 55

typedef std::vector<AudioBuffer> AudioBufferStack;

56
namespace ost {
57
class Time;
58
}
59

60
class AudioLayer {
61

62
    private:
63
        NON_COPYABLE(AudioLayer);
64 65

    public:
66 67 68 69 70 71 72
        enum PCMType {
            SFL_PCM_BOTH = 0x0021,          /** To open both playback and capture devices */
            SFL_PCM_PLAYBACK = 0x0022,      /** To open playback device only */
            SFL_PCM_CAPTURE = 0x0023,       /** To open capture device only */
            SFL_PCM_RINGTONE = 0x0024       /** To open the ringtone device only */
        };

73
        enum PlaybackMode {
74
            NONE = 0,
75 76 77
            TONE,
            RINGTONE,
            VOICE,
78 79
            URGENT,
            ZEROS
80 81
        };

82
        AudioLayer();
83
        virtual ~AudioLayer() = default;
84

85 86
        virtual std::vector<std::string> getCaptureDeviceList() const = 0;
        virtual std::vector<std::string> getPlaybackDeviceList() const = 0;
87

88 89
        virtual int getAudioDeviceIndex(const std::string& name) const = 0;
        virtual std::string getAudioDeviceName(int index, PCMType type) const = 0;
90 91 92
        virtual int getIndexCapture() const = 0;
        virtual int getIndexPlayback() const = 0;
        virtual int getIndexRingtone() const = 0;
93

94
        /**
Julien Bonjean's avatar
Julien Bonjean committed
95
         * Start the capture stream and prepare the playback stream.
96 97 98
         * The playback starts accordingly to its threshold
         * ALSA Library API
         */
99
        virtual void startStream() = 0;
100 101

        /**
Julien Bonjean's avatar
Julien Bonjean committed
102
         * Stop the playback and capture streams.
103 104 105
         * Drops the pending frames and put the capture and playback handles to PREPARED state
         * ALSA Library API
         */
106
        virtual void stopStream() = 0;
107

108
        /**
109
         * Determine wether or not the audio layer is active (i.e. stream opened)
110
         */
111
        bool isStarted() const {
112 113
            return isStarted_;
        }
114

115 116 117 118 119 120 121 122
        PlaybackMode getPlaybackMode() const {
            return playbackMode_;
        }

        void setPlaybackMode(PlaybackMode mode) {
            playbackMode_ = mode;
        }

123 124
        /**
         * Send a chunk of data to the hardware buffer to start the playback
Julien Bonjean's avatar
Julien Bonjean committed
125
         * Copy data in the urgent buffer.
126 127
         * @param buffer The buffer containing the data to be played ( ringtones )
         */
128
        void putUrgent(AudioBuffer& buffer);
129

130
        /**
131
         * Flush main buffer
132
         */
133
        void flushMain();
134

135 136 137
        /**
         * Flush urgent buffer
         */
138
        void flushUrgent();
139

140 141
        /**
         * Set capture stream gain (microphone)
142
         * Range should be [-1.0, 1.0]
143
         */
144
        void setCaptureGain(double gain) {
145 146
            captureGain_ = gain;
        }
147

148
        /**
Alexandre Lision's avatar
Alexandre Lision committed
149
         * Get capture stream gain (microphone)
150
         */
151
        double getCaptureGain() const {
152 153 154 155 156
            return captureGain_;
        }

        /**
         * Set playback stream gain (speaker)
157
         * Range should be [-1.0, 1.0]
158
         */
159
        void setPlaybackGain(double gain) {
160 161
            playbackGain_ = gain;
        }
162

163
        /**
Tristan Matthews's avatar
Tristan Matthews committed
164
         * Get playback stream gain (speaker)
165
         */
166
        double getPlaybackGain() const {
167 168
            return playbackGain_;
        }
169

170 171 172 173 174
        /**
         * Get the sample rate of the audio layer
         * @return unsigned int The sample rate
         *			    default: 44100 HZ
         */
175
        unsigned int getSampleRate() const {
176
            return sampleRate_;
Julien Bonjean's avatar
Julien Bonjean committed
177
        }
178

179
        /**
180 181
         * Emit an audio notification on incoming calls
         */
182
        void notifyIncomingCall();
183

184
        virtual void updatePreference(AudioPreference &pref, int index, PCMType type) = 0;
185

186
    protected:
187 188 189 190

        /**
         * Gain applied to mic signal
         */
191
        double captureGain_;
192 193 194 195

        /**
         * Gain applied to playback signal
         */
196
        double playbackGain_;
197

198
        /**
199
         * Whether or not the audio layer stream is started
200
         */
201
        bool isStarted_;
202

203 204
        PlaybackMode playbackMode_;

205 206 207
        /**
         * Urgent ring buffer used for ringtones
         */
208
        RingBuffer urgentRingBuffer_;
209

210
        /**
Julien Bonjean's avatar
Julien Bonjean committed
211
         * Sample Rate SFLphone should send sound data to the sound card
212 213
         * The value can be set in the user config file- now: 44100HZ
         */
214
        unsigned int sampleRate_;
215

Julien Bonjean's avatar
Julien Bonjean committed
216 217 218
        /**
         * Lock for the entire audio layer
         */
219
        std::mutex mutex_;
220 221 222 223

        /**
         * Remove audio offset that can be introduced by certain cheap audio device
         */
224
        DcBlocker dcblocker_;
225 226 227 228

        /**
         * Manage sampling rate conversion
         */
229
        SamplerateConverter converter_;
230

231
    private:
232

233
        /**
234
         * Time of the last incoming call notification
235
         */
236
        time_t lastNotificationTime_;
jpbl's avatar
jpbl committed
237 238 239
};

#endif // _AUDIO_LAYER_H_