mainbuffer.h 4.59 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3
 *  Author : Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Julien Bonjean's avatar
Julien Bonjean committed
4
 *
5 6 7 8
 *  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.
Julien Bonjean's avatar
Julien Bonjean committed
9
 *
10 11 12 13
 *  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
14
 *
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
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 19 20 21 22 23 24 25 26 27 28
 *
 *  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.
29 30
 */

31 32
#ifndef MAIN_BUFFER_H_
#define MAIN_BUFFER_H_
33 34

#include <map>
35
#include <set>
36
#include <string>
37

38
#include "cc_thread.h" // for ost::Mutex
39 40

class RingBuffer;
41

42
typedef std::set<std::string> CallIDSet;
43

44
class MainBuffer {
45

Julien Bonjean's avatar
Julien Bonjean committed
46
    public:
47
        static const char * const DEFAULT_ID;
48 49 50 51 52

        MainBuffer();

        ~MainBuffer();

53
        void setInternalSamplingRate(int sr);
Julien Bonjean's avatar
Julien Bonjean committed
54

55
        int getInternalSamplingRate() const {
56
            return internalSamplingRate_;
Julien Bonjean's avatar
Julien Bonjean committed
57
        }
58

Julien Bonjean's avatar
Julien Bonjean committed
59
        /**
60 61
         * Bind together two audio streams so taht a client will be able
         * to put and get data specifying its callid only.
Julien Bonjean's avatar
Julien Bonjean committed
62
         */
63
        void bindCallID(const std::string &call_id1, const std::string &call_id2);
64

Julien Bonjean's avatar
Julien Bonjean committed
65 66 67 68 69
        /**
         * Add a new call_id to unidirectional outgoing stream
         * \param call_id New call id to be added for this stream
         * \param process_id Process that require this stream
         */
70
        void bindHalfDuplexOut(const std::string &process_id, const std::string &call_id);
71

Julien Bonjean's avatar
Julien Bonjean committed
72 73 74
        /**
         * Unbind two calls
         */
75
        void unBindCallID(const std::string &call_id1, const std::string &call_id2);
76

77 78 79
        /**
         * Unbind a unidirectional stream
         */
80
        void unBindHalfDuplexOut(const std::string &process_id, const std::string &call_id);
81

82
        void unBindAll(const std::string &call_id);
83

84
        void putData(void *buffer, size_t toCopy, const std::string &call_id);
85

86
        size_t getData(void *buffer, size_t toCopy, const std::string &call_id);
87

88
        size_t availableForGet(const std::string &call_id);
89

90
        size_t discard(size_t toDiscard, const std::string &call_id);
91

92
        void flush(const std::string &call_id);
93

Julien Bonjean's avatar
Julien Bonjean committed
94
        void flushAllBuffers();
95

96
        void dumpInfo();
97

98
    private:
99

100
        CallIDSet* getCallIDSet(const std::string &call_id);
101

102
        void createCallIDSet(const std::string &set_id);
103

104
        void removeCallIDSet(const std::string &set_id);
105 106 107 108

        /**
         * Add a new call id to this set
         */
109
        void addCallIDtoSet(const std::string &set_id, const std::string &call_id);
110

111
        void removeCallIDfromSet(const std::string &set_id, const std::string &call_id);
112 113 114 115

        /**
         * Create a new ringbuffer with default readpointer
         */
116
        void createRingBuffer(const std::string &call_id);
117

118
        void removeRingBuffer(const std::string &call_id);
119

120
        RingBuffer* getRingBuffer(const std::string &call_id);
121
        const RingBuffer* getRingBuffer(const std::string & call_id) const;
122

123
        size_t getDataByID(void *buffer, size_t toCopy, const std::string &call_id, const std::string &reader_id);
124

125
        size_t availableForGetByID(const std::string &call_id, const std::string &reader_id) const;
126

127
        void discardByID(size_t toDiscard, const std::string &call_id, const std::string &reader_id);
128

129
        void flushByID(const std::string &call_id, const std::string &reader_id);
130

131
        typedef std::map<std::string, RingBuffer*> RingBufferMap;
132
        RingBufferMap ringBufferMap_;
133

134
        typedef std::map<std::string, CallIDSet*> CallIDMap;
135
        CallIDMap callIDMap_;
136

137
        ost::Mutex mutex_;
138

139
        int internalSamplingRate_;
140

Julien Bonjean's avatar
Julien Bonjean committed
141
        friend class MainBufferTest;
142 143
};

144
#endif  // MainBuffer