@@ -43,14 +43,12 @@ MediaPlayer::MediaPlayer()
4343 mLeftVolume = mRightVolume = 1.0 ;
4444 mVideoWidth = mVideoHeight = 0 ;
4545 mVideoQueue = new PacketQueue ();
46- mAudioQueue = new PacketQueue ();
4746 sPlayer = this ;
4847}
4948
5049MediaPlayer::~MediaPlayer ()
5150{
5251 free (mVideoQueue );
53- free (mAudioQueue );
5452 if (mListener != NULL ) {
5553 free (mListener );
5654 }
@@ -208,6 +206,11 @@ status_t MediaPlayer::suspend() {
208206 __android_log_print (ANDROID_LOG_INFO, TAG, " suspend" );
209207
210208 mCurrentState = MEDIA_PLAYER_STOPPED;
209+ if (mDecoderAudio != NULL )
210+ {
211+ mDecoderAudio ->stop ();
212+ }
213+
211214 if (pthread_join (mPlayerThread , NULL ) != 0 ) {
212215 __android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel player thread" );
213216 }
@@ -271,16 +274,6 @@ status_t MediaPlayer::processVideo(AVPacket *packet, AVFrame *pFrame)
271274 return INVALID_OPERATION;
272275}
273276
274- status_t MediaPlayer::processAudio (AVPacket *packet, int16_t *samples, int samples_size)
275- {
276- int size = samples_size;
277- int len = avcodec_decode_audio3 (mFFmpegStorage .audio .codec_ctx , samples, &size, packet);
278- if (Output::AudioDriver_write (samples, size) <= 0 ) {
279- return INVALID_OPERATION;
280- }
281- return NO_ERROR;
282- }
283-
284277bool MediaPlayer::shouldCancel (PacketQueue* queue)
285278{
286279 return (mCurrentState == MEDIA_PLAYER_STATE_ERROR || mCurrentState == MEDIA_PLAYER_STOPPED ||
@@ -356,105 +349,32 @@ void MediaPlayer::decodeVideo(void* ptr)
356349 av_free (pFrameRGB);
357350}
358351
359- void MediaPlayer::decodeAudio (void * ptr)
360- {
361- AVPacket pPacket;
362- int16_t * pAudioSamples;
363- timeval pTime;
364- int frames = 0 ;
365- double t1 = -1 ;
366- double t2 = -1 ;
367- bool run = true ;
368-
369- pAudioSamples = (int16_t *) av_malloc (AVCODEC_MAX_AUDIO_FRAME_SIZE);
370-
371- int streamType = MUSIC;
372- int sampleRate = mFFmpegStorage .audio .codec_ctx ->sample_rate ;
373- int format = PCM_16_BIT;
374- int channels = (mFFmpegStorage .audio .codec_ctx ->channels == 2 ) ? CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO;
375- if (Output::AudioDriver_set (streamType,
376- sampleRate,
377- format,
378- channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
379- goto end;
380- }
381- if (Output::AudioDriver_start () != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
382- goto end;
383- }
384-
385- __android_log_print (ANDROID_LOG_INFO, TAG, " decoding audio" );
386-
387- while (run)
388- {
389- /*
390- gettimeofday(&pTime, NULL);
391- t2=pTime.tv_sec+(pTime.tv_usec/1000000.0);
392- if(t1 == -1 || t2 > t1 + 1)
393- {
394- __android_log_print(ANDROID_LOG_ERROR, TAG, "Audio frame rate: %ifps", frames);
395- t1=t2;
396- frames = 0;
397- }
398- frames++;
399- */
400- if (mCurrentState == MEDIA_PLAYER_PAUSED) {
401- usleep (50 );
402- continue ;
403- }
404- if (shouldCancel (mAudioQueue )) {
405- run = false ;
406- continue ;
407- }
408- if (mAudioQueue ->get (&pPacket, true ) < 0 )
409- {
410- mCurrentState = MEDIA_PLAYER_STATE_ERROR;
411- }
412- if (processAudio (&pPacket, pAudioSamples, AVCODEC_MAX_AUDIO_FRAME_SIZE) != NO_ERROR)
413- {
414- mCurrentState = MEDIA_PLAYER_STATE_ERROR;
415- }
416- // Free the packet that was allocated by av_read_frame
417- av_free_packet (&pPacket);
418- }
419-
420- end:
421- __android_log_print (ANDROID_LOG_INFO, TAG, " decoding audio ended" );
422-
423- Output::AudioDriver_unregister ();
424-
425- // Free audio samples buffer
426- av_free (pAudioSamples);
427- }
428-
429352void MediaPlayer::decodeMovie (void * ptr)
430353{
431354 AVPacket pPacket;
432355 char err[256 ];
433356
434357 pthread_create (&mVideoThread , NULL , startVideoDecoding, NULL );
435- pthread_create (&mAudioThread , NULL , startAudioDecoding, NULL );
436-
437- /*
438- DecoderAudioConfig cfg;
439- cfg.streamType = MUSIC;
440- cfg.sampleRate = mFFmpegStorage.audio.codec_ctx->sample_rate;
441- cfg.format = PCM_16_BIT;
442- cfg.channels = (mFFmpegStorage.audio.codec_ctx->channels == 2) ? CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO;
443- mDecoderAudio = new DecoderAudio(mFFmpegStorage.audio.codec_ctx, &cfg);
444- if(!mDecoderAudio->startAsync(err))
445- {
446- __android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't start audio decoder: %s", err);
447- return;
448- }
449- */
358+
359+ DecoderAudioConfig cfg;
360+ cfg.streamType = MUSIC;
361+ cfg.sampleRate = mFFmpegStorage .audio .codec_ctx ->sample_rate ;
362+ cfg.format = PCM_16_BIT;
363+ cfg.channels = (mFFmpegStorage .audio .codec_ctx ->channels == 2 ) ? CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO;
364+ mDecoderAudio = new DecoderAudio (mFFmpegStorage .audio .codec_ctx , &cfg);
365+ if (!mDecoderAudio ->startAsync (err))
366+ {
367+ __android_log_print (ANDROID_LOG_INFO, TAG, " Couldn't start audio decoder: %s" , err);
368+ return ;
369+ }
450370
451371 mCurrentState = MEDIA_PLAYER_STARTED;
452372 __android_log_print (ANDROID_LOG_INFO, TAG, " playing %ix%i" , mVideoWidth , mVideoHeight );
453373 while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&
454374 mCurrentState != MEDIA_PLAYER_STATE_ERROR)
455375 {
456376 if (mVideoQueue ->size () > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
457- mAudioQueue -> size () > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
377+ mDecoderAudio -> packets () > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
458378 usleep (200 );
459379 continue ;
460380 }
@@ -469,7 +389,7 @@ void MediaPlayer::decodeMovie(void* ptr)
469389 mVideoQueue ->put (&pPacket);
470390 }
471391 else if (pPacket.stream_index == mFFmpegStorage .audio .stream ) {
472- mAudioQueue -> put (&pPacket);
392+ mDecoderAudio -> enqueue (&pPacket);
473393 }
474394 else {
475395 // Free the packet that was allocated by av_read_frame
@@ -484,17 +404,11 @@ void MediaPlayer::decodeMovie(void* ptr)
484404 __android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel video thread: %i" , ret);
485405 }
486406
487- __android_log_print (ANDROID_LOG_ERROR, TAG, " waiting on audio thread" );
488- if ((ret = pthread_join (mAudioThread , NULL )) != 0 ) {
489- __android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel audio thread: %i" , ret);
490- }
491- /*
492407 __android_log_print (ANDROID_LOG_ERROR, TAG, " waiting on audio thread" );
493- if((ret = mDecoderAudio->wait()) != 0) {
408+ if ((ret = mDecoderAudio ->wait ()) != 0 ) {
494409 __android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel audio thread: %i" , ret);
495410 }
496- */
497-
411+
498412 if (mCurrentState == MEDIA_PLAYER_STATE_ERROR) {
499413 __android_log_print (ANDROID_LOG_INFO, TAG, " playing err" );
500414 }
@@ -508,12 +422,6 @@ void* MediaPlayer::startVideoDecoding(void* ptr)
508422 sPlayer ->decodeVideo (ptr);
509423}
510424
511- void * MediaPlayer::startAudioDecoding (void * ptr)
512- {
513- __android_log_print (ANDROID_LOG_INFO, TAG, " starting audio thread" );
514- sPlayer ->decodeAudio (ptr);
515- }
516-
517425void * MediaPlayer::startPlayer (void * ptr)
518426{
519427 __android_log_print (ANDROID_LOG_INFO, TAG, " starting main player thread" );
0 commit comments