audiorecord.h 3.9 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2016 Savoir-faire Linux Inc.
3
 *
4
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  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.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
20 21
 */

22
#pragma once
23

24
#include "audiobuffer.h"
25
#include "audiorecorder.h"
26
#include "noncopyable.h"
27

28
#include <atomic>
29 30 31
#include <memory>
#include <string>
#include <cstdlib>
32

33 34
class SndfileHandle;

Adrien Béraud's avatar
Adrien Béraud committed
35
namespace ring {
36

37
class AudioRecord {
Julien Bonjean's avatar
Julien Bonjean committed
38 39
    public:
        AudioRecord();
40
        ~AudioRecord();
Julien Bonjean's avatar
Julien Bonjean committed
41

42 43
        void setSndFormat(AudioFormat format);
        void setRecordingOptions(AudioFormat format, const std::string &path);
Julien Bonjean's avatar
Julien Bonjean committed
44

45 46 47
        /**
         * Init recording file path
         */
48
        void initFilename(const std::string &peerNumber);
Julien Bonjean's avatar
Julien Bonjean committed
49

50
        /**
51 52
         * Return the filepath of the recording
         */
53
        std::string getFilename() const;
54

Julien Bonjean's avatar
Julien Bonjean committed
55 56
        /**
         * Check if no otehr file is opened, then create a new one
57
         * @param filename A string containing teh file (with/without extension)
Julien Bonjean's avatar
Julien Bonjean committed
58 59
         * @param type     The sound file format (FILE_RAW, FILE_WAVE)
         * @param format   Internal sound format (INT16 / INT32)
60
         * @return bool    True if file was opened
Julien Bonjean's avatar
Julien Bonjean committed
61
         */
62
        bool openFile();
Julien Bonjean's avatar
Julien Bonjean committed
63 64 65 66 67 68 69 70 71

        /**
         * Close the opend recording file. If wave: cout the number of byte
         */
        void closeFile();

        /**
         * Check if a file is already opened
         */
72
        bool isOpenFile() const noexcept;
Julien Bonjean's avatar
Julien Bonjean committed
73 74

        /**
75
         * Check if a file already exists
Julien Bonjean's avatar
Julien Bonjean committed
76
         */
77
        bool fileExists() const;
Julien Bonjean's avatar
Julien Bonjean committed
78 79 80 81

        /**
         * Check recording state
         */
82
        bool isRecording() const;
Julien Bonjean's avatar
Julien Bonjean committed
83 84

        /**
85
         * Toggle recording state
Julien Bonjean's avatar
Julien Bonjean committed
86
         */
87
        bool toggleRecording();
Julien Bonjean's avatar
Julien Bonjean committed
88 89 90 91

        /**
         * Stop recording flag
         */
92
        void stopRecording() const noexcept;
Julien Bonjean's avatar
Julien Bonjean committed
93 94 95 96 97 98

        /**
         * Record a chunk of data in an openend file
         * @param buffer  The data chunk to be recorded
         * @param nSamples Number of samples (number of bytes) to be recorded
         */
99
        void recData(AudioBuffer& buffer);
Julien Bonjean's avatar
Julien Bonjean committed
100

101 102 103 104
        std::string getRecorderID() const {
            return recorder_.getRecorderID();
        }

105 106
    private:
        NON_COPYABLE(AudioRecord);
Julien Bonjean's avatar
Julien Bonjean committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

        /**
         * Open an existing raw file, used when the call is set on hold
         */
        bool openExistingRawFile();

        /**
         * Open an existing wav file, used when the call is set on hold
         */
        bool openExistingWavFile();

        /**
         * Compute the number of byte recorded and close the file
         */
        void closeWavFile();

        /**
         * Pointer to the recorded file
         */
126
        std::shared_ptr<SndfileHandle> fileHandle_;
Julien Bonjean's avatar
Julien Bonjean committed
127 128 129 130

        /**
         * Number of channels
         */
131
        AudioFormat sndFormat_;
Julien Bonjean's avatar
Julien Bonjean committed
132 133 134 135

        /**
         * Recording flage
         */
136
        mutable std::atomic<bool> recordingEnabled_ {false};
Julien Bonjean's avatar
Julien Bonjean committed
137 138 139 140

        /**
         * Filename for this recording
         */
141
        std::string filename_;
Julien Bonjean's avatar
Julien Bonjean committed
142 143 144 145 146

        /**
         * Path for this recording
         */
        std::string savePath_;
147 148 149 150 151

        /**
         * Audio recording thread
         */
        AudioRecorder recorder_;
152
};
153

154
} // namespace ring