niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 | |
| 14 | namespace webrtc { |
| 15 | |
| 16 | DtmfInbandQueue::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 | |
| 26 | DtmfInbandQueue::~DtmfInbandQueue() |
| 27 | { |
| 28 | delete &_DtmfCritsect; |
| 29 | } |
| 30 | |
| 31 | int |
| 32 | DtmfInbandQueue::AddDtmf(WebRtc_UWord8 key, |
| 33 | WebRtc_UWord16 len, |
| 34 | WebRtc_UWord8 level) |
| 35 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 36 | CriticalSectionScoped lock(&_DtmfCritsect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
| 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 | |
| 52 | WebRtc_Word8 |
| 53 | DtmfInbandQueue::NextDtmf(WebRtc_UWord16* len, WebRtc_UWord8* level) |
| 54 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 55 | CriticalSectionScoped lock(&_DtmfCritsect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
| 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 | |
| 76 | bool |
| 77 | DtmfInbandQueue::PendingDtmf() |
| 78 | { |
| 79 | return(_nextEmptyIndex>0); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | DtmfInbandQueue::ResetDtmf() |
| 84 | { |
| 85 | _nextEmptyIndex = 0; |
| 86 | } |
| 87 | |
| 88 | } // namespace webrtc |