@@ -43,14 +43,12 @@ MediaPlayer::MediaPlayer()
43
43
mLeftVolume = mRightVolume = 1.0 ;
44
44
mVideoWidth = mVideoHeight = 0 ;
45
45
mVideoQueue = new PacketQueue ();
46
- mAudioQueue = new PacketQueue ();
47
46
sPlayer = this ;
48
47
}
49
48
50
49
MediaPlayer::~MediaPlayer ()
51
50
{
52
51
free (mVideoQueue );
53
- free (mAudioQueue );
54
52
if (mListener != NULL ) {
55
53
free (mListener );
56
54
}
@@ -208,6 +206,11 @@ status_t MediaPlayer::suspend() {
208
206
__android_log_print (ANDROID_LOG_INFO, TAG, " suspend" );
209
207
210
208
mCurrentState = MEDIA_PLAYER_STOPPED;
209
+ if (mDecoderAudio != NULL )
210
+ {
211
+ mDecoderAudio ->stop ();
212
+ }
213
+
211
214
if (pthread_join (mPlayerThread , NULL ) != 0 ) {
212
215
__android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel player thread" );
213
216
}
@@ -271,16 +274,6 @@ status_t MediaPlayer::processVideo(AVPacket *packet, AVFrame *pFrame)
271
274
return INVALID_OPERATION;
272
275
}
273
276
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
-
284
277
bool MediaPlayer::shouldCancel (PacketQueue* queue)
285
278
{
286
279
return (mCurrentState == MEDIA_PLAYER_STATE_ERROR || mCurrentState == MEDIA_PLAYER_STOPPED ||
@@ -356,105 +349,32 @@ void MediaPlayer::decodeVideo(void* ptr)
356
349
av_free (pFrameRGB);
357
350
}
358
351
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
-
429
352
void MediaPlayer::decodeMovie (void * ptr)
430
353
{
431
354
AVPacket pPacket;
432
355
char err[256 ];
433
356
434
357
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
+ }
450
370
451
371
mCurrentState = MEDIA_PLAYER_STARTED;
452
372
__android_log_print (ANDROID_LOG_INFO, TAG, " playing %ix%i" , mVideoWidth , mVideoHeight );
453
373
while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&
454
374
mCurrentState != MEDIA_PLAYER_STATE_ERROR)
455
375
{
456
376
if (mVideoQueue ->size () > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
457
- mAudioQueue -> size () > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
377
+ mDecoderAudio -> packets () > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
458
378
usleep (200 );
459
379
continue ;
460
380
}
@@ -469,7 +389,7 @@ void MediaPlayer::decodeMovie(void* ptr)
469
389
mVideoQueue ->put (&pPacket);
470
390
}
471
391
else if (pPacket.stream_index == mFFmpegStorage .audio .stream ) {
472
- mAudioQueue -> put (&pPacket);
392
+ mDecoderAudio -> enqueue (&pPacket);
473
393
}
474
394
else {
475
395
// Free the packet that was allocated by av_read_frame
@@ -484,17 +404,11 @@ void MediaPlayer::decodeMovie(void* ptr)
484
404
__android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel video thread: %i" , ret);
485
405
}
486
406
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
- /*
492
407
__android_log_print (ANDROID_LOG_ERROR, TAG, " waiting on audio thread" );
493
- if((ret = mDecoderAudio->wait()) != 0) {
408
+ if ((ret = mDecoderAudio ->wait ()) != 0 ) {
494
409
__android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel audio thread: %i" , ret);
495
410
}
496
- */
497
-
411
+
498
412
if (mCurrentState == MEDIA_PLAYER_STATE_ERROR) {
499
413
__android_log_print (ANDROID_LOG_INFO, TAG, " playing err" );
500
414
}
@@ -508,12 +422,6 @@ void* MediaPlayer::startVideoDecoding(void* ptr)
508
422
sPlayer ->decodeVideo (ptr);
509
423
}
510
424
511
- void * MediaPlayer::startAudioDecoding (void * ptr)
512
- {
513
- __android_log_print (ANDROID_LOG_INFO, TAG, " starting audio thread" );
514
- sPlayer ->decodeAudio (ptr);
515
- }
516
-
517
425
void * MediaPlayer::startPlayer (void * ptr)
518
426
{
519
427
__android_log_print (ANDROID_LOG_INFO, TAG, " starting main player thread" );
0 commit comments