1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org#include "webrtc/modules/rtp_rtcp/source/dtmf_queue.h"
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
13a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org#include <string.h>  //memset
14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
15b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc {
16a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.orgDTMFqueue::DTMFqueue()
17a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org    : dtmf_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
18a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org      next_empty_index_(0) {
19a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  memset(dtmf_key_, 0, sizeof(dtmf_key_));
20a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  memset(dtmf_length, 0, sizeof(dtmf_length));
21a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  memset(dtmf_level_, 0, sizeof(dtmf_level_));
22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
24a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.orgDTMFqueue::~DTMFqueue() { delete dtmf_critsect_; }
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
26a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.orgint32_t DTMFqueue::AddDTMF(uint8_t key, uint16_t len, uint8_t level) {
27a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  CriticalSectionScoped lock(dtmf_critsect_);
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
29a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  if (next_empty_index_ >= DTMF_OUTBAND_MAX) {
30a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org    return -1;
31a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  }
32a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  int32_t index = next_empty_index_;
33a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  dtmf_key_[index] = key;
34a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  dtmf_length[index] = len;
35a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  dtmf_level_[index] = level;
36a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  next_empty_index_++;
37a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  return 0;
38b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
39b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
40a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.orgint8_t DTMFqueue::NextDTMF(uint8_t* dtmf_key, uint16_t* len, uint8_t* level) {
41a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  CriticalSectionScoped lock(dtmf_critsect_);
42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
43a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  if (!PendingDTMF()) {
44a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org    return -1;
45a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  }
46a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  *dtmf_key = dtmf_key_[0];
47a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  *len = dtmf_length[0];
48a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  *level = dtmf_level_[0];
49b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
50a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  memmove(&(dtmf_key_[0]), &(dtmf_key_[1]),
51a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org          next_empty_index_ * sizeof(uint8_t));
52a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  memmove(&(dtmf_length[0]), &(dtmf_length[1]),
53a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org          next_empty_index_ * sizeof(uint16_t));
54a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  memmove(&(dtmf_level_[0]), &(dtmf_level_[1]),
55a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org          next_empty_index_ * sizeof(uint8_t));
56b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
57a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  next_empty_index_--;
58a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org  return 0;
59b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
60b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
61a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.orgbool DTMFqueue::PendingDTMF() { return (next_empty_index_ > 0); }
62b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
63a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.orgvoid DTMFqueue::ResetDTMF() { next_empty_index_ = 0; }
64a149ea3907602ca7721a8b0ab139e5ce1b33bc4ephoglund@webrtc.org}  // namespace webrtc
65