| 5 |
| #include <stdlib.h> |
| 6 | #include <time.h> |
| 7 | #include <opusfile.h> |
| 8 | #include "utils.h" |
| 9 |
| 10 | typedef struct { |
| 11 | int version; |
| 12 | int channels; /* Number of channels: 1..255 */ |
| 13 | int preskip; |
| 14 | ogg_uint32_t input_sample_rate; |
| 15 | int gain; /* in dB S7.8 should be zero whenever possible */ |
| 16 | int channel_mapping; |
| 17 | /* The rest is only used if channel_mapping != 0 */ |
| 18 | int nb_streams; |
| 19 | int nb_coupled; |
| 20 | unsigned char stream_map[255]; |
| 21 | } OpusHeader; |
| 22 |
| 23 | typedef struct { |
| 24 | unsigned char *data; |
| 25 | int maxlen; |
| 26 | int pos; |
| 27 | } Packet; |
| 28 |
| 29 | typedef struct { |
| 30 | const unsigned char *data; |
| 31 | int maxlen; |
| 32 | int pos; |
| 33 | } ROPacket; |
| 34 |
| 35 | typedef struct { |
| 36 | void *readdata; |
| 37 | opus_int64 total_samples_per_channel; |
| 38 | int rawmode; |
| 39 | int channels; |
| 40 | long rate; |
| 41 | int gain; |
| 42 | int samplesize; |
| 43 | int endianness; |
| 44 | char *infilename; |
| 45 | int ignorelength; |
| 46 | int skip; |
| 47 | int extraout; |
| 48 | char *comments; |
| 49 | int comments_length; |
| 50 | int copy_comments; |
| 51 | } oe_enc_opt; |
| 52 |
| 53 | static int write_uint32(Packet *p, ogg_uint32_t val) { |
| 54 | if (p->pos > p->maxlen - 4) { |
| 55 | return 0; |
| 56 | } |
| 57 | p->data[p->pos ] = (val ) & 0xFF; |
| 58 | p->data[p->pos+1] = (val>> 8) & 0xFF; |
| 59 | p->data[p->pos+2] = (val>>16) & 0xFF; |
| 60 | p->data[p->pos+3] = (val>>24) & 0xFF; |
| 61 | p->pos += 4; |
| 62 | return 1; |
| 63 | } |
| 64 |
| 65 | static int write_uint16(Packet *p, ogg_uint16_t val) { |
| 66 | if (p->pos > p->maxlen-2) { |
| 67 | return 0; |
| 68 | } |
| 69 | p->data[p->pos ] = (val ) & 0xFF; |
| 70 | p->data[p->pos+1] = (val>> 8) & 0xFF; |
| 71 | p->pos += 2; |
| 72 | return 1; |
| 73 | } |
| 74 |
| 75 | static int write_chars(Packet *p, const unsigned char *str, int nb_chars) |
| 76 | { |
| 77 | int i; |
| 78 | if (p->pos>p->maxlen-nb_chars) |
| 79 | return 0; |
| 80 | for (i=0;i<nb_chars;i++) |
| 81 | p->data[p->pos++] = str[i]; |
| 82 | return 1; |
| 83 | } |
| 84 |
| 85 | static int read_uint32(ROPacket *p, ogg_uint32_t *val) |
| 86 | { |
| 87 | if (p->pos>p->maxlen-4) |
| 88 | return 0; |
| 89 | *val = (ogg_uint32_t)p->data[p->pos ]; |
| 90 | *val |= (ogg_uint32_t)p->data[p->pos+1]<< 8; |
| 91 | *val |= (ogg_uint32_t)p->data[p->pos+2]<<16; |
| 92 | *val |= (ogg_uint32_t)p->data[p->pos+3]<<24; |
| 93 | p->pos += 4; |
| 94 | return 1; |
| 95 | } |
| 96 |
| 97 | static int read_uint16(ROPacket *p, ogg_uint16_t *val) |
| 98 | { |
| 99 | if (p->pos>p->maxlen-2) |
| 100 | return 0; |
| 101 | *val = (ogg_uint16_t)p->data[p->pos ]; |
| 102 | *val |= (ogg_uint16_t)p->data[p->pos+1]<<8; |
| 103 | p->pos += 2; |
| 104 | return 1; |
| 105 | } |
| 106 |
| 107 | static int read_chars(ROPacket *p, unsigned char *str, int nb_chars) |
| 108 | { |
| 109 | int i; |
| 110 | if (p->pos>p->maxlen-nb_chars) |
| 111 | return 0; |
| 112 | for (i=0;i<nb_chars;i++) |
| 113 | str[i] = p->data[p->pos++]; |
| 114 | return 1; |
| 115 | } |
| 116 |
| 117 | int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len) { |
| 118 | int i; |
| 119 | Packet p; |
| 120 | unsigned char ch; |
| 121 |
| 122 | p.data = packet; |
| 123 | p.maxlen = len; |
| 124 | p.pos = 0; |
| 125 | if (len < 19) { |
| 126 | return 0; |
| 127 | } |
| 128 | if (!write_chars(&p, (const unsigned char *)"OpusHead", 8)) { |
| 129 | return 0; |
| 130 | } |
| 131 |
| 132 | ch = 1; |
| 133 | if (!write_chars(&p, &ch, 1)) { |
| 134 | return 0; |
| 135 | } |
| 136 |
| 137 | ch = h->channels; |
| 138 | if (!write_chars(&p, &ch, 1)) { |
| 139 | return 0; |
| 140 | } |
| 141 |
| 142 | if (!write_uint16(&p, h->preskip)) { |
| 143 | return 0; |
| 144 | } |
| 145 |
| 146 | if (!write_uint32(&p, h->input_sample_rate)) { |
| 147 | return 0; |
| 148 | } |
| 149 |
| 150 | if (!write_uint16(&p, h->gain)) { |
| 151 | return 0; |
| 152 | } |
| 153 |
| 154 | ch = h->channel_mapping; |
| 155 | if (!write_chars(&p, &ch, 1)) { |
| 156 | return 0; |
| 157 | } |
| 158 |
| 159 | if (h->channel_mapping != 0) { |
| 160 | ch = h->nb_streams; |
| 161 | if (!write_chars(&p, &ch, 1)) { |
| 162 | return 0; |
| 163 | } |
| 164 |
| 165 | ch = h->nb_coupled; |
| 166 | if (!write_chars(&p, &ch, 1)) { |
| 167 | return 0; |
| 168 | } |
| 169 |
| 170 | /* Multi-stream support */ |
| 171 | for (i = 0; i < h->channels; i++) { |
| 172 | if (!write_chars(&p, &h->stream_map[i], 1)) { |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 |
| 178 | return p.pos; |
| 179 | } |
| 180 |
| 181 | #define writeint(buf, base, val) do { buf[base + 3] = ((val) >> 24) & 0xff; \ |
| 182 | buf[base + 2]=((val) >> 16) & 0xff; \ |
| 183 | buf[base + 1]=((val) >> 8) & 0xff; \ |
| 184 | buf[base] = (val) & 0xff; \ |
| 185 | } while(0) |
| 186 |
| 187 | static void comment_init(char **comments, int *length, const char *vendor_string) { |
| 188 | // The 'vendor' field should be the actual encoding library used |
| 189 | int vendor_length = strlen(vendor_string); |
| 190 | int user_comment_list_length = 0; |
| 191 | int len = 8 + 4 + vendor_length + 4; |
| 192 | char *p = (char *)malloc(len); |
| 193 | memcpy(p, "OpusTags", 8); |
| 194 | writeint(p, 8, vendor_length); |
| 195 | memcpy(p + 12, vendor_string, vendor_length); |
| 196 | writeint(p, 12 + vendor_length, user_comment_list_length); |
| 197 | *length = len; |
| 198 | *comments = p; |
| 199 | } |
| 200 |
| 201 | static void comment_pad(char **comments, int* length, int amount) { |
| 202 | if (amount > 0) { |
| 203 | char *p = *comments; |
| 204 | // Make sure there is at least amount worth of padding free, and round up to the maximum that fits in the current ogg segments |
| 205 | int newlen = (*length + amount + 255) / 255 * 255 - 1; |
| 206 | p = realloc(p, newlen); |
| 207 | for (int i = *length; i < newlen; i++) { |
| 208 | p[i] = 0; |
| 209 | } |
| 210 | *comments = p; |
| 211 | *length = newlen; |
| 212 | } |
| 213 | } |
| 214 |
| 215 | static int writeOggPage(ogg_page *page, FILE *os) { |
| 216 | int written = fwrite(page->header, sizeof(unsigned char), page->header_len, os); |
| 217 | written += fwrite(page->body, sizeof(unsigned char), page->body_len, os); |
| 218 | return written; |
| 219 | } |
| 220 |
| 221 | const opus_int32 bitrate = 16000; |
| 222 | const opus_int32 rate = 16000; |
| 223 | const opus_int32 frame_size = 960; |
| 224 | const int with_cvbr = 1; |
| 225 | const int max_ogg_delay = 0; |
| 226 | const int comment_padding = 512; |
| 227 |
| 228 | opus_int32 coding_rate = 16000; |
| 229 | ogg_int32_t _packetId; |
| 230 | OpusEncoder *_encoder = 0; |
| 231 | uint8_t *_packet = 0; |
| 232 | ogg_stream_state os; |
| 233 | FILE *_fileOs = 0; |
| 234 | oe_enc_opt inopt; |
| 235 | OpusHeader header; |
| 236 | opus_int32 min_bytes; |
| 237 | int max_frame_bytes; |
| 238 | ogg_packet op; |
| 239 | ogg_page og; |
| 240 | opus_int64 bytes_written; |
| 241 | opus_int64 pages_out; |
| 242 | opus_int64 total_samples; |
| 243 | ogg_int64_t enc_granulepos; |
| 244 | ogg_int64_t last_granulepos; |
| 245 | int size_segments; |
| 246 | int last_segments; |
| 247 |
| 248 | void cleanupRecorder() { |
| 249 |
| 250 | ogg_stream_flush(&os, &og); |
| 251 |
| 252 | if (_encoder) { |
| 253 | opus_encoder_destroy(_encoder); |
| 254 | _encoder = 0; |
| 255 | } |
| 256 |
| 257 | ogg_stream_clear(&os); |
| 258 |
| 259 | if (_packet) { |
| 260 | free(_packet); |
| 261 | _packet = 0; |
| 262 | } |
| 263 |
| 264 | if (_fileOs) { |
| 265 | fclose(_fileOs); |
| 266 | _fileOs = 0; |
| 267 | } |
| 268 |
| 269 | _packetId = -1; |
| 270 | bytes_written = 0; |
| 271 | pages_out = 0; |
| 272 | total_samples = 0; |
| 273 | enc_granulepos = 0; |
| 274 | size_segments = 0; |
| 275 | last_segments = 0; |
| 276 | last_granulepos = 0; |
| 277 | memset(&os, 0, sizeof(ogg_stream_state)); |
| 278 | memset(&inopt, 0, sizeof(oe_enc_opt)); |
| 279 | memset(&header, 0, sizeof(OpusHeader)); |
| 280 | memset(&op, 0, sizeof(ogg_packet)); |
| 281 | memset(&og, 0, sizeof(ogg_page)); |
| 282 | } |
| 283 |
| 284 | int initRecorder(const char *path) { |
| 285 | cleanupRecorder(); |
| 286 |
| 287 | if (!path) { |
| 288 | return 0; |
| 289 | } |
| 290 |
| 291 | _fileOs = fopen(path, "wb"); |
| 292 | if (!_fileOs) { |
| 293 | return 0; |
| 294 | } |
| 295 |
| 296 | inopt.rate = rate; |
| 297 | inopt.gain = 0; |
| 298 | inopt.endianness = 0; |
| 299 | inopt.copy_comments = 0; |
| 300 | inopt.rawmode = 1; |
| 301 | inopt.ignorelength = 1; |
| 302 | inopt.samplesize = 16; |
| 303 | inopt.channels = 1; |
| 304 | inopt.skip = 0; |
| 305 |
| 306 | comment_init(&inopt.comments, &inopt.comments_length, opus_get_version_string()); |
| 307 |
| 308 | if (rate > 24000) { |
| 309 | coding_rate = 48000; |
| 310 | } else if (rate > 16000) { |
| 311 | coding_rate = 24000; |
| 312 | } else if (rate > 12000) { |
| 313 | coding_rate = 16000; |
| 314 | } else if (rate > 8000) { |
| 315 | coding_rate = 12000; |
| 316 | } else { |
| 317 | coding_rate = 8000; |
| 318 | } |
| 319 |
| 320 | if (rate != coding_rate) { |
| 321 | LOGE("Invalid rate"); |
| 322 | return 0; |
| 323 | } |
| 324 |
| 325 | header.channels = 1; |
| 326 | header.channel_mapping = 0; |
| 327 | header.input_sample_rate = rate; |
| 328 | header.gain = inopt.gain; |
| 329 | header.nb_streams = 1; |
| 330 |
| 331 | int result = OPUS_OK; |
| 332 | _encoder = opus_encoder_create(coding_rate, 1, OPUS_APPLICATION_AUDIO, &result); |
| 333 | if (result != OPUS_OK) { |
| 334 | LOGE("Error cannot create encoder: %s", opus_strerror(result)); |
| 335 | return 0; |
| 336 | } |
| 337 |
| 338 | min_bytes = max_frame_bytes = (1275 * 3 + 7) * header.nb_streams; |
| 339 | _packet = malloc(max_frame_bytes); |
| 340 |
| 341 | result = opus_encoder_ctl(_encoder, OPUS_SET_BITRATE(bitrate)); |
| 342 | if (result != OPUS_OK) { |
| 343 | LOGE("Error OPUS_SET_BITRATE returned: %s", opus_strerror(result)); |
| 344 | return 0; |
| 345 | } |
| 346 |
| 347 | #ifdef OPUS_SET_LSB_DEPTH |
| 348 | result = opus_encoder_ctl(_encoder, OPUS_SET_LSB_DEPTH(max(8, min(24, inopt.samplesize)))); |
| 349 | if (result != OPUS_OK) { |
| 350 | LOGE("Warning OPUS_SET_LSB_DEPTH returned: %s", opus_strerror(result)); |
| 351 | } |
| 352 | #endif |
| 353 |
| 354 | opus_int32 lookahead; |
| 355 | result = opus_encoder_ctl(_encoder, OPUS_GET_LOOKAHEAD(&lookahead)); |
| 356 | if (result != OPUS_OK) { |
| 357 | LOGE("Error OPUS_GET_LOOKAHEAD returned: %s", opus_strerror(result)); |
| 358 | return 0; |
| 359 | } |
| 360 |
| 361 | inopt.skip += lookahead; |
| 362 | header.preskip = (int)(inopt.skip * (48000.0 / coding_rate)); |
| 363 | inopt.extraout = (int)(header.preskip * (rate / 48000.0)); |
| 364 |
| 365 | if (ogg_stream_init(&os, rand()) == -1) { |
| 366 | LOGE("Error: stream init failed"); |
| 367 | return 0; |
| 368 | } |
| 369 |
| 370 | unsigned char header_data[100]; |
| 371 | int packet_size = opus_header_to_packet(&header, header_data, 100); |
| 372 | op.packet = header_data; |
| 373 | op.bytes = packet_size; |
| 374 | op.b_o_s = 1; |
| 375 | op.e_o_s = 0; |
| 376 | op.granulepos = 0; |
| 377 | op.packetno = 0; |
| 378 | ogg_stream_packetin(&os, &op); |
| 379 |
| 380 | while ((result = ogg_stream_flush(&os, &og))) { |
| 381 | if (!result) { |
| 382 | break; |
| 383 | } |
| 384 |
| 385 | int pageBytesWritten = writeOggPage(&og, _fileOs); |
| 386 | if (pageBytesWritten != og.header_len + og.body_len) { |
| 387 | LOGE("Error: failed writing header to output stream"); |
| 388 | return 0; |
| 389 | } |
| 390 | bytes_written += pageBytesWritten; |
| 391 | pages_out++; |
| 392 | } |
| 393 |
| 394 | comment_pad(&inopt.comments, &inopt.comments_length, comment_padding); |
| 395 | op.packet = (unsigned char *)inopt.comments; |
| 396 | op.bytes = inopt.comments_length; |
| 397 | op.b_o_s = 0; |
| 398 | op.e_o_s = 0; |
| 399 | op.granulepos = 0; |
| 400 | op.packetno = 1; |
| 401 | ogg_stream_packetin(&os, &op); |
| 402 |
| 403 | while ((result = ogg_stream_flush(&os, &og))) { |
| 404 | if (result == 0) { |
| 405 | break; |
| 406 | } |
| 407 |
| 408 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 409 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 410 | LOGE("Error: failed writing header to output stream"); |
| 411 | return 0; |
| 412 | } |
| 413 |
| 414 | bytes_written += writtenPageBytes; |
| 415 | pages_out++; |
| 416 | } |
| 417 |
| 418 | free(inopt.comments); |
| 419 |
| 420 | return 1; |
| 421 | } |
| 422 |
| 423 | int writeFrame(uint8_t *framePcmBytes, unsigned int frameByteCount) { |
| 424 | int cur_frame_size = frame_size; |
| 425 | _packetId++; |
| 426 |
| 427 | opus_int32 nb_samples = frameByteCount / 2; |
| 428 | total_samples += nb_samples; |
| 429 | if (nb_samples < frame_size) { |
| 430 | op.e_o_s = 1; |
| 431 | } else { |
| 432 | op.e_o_s = 0; |
| 433 | } |
| 434 |
| 435 | int nbBytes = 0; |
| 436 |
| 437 | if (nb_samples != 0) { |
| 438 | uint8_t *paddedFrameBytes = framePcmBytes; |
| 439 | int freePaddedFrameBytes = 0; |
| 440 |
| 441 | if (nb_samples < cur_frame_size) { |
| 442 | paddedFrameBytes = malloc(cur_frame_size * 2); |
| 443 | freePaddedFrameBytes = 1; |
| 444 | memcpy(paddedFrameBytes, framePcmBytes, frameByteCount); |
| 445 | memset(paddedFrameBytes + nb_samples * 2, 0, cur_frame_size * 2 - nb_samples * 2); |
| 446 | } |
| 447 |
| 448 | nbBytes = opus_encode(_encoder, (opus_int16 *)paddedFrameBytes, cur_frame_size, _packet, max_frame_bytes / 10); |
| 449 | if (freePaddedFrameBytes) { |
| 450 | free(paddedFrameBytes); |
| 451 | paddedFrameBytes = NULL; |
| 452 | } |
| 453 |
| 454 | if (nbBytes < 0) { |
| 455 | LOGE("Encoding failed: %s. Aborting.", opus_strerror(nbBytes)); |
| 456 | return 0; |
| 457 | } |
| 458 |
| 459 | enc_granulepos += cur_frame_size * 48000 / coding_rate; |
| 460 | size_segments = (nbBytes + 255) / 255; |
| 461 | min_bytes = min(nbBytes, min_bytes); |
| 462 | } |
| 463 |
| 464 | while ((((size_segments <= 255) && (last_segments + size_segments > 255)) || (enc_granulepos - last_granulepos > max_ogg_delay)) && ogg_stream_flush_fill(&os, &og, 255 * 255)) { |
| 465 | if (ogg_page_packets(&og) != 0) { |
| 466 | last_granulepos = ogg_page_granulepos(&og); |
| 467 | } |
| 468 |
| 469 | last_segments -= og.header[26]; |
| 470 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 471 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 472 | LOGE("Error: failed writing data to output stream"); |
| 473 | return 0; |
| 474 | } |
| 475 | bytes_written += writtenPageBytes; |
| 476 | pages_out++; |
| 477 | } |
| 478 |
| 479 | op.packet = (unsigned char *)_packet; |
| 480 | op.bytes = nbBytes; |
| 481 | op.b_o_s = 0; |
| 482 | op.granulepos = enc_granulepos; |
| 483 | if (op.e_o_s) { |
| 484 | op.granulepos = ((total_samples * 48000 + rate - 1) / rate) + header.preskip; |
| 485 | } |
| 486 | op.packetno = 2 + _packetId; |
| 487 | ogg_stream_packetin(&os, &op); |
| 488 | last_segments += size_segments; |
| 489 |
| 490 | while ((op.e_o_s || (enc_granulepos + (frame_size * 48000 / coding_rate) - last_granulepos > max_ogg_delay) || (last_segments >= 255)) ? ogg_stream_flush_fill(&os, &og, 255 * 255) : ogg_stream_pageout_fill(&os, &og, 255 * 255)) { |
| 491 | if (ogg_page_packets(&og) != 0) { |
| 492 | last_granulepos = ogg_page_granulepos(&og); |
| 493 | } |
| 494 | last_segments -= og.header[26]; |
| 495 | int writtenPageBytes = writeOggPage(&og, _fileOs); |
| 496 | if (writtenPageBytes != og.header_len + og.body_len) { |
| 497 | LOGE("Error: failed writing data to output stream"); |
| 498 | return 0; |
| 499 | } |
| 500 | bytes_written += writtenPageBytes; |
| 501 | pages_out++; |
| 502 | } |
| 503 |
| 504 | return 1; |
| 505 | } |
| 506 |
| 507 | JNIEXPORT int Java_org_telegram_android_MediaController_startRecord(JNIEnv *env, jclass class, jstring path) { |
| 508 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 509 |
| 510 | int result = initRecorder(pathStr); |
| 511 |
| 512 | if (pathStr != 0) { |
| 513 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 514 | } |
| 515 |
| 516 | return result; |
| 517 | } |
| 518 |
| 519 | JNIEXPORT int Java_org_telegram_android_MediaController_writeFrame(JNIEnv *env, jclass class, jobject frame, jint len) { |
| 520 | jbyte *frameBytes = (*env)->GetDirectBufferAddress(env, frame); |
| 521 | return writeFrame(frameBytes, len); |
| 522 | } |
| 523 |
| 524 | JNIEXPORT void Java_org_telegram_android_MediaController_stopRecord(JNIEnv *env, jclass class) { |
| 525 | cleanupRecorder(); |
| 526 | } |
| 527 |
| 528 | //player |
| 529 | OggOpusFile *_opusFile; |
| 530 | int _isSeekable = 0; |
| 531 | int64_t _totalPcmDuration = 0; |
| 532 | int64_t _currentPcmOffset = 0; |
| 533 | int _finished = 0; |
| 534 | static const int playerBuffersCount = 3; |
| 535 | static const int playerSampleRate = 48000; |
| 536 |
| 537 | void cleanupPlayer() { |
| 538 | if (_opusFile) { |
| 539 | op_free(_opusFile); |
| 540 | _opusFile = 0; |
| 541 | } |
| 542 | _isSeekable = 0; |
| 543 | _totalPcmDuration = 0; |
| 544 | _currentPcmOffset = 0; |
| 545 | _finished = 0; |
| 546 | } |
| 547 |
| 548 | int seekPlayer(float position) { |
| 549 | if (!_opusFile || !_isSeekable || position < 0) { |
| 550 | return 0; |
| 551 | } |
| 552 | int result = op_pcm_seek(_opusFile, (ogg_int64_t)(position * _totalPcmDuration)); |
| 553 | if (result != OPUS_OK) { |
| 554 | LOGE("op_pcm_seek failed: %d", result); |
| 555 | } |
| 556 | ogg_int64_t pcmPosition = op_pcm_tell(_opusFile); |
| 557 | _currentPcmOffset = pcmPosition; |
| 558 | return result == OPUS_OK; |
| 559 | } |
| 560 |
| 561 | int initPlayer(const char *path) { |
| 562 | cleanupPlayer(); |
| 563 |
| 564 | int openError = OPUS_OK; |
| 565 | _opusFile = op_open_file(path, &openError); |
| 566 | if (!_opusFile || openError != OPUS_OK) { |
| 567 | LOGE("op_open_file failed: %d", openError); |
| 568 | cleanupPlayer(); |
| 569 | return 0; |
| 570 | } |
| 571 |
| 572 | _isSeekable = op_seekable(_opusFile); |
| 573 | _totalPcmDuration = op_pcm_total(_opusFile, -1); |
| 574 |
| 575 | return 1; |
| 576 | } |
| 577 |
| 578 | void fillBuffer(uint8_t *buffer, int capacity, int *args) { |
| 579 | if (_opusFile) { |
| 580 | args[1] = max(0, op_pcm_tell(_opusFile)); |
| 581 |
| 582 | if (_finished) { |
| 583 | args[0] = 0; |
| 584 | args[1] = 0; |
| 585 | args[2] = 1; |
| 586 | return; |
| 587 | } else { |
| 588 | int writtenOutputBytes = 0; |
| 589 | int endOfFileReached = 0; |
| 590 |
| 591 | while (writtenOutputBytes < capacity) { |
| 592 | int readSamples = op_read(_opusFile, (opus_int16 *)(buffer + writtenOutputBytes), (capacity - writtenOutputBytes) / 2, NULL); |
| 593 |
| 594 | if (readSamples > 0) { |
| 595 | writtenOutputBytes += readSamples * 2; |
| 596 | } else { |
| 597 | if (readSamples < 0) { |
| 598 | LOGE("op_read failed: %d", readSamples); |
| 599 | } |
| 600 | endOfFileReached = 1; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 |
| 605 | args[0] = writtenOutputBytes; |
| 606 |
| 607 | if (endOfFileReached || args[1] + args[0] == _totalPcmDuration) { |
| 608 | _finished = 1; |
| 609 | args[2] = 1; |
| 610 | } else { |
| 611 | args[2] = 0; |
| 612 | } |
| 613 | } |
| 614 | } else { |
| 615 | memset(buffer, 0, capacity); |
| 616 | args[0] = capacity; |
| 617 | args[1] = _totalPcmDuration; |
| 618 | } |
| 619 | } |
| 620 |
| 621 | JNIEXPORT jlong Java_org_telegram_android_MediaController_getTotalPcmDuration(JNIEnv *env, jclass class) { |
| 622 | return _totalPcmDuration; |
| 623 | } |
| 624 |
| 625 | JNIEXPORT void Java_org_telegram_android_MediaController_readOpusFile(JNIEnv *env, jclass class, jobject buffer, jint capacity, jintArray args) { |
| 626 | jint *argsArr = (*env)->GetIntArrayElements(env, args, 0); |
| 627 | jbyte *bufferBytes = (*env)->GetDirectBufferAddress(env, buffer); |
| 628 | fillBuffer(bufferBytes, capacity, argsArr); |
| 629 | (*env)->ReleaseIntArrayElements(env, args, argsArr, 0); |
| 630 | } |
| 631 |
| 632 | JNIEXPORT int Java_org_telegram_android_MediaController_seekOpusFile(JNIEnv *env, jclass class, jfloat position) { |
| 633 | return seekPlayer(position); |
| 634 | } |
| 635 |
| 636 | JNIEXPORT int Java_org_telegram_android_MediaController_openOpusFile(JNIEnv *env, jclass class, jstring path) { |
| 637 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 638 |
| 639 | int result = initPlayer(pathStr); |
| 640 |
| 641 | if (pathStr != 0) { |
| 642 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 643 | } |
| 644 |
| 645 | return result; |
| 646 | } |
| 647 |
| 648 | JNIEXPORT void Java_org_telegram_android_MediaController_closeOpusFile(JNIEnv *env, jclass class) { |
| 649 | cleanupPlayer(); |
| 650 | } |
| 651 |
| 652 | JNIEXPORT int Java_org_telegram_android_MediaController_isOpusFile(JNIEnv *env, jclass class, jstring path) { |
| 653 | const char *pathStr = (*env)->GetStringUTFChars(env, path, 0); |
| 654 |
| 655 | int result = 0; |
| 656 |
| 657 | int error = OPUS_OK; |
| 658 | OggOpusFile *file = op_test_file(pathStr, &error); |
| 659 | if (file != NULL) { |
| 660 | int error = op_test_open(file); |
| 661 | op_free(file); |
| 662 |
| 663 | result = error == OPUS_OK; |
| 664 | } |
| 665 |
| 666 | if (pathStr != 0) { |
| 667 | (*env)->ReleaseStringUTFChars(env, path, pathStr); |
| 668 | } |
| 669 |
| 670 | return result; |
| 671 | } |
| 672 |