-
Notifications
You must be signed in to change notification settings - Fork 9
/
mod_shine.c
316 lines (258 loc) · 9.28 KB
/
mod_shine.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* FreeSWITCH Module for MP3 recording
* Copyright (C) 2013-2020, Seven Du <dujinfang@gmail.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH FreeSWITCH Module for MP3 recording
*
* The Initial Developer of the Original Code is
* Seven Du <dujinfang@gmail.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*
* mod_shine.c -- Encode mp3 using shine
*
*/
#include <switch.h>
#include "layer3.h"
#define DEFAULT_SAMPLE_RATE 32000
SWITCH_MODULE_LOAD_FUNCTION(mod_shine_load);
SWITCH_MODULE_DEFINITION(mod_shine, mod_shine_load, NULL, NULL);
struct shine_context {
switch_file_t *fd;
shine_config_t config;
shine_t s;
int16_t *buffer;
size_t buffer_used;
};
typedef struct shine_context shine_context;
/* remove warning */
const long *rates = samplerates;
const int *bitrate = bitrates;
/* Print some info about what we're going to encode */
static void check_config(shine_config_t *config)
{
static char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "mono" };
static char *demp_names[4] = { "none", "50/15us", "", "CITT" };
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
"MPEG-I layer III, %s Psychoacoustic Model: Shine\n",
mode_names[config->mpeg.mode]);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Bitrate: %d kbps\n", config->mpeg.bitr);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "De-emphasis: %s %s %s\n",
demp_names[config->mpeg.emph],
((config->mpeg.original) ? "Original" : ""),
((config->mpeg.copyright) ? "(C)" : ""));
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Encoding \"%s\" to \"%s\"\n", infname, outfname);
}
static switch_status_t shine_file_open(switch_file_handle_t *handle, const char *path)
{
shine_context *context;
char *ext;
unsigned int flags = 0;
if ((ext = strrchr(path, '.')) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Format\n");
return SWITCH_STATUS_GENERR;
}
ext++;
if ((context = switch_core_alloc(handle->memory_pool, sizeof(*context))) == 0) {
return SWITCH_STATUS_MEMERR;
}
if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
flags |= SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE;
if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND) || switch_test_flag(handle, SWITCH_FILE_WRITE_OVER)) {
flags |= SWITCH_FOPEN_READ;
} else {
flags |= SWITCH_FOPEN_TRUNCATE;
}
}
if (switch_test_flag(handle, SWITCH_FILE_FLAG_READ)) {
flags |= SWITCH_FOPEN_READ;
}
if (switch_file_open(&context->fd, path, flags, SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE, handle->memory_pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening %s\n", path);
return SWITCH_STATUS_GENERR;
}
if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) {
int64_t samples = 0;
switch_file_seek(context->fd, SEEK_END, &samples);
handle->pos = samples;
}
shine_set_config_mpeg_defaults(&(context->config.mpeg));
context->config.mpeg.bitr = 48;
context->config.wave.channels = 2; // even setting channels = 1, the result will be 2 channels, why?
context->config.wave.samplerate = DEFAULT_SAMPLE_RATE;
context->s = shine_initialise(&context->config);
context->buffer_used = 0;
context->buffer = (int16_t *)malloc(2 * samp_per_frame * sizeof(int16_t));
if (!context->buffer) return SWITCH_STATUS_MEMERR;
if (context->config.wave.channels > 1) {
context->config.mpeg.mode = STEREO;
} else {
context->config.mpeg.mode = MONO;
}
/* See if samplerate is valid */
if (shine_find_samplerate_index(context->config.wave.samplerate) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unsupported samplerate");
}
/* See if bitrate is valid */
if (shine_find_bitrate_index(context->config.mpeg.bitr) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unsupported bitrate\n");
}
check_config(&context->config);
handle->samples = 0;
handle->samplerate = DEFAULT_SAMPLE_RATE;
// handle->channels = 1;
handle->format = 0;
handle->sections = 0;
handle->seekable = 0;
handle->speed = 0;
handle->pos = 0;
handle->private_info = context;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Opening File [%s] %dhz\n", path, handle->samplerate);
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t shine_file_truncate(switch_file_handle_t *handle, int64_t offset)
{
shine_context *context = handle->private_info;
switch_status_t status;
if ((status = switch_file_trunc(context->fd, offset)) == SWITCH_STATUS_SUCCESS) {
handle->pos = 0;
}
return status;
}
static switch_status_t shine_file_close(switch_file_handle_t *handle)
{
shine_context *context = handle->private_info;
long written = 0;
size_t writing = 0;
unsigned char *mp3_data;
int padding = samp_per_frame - context->buffer_used;
switch_status_t status = SWITCH_STATUS_SUCCESS;
if (context->buffer_used > 0) {
// padding with 0
memset(context->buffer + context->buffer_used, 0, padding);
memset(context->buffer + context->buffer_used + samp_per_frame, 0, padding);
mp3_data = shine_encode_frame(context->s, (void *)context->buffer, &written);
writing = written;
if (writing && mp3_data) {
status = switch_file_write(context->fd, mp3_data, &writing);
if (status != SWITCH_STATUS_SUCCESS) goto end;
}
}
// write any pendding data;
mp3_data = shine_flush(context->s, &written);
writing = written;
switch_file_write(context->fd, mp3_data, &writing);
end:
switch_file_close(context->fd);
/* Close encoder. */
shine_close(context->s);
switch_safe_free(context->buffer);
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t shine_file_seek(switch_file_handle_t *handle, unsigned int *cur_sample, int64_t samples, int whence)
{
return SWITCH_STATUS_FALSE;
}
static switch_status_t shine_file_read(switch_file_handle_t *handle, void *data, size_t *len)
{
*len = 0;
return SWITCH_STATUS_FALSE;
}
static switch_status_t shine_file_write(switch_file_handle_t *handle, void *data, size_t *len)
{
shine_context *context = handle->private_info;
long written = 0;
size_t writing = 0;
int16_t *samples = data;
size_t left = *len;
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "writing %ld buffer_used:%ld\n", *len, context->buffer_used);
while (left > 0) {
size_t room;
int16_t *buf1 = context->buffer + context->buffer_used;
int16_t *buf2 = buf1 + samp_per_frame;
int i;
assert(samp_per_frame > context->buffer_used);
room = samp_per_frame - context->buffer_used;
if (left < room) {
for(i = 0; i < left; i++) {
*(buf1 + i) = *samples++;
*(buf2 + i) = handle->channels == 1 ? 0 : *samples++;
}
context->buffer_used += left;
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "buffering %u samples\n", (uint32_t)left);
return status;
} else {
unsigned char *mp3_data;
for(i = 0; i < room; i++) {
*(buf1 + i) = *samples++;
*(buf2 + i) = 0;
*(buf2 + i) = handle->channels == 1 ? 0 : *samples++;
}
mp3_data = shine_encode_frame(context->s, (void *)context->buffer, &written);
writing = written;
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "writing %ld %ld\n", *len, writing);
if (writing && mp3_data) {
status = switch_file_write(context->fd, mp3_data, &writing);
if (status != SWITCH_STATUS_SUCCESS) return status;
}
left -= room;
context->buffer_used = 0;
}
}
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t shine_file_set_string(switch_file_handle_t *handle, switch_audio_col_t col, const char *string)
{
return SWITCH_STATUS_FALSE;
}
static switch_status_t shine_file_get_string(switch_file_handle_t *handle, switch_audio_col_t col, const char **string)
{
return SWITCH_STATUS_FALSE;
}
/* Registration */
static char *supported_formats[2] = { 0 };
SWITCH_MODULE_LOAD_FUNCTION(mod_shine_load)
{
switch_file_interface_t *file_interface;
supported_formats[0] = "mp3";
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
file_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_FILE_INTERFACE);
file_interface->interface_name = modname;
file_interface->extens = supported_formats;
file_interface->file_open = shine_file_open;
file_interface->file_close = shine_file_close;
file_interface->file_truncate = shine_file_truncate;
file_interface->file_read = shine_file_read;
file_interface->file_write = shine_file_write;
file_interface->file_seek = shine_file_seek;
file_interface->file_set_string = shine_file_set_string;
file_interface->file_get_string = shine_file_get_string;
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/