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 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 16 | DtmfInbandQueue::DtmfInbandQueue(const int32_t id): |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 32 | DtmfInbandQueue::AddDtmf(uint8_t key, uint16_t len, uint8_t level) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 34 | CriticalSectionScoped lock(&_DtmfCritsect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
| 36 | if (_nextEmptyIndex >= kDtmfInbandMax) |
| 37 | { |
| 38 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_id,-1), |
| 39 | "DtmfInbandQueue::AddDtmf() unable to add Dtmf tone"); |
| 40 | return -1; |
| 41 | } |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 42 | int32_t index = _nextEmptyIndex; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | _DtmfKey[index] = key; |
| 44 | _DtmfLen[index] = len; |
| 45 | _DtmfLevel[index] = level; |
| 46 | _nextEmptyIndex++; |
| 47 | return 0; |
| 48 | } |
| 49 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 50 | int8_t |
| 51 | DtmfInbandQueue::NextDtmf(uint16_t* len, uint8_t* level) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 53 | CriticalSectionScoped lock(&_DtmfCritsect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
| 55 | if(!PendingDtmf()) |
| 56 | { |
| 57 | return -1; |
| 58 | } |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 59 | int8_t nextDtmf = _DtmfKey[0]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | *len=_DtmfLen[0]; |
| 61 | *level=_DtmfLevel[0]; |
| 62 | |
| 63 | memmove(&(_DtmfKey[0]), &(_DtmfKey[1]), |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 64 | _nextEmptyIndex*sizeof(uint8_t)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | memmove(&(_DtmfLen[0]), &(_DtmfLen[1]), |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 66 | _nextEmptyIndex*sizeof(uint16_t)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | memmove(&(_DtmfLevel[0]), &(_DtmfLevel[1]), |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame^] | 68 | _nextEmptyIndex*sizeof(uint8_t)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
| 70 | _nextEmptyIndex--; |
| 71 | return nextDtmf; |
| 72 | } |
| 73 | |
| 74 | bool |
| 75 | DtmfInbandQueue::PendingDtmf() |
| 76 | { |
| 77 | return(_nextEmptyIndex>0); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | DtmfInbandQueue::ResetDtmf() |
| 82 | { |
| 83 | _nextEmptyIndex = 0; |
| 84 | } |
| 85 | |
| 86 | } // namespace webrtc |