blob: b81d8273cfb43ddcc5589b93701656bfd47a2564 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "dtmf_inband_queue.h"
12#include "trace.h"
13
14namespace webrtc {
15
16DtmfInbandQueue::DtmfInbandQueue(const WebRtc_Word32 id):
17 _id(id),
18 _DtmfCritsect(*CriticalSectionWrapper::CreateCriticalSection()),
19 _nextEmptyIndex(0)
20{
21 memset(_DtmfKey,0, sizeof(_DtmfKey));
22 memset(_DtmfLen,0, sizeof(_DtmfLen));
23 memset(_DtmfLevel,0, sizeof(_DtmfLevel));
24}
25
26DtmfInbandQueue::~DtmfInbandQueue()
27{
28 delete &_DtmfCritsect;
29}
30
31int
32DtmfInbandQueue::AddDtmf(WebRtc_UWord8 key,
33 WebRtc_UWord16 len,
34 WebRtc_UWord8 level)
35{
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +000036 CriticalSectionScoped lock(&_DtmfCritsect);
niklase@google.com470e71d2011-07-07 08:21:25 +000037
38 if (_nextEmptyIndex >= kDtmfInbandMax)
39 {
40 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_id,-1),
41 "DtmfInbandQueue::AddDtmf() unable to add Dtmf tone");
42 return -1;
43 }
44 WebRtc_Word32 index = _nextEmptyIndex;
45 _DtmfKey[index] = key;
46 _DtmfLen[index] = len;
47 _DtmfLevel[index] = level;
48 _nextEmptyIndex++;
49 return 0;
50}
51
52WebRtc_Word8
53DtmfInbandQueue::NextDtmf(WebRtc_UWord16* len, WebRtc_UWord8* level)
54{
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +000055 CriticalSectionScoped lock(&_DtmfCritsect);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
57 if(!PendingDtmf())
58 {
59 return -1;
60 }
61 WebRtc_Word8 nextDtmf = _DtmfKey[0];
62 *len=_DtmfLen[0];
63 *level=_DtmfLevel[0];
64
65 memmove(&(_DtmfKey[0]), &(_DtmfKey[1]),
66 _nextEmptyIndex*sizeof(WebRtc_UWord8));
67 memmove(&(_DtmfLen[0]), &(_DtmfLen[1]),
68 _nextEmptyIndex*sizeof(WebRtc_UWord16));
69 memmove(&(_DtmfLevel[0]), &(_DtmfLevel[1]),
70 _nextEmptyIndex*sizeof(WebRtc_UWord8));
71
72 _nextEmptyIndex--;
73 return nextDtmf;
74}
75
76bool
77DtmfInbandQueue::PendingDtmf()
78{
79 return(_nextEmptyIndex>0);
80}
81
82void
83DtmfInbandQueue::ResetDtmf()
84{
85 _nextEmptyIndex = 0;
86}
87
88} // namespace webrtc