Skip to content

Commit 7ef242c

Browse files
committed
implement new thread class
1 parent 7c27aa2 commit 7ef242c

11 files changed

+306
-176
lines changed

jni/jni/com_media_ffmpeg_FFMpegPlayer.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ static MediaPlayer* setMediaPlayer(JNIEnv* env, jobject thiz, MediaPlayer* playe
8686
{
8787
MediaPlayer* old = (MediaPlayer*)env->GetIntField(thiz, fields.context);
8888
if (old != NULL) {
89-
__android_log_print(ANDROID_LOG_INFO, TAG, "freeing old mediaplayer object");
90-
free(old);
91-
}
89+
__android_log_print(ANDROID_LOG_INFO, TAG, "freeing old mediaplayer object");
90+
free(old);
91+
}
9292
env->SetIntField(thiz, fields.context, (int)player);
9393
return old;
9494
}
@@ -100,12 +100,12 @@ static MediaPlayer* setMediaPlayer(JNIEnv* env, jobject thiz, MediaPlayer* playe
100100
static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message)
101101
{
102102
if (exception == NULL) { // Don't throw exception. Instead, send an event.
103-
/*
103+
/*
104104
if (opStatus != (status_t) OK) {
105105
sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
106106
if (mp != 0) mp->notify(MEDIA_ERROR, opStatus, 0);
107107
}
108-
*/
108+
*/
109109
} else { // Throw exception!
110110
if ( opStatus == (status_t) INVALID_OPERATION ) {
111111
jniThrowException(env, "java/lang/IllegalStateException", NULL);
@@ -172,8 +172,8 @@ com_media_ffmpeg_FFMpegPlayer_setVideoSurface(JNIEnv *env, jobject thiz, jobject
172172
jniThrowException(env, "java/lang/IllegalStateException", NULL);
173173
return;
174174
}
175-
process_media_player_call( env, thiz, mp->setVideoSurface(env, jsurface),
176-
"java/io/IOException", "Set video surface failed.");
175+
process_media_player_call( env, thiz, mp->setVideoSurface(env, jsurface),
176+
"java/io/IOException", "Set video surface failed.");
177177
}
178178

179179
static void

jni/libmediaplayer/decoder.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,49 @@
55

66
IDecoder::IDecoder(AVStream* stream)
77
{
8-
mQueue = new PacketQueue();
9-
mStream = stream;
8+
mQueue = new PacketQueue();
9+
mStream = stream;
1010
}
1111

1212
IDecoder::~IDecoder()
1313
{
14-
if(mRunning)
14+
if(mRunning)
1515
{
1616
stop();
1717
}
18-
free(mQueue);
19-
avcodec_close(mStream->codec);
18+
free(mQueue);
19+
avcodec_close(mStream->codec);
2020
}
2121

2222
void IDecoder::enqueue(AVPacket* packet)
2323
{
24-
mQueue->put(packet);
24+
mQueue->put(packet);
2525
}
2626

2727
int IDecoder::packets()
2828
{
29-
return mQueue->size();
29+
return mQueue->size();
3030
}
3131

3232
void IDecoder::stop()
3333
{
3434
mQueue->abort();
3535
__android_log_print(ANDROID_LOG_INFO, TAG, "waiting on end of decoder thread");
3636
int ret = -1;
37-
if((ret = wait()) != 0) {
37+
if((ret = join()) != 0) {
3838
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel IDecoder: %i", ret);
3939
return;
4040
}
4141
}
4242

43-
void IDecoder::handleRun(void* ptr)
43+
void IDecoder::run()
4444
{
45-
if(!prepare())
45+
if(!prepare())
4646
{
47-
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't prepare decoder");
47+
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't prepare decoder");
4848
return;
4949
}
50-
decode(ptr);
50+
decode();
5151
}
5252

5353
bool IDecoder::prepare()
@@ -57,10 +57,10 @@ bool IDecoder::prepare()
5757

5858
bool IDecoder::process(AVPacket *packet)
5959
{
60-
return false;
60+
return false;
6161
}
6262

63-
bool IDecoder::decode(void* ptr)
63+
bool IDecoder::decode()
6464
{
6565
return false;
6666
}

jni/libmediaplayer/decoder.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@ extern "C" {
1414
class IDecoder : public Thread
1515
{
1616
public:
17-
IDecoder(AVStream* stream);
18-
~IDecoder();
17+
IDecoder(AVStream* stream);
18+
~IDecoder();
1919

20-
void stop();
21-
void enqueue(AVPacket* packet);
22-
int packets();
20+
void stop();
21+
void enqueue(AVPacket* packet);
22+
int packets();
2323

2424
protected:
25-
PacketQueue* mQueue;
26-
AVStream* mStream;
25+
PacketQueue* mQueue;
26+
AVStream* mStream;
2727

28-
virtual bool prepare();
29-
virtual bool decode(void* ptr);
30-
virtual bool process(AVPacket *packet);
31-
void handleRun(void* ptr);
28+
virtual bool prepare();
29+
virtual bool decode();
30+
virtual bool process(AVPacket *packet);
31+
32+
virtual void run();
3233
};
3334

3435
#endif //FFMPEG_DECODER_H

jni/libmediaplayer/decoder_audio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bool DecoderAudio::process(AVPacket *packet)
3232
return true;
3333
}
3434

35-
bool DecoderAudio::decode(void* ptr)
35+
bool DecoderAudio::decode()
3636
{
3737
AVPacket pPacket;
3838

jni/libmediaplayer/decoder_audio.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ class DecoderAudio : public IDecoder
1212

1313
~DecoderAudio();
1414

15-
AudioDecodingHandler onDecode;
15+
AudioDecodingHandler onDecode;
1616

1717
private:
18-
int16_t* mSamples;
19-
int mSamplesSize;
18+
int16_t* mSamples;
19+
int mSamplesSize;
2020

21-
bool prepare();
22-
bool decode(void* ptr);
23-
bool process(AVPacket *packet);
21+
bool prepare();
22+
bool decode();
23+
bool process(AVPacket *packet);
2424
};
2525

2626
#endif //FFMPEG_DECODER_AUDIO_H

jni/libmediaplayer/decoder_video.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ static uint64_t global_video_pkt_pts = AV_NOPTS_VALUE;
77

88
DecoderVideo::DecoderVideo(AVStream* stream) : IDecoder(stream)
99
{
10-
mStream->codec->get_buffer = getBuffer;
11-
mStream->codec->release_buffer = releaseBuffer;
10+
mStream->codec->get_buffer = getBuffer;
11+
mStream->codec->release_buffer = releaseBuffer;
1212
}
1313

1414
DecoderVideo::~DecoderVideo()
@@ -17,11 +17,11 @@ DecoderVideo::~DecoderVideo()
1717

1818
bool DecoderVideo::prepare()
1919
{
20-
mFrame = avcodec_alloc_frame();
21-
if (mFrame == NULL) {
22-
return false;
23-
}
24-
return true;
20+
mFrame = avcodec_alloc_frame();
21+
if (mFrame == NULL) {
22+
return false;
23+
}
24+
return true;
2525
}
2626

2727
double DecoderVideo::synchronize(AVFrame *src_frame, double pts) {
@@ -75,11 +75,11 @@ bool DecoderVideo::process(AVPacket *packet)
7575
return false;
7676
}
7777

78-
bool DecoderVideo::decode(void* ptr)
78+
bool DecoderVideo::decode()
7979
{
80-
AVPacket pPacket;
80+
AVPacket pPacket;
8181

82-
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video");
82+
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video");
8383

8484
while(mRunning)
8585
{

jni/libmediaplayer/decoder_video.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ class DecoderVideo : public IDecoder
1111
DecoderVideo(AVStream* stream);
1212
~DecoderVideo();
1313

14-
VideoDecodingHandler onDecode;
14+
VideoDecodingHandler onDecode;
1515

1616
private:
17-
AVFrame* mFrame;
18-
double mVideoClock;
19-
20-
bool prepare();
21-
double synchronize(AVFrame *src_frame, double pts);
22-
bool decode(void* ptr);
23-
bool process(AVPacket *packet);
24-
static int getBuffer(struct AVCodecContext *c, AVFrame *pic);
25-
static void releaseBuffer(struct AVCodecContext *c, AVFrame *pic);
17+
AVFrame* mFrame;
18+
double mVideoClock;
19+
20+
bool prepare();
21+
double synchronize(AVFrame *src_frame, double pts);
22+
bool decode();
23+
bool process(AVPacket *packet);
24+
25+
static int getBuffer(struct AVCodecContext *c, AVFrame *pic);
26+
static void releaseBuffer(struct AVCodecContext *c, AVFrame *pic);
2627
};
2728

2829
#endif //FFMPEG_DECODER_AUDIO_H

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,12 @@ void MediaPlayer::decodeMovie(void* ptr)
327327
AVStream* stream_audio = mMovieFile->streams[mAudioStreamIndex];
328328
mDecoderAudio = new DecoderAudio(stream_audio);
329329
mDecoderAudio->onDecode = decode;
330-
mDecoderAudio->startAsync();
330+
mDecoderAudio->start();
331331

332332
AVStream* stream_video = mMovieFile->streams[mVideoStreamIndex];
333333
mDecoderVideo = new DecoderVideo(stream_video);
334334
mDecoderVideo->onDecode = decode;
335-
mDecoderVideo->startAsync();
335+
mDecoderVideo->start();
336336

337337
mCurrentState = MEDIA_PLAYER_STARTED;
338338
__android_log_print(ANDROID_LOG_INFO, TAG, "playing %ix%i", mVideoWidth, mVideoHeight);
@@ -366,17 +366,17 @@ void MediaPlayer::decodeMovie(void* ptr)
366366
//waits on end of video thread
367367
__android_log_print(ANDROID_LOG_ERROR, TAG, "waiting on video thread");
368368
int ret = -1;
369-
if((ret = mDecoderVideo->wait()) != 0) {
370-
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel video thread: %i", ret);
369+
if((ret = mDecoderVideo->join()) != 0) {
370+
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel video thread: %i", ret);
371371
}
372372

373373
__android_log_print(ANDROID_LOG_ERROR, TAG, "waiting on audio thread");
374-
if((ret = mDecoderAudio->wait()) != 0) {
375-
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel audio thread: %i", ret);
374+
if((ret = mDecoderAudio->join()) != 0) {
375+
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel audio thread: %i", ret);
376376
}
377377

378378
if(mCurrentState == MEDIA_PLAYER_STATE_ERROR) {
379-
__android_log_print(ANDROID_LOG_INFO, TAG, "playing err");
379+
__android_log_print(ANDROID_LOG_INFO, TAG, "playing err");
380380
}
381381
mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
382382
__android_log_print(ANDROID_LOG_INFO, TAG, "end of playing");

0 commit comments

Comments
 (0)