| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "mp3_port.h" |
| 24 |
#include <pjmedia/errno.h> |
| 25 |
#include <pj/assert.h> |
| 26 |
#include <pj/file_access.h> |
| 27 |
#include <pj/file_io.h> |
| 28 |
#include <pj/log.h> |
| 29 |
#include <pj/pool.h> |
| 30 |
#include <pj/string.h> |
| 31 |
#include <pj/unicode.h> |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#include "BladeMP3EncDLL.h" |
| 36 |
|
| 37 |
|
| 38 |
#define THIS_FILE "mp3_writer.c" |
| 39 |
#define SIGNATURE PJMEDIA_SIG_CLASS_PORT_AUD('M','W') |
| 40 |
#define BYTES_PER_SAMPLE 2 |
| 41 |
|
| 42 |
static struct BladeDLL |
| 43 |
{ |
| 44 |
void *hModule; |
| 45 |
int refCount; |
| 46 |
BEINITSTREAM beInitStream; |
| 47 |
BEENCODECHUNK beEncodeChunk; |
| 48 |
BEDEINITSTREAM beDeinitStream; |
| 49 |
BECLOSESTREAM beCloseStream; |
| 50 |
BEVERSION beVersion; |
| 51 |
BEWRITEVBRHEADER beWriteVBRHeader; |
| 52 |
BEWRITEINFOTAG beWriteInfoTag; |
| 53 |
} BladeDLL; |
| 54 |
|
| 55 |
|
| 56 |
struct mp3_file_port |
| 57 |
{ |
| 58 |
pjmedia_port base; |
| 59 |
pj_size_t total; |
| 60 |
pj_oshandle_t fd; |
| 61 |
pj_size_t cb_size; |
| 62 |
pj_status_t (*cb)(pjmedia_port*, void*); |
| 63 |
pj_bool_t subscribed; |
| 64 |
pj_bool_t cb_called; |
| 65 |
void (*cb2)(pjmedia_port*, void*); |
| 66 |
|
| 67 |
unsigned silence_duration; |
| 68 |
|
| 69 |
pj_str_t mp3_filename; |
| 70 |
pjmedia_mp3_encoder_option mp3_option; |
| 71 |
unsigned mp3_samples_per_frame; |
| 72 |
pj_int16_t *mp3_sample_buf; |
| 73 |
unsigned mp3_sample_pos; |
| 74 |
HBE_STREAM mp3_stream; |
| 75 |
unsigned char *mp3_buf; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
static pj_status_t file_put_frame(pjmedia_port *this_port, |
| 80 |
const pjmedia_frame *frame); |
| 81 |
static pj_status_t file_get_frame(pjmedia_port *this_port, |
| 82 |
pjmedia_frame *frame); |
| 83 |
static pj_status_t file_on_destroy(pjmedia_port *this_port); |
| 84 |
|
| 85 |
|
| 86 |
#if defined(PJ_WIN32) || defined(_WIN32) || defined(WIN32) |
| 87 |
|
| 88 |
#include <windows.h> |
| 89 |
#define DLL_NAME PJ_T("LAME_ENC.DLL") |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static pj_status_t init_blade_dll(void) |
| 95 |
{ |
| 96 |
if (BladeDLL.refCount == 0) { |
| 97 |
#define GET_PROC(type, name) \ |
| 98 |
BladeDLL.name = (type)GetProcAddress(BladeDLL.hModule, PJ_T(#name)); \ |
| 99 |
if (BladeDLL.name == NULL) { \ |
| 100 |
PJ_LOG(1,(THIS_FILE, "Unable to find %s in %s", #name, DLL_NAME)); \ |
| 101 |
return PJ_RETURN_OS_ERROR(GetLastError()); \ |
| 102 |
} |
| 103 |
|
| 104 |
BE_VERSION beVersion; |
| 105 |
BladeDLL.hModule = (void*)LoadLibrary(DLL_NAME); |
| 106 |
if (BladeDLL.hModule == NULL) { |
| 107 |
pj_status_t status = PJ_RETURN_OS_ERROR(GetLastError()); |
| 108 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 109 |
|
| 110 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 111 |
PJ_LOG(1,(THIS_FILE, "Unable to load %s: %s", DLL_NAME, errmsg)); |
| 112 |
return status; |
| 113 |
} |
| 114 |
|
| 115 |
GET_PROC(BEINITSTREAM, beInitStream); |
| 116 |
GET_PROC(BEENCODECHUNK, beEncodeChunk); |
| 117 |
GET_PROC(BEDEINITSTREAM, beDeinitStream); |
| 118 |
GET_PROC(BECLOSESTREAM, beCloseStream); |
| 119 |
GET_PROC(BEVERSION, beVersion); |
| 120 |
GET_PROC(BEWRITEVBRHEADER, beWriteVBRHeader); |
| 121 |
GET_PROC(BEWRITEINFOTAG, beWriteInfoTag); |
| 122 |
|
| 123 |
#undef GET_PROC |
| 124 |
|
| 125 |
BladeDLL.beVersion(&beVersion); |
| 126 |
PJ_LOG(4,(THIS_FILE, "%s encoder v%d.%d loaded (%s)", DLL_NAME, |
| 127 |
beVersion.byMajorVersion, beVersion.byMinorVersion, |
| 128 |
beVersion.zHomepage)); |
| 129 |
} |
| 130 |
++BladeDLL.refCount; |
| 131 |
return PJ_SUCCESS; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void deinit_blade_dll() |
| 138 |
{ |
| 139 |
--BladeDLL.refCount; |
| 140 |
if (BladeDLL.refCount == 0 && BladeDLL.hModule) { |
| 141 |
FreeLibrary(BladeDLL.hModule); |
| 142 |
BladeDLL.hModule = NULL; |
| 143 |
PJ_LOG(4,(THIS_FILE, "%s unloaded", DLL_NAME)); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#else |
| 148 |
|
| 149 |
static pj_status_t init_blade_dll(void) |
| 150 |
{ |
| 151 |
PJ_LOG(1,(THIS_FILE, "Error: MP3 writer port only works on Windows for now")); |
| 152 |
return PJ_ENOTSUP; |
| 153 |
} |
| 154 |
|
| 155 |
static void deinit_blade_dll() |
| 156 |
{ |
| 157 |
} |
| 158 |
#endif |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static pj_status_t init_mp3_encoder(struct mp3_file_port *fport, |
| 166 |
pj_pool_t *pool) |
| 167 |
{ |
| 168 |
BE_CONFIG LConfig; |
| 169 |
unsigned long InSamples; |
| 170 |
unsigned long OutBuffSize; |
| 171 |
long MP3Err; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
pj_bzero(&LConfig, sizeof(BE_CONFIG)); |
| 177 |
LConfig.dwConfig = BE_CONFIG_LAME; |
| 178 |
LConfig.format.LHV1.dwStructVersion = 1; |
| 179 |
LConfig.format.LHV1.dwStructSize = sizeof(BE_CONFIG); |
| 180 |
LConfig.format.LHV1.dwSampleRate = PJMEDIA_PIA_SRATE(&fport->base.info); |
| 181 |
LConfig.format.LHV1.dwReSampleRate = 0; |
| 182 |
|
| 183 |
if (PJMEDIA_PIA_CCNT(&fport->base.info)==1) |
| 184 |
LConfig.format.LHV1.nMode = BE_MP3_MODE_MONO; |
| 185 |
else if (PJMEDIA_PIA_CCNT(&fport->base.info)==2) |
| 186 |
LConfig.format.LHV1.nMode = BE_MP3_MODE_STEREO; |
| 187 |
else |
| 188 |
return PJMEDIA_ENCCHANNEL; |
| 189 |
|
| 190 |
LConfig.format.LHV1.dwBitrate = fport->mp3_option.bit_rate / 1000; |
| 191 |
LConfig.format.LHV1.nPreset = LQP_NOPRESET; |
| 192 |
LConfig.format.LHV1.bCopyright = 0; |
| 193 |
LConfig.format.LHV1.bCRC = 1; |
| 194 |
LConfig.format.LHV1.bOriginal = 1; |
| 195 |
LConfig.format.LHV1.bPrivate = 0; |
| 196 |
|
| 197 |
if (!fport->mp3_option.vbr) { |
| 198 |
LConfig.format.LHV1.nVbrMethod = VBR_METHOD_NONE; |
| 199 |
LConfig.format.LHV1.bWriteVBRHeader = 0; |
| 200 |
LConfig.format.LHV1.bEnableVBR = 0; |
| 201 |
} else { |
| 202 |
LConfig.format.LHV1.nVbrMethod = VBR_METHOD_DEFAULT; |
| 203 |
LConfig.format.LHV1.bWriteVBRHeader = 1; |
| 204 |
LConfig.format.LHV1.dwVbrAbr_bps = fport->mp3_option.bit_rate; |
| 205 |
LConfig.format.LHV1.nVBRQuality = (pj_uint16_t) |
| 206 |
fport->mp3_option.quality; |
| 207 |
LConfig.format.LHV1.bEnableVBR = 1; |
| 208 |
} |
| 209 |
|
| 210 |
LConfig.format.LHV1.nQuality = (pj_uint16_t) |
| 211 |
(((0-fport->mp3_option.quality-1)<<8) | |
| 212 |
fport->mp3_option.quality); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
InSamples = 0; |
| 218 |
MP3Err = BladeDLL.beInitStream(&LConfig, &InSamples, &OutBuffSize, |
| 219 |
&fport->mp3_stream); |
| 220 |
if (MP3Err != BE_ERR_SUCCESSFUL) |
| 221 |
return PJMEDIA_ERROR; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
fport->mp3_samples_per_frame = (unsigned)InSamples; |
| 227 |
fport->mp3_sample_buf = pj_pool_alloc(pool, fport->mp3_samples_per_frame * 2); |
| 228 |
if (!fport->mp3_sample_buf) |
| 229 |
return PJ_ENOMEM; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
fport->mp3_buf = pj_pool_alloc(pool, (pj_size_t)OutBuffSize); |
| 235 |
if (fport->mp3_buf == NULL) |
| 236 |
return PJ_ENOMEM; |
| 237 |
|
| 238 |
|
| 239 |
return PJ_SUCCESS; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
PJ_DEF(pj_status_t) |
| 247 |
pjmedia_mp3_writer_port_create( pj_pool_t *pool, |
| 248 |
const char *filename, |
| 249 |
unsigned sampling_rate, |
| 250 |
unsigned channel_count, |
| 251 |
unsigned samples_per_frame, |
| 252 |
unsigned bits_per_sample, |
| 253 |
const pjmedia_mp3_encoder_option *param_option, |
| 254 |
pjmedia_port **p_port ) |
| 255 |
{ |
| 256 |
struct mp3_file_port *fport; |
| 257 |
pj_status_t status; |
| 258 |
|
| 259 |
status = init_blade_dll(); |
| 260 |
if (status != PJ_SUCCESS) |
| 261 |
return status; |
| 262 |
|
| 263 |
|
| 264 |
PJ_ASSERT_RETURN(pool && filename && p_port, PJ_EINVAL); |
| 265 |
|
| 266 |
|
| 267 |
PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL); |
| 268 |
|
| 269 |
|
| 270 |
fport = pj_pool_zalloc(pool, sizeof(struct mp3_file_port)); |
| 271 |
PJ_ASSERT_RETURN(fport != NULL, PJ_ENOMEM); |
| 272 |
|
| 273 |
|
| 274 |
pj_strdup2_with_null(pool, &fport->mp3_filename, filename); |
| 275 |
pjmedia_port_info_init(&fport->base.info, &fport->mp3_filename, SIGNATURE, |
| 276 |
sampling_rate, channel_count, bits_per_sample, |
| 277 |
samples_per_frame); |
| 278 |
|
| 279 |
fport->base.get_frame = &file_get_frame; |
| 280 |
fport->base.put_frame = &file_put_frame; |
| 281 |
fport->base.on_destroy = &file_on_destroy; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
status = pj_file_open(pool, filename, PJ_O_WRONLY | PJ_O_CLOEXEC, |
| 289 |
&fport->fd); |
| 290 |
if (status != PJ_SUCCESS) { |
| 291 |
deinit_blade_dll(); |
| 292 |
return status; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
if (param_option) { |
| 297 |
pj_memcpy(&fport->mp3_option, param_option, |
| 298 |
sizeof(pjmedia_mp3_encoder_option)); |
| 299 |
} else { |
| 300 |
pj_bzero(&fport->mp3_option, sizeof(pjmedia_mp3_encoder_option)); |
| 301 |
fport->mp3_option.vbr = PJ_TRUE; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
if (fport->mp3_option.bit_rate == 0 && !fport->mp3_option.vbr) |
| 306 |
fport->mp3_option.bit_rate = sampling_rate * channel_count; |
| 307 |
|
| 308 |
|
| 309 |
if (fport->mp3_option.quality == 0) |
| 310 |
fport->mp3_option.quality = 2; |
| 311 |
|
| 312 |
|
| 313 |
status = init_mp3_encoder(fport, pool); |
| 314 |
if (status != PJ_SUCCESS) { |
| 315 |
pj_file_close(fport->fd); |
| 316 |
deinit_blade_dll(); |
| 317 |
return status; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
*p_port = &fport->base; |
| 322 |
|
| 323 |
PJ_LOG(4,(THIS_FILE, |
| 324 |
"MP3 file writer '%.*s' created: samp.rate=%dKHz, " |
| 325 |
"bitrate=%dkbps%s, quality=%d", |
| 326 |
(int)fport->base.info.name.slen, |
| 327 |
fport->base.info.name.ptr, |
| 328 |
PJMEDIA_PIA_SRATE(&fport->base.info), |
| 329 |
fport->mp3_option.bit_rate/1000, |
| 330 |
(fport->mp3_option.vbr ? " (VBR)" : ""), |
| 331 |
fport->mp3_option.quality)); |
| 332 |
|
| 333 |
return PJ_SUCCESS; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
#if !DEPRECATED_FOR_TICKET_2251 |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
PJ_DEF(pj_status_t) |
| 342 |
pjmedia_mp3_writer_port_set_cb( pjmedia_port *port, |
| 343 |
pj_size_t pos, |
| 344 |
void *user_data, |
| 345 |
pj_status_t (*cb)(pjmedia_port *port, |
| 346 |
void *usr_data)) |
| 347 |
{ |
| 348 |
struct mp3_file_port *fport; |
| 349 |
|
| 350 |
|
| 351 |
PJ_ASSERT_RETURN(port && cb, PJ_EINVAL); |
| 352 |
|
| 353 |
|
| 354 |
PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP); |
| 355 |
|
| 356 |
PJ_LOG(1, (THIS_FILE, "pjmedia_mp3_writer_port_set_cb() is deprecated. " |
| 357 |
"Use pjmedia_mp3_writer_port_set_cb2() instead.")); |
| 358 |
|
| 359 |
fport = (struct mp3_file_port*) port; |
| 360 |
|
| 361 |
fport->cb_size = pos; |
| 362 |
fport->base.port_data.pdata = user_data; |
| 363 |
fport->cb = cb; |
| 364 |
|
| 365 |
return PJ_SUCCESS; |
| 366 |
|
| 367 |
} |
| 368 |
#endif |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
PJ_DEF(pj_status_t) |
| 375 |
pjmedia_mp3_writer_port_set_cb2(pjmedia_port *port, |
| 376 |
pj_size_t pos, |
| 377 |
void *user_data, |
| 378 |
void (*cb)(pjmedia_port *port, |
| 379 |
void *usr_data)) |
| 380 |
{ |
| 381 |
struct mp3_file_port *fport; |
| 382 |
|
| 383 |
|
| 384 |
PJ_ASSERT_RETURN(port && cb, PJ_EINVAL); |
| 385 |
|
| 386 |
|
| 387 |
PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP); |
| 388 |
|
| 389 |
fport = (struct mp3_file_port*) port; |
| 390 |
|
| 391 |
fport->cb_size = pos; |
| 392 |
fport->base.port_data.pdata = user_data; |
| 393 |
fport->cb2 = cb; |
| 394 |
fport->cb_called = PJ_FALSE; |
| 395 |
|
| 396 |
return PJ_SUCCESS; |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
static pj_status_t file_on_event(pjmedia_event *event, |
| 402 |
void *user_data) |
| 403 |
{ |
| 404 |
struct file_port *fport = (struct file_port*)user_data; |
| 405 |
|
| 406 |
if (event->type == PJMEDIA_EVENT_CALLBACK) { |
| 407 |
if (fport->cb2) |
| 408 |
(*fport->cb2)(&fport->base, fport->base.port_data.pdata); |
| 409 |
} |
| 410 |
|
| 411 |
return PJ_SUCCESS; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
static pj_status_t file_put_frame(pjmedia_port *this_port, |
| 420 |
const pjmedia_frame *frame) |
| 421 |
{ |
| 422 |
struct mp3_file_port *fport = (struct mp3_file_port *)this_port; |
| 423 |
unsigned long MP3Err; |
| 424 |
pj_ssize_t bytes; |
| 425 |
pj_status_t status; |
| 426 |
unsigned long WriteSize; |
| 427 |
|
| 428 |
|
| 429 |
if (frame->type == PJMEDIA_FRAME_TYPE_NONE || frame->size == 0) { |
| 430 |
unsigned samples_left = PJMEDIA_PIA_SPF(&fport->base.info); |
| 431 |
unsigned samples_copied = 0; |
| 432 |
|
| 433 |
|
| 434 |
if (fport->silence_duration >= PJMEDIA_PIA_SRATE(&fport->base.info)) |
| 435 |
return PJ_SUCCESS; |
| 436 |
|
| 437 |
while (samples_left) { |
| 438 |
unsigned samples_needed = fport->mp3_samples_per_frame - |
| 439 |
fport->mp3_sample_pos; |
| 440 |
if (samples_needed > samples_left) |
| 441 |
samples_needed = samples_left; |
| 442 |
|
| 443 |
pjmedia_zero_samples(fport->mp3_sample_buf + fport->mp3_sample_pos, |
| 444 |
samples_needed); |
| 445 |
fport->mp3_sample_pos += samples_needed; |
| 446 |
samples_left -= samples_needed; |
| 447 |
samples_copied += samples_needed; |
| 448 |
|
| 449 |
|
| 450 |
if (fport->mp3_sample_pos == fport->mp3_samples_per_frame) { |
| 451 |
|
| 452 |
|
| 453 |
fport->mp3_sample_pos = 0; |
| 454 |
|
| 455 |
|
| 456 |
MP3Err = BladeDLL.beEncodeChunk(fport->mp3_stream, |
| 457 |
fport->mp3_samples_per_frame, |
| 458 |
fport->mp3_sample_buf, |
| 459 |
fport->mp3_buf, |
| 460 |
&WriteSize); |
| 461 |
if (MP3Err != BE_ERR_SUCCESSFUL) |
| 462 |
return PJMEDIA_ERROR; |
| 463 |
|
| 464 |
|
| 465 |
bytes = WriteSize; |
| 466 |
status = pj_file_write(fport->fd, fport->mp3_buf, &bytes); |
| 467 |
if (status != PJ_SUCCESS) |
| 468 |
return status; |
| 469 |
|
| 470 |
|
| 471 |
fport->total += bytes; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
fport->silence_duration += PJMEDIA_PIA_SPF(&fport->base.info); |
| 476 |
|
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
else if (fport->mp3_samples_per_frame != |
| 482 |
PJMEDIA_PIA_SPF(&fport->base.info)) |
| 483 |
{ |
| 484 |
unsigned samples_left = frame->size / 2; |
| 485 |
unsigned samples_copied = 0; |
| 486 |
const pj_int16_t *src_samples = frame->buf; |
| 487 |
|
| 488 |
fport->silence_duration = 0; |
| 489 |
|
| 490 |
while (samples_left) { |
| 491 |
unsigned samples_needed = fport->mp3_samples_per_frame - |
| 492 |
fport->mp3_sample_pos; |
| 493 |
if (samples_needed > samples_left) |
| 494 |
samples_needed = samples_left; |
| 495 |
|
| 496 |
pjmedia_copy_samples(fport->mp3_sample_buf + fport->mp3_sample_pos, |
| 497 |
src_samples + samples_copied, |
| 498 |
samples_needed); |
| 499 |
fport->mp3_sample_pos += samples_needed; |
| 500 |
samples_left -= samples_needed; |
| 501 |
samples_copied += samples_needed; |
| 502 |
|
| 503 |
|
| 504 |
if (fport->mp3_sample_pos == fport->mp3_samples_per_frame) { |
| 505 |
|
| 506 |
|
| 507 |
fport->mp3_sample_pos = 0; |
| 508 |
|
| 509 |
|
| 510 |
MP3Err = BladeDLL.beEncodeChunk(fport->mp3_stream, |
| 511 |
fport->mp3_samples_per_frame, |
| 512 |
fport->mp3_sample_buf, |
| 513 |
fport->mp3_buf, |
| 514 |
&WriteSize); |
| 515 |
if (MP3Err != BE_ERR_SUCCESSFUL) |
| 516 |
return PJMEDIA_ERROR; |
| 517 |
|
| 518 |
|
| 519 |
bytes = WriteSize; |
| 520 |
status = pj_file_write(fport->fd, fport->mp3_buf, &bytes); |
| 521 |
if (status != PJ_SUCCESS) |
| 522 |
return status; |
| 523 |
|
| 524 |
|
| 525 |
fport->total += bytes; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
} else { |
| 530 |
|
| 531 |
fport->silence_duration = 0; |
| 532 |
|
| 533 |
|
| 534 |
MP3Err = BladeDLL.beEncodeChunk(fport->mp3_stream, |
| 535 |
fport->mp3_samples_per_frame, |
| 536 |
frame->buf, |
| 537 |
fport->mp3_buf, |
| 538 |
&WriteSize); |
| 539 |
if (MP3Err != BE_ERR_SUCCESSFUL) |
| 540 |
return PJMEDIA_ERROR; |
| 541 |
|
| 542 |
|
| 543 |
bytes = WriteSize; |
| 544 |
status = pj_file_write(fport->fd, fport->mp3_buf, &bytes); |
| 545 |
if (status != PJ_SUCCESS) |
| 546 |
return status; |
| 547 |
|
| 548 |
|
| 549 |
fport->total += bytes; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
if (fport->total >= fport->cb_size) { |
| 554 |
if (fport->cb2) { |
| 555 |
if (!fport->subscribed) { |
| 556 |
pj_status_t status; |
| 557 |
|
| 558 |
status = pjmedia_event_subscribe(NULL, &file_on_event, |
| 559 |
fport, fport); |
| 560 |
fport->subscribed = (status == PJ_SUCCESS)? PJ_TRUE: |
| 561 |
PJ_FALSE; |
| 562 |
} |
| 563 |
|
| 564 |
if (fport->subscribed && !fport->cb_called) { |
| 565 |
pjmedia_event event; |
| 566 |
|
| 567 |
|
| 568 |
fport->cb_called = PJ_TRUE; |
| 569 |
|
| 570 |
pjmedia_event_init(&event, PJMEDIA_EVENT_CALLBACK, |
| 571 |
NULL, fport); |
| 572 |
pjmedia_event_publish(NULL, fport, &event, |
| 573 |
PJMEDIA_EVENT_PUBLISH_POST_EVENT); |
| 574 |
} |
| 575 |
} else if (fport->cb) { |
| 576 |
pj_status_t (*cb)(pjmedia_port*, void*); |
| 577 |
pj_status_t status; |
| 578 |
|
| 579 |
cb = fport->cb; |
| 580 |
fport->cb = NULL; |
| 581 |
|
| 582 |
status = (*cb)(this_port, this_port->port_data.pdata); |
| 583 |
return status; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
return PJ_SUCCESS; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
static pj_status_t file_get_frame(pjmedia_port *this_port, |
| 594 |
pjmedia_frame *frame) |
| 595 |
{ |
| 596 |
PJ_UNUSED_ARG(this_port); |
| 597 |
PJ_UNUSED_ARG(frame); |
| 598 |
return PJ_EINVALIDOP; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
static pj_status_t file_on_destroy(pjmedia_port *this_port) |
| 606 |
{ |
| 607 |
struct mp3_file_port *fport = (struct mp3_file_port*)this_port; |
| 608 |
pj_status_t status; |
| 609 |
unsigned long WriteSize; |
| 610 |
unsigned long MP3Err; |
| 611 |
|
| 612 |
if (fport->subscribed) { |
| 613 |
pjmedia_event_unsubscribe(NULL, &file_on_event, fport, fport); |
| 614 |
fport->subscribed = PJ_FALSE; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
MP3Err = BladeDLL.beDeinitStream(fport->mp3_stream, fport->mp3_buf, |
| 619 |
&WriteSize); |
| 620 |
if (MP3Err == BE_ERR_SUCCESSFUL) { |
| 621 |
pj_ssize_t bytes = WriteSize; |
| 622 |
status = pj_file_write(fport->fd, fport->mp3_buf, &bytes); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
status = pj_file_close(fport->fd); |
| 627 |
|
| 628 |
|
| 629 |
if (fport->mp3_option.vbr) { |
| 630 |
MP3Err = BladeDLL.beWriteVBRHeader(fport->mp3_filename.ptr); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
deinit_blade_dll(); |
| 636 |
|
| 637 |
|
| 638 |
return PJ_SUCCESS; |
| 639 |
} |
| 640 |
|