blob: 717b329b74a53f0c82f0356f9f2aa873783ca7b1 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000011#include "webrtc/modules/rtp_rtcp/source/dtmf_queue.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000013#include <string.h> //memset
niklase@google.com470e71d2011-07-07 08:21:25 +000014
15namespace webrtc {
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000016DTMFqueue::DTMFqueue()
17 : dtmf_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
18 next_empty_index_(0) {
19 memset(dtmf_key_, 0, sizeof(dtmf_key_));
20 memset(dtmf_length, 0, sizeof(dtmf_length));
21 memset(dtmf_level_, 0, sizeof(dtmf_level_));
niklase@google.com470e71d2011-07-07 08:21:25 +000022}
23
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000024DTMFqueue::~DTMFqueue() { delete dtmf_critsect_; }
25
26int32_t DTMFqueue::AddDTMF(uint8_t key, uint16_t len, uint8_t level) {
27 CriticalSectionScoped lock(dtmf_critsect_);
28
29 if (next_empty_index_ >= DTMF_OUTBAND_MAX) {
30 return -1;
31 }
32 int32_t index = next_empty_index_;
33 dtmf_key_[index] = key;
34 dtmf_length[index] = len;
35 dtmf_level_[index] = level;
36 next_empty_index_++;
37 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000040int8_t DTMFqueue::NextDTMF(uint8_t* dtmf_key, uint16_t* len, uint8_t* level) {
41 CriticalSectionScoped lock(dtmf_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000042
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000043 if (!PendingDTMF()) {
44 return -1;
45 }
46 *dtmf_key = dtmf_key_[0];
47 *len = dtmf_length[0];
48 *level = dtmf_level_[0];
49
50 memmove(&(dtmf_key_[0]), &(dtmf_key_[1]),
51 next_empty_index_ * sizeof(uint8_t));
52 memmove(&(dtmf_length[0]), &(dtmf_length[1]),
53 next_empty_index_ * sizeof(uint16_t));
54 memmove(&(dtmf_level_[0]), &(dtmf_level_[1]),
55 next_empty_index_ * sizeof(uint8_t));
56
57 next_empty_index_--;
58 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000061bool DTMFqueue::PendingDTMF() { return (next_empty_index_ > 0); }
niklase@google.com470e71d2011-07-07 08:21:25 +000062
phoglund@webrtc.org315d3982013-05-08 10:04:06 +000063void DTMFqueue::ResetDTMF() { next_empty_index_ = 0; }
64} // namespace webrtc