blob: 61dfdc94ad46f4e8c69e70905dca942b28ed3e4b [file] [log] [blame]
Chia-chi Yeha6f950c2010-09-29 10:36:52 +08001/*
2 * Copyrightm (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "AudioCodec.h"
18
19extern "C" {
20#include "gsm.h"
21}
22
23namespace {
24
25class GsmCodec : public AudioCodec
26{
27public:
28 GsmCodec() {
29 mEncode = gsm_create();
30 mDecode = gsm_create();
31 }
32
33 ~GsmCodec() {
34 if (mEncode) {
35 gsm_destroy(mEncode);
36 }
37 if (mDecode) {
38 gsm_destroy(mDecode);
39 }
40 }
41
42 int set(int sampleRate, const char *fmtp) {
43 return (sampleRate == 8000 && mEncode && mDecode) ? 160 : -1;
44 }
45
46 int encode(void *payload, int16_t *samples);
Chia-chi Yeh35d05dc2011-09-06 14:18:37 -070047 int decode(int16_t *samples, int count, void *payload, int length);
Chia-chi Yeha6f950c2010-09-29 10:36:52 +080048
49private:
50 gsm mEncode;
51 gsm mDecode;
52};
53
54int GsmCodec::encode(void *payload, int16_t *samples)
55{
56 gsm_encode(mEncode, samples, (unsigned char *)payload);
57 return 33;
58}
59
Chia-chi Yeh35d05dc2011-09-06 14:18:37 -070060int GsmCodec::decode(int16_t *samples, int count, void *payload, int length)
Chia-chi Yeha6f950c2010-09-29 10:36:52 +080061{
Chia-chi Yeh35d05dc2011-09-06 14:18:37 -070062 unsigned char *bytes = (unsigned char *)payload;
63 int n = 0;
64 while (n + 160 <= count && length >= 33 &&
65 gsm_decode(mDecode, bytes, &samples[n]) == 0) {
66 n += 160;
67 length -= 33;
68 bytes += 33;
Chia-chi Yeha6f950c2010-09-29 10:36:52 +080069 }
Chia-chi Yeh35d05dc2011-09-06 14:18:37 -070070 return n;
Chia-chi Yeha6f950c2010-09-29 10:36:52 +080071}
72
73} // namespace
74
75AudioCodec *newGsmCodec()
76{
77 return new GsmCodec;
78}