/** Init native decoder by initializing libmpg123 and Java callbacks. */ jint Java_com_denisigo_netradioplayer_Decoder_initNative(JNIEnv* env, jobject thiz){ int ret; // Init mpg123 decoder mpg123_init(); mpg123_handle* hnd = mpg123_new(NULL, &ret); if(hnd == NULL) { LOGE("Unable to create mpg123 handle: %s\n", mpg123_plain_strerror(ret)); return 0; } // Set mpg123 to feed mode since we're providing data ourseves ret = mpg123_open_feed(hnd); if(ret != MPG123_OK) { LOGE("Unable open feed: %s\n", mpg123_plain_strerror(ret)); return 0; } // Init Java callback jclass thisClass = (*env)->GetObjectClass(env, thiz); method_onNewFormatCallback = (*env)->GetMethodID(env, thisClass, "onNewFormatCallback", "(III)V"); return (jint) hnd; }
void input_mpg123::open(std::string path) { int err; int encoding; handle = mpg123_new(NULL, &err); if (handle == NULL) { throw input_error(mpg123_plain_strerror(err)); } if ((err = mpg123_open(handle, path.c_str())) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } if ((err = mpg123_getformat(handle, &_rate, &_channels, &encoding)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } read_metadata(); /* force 16 bit format */ encoding = MPG123_ENC_SIGNED_16; mpg123_format_none(handle); mpg123_format(handle, _rate, _channels, encoding); double seconds_left; if ((err = mpg123_position(handle, 0, 0, NULL, NULL, NULL, &seconds_left)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } this->_encoding = encoding; this->_length = seconds_left; }
JNIEXPORT void JNICALL Java_com_axelby_mp3decoders_MPG123_feed (JNIEnv *env, jclass c, jlong handle, jbyteArray in_buffer, jint in_size) { MP3File *mp3 = (MP3File*)handle; mpg123_handle *mh = mp3->handle; jboolean isCopy; jbyte* b = (*env)->GetByteArrayElements(env, in_buffer, &isCopy); int err = mpg123_feed(mh, b, in_size); if (err != MPG123_OK) __android_log_print(ANDROID_LOG_INFO, "podax-jni", "mpg123_feed error: %s", mpg123_plain_strerror(err)); (*env)->ReleaseByteArrayElements(env, in_buffer, b, JNI_ABORT); if (mp3->rate == 0) { off_t frame_offset; unsigned char* audio; size_t bytes_done; err = mpg123_decode_frame(mh, &frame_offset, &audio, &bytes_done); if (err == MPG123_NEW_FORMAT) { int encoding; err = mpg123_getformat(mh, &mp3->rate, &mp3->channels, &encoding); if (err != MPG123_NEED_MORE && err != MPG123_OK) { printerr("mpg123_getformat", err); return; } mp3->samples_per_frame = mpg123_spf(mh); mp3->secs_per_frame = mpg123_tpf(mh); } if (err != MPG123_OK && err != MPG123_NEED_MORE) __android_log_print(ANDROID_LOG_INFO, "podax-jni", "cannot get rate: %s", mpg123_plain_strerror(err)); } }
void input_mpg123::seek(double pos) { int err; off_t off = mpg123_timeframe(handle, pos); if (off < 0) { throw input_error(mpg123_plain_strerror(off)); } if ((err = mpg123_seek(handle, off, SEEK_SET)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } }
mpg123_handle *our_mpg123_new(const char *decoder, int *error) { mpg123_handle *mh; const char *arch = "auto"; int x64 = 0; int rc = 0; const char *err = NULL; if (*globals.decoder || globals.outscale || globals.vol) { if (*globals.decoder) { arch = globals.decoder; } if ((mh = mpg123_new(arch, &rc))) { if (rc) { err = mpg123_plain_strerror(rc); } if (globals.outscale) { mpg123_param(mh, MPG123_OUTSCALE, globals.outscale, 0); } if (globals.vol) { mpg123_volume(mh, globals.vol); } } } else { #ifdef WIN32 x64++; #else if (sizeof(void *) == 4) { arch = "i586"; } else { x64++; } #endif if ((mh = mpg123_new(arch, &rc))) { if (rc) { err = mpg123_plain_strerror(rc); } if (x64) { mpg123_param(mh, MPG123_OUTSCALE, 8192, 0); } } } if (err) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error allocating mpg123 handle! %s\n", err); } return mh; }
static void* stream_thread_main(void *args) { int status; thread_args_t *thread_args; fprintf(stderr, "start streaming thread\n"); /* initialize global state variables used in this thread */ g_busy = 1; g_stop = 0; g_paused = 0; thread_args = (thread_args_t*)args; /* go into loop which reads from connection and parses it to dev */ status = stream_loop(thread_args->mh, thread_args->stream_socket); fprintf(stderr, "close stream_thread\n"); fprintf(stderr, "%s\n", mpg123_plain_strerror(status)); close(thread_args->stream_socket); write(g_cmd_sock, REMOTE_CMD_DONE, strlen(REMOTE_CMD_DONE)); close_cmd_sock(); free(thread_args); g_busy = 0; return NULL; }
MP3Decoder::MP3Decoder(const char* filename) { f = fopen(filename,"rb"); fseek(f,0,SEEK_END); size_t size = ftell(f); cbuf = (uchar*)malloc(size*24); size_t buffer_size = 0; size_t done = 0; rate = 0; channels = 0; encoding = 0; int err = MPG123_OK; err = mpg123_init(); if(err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL) { QMessageBox::critical(0,QString("MP3 decoding error!"),QString("Basic setup goes wrong: ")+QString(mpg123_plain_strerror(err))); cleanup(mh); return ; } /* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */ if( mpg123_open(mh, filename) != MPG123_OK /* Peek into track and get first output format. */ || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { QMessageBox::critical(0,QString("MP3 decoding error!"),QString("Error decoding mp3 file: ")+QString(mpg123_strerror(mh))); cleanup(mh); return ; } if(encoding != MPG123_ENC_SIGNED_16 && encoding != MPG123_ENC_FLOAT_32) { /* Signed 16 is the default output format anyways; it would actually by only different if we forced it. So this check is here just for this explanation. */ cleanup(mh); QMessageBox::critical(0,QString("MP3 decoding error!"),"Bad encoding: 0x"+QString::number(encoding,16)); return; } /* Ensure that this output format will not change (it could, when we allow it). */ mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); buffer_size = mpg123_outblock(mh); cdone=0; buffer = (unsigned char*)malloc( buffer_size ); do { err = mpg123_read( mh, buffer, buffer_size, &done ); if(done<1)qDebug()<<"Error while reading mp3: "<<mpg123_errcode(mh); else memcpy(cbuf+cdone,buffer,done); cdone+=done; } while (err==MPG123_OK); cbuf[cdone-1]=0; if(err != MPG123_DONE){ fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n", err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) ); QMessageBox::critical(0,QString("MP3 decoding error!"),QString("MP3 Decoding ended prematurely: %1").arg(err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) )); return; } }
VALUE rb_mpg123_new(VALUE klass, VALUE filename) { int err = MPG123_OK; mpg123_handle *mh; VALUE mpg123; long rate; int channels, encoding; Check_Type(filename, T_STRING); if ((mh = mpg123_new(NULL, &err)) == NULL) { rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err)); } mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); if (mpg123_open(mh, (char*) RSTRING_PTR(filename)) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) { rb_raise(rb_eStandardError, "%s", mpg123_strerror(mh)); } if (encoding != MPG123_ENC_FLOAT_32) { rb_raise(rb_eStandardError, "bad encoding"); } mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); VALUE new_mpg123 = Data_Wrap_Struct(rb_cMpg123, 0, cleanup, mh); rb_iv_set(new_mpg123, "@file", filename); return new_mpg123; }
VALUE rb_mpg123_read(VALUE self, VALUE _size) { int size = FIX2INT(_size); float *buffer = malloc(size * sizeof(float)); VALUE result = rb_ary_new2(size); mpg123_handle *mh = NULL; size_t done = 0; int i; int err = MPG123_OK; Data_Get_Struct(self, mpg123_handle, mh); err = mpg123_read(mh, (unsigned char *) buffer, size * sizeof(float), &done); if (err == MPG123_OK || err == MPG123_DONE) { for (i = 0; i < size; i++) { rb_ary_store(result, i, rb_float_new(buffer[i])); } free(buffer); return result; } else { free(buffer); rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err)); } }
void Init_mpg123(void) { int err = MPG123_OK; err = mpg123_init(); if (err != MPG123_OK) { rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err)); } rb_cMpg123 = rb_define_class("Mpg123", rb_cObject); rb_define_singleton_method(rb_cMpg123, "new", rb_mpg123_new, 1); rb_define_method(rb_cMpg123, "file", rb_mpg123_file, 0); rb_define_method(rb_cMpg123, "close", rb_mpg123_close, 0); rb_define_method(rb_cMpg123, "read", rb_mpg123_read, 1); rb_define_method(rb_cMpg123, "length", rb_mpg123_length, 0); rb_define_method(rb_cMpg123, "spf", rb_mpg123_spf, 0); rb_define_method(rb_cMpg123, "tpf", rb_mpg123_tpf, 0); rb_define_method(rb_cMpg123, "tell", rb_mpg123_tell, 0); rb_define_method(rb_cMpg123, "tellframe", rb_mpg123_tellframe, 0); rb_define_method(rb_cMpg123, "seek", rb_mpg123_seek, 1); rb_define_method(rb_cMpg123, "seek_frame", rb_mpg123_seek_frame, 1); rb_define_method(rb_cMpg123, "timeframe", rb_mpg123_timeframe, 1); }
static void gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) { int error; GstMpg123AudioDec *mpg123_decoder; hard = hard; GST_LOG_OBJECT (dec, "Flushing decoder"); mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); g_assert (mpg123_decoder->handle != NULL); /* Flush by reopening the feed */ mpg123_close (mpg123_decoder->handle); error = mpg123_open_feed (mpg123_decoder->handle); if (G_UNLIKELY (error != MPG123_OK)) { GST_ELEMENT_ERROR (dec, LIBRARY, INIT, (NULL), ("Error while reopening mpg123 feed: %s", mpg123_plain_strerror (error))); mpg123_close (mpg123_decoder->handle); mpg123_delete (mpg123_decoder->handle); mpg123_decoder->handle = NULL; } mpg123_decoder->has_next_audioinfo = FALSE; /* opening/closing feeds do not affect the format defined by the * mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(), * and since the up/downstream caps are not expected to change here, no * mpg123_format() calls are done */ }
void mpg123__initialize() { int err = mpg123_init(); if (err != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } }
Mp3::Mp3() : myHandle (nullptr), myBufferSize(0), myBuffer (nullptr) { int err = MPG123_OK; if ((err = mpg123_init()) != MPG123_OK) { std::cerr << mpg123_plain_strerror(err) << std::endl; return; } myHandle = mpg123_new(NULL, &err); if (!myHandle) { std::cerr << "Unable to create mpg123 handle: " << mpg123_plain_strerror(err) << std::endl; return; } }
JNIEXPORT jlong JNICALL Java_com_axelby_podax_player_MPG123_openFile (JNIEnv *env, jclass c, jstring filename) { int err = MPG123_OK; mpg123_handle *mh = mpg123_new(NULL, &err); if (err == MPG123_OK && mh != NULL) { MP3File* mp3 = mp3file_init(mh); const char* fileString = (*env)->GetStringUTFChars(env, filename, NULL); err = mpg123_open(mh, fileString); (*env)->ReleaseStringUTFChars(env, filename, fileString); if (err == MPG123_OK) { int encoding; if (mpg123_getformat(mh, &mp3->rate, &mp3->channels, &encoding) == MPG123_OK) { if(encoding == MPG123_ENC_SIGNED_16) { // Signed 16 is the default output format anyway; // it would actually by only different if we forced it. // So this check is here just for this explanation. // Ensure that this output format will not change // (it could, when we allow it). mpg123_format_none(mh); mpg123_format(mh, mp3->rate, mp3->channels, encoding); mp3->buffer_size = mpg123_outblock(mh); mp3->buffer = (unsigned char*)malloc(mp3->buffer_size); mp3->num_samples = mpg123_length(mh); mp3->samples_per_frame = mpg123_spf(mh); mp3->secs_per_frame = mpg123_tpf(mh); if (mp3->num_samples == MPG123_ERR || mp3->samples_per_frame < 0) mp3->num_frames = 0; else mp3->num_frames = mp3->num_samples / mp3->samples_per_frame; if (mp3->num_samples == MPG123_ERR || mp3->samples_per_frame < 0 || mp3->secs_per_frame < 0) mp3->duration = 0; else mp3->duration = mp3->num_samples / mp3->samples_per_frame * mp3->secs_per_frame; return (jlong)mp3; } } } mp3file_delete(mp3); } else { __android_log_write(ANDROID_LOG_INFO, "podax-jni", mpg123_plain_strerror(err)); } return 0; }
int rst_audio_init(void) { int err; err = mpg123_init(); if (err != MPG123_OK) { warning("rst: mpg123_init: %s\n", mpg123_plain_strerror(err)); return ENODEV; } return ausrc_register(&ausrc, baresip_ausrcl(), "rst", alloc_handler); }
Mpg123Decoder::Mpg123Decoder() : handle(nullptr, mpg123_delete) { music_type = "mp3"; err = mpg123_init(); if (err != MPG123_OK) { error_message = "mpg123: " + std::string(mpg123_plain_strerror(err)); return; } handle.reset(mpg123_new(nullptr, &err)); mpg123_replace_reader_handle(handle.get(), custom_read, custom_seek, custom_close); if (!handle) { error_message = "mpg123: " + std::string(mpg123_plain_strerror(err)); return; } init = true; }
qint64 DecoderMPG123::read(unsigned char *data, qint64 size) { size_t done = 0; int err = mpg123_read(m_handle, data, size, &done); if(err != MPG123_DONE && err != MPG123_OK) { qWarning("DecoderMPG123: decoder error: %s", mpg123_plain_strerror(err)); return -1; } mpg123_info(m_handle, &m_frame_info); return done; }
JNIEXPORT jlong JNICALL Java_com_axelby_mp3decoders_MPG123_openStream (JNIEnv *env, jclass c) { // init mpg123 handle int err = MPG123_OK; mpg123_handle *mh = mpg123_new(NULL, &err); if (err != MPG123_OK) { __android_log_print(ANDROID_LOG_INFO, "podax-jni", "mpg123_new error: %s", mpg123_plain_strerror(err)); return 0; } // set handle up as stream err = mpg123_open_feed(mh); if (err != MPG123_OK) { __android_log_print(ANDROID_LOG_INFO, "podax-jni", "mpg123_open_feed error: %s", mpg123_plain_strerror(err)); return 0; } MP3File* stream = mp3file_init(mh); return (jlong)stream; }
static inline jlong wrapped_Java_com_badlogic_gdx_audio_io_Mpg123Decoder_openFile (JNIEnv* env, jobject object, jstring obj_filename, char* filename) { //@line:111 mpg123_handle *mh = NULL; int channels = 0, encoding = 0; long rate = 0; int err = MPG123_OK; err = mpg123_init(); if( err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL || mpg123_open(mh, filename) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { fprintf( stderr, "Trouble with mpg123: %s\n", mh==NULL ? mpg123_plain_strerror(err) : mpg123_strerror(mh) ); cleanup(mh); return 0; } if(encoding != MPG123_ENC_SIGNED_16) { // Signed 16 is the default output format anyways; it would actually by only different if we forced it. // So this check is here just for this explanation. cleanup(mh); return 0; } // Ensure that this output format will not change (it could, when we allow it). mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); size_t buffer_size = mpg123_outblock( mh ); unsigned char* buffer = (unsigned char*)malloc(buffer_size); size_t done = 0; int samples = 0; Mp3File* mp3 = new Mp3File(); mp3->handle = mh; mp3->channels = channels; mp3->rate = rate; mp3->buffer = buffer; mp3->buffer_size = buffer_size; int length = mpg123_length( mh ); if( length == MPG123_ERR ) mp3->length = 0; else mp3->length = length / rate; return (jlong)mp3; }
VALUE rb_mpg123_new(VALUE klass, VALUE filename, VALUE decideRate) { printf("Made it: 1\n"); int err = MPG123_OK; mpg123_handle *mh; printf("mh: "); printf("%d", mh); printf("\n"); VALUE mpg123; long rate; rate = decideRate; printf("rate: "); printf("%d", rate); printf("\n"); int channels, encoding; printf("Made it: 2\n"); printf("rate: "); printf("%d", rate); printf("\n"); Check_Type(filename, T_STRING); printf("Made it: 3\n"); printf("rate: "); printf("%d", rate); printf("\n"); if ((mh = mpg123_new(NULL, &err)) == NULL) { rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err)); } printf("Made it: 4\n"); printf("rate: "); printf("%d", rate); printf("\n"); mpg123_param(mh, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); printf("mpg123_open is: "); printf("%d", mpg123_open(mh, (char*) RSTRING_PTR(filename))); printf("\n"); printf("mh: "); printf("%d", mh); printf("\n"); printf("rate: "); printf("%d", rate); printf("\n"); printf("channels: "); printf("%d", channels); printf("\n"); printf("encoding: "); printf("%d", encoding); printf("\n"); printf("mpg123_getformat is: "); printf("%d", mpg123_getformat(mh, &rate, &channels, &encoding)); printf("\n"); printf("MPG123_OK is: "); printf("%d", MPG123_OK); printf("\n"); if (mpg123_open(mh, (char*) RSTRING_PTR(filename)) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) { rb_raise(rb_eStandardError, "%s", mpg123_strerror(mh)); } printf("Made it: 5"); if (encoding != MPG123_ENC_FLOAT_32) { rb_raise(rb_eStandardError, "bad encoding"); } printf("Made it: 6"); mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); printf("Made it: 7"); return Data_Wrap_Struct(rb_cMpg123, 0, cleanup, mh); }
pcm_meta input_mpg123::tell() { int err; double seconds; if ((err = mpg123_position(handle, 0, 0, NULL, NULL, &seconds, NULL)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } pcm_meta meta; meta.current = seconds; meta.length = seconds + 100; return meta; }
bool Mpg123Decoder::Open(FILE* file) { if (!init) { return false; } finished = false; err = mpg123_open_handle(handle.get(), file); if (err != MPG123_OK) { error_message = "mpg123: " + std::string(mpg123_plain_strerror(err)); return false; } return true; }
static bool is_mp3(FILE *fp, struct file_type **ft) { mpg123_init(); int err; mpg123_handle *mh = mpg123_new(NULL, &err); if (mh == NULL) { fprintf(stderr, "Could not create mpg123 handle: %s\n", mpg123_plain_strerror(err)); return false; } long pos = ftell(fp); if (mpg123_open_fd(mh, fileno(fp)) != MPG123_OK) { fseek(fp, pos, SEEK_SET); return false; } mpg123_scan(mh); struct mpg123_frameinfo fi; mpg123_info(mh, &fi); if (mpg123_format(mh, fi.rate, MPG123_STEREO, MPG123_ENC_SIGNED_16) != MPG123_OK) { fseek(fp, pos, SEEK_SET); return false; } off_t length = mpg123_length(mh); struct mp3_format *fmt = calloc(sizeof(*fmt), 1); *ft = &fmt->ft; fmt->mh = mh; fmt->tpf = mpg123_tpf(mh); fmt->ft.channels = 2; fmt->ft.position = 0; fmt->ft.length = (length + (fi.rate / 2)) / fi.rate; fmt->ft.sample_rate = fi.rate; fmt->ft.sample_size = 2; fmt->ft.sample_type = ST_SIGNED_INTEGER_LE; fmt->ft.bitrate = (double)fi.abr_rate; return true; }
stream *stream_new() { int err; stream *stream = malloc(sizeof(struct stream)); memset(stream, 0, sizeof(struct stream)); if ((stream->mpg123 = mpg123_new(NULL, &err)) == NULL) { fprintf(stderr, "%s", mpg123_plain_strerror(err)); return NULL; } mpg123_param(stream->mpg123, MPG123_VERBOSE, 0, 0); mpg123_param(stream->mpg123, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); return stream; }
static int UpdateAudioFormat( decoder_t *p_dec ) { int i_err; decoder_sys_t *p_sys = p_dec->p_sys; struct mpg123_frameinfo frame_info; /* Get details about the stream */ i_err = mpg123_info( p_sys->p_handle, &frame_info ); if( i_err != MPG123_OK ) { msg_Err( p_dec, "mpg123_info failed: %s", mpg123_plain_strerror( i_err ) ); return VLC_EGENERIC; } p_dec->fmt_out.i_bitrate = frame_info.bitrate * 1000; switch( frame_info.mode ) { case MPG123_M_DUAL: p_dec->fmt_out.audio.i_chan_mode = AOUT_CHANMODE_DUALMONO; /* fall through */ case MPG123_M_STEREO: case MPG123_M_JOINT: p_dec->fmt_out.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT; break; case MPG123_M_MONO: p_dec->fmt_out.audio.i_physical_channels = AOUT_CHAN_CENTER; break; default: return VLC_EGENERIC; } aout_FormatPrepare( &p_dec->fmt_out.audio ); /* Date management */ if( p_dec->fmt_out.audio.i_rate != frame_info.rate ) { p_dec->fmt_out.audio.i_rate = frame_info.rate; date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 ); date_Set( &p_sys->end_date, 0 ); } return decoder_UpdateAudioFormat( p_dec ); }
VALUE rb_portaudio_write_from_mpg(VALUE self, VALUE mpg) { int err; size_t done = 0; Portaudio *portaudio; mpg123_handle *mh = NULL; Data_Get_Struct(self, Portaudio, portaudio); Data_Get_Struct(mpg, mpg123_handle, mh); err = mpg123_read(mh, (unsigned char *) portaudio->buffer, portaudio->size * sizeof(float), &done); portaudio->rms = rms(portaudio->buffer, portaudio->size); switch (err) { case MPG123_OK: return ID2SYM(rb_intern("ok")); case MPG123_DONE: return ID2SYM(rb_intern("done")); } rb_raise(rb_eStandardError, "%s", mpg123_plain_strerror(err)); }
static gboolean gst_mpg123_audio_dec_start (GstAudioDecoder * dec) { GstMpg123AudioDec *mpg123_decoder; int error; mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); error = 0; mpg123_decoder->handle = mpg123_new (NULL, &error); mpg123_decoder->has_next_audioinfo = FALSE; mpg123_decoder->frame_offset = 0; /* Initially, the mpg123 handle comes with a set of default formats supported. This clears this set. This is necessary, since only one format shall be supported (see set_format for more). */ mpg123_format_none (mpg123_decoder->handle); mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0); /* Built-in mpg123 support for gapless decoding is disabled for now, since it does not work well with seeking */ mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_SEEKBUFFER, 0); /* Tells mpg123 to use a small read-ahead buffer for better MPEG sync; essential for MP3 radio streams */ mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); /* Sets the resync limit to the end of the stream (e.g. don't give up prematurely) */ /* Open in feed mode (= encoded data is fed manually into the handle). */ error = mpg123_open_feed (mpg123_decoder->handle); if (G_UNLIKELY (error != MPG123_OK)) { GstElement *element = GST_ELEMENT (dec); GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("Error opening mpg123 feed: %s", mpg123_plain_strerror (error))); mpg123_close (mpg123_decoder->handle); mpg123_delete (mpg123_decoder->handle); mpg123_decoder->handle = NULL; return FALSE; } GST_DEBUG_OBJECT (dec, "mpg123 decoder started"); return TRUE; }
static void gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) { int error; GstMpg123AudioDec *mpg123_decoder; hard = hard; GST_DEBUG_OBJECT (dec, "Flushing decoder"); mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); if (G_UNLIKELY (mpg123_decoder->handle == NULL)) { GstElement *element = GST_ELEMENT (dec); GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("mpg123 handle is NULL")); return; } /* Flush by reopening the feed */ mpg123_close (mpg123_decoder->handle); error = mpg123_open_feed (mpg123_decoder->handle); if (G_UNLIKELY (error != MPG123_OK)) { GstElement *element = GST_ELEMENT (dec); GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("Error reopening mpg123 feed: %s", mpg123_plain_strerror (error))); mpg123_close (mpg123_decoder->handle); mpg123_delete (mpg123_decoder->handle); mpg123_decoder->handle = NULL; } mpg123_decoder->has_next_audioinfo = FALSE; /* opening/closing feeds do not affect the format defined by the mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(), and since the up/downstream caps are not expected to change here, no mpg123_format() calls are done */ }
inline void MP3Decoder::openFile(const char *filename) { mpg123_handle *mh = NULL; int channels = 0, encoding = 0; long rate = 0; int err = MPG123_OK; err = mpg123_init(); if( err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL || mpg123_open(mh, filename) != MPG123_OK || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK ) { printf( "Trouble with mpg123: %s with file: %s\n", mh==NULL ? mpg123_plain_strerror(err) : mpg123_strerror(mh), filename); this->cleanup(mh); return; } // Ensure that this output format will not change (it could, when we allow it). mpg123_format_none(mh); mpg123_format(mh, rate, channels, encoding); size_t buffer_size = mpg123_length(mh) * channels;//mpg123_outblock( mh ); printf("buffer size: %ld\n", buffer_size); unsigned char* buffer = (unsigned char*)malloc(buffer_size); size_t done = 0; int samples = 0; mp3File = MP3File(); mp3File.handle = mh; mp3File.channels = channels; mp3File.rate = rate; mp3File.buffer = buffer; mp3File.buffer_size = buffer_size; int length = mpg123_length( mh ); if( length == MPG123_ERR ) mp3File.length = 0; else mp3File.length = length / rate; }
static inline int decode(struct ausrc_st *st) { int err, ch, encoding; struct mbuf *mb; long srate; mb = mbuf_alloc(4096); if (!mb) return ENOMEM; err = mpg123_read(st->mp3, mb->buf, mb->size, &mb->end); switch (err) { case MPG123_NEW_FORMAT: mpg123_getformat(st->mp3, &srate, &ch, &encoding); info("rst: new format: %i hz, %i ch, encoding 0x%04x\n", srate, ch, encoding); /*@fallthrough@*/ case MPG123_OK: case MPG123_NEED_MORE: if (mb->end == 0) break; aubuf_append(st->aubuf, mb); break; default: warning("rst: mpg123_read error: %s\n", mpg123_plain_strerror(err)); break; } mem_deref(mb); return err; }