-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgsm_jni.cpp
159 lines (120 loc) · 4.04 KB
/
gsm_jni.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (C) 2009 The Sipdroid Open Source Project
*
* This file is part of Sipdroid (http://www.sipdroid.org)
*
* Sipdroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <memory.h>
#include <ctype.h>
#include <jni.h>
#include <android/log.h>
#include "spandsp.h"
/* Define codec specific settings */
#define BLOCK_LEN 160
#define LOG_TAG "gsm" // text for log tag
#undef DEBUG_GSM
// the header length of the RTP frame (must skip when en/decoding)
#define RTP_HDR_SIZE 12
static int codec_open = 0;
static JavaVM *gJavaVM;
const char *kInterfacePath = "org/sipdroid/pjlib/gsm";
gsm0610_state_t *gsm0610_enc_state;
gsm0610_state_t *gsm0610_dec_state;
extern "C"
JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_GSM_open
(JNIEnv *env, jobject obj) {
int ret;
if (codec_open++ != 0)
return (jint)0;
if ((gsm0610_enc_state = gsm0610_init(NULL, GSM0610_PACKING_VOIP)) == NULL)
{
fprintf(stderr, " Cannot create encoder\n");
exit(2);
}
if ((gsm0610_dec_state = gsm0610_init(NULL, GSM0610_PACKING_VOIP)) == NULL)
{
fprintf(stderr, " Cannot create decoder\n");
exit(2);
}
return (jint)0;
}
extern "C"
JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_GSM_encode
(JNIEnv *env, jobject obj, jshortArray lin, jint offset, jbyteArray encoded, jint size) {
jshort pre_amp[BLOCK_LEN];
jbyte gsm0610_data[BLOCK_LEN];
int ret,i,frsz=BLOCK_LEN;
unsigned int lin_pos = 0;
if (!codec_open)
return 0;
#ifdef DEBUG_GSM
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,
"encoding frame size: %d\toffset: %d\n", size, offset);
#endif
for (i = 0; i < size; i+=BLOCK_LEN) {
#ifdef DEBUG_GSM
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,
"encoding frame size: %d\toffset: %d i: %d\n", size, offset, i);
#endif
env->GetShortArrayRegion(lin, offset + i,frsz, pre_amp);
ret=gsm0610_encode(gsm0610_enc_state, (uint8_t *) gsm0610_data, pre_amp, size);
#ifdef DEBUG_GSM
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,
"Enocded Bytes: %d\n", ret);
#endif
/* Write payload */
env->SetByteArrayRegion(encoded, RTP_HDR_SIZE+ lin_pos, ret, gsm0610_data);
lin_pos += ret;
}
#ifdef DEBUG_GSM
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,
"encoding **END** frame size: %d\toffset: %d i: %d lin_pos: %d\n", size, offset, i, lin_pos);
#endif
return (jint)lin_pos;
}
extern "C"
JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_GSM_decode
(JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin, jint size) {
jshort post_amp[BLOCK_LEN];
jbyte gsm0610_data[BLOCK_LEN];
int len;
if (!codec_open)
return 0;
#ifdef DEBUG_GSM
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,
"##### BEGIN DECODE ******** decoding frame size: %d\n", size);
#endif
env->GetByteArrayRegion(encoded, RTP_HDR_SIZE, size, gsm0610_data);
len = gsm0610_decode(gsm0610_dec_state, post_amp,(uint8_t *) gsm0610_data, size);
#ifdef DEBUG_GSM
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,
"##### DECODED length: %d\n", len);
#endif
env->SetShortArrayRegion(lin, 0, len,post_amp);
return (jint)len;
}
extern "C"
JNIEXPORT void JNICALL Java_org_sipdroid_codecs_GSM_close
(JNIEnv *env, jobject obj) {
if (--codec_open != 0)
return;
gsm0610_release(gsm0610_enc_state);
gsm0610_release(gsm0610_dec_state);
}