• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include "gsm.h"
20 
21 namespace {
22 
23 class GsmCodec : public AudioCodec
24 {
25 public:
GsmCodec()26     GsmCodec() {
27         mEncode = gsm_create();
28         mDecode = gsm_create();
29     }
30 
~GsmCodec()31     ~GsmCodec() {
32         if (mEncode) {
33             gsm_destroy(mEncode);
34         }
35         if (mDecode) {
36             gsm_destroy(mDecode);
37         }
38     }
39 
set(int sampleRate,const char *)40     int set(int sampleRate, const char */* fmtp */) {
41         return (sampleRate == 8000 && mEncode && mDecode) ? 160 : -1;
42     }
43 
44     int encode(void *payload, int16_t *samples);
45     int decode(int16_t *samples, int count, void *payload, int length);
46 
47 private:
48     gsm mEncode;
49     gsm mDecode;
50 };
51 
encode(void * payload,int16_t * samples)52 int GsmCodec::encode(void *payload, int16_t *samples)
53 {
54     gsm_encode(mEncode, samples, (unsigned char *)payload);
55     return 33;
56 }
57 
decode(int16_t * samples,int count,void * payload,int length)58 int GsmCodec::decode(int16_t *samples, int count, void *payload, int length)
59 {
60     unsigned char *bytes = (unsigned char *)payload;
61     int n = 0;
62     while (n + 160 <= count && length >= 33 &&
63         gsm_decode(mDecode, bytes, &samples[n]) == 0) {
64         n += 160;
65         length -= 33;
66         bytes += 33;
67     }
68     return n;
69 }
70 
71 } // namespace
72 
newGsmCodec()73 AudioCodec *newGsmCodec()
74 {
75     return new GsmCodec;
76 }
77