Skip to content

Commit 1f71a19

Browse files
committed
add working audio decoder class
1 parent 7dd4e74 commit 1f71a19

File tree

7 files changed

+41
-122
lines changed

7 files changed

+41
-122
lines changed

jni/jni/com_media_ffmpeg_FFMpegPlayer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,14 @@ com_media_ffmpeg_FFMpegPlayer_native_init(JNIEnv *env)
340340
jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaPlayer.mNativeContext");
341341
return;
342342
}
343+
/*
343344
fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
344345
"(Ljava/lang/Object;IIILjava/lang/Object;)V");
345346
if (fields.post_event == NULL) {
346347
jniThrowException(env, "java/lang/RuntimeException", "Can't find FFMpegMediaPlayer.postEventFromNative");
347348
return;
348349
}
350+
*/
349351
}
350352

351353
static void

jni/libmediaplayer/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ LOCAL_C_INCLUDES += \
88
$(LOCAL_PATH)/../include
99

1010
LOCAL_SRC_FILES += \
11-
mediaplayer.cpp \
1211
packetqueue.cpp \
1312
output.cpp \
13+
mediaplayer.cpp \
1414
decoder.cpp \
1515
decoder_audio.cpp \
1616
decoder_video.cpp

jni/libmediaplayer/decoder.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ void IDecoder::enqueue(AVPacket* packet)
2525
mQueue->put(packet);
2626
}
2727

28+
int IDecoder::packets()
29+
{
30+
return mQueue->size();
31+
}
32+
2833
bool IDecoder::start(const char* err)
2934
{
3035
if(!prepare(err))
@@ -47,6 +52,9 @@ bool IDecoder::startAsync(const char* err)
4752

4853
int IDecoder::wait()
4954
{
55+
if (!mDecoding) {
56+
return 0;
57+
}
5058
return pthread_join(mThread, NULL);
5159
}
5260

@@ -59,13 +67,15 @@ void IDecoder::stop()
5967
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel audio IDecoder: %i", ret);
6068
return;
6169
}
62-
__android_log_print(ANDROID_LOG_INFO, TAG, "audio IDecoder stopped");
70+
__android_log_print(ANDROID_LOG_INFO, TAG, "audio IDecoder ended");
6371
}
6472

6573
void* IDecoder::startDecoding(void* ptr)
6674
{
67-
__android_log_print(ANDROID_LOG_INFO, TAG, "starting audio thread");
75+
__android_log_print(ANDROID_LOG_INFO, TAG, "starting decoder thread");
76+
sInstance->mDecoding = true;
6877
sInstance->decode(ptr);
78+
sInstance->mDecoding = false;
6979
}
7080

7181
bool IDecoder::prepare(const char *err)

jni/libmediaplayer/decoder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
#include <pthread.h>
55

6+
extern "C" {
7+
68
#include "libavcodec/avcodec.h"
79
#include "libavformat/avformat.h"
810

11+
}
12+
913
#include "packetqueue.h"
1014

1115
class IDecoder
@@ -19,6 +23,7 @@ class IDecoder
1923
int wait();
2024
void stop();
2125
void enqueue(AVPacket* packet);
26+
int packets();
2227

2328
protected:
2429
PacketQueue* mQueue;

jni/libmediaplayer/decoder_audio.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ bool DecoderAudio::decode(void* ptr)
5959

6060
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding audio");
6161

62-
mDecoding = true;
6362
while(mDecoding)
6463
{
6564
if(mQueue->get(&pPacket, true) < 0)

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 21 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5049
MediaPlayer::~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-
284277
bool 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-
429352
void 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-
517425
void* MediaPlayer::startPlayer(void* ptr)
518426
{
519427
__android_log_print(ANDROID_LOG_INFO, TAG, "starting main player thread");

jni/libmediaplayer/mediaplayer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,19 @@ class MediaPlayer
170170
status_t prepareAudio();
171171
status_t prepareVideo();
172172
status_t processVideo(AVPacket *packet, AVFrame *pFrame);
173-
status_t processAudio(AVPacket *packet, int16_t *samples, int samples_size);
174173
AVFrame* createAndroidFrame();
175174
bool shouldCancel(PacketQueue* queue);
176175
static void ffmpegNotify(void* ptr, int level, const char* fmt, va_list vl);
177176
static void* startVideoDecoding(void* ptr);
178-
static void* startAudioDecoding(void* ptr);
179177
static void* startPlayer(void* ptr);
180178
void decodeMovie(void* ptr);
181179
void decodeVideo(void* ptr);
182-
void decodeAudio(void* ptr);
183180

184181
double mTime;
185182
pthread_mutex_t mLock;
186183
pthread_t mVideoThread;
187-
pthread_t mAudioThread;
188184
pthread_t mPlayerThread;
189185
PacketQueue* mVideoQueue;
190-
PacketQueue* mAudioQueue;
191186
//Mutex mNotifyLock;
192187
//Condition mSignal;
193188
MediaPlayerListener* mListener;

0 commit comments

Comments
 (0)