summaryrefslogtreecommitdiff
path: root/voice_engine/dtmf_inband_queue.cc
blob: 86e8d62b72c3897481f2dde9499f011e012f5247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/system_wrappers/interface/trace.h"
#include "webrtc/voice_engine/dtmf_inband_queue.h"

namespace webrtc {

DtmfInbandQueue::DtmfInbandQueue(int32_t id):
    _id(id),
    _DtmfCritsect(*CriticalSectionWrapper::CreateCriticalSection()),
    _nextEmptyIndex(0)
{
    memset(_DtmfKey,0, sizeof(_DtmfKey));
    memset(_DtmfLen,0, sizeof(_DtmfLen));
    memset(_DtmfLevel,0, sizeof(_DtmfLevel));
}

DtmfInbandQueue::~DtmfInbandQueue()
{
    delete &_DtmfCritsect;
}

int
DtmfInbandQueue::AddDtmf(uint8_t key, uint16_t len, uint8_t level)
{
    CriticalSectionScoped lock(&_DtmfCritsect);

    if (_nextEmptyIndex >= kDtmfInbandMax)
    {
        WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_id,-1),
                   "DtmfInbandQueue::AddDtmf() unable to add Dtmf tone");
        return -1;
    }
    int32_t index = _nextEmptyIndex;
    _DtmfKey[index] = key;
    _DtmfLen[index] = len;
    _DtmfLevel[index] = level;
    _nextEmptyIndex++;
    return 0;
}

int8_t
DtmfInbandQueue::NextDtmf(uint16_t* len, uint8_t* level)
{
    CriticalSectionScoped lock(&_DtmfCritsect);

    if(!PendingDtmf())
    {
        return -1;
    }
    int8_t nextDtmf = _DtmfKey[0];
    *len=_DtmfLen[0];
    *level=_DtmfLevel[0];

    memmove(&(_DtmfKey[0]), &(_DtmfKey[1]),
            _nextEmptyIndex*sizeof(uint8_t));
    memmove(&(_DtmfLen[0]), &(_DtmfLen[1]),
            _nextEmptyIndex*sizeof(uint16_t));
    memmove(&(_DtmfLevel[0]), &(_DtmfLevel[1]),
            _nextEmptyIndex*sizeof(uint8_t));

    _nextEmptyIndex--;
    return nextDtmf;
}

bool
DtmfInbandQueue::PendingDtmf()
{
    CriticalSectionScoped lock(&_DtmfCritsect);
    return _nextEmptyIndex > 0;
}

void
DtmfInbandQueue::ResetDtmf()
{
    CriticalSectionScoped lock(&_DtmfCritsect);
    _nextEmptyIndex = 0;
}

}  // namespace webrtc