Skip to content

Commit 3b21581

Browse files
committed
working thread class
1 parent 3a6c96e commit 3b21581

File tree

8 files changed

+32
-79
lines changed

8 files changed

+32
-79
lines changed

jni/libmediaplayer/decoder.cpp

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ IDecoder::IDecoder(AVStream* stream)
1111

1212
IDecoder::~IDecoder()
1313
{
14-
if(mDecoding)
14+
if(mRunning)
1515
{
1616
stop();
1717
}
@@ -29,34 +29,6 @@ int IDecoder::packets()
2929
return mQueue->size();
3030
}
3131

32-
bool IDecoder::start(const char* err)
33-
{
34-
if(!prepare(err))
35-
{
36-
return false;
37-
}
38-
return decode(NULL);
39-
}
40-
41-
bool IDecoder::startAsync(const char* err)
42-
{
43-
if(!prepare(err))
44-
{
45-
return false;
46-
}
47-
48-
pthread_create(&mThread, NULL, startDecoding, this);
49-
return true;
50-
}
51-
52-
int IDecoder::wait()
53-
{
54-
if (!mDecoding) {
55-
return 0;
56-
}
57-
return pthread_join(mThread, NULL);
58-
}
59-
6032
void IDecoder::stop()
6133
{
6234
mQueue->abort();
@@ -68,19 +40,18 @@ void IDecoder::stop()
6840
}
6941
}
7042

71-
void* IDecoder::startDecoding(void* ptr)
43+
void IDecoder::handleRun(void* ptr)
7244
{
73-
__android_log_print(ANDROID_LOG_INFO, TAG, "starting decoder thread");
74-
IDecoder* decoder = (IDecoder *) ptr;
75-
decoder->mDecoding = true;
76-
decoder->decode(ptr);
77-
decoder->mDecoding = false;
78-
__android_log_print(ANDROID_LOG_INFO, TAG, "decoder thread ended");
45+
if(!prepare())
46+
{
47+
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't prepare decoder");
48+
return;
49+
}
50+
decode(ptr);
7951
}
8052

81-
bool IDecoder::prepare(const char *err)
53+
bool IDecoder::prepare()
8254
{
83-
err = "Not implemented";
8455
return false;
8556
}
8657

jni/libmediaplayer/decoder.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
#ifndef FFMPEG_DECODER_H
22
#define FFMPEG_DECODER_H
33

4-
#include <pthread.h>
5-
64
extern "C" {
75

86
#include "libavcodec/avcodec.h"
97
#include "libavformat/avformat.h"
108

119
}
1210

11+
#include "thread.h"
1312
#include "packetqueue.h"
1413

15-
class IDecoder
14+
class IDecoder : public Thread
1615
{
1716
public:
1817
IDecoder(AVStream* stream);
1918
~IDecoder();
2019

21-
bool start(const char* err);
22-
bool startAsync(const char* err);
23-
int wait();
2420
void stop();
2521
void enqueue(AVPacket* packet);
2622
int packets();
2723

2824
protected:
2925
PacketQueue* mQueue;
3026
AVStream* mStream;
31-
bool mDecoding;
32-
pthread_t mThread;
3327

34-
static void* startDecoding(void* ptr);
35-
virtual bool prepare(const char *err);
28+
virtual bool prepare();
3629
virtual bool decode(void* ptr);
3730
virtual bool process(AVPacket *packet);
31+
void handleRun(void* ptr);
3832
};
3933

4034
#endif //FFMPEG_DECODER_H

jni/libmediaplayer/decoder_audio.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ DecoderAudio::~DecoderAudio()
1313
{
1414
}
1515

16-
bool DecoderAudio::prepare(const char *err)
16+
bool DecoderAudio::prepare()
1717
{
1818
mSamplesSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1919
mSamples = (int16_t *) av_malloc(mSamplesSize);
@@ -24,11 +24,11 @@ bool DecoderAudio::prepare(const char *err)
2424
(mStream->codec->channels == 2) ?
2525
CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO) != ANDROID_AUDIOTRACK_RESULT_SUCCESS)
2626
{
27-
err = "Couldnt' set audio track";
27+
//err = "Couldnt' set audio track";
2828
return false;
2929
}
3030
if(Output::AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
31-
err = "Couldnt' start audio track";
31+
//err = "Couldnt' start audio track";
3232
return false;
3333
}
3434
return true;
@@ -50,16 +50,16 @@ bool DecoderAudio::decode(void* ptr)
5050

5151
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding audio");
5252

53-
while(mDecoding)
53+
while(mRunning)
5454
{
5555
if(mQueue->get(&pPacket, true) < 0)
5656
{
57-
mDecoding = false;
57+
mRunning = false;
5858
return false;
5959
}
6060
if(!process(&pPacket))
6161
{
62-
mDecoding = false;
62+
mRunning = false;
6363
return false;
6464
}
6565
// Free the packet that was allocated by av_read_frame

jni/libmediaplayer/decoder_audio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DecoderAudio : public IDecoder
1414
int16_t* mSamples;
1515
int mSamplesSize;
1616

17-
bool prepare(const char *err);
17+
bool prepare();
1818
bool decode(void* ptr);
1919
bool process(AVPacket *packet);
2020
};

jni/libmediaplayer/decoder_video.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ DecoderVideo::~DecoderVideo()
2121
{
2222
}
2323

24-
bool DecoderVideo::prepare(const char *err)
24+
bool DecoderVideo::prepare()
2525
{
2626
void* pixels;
2727

2828
mFrame = avcodec_alloc_frame();
2929
if (mFrame == NULL) {
30-
err = "Couldn't allocate mFrame";
30+
//err = "Couldn't allocate mFrame";
3131
return false;
3232
}
3333

3434
mTempFrame = avcodec_alloc_frame();
3535
if (mTempFrame == NULL) {
36-
err = "Couldn't allocate mTempFrame";
36+
//err = "Couldn't allocate mTempFrame";
3737
return false;
3838
}
3939

@@ -48,14 +48,14 @@ bool DecoderVideo::prepare(const char *err)
4848
NULL,
4949
NULL);
5050
if (mConvertCtx == NULL) {
51-
err = "Couldn't allocate mConvertCtx";
51+
//err = "Couldn't allocate mConvertCtx";
5252
return false;
5353
}
5454

5555
if(Output::VideoDriver_getPixels(mStream->codec->width,
5656
mStream->codec->height,
5757
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
58-
err = "Couldn't get pixels from android surface wrapper";
58+
//err = "Couldn't get pixels from android surface wrapper";
5959
return false;
6060
}
6161

@@ -135,16 +135,16 @@ bool DecoderVideo::decode(void* ptr)
135135

136136
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video");
137137

138-
while(mDecoding)
138+
while(mRunning)
139139
{
140140
if(mQueue->get(&pPacket, true) < 0)
141141
{
142-
mDecoding = false;
142+
mRunning = false;
143143
return false;
144144
}
145145
if(!process(&pPacket))
146146
{
147-
mDecoding = false;
147+
mRunning = false;
148148
return false;
149149
}
150150
// Free the packet that was allocated by av_read_frame

jni/libmediaplayer/decoder_video.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DecoderVideo : public IDecoder
1515
struct SwsContext* mConvertCtx;
1616
double mVideoClock;
1717

18-
bool prepare(const char *err);
18+
bool prepare();
1919
double synchronize(AVFrame *src_frame, double pts);
2020
bool decode(void* ptr);
2121
bool process(AVPacket *packet);

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,24 +230,15 @@ bool MediaPlayer::shouldCancel(PacketQueue* queue)
230230
void MediaPlayer::decodeMovie(void* ptr)
231231
{
232232
AVPacket pPacket;
233-
char err[256];
234233

235234
AVStream* stream_audio = mMovieFile->streams[mAudioStreamIndex];
236235
mDecoderAudio = new DecoderAudio(stream_audio);
237-
if(!mDecoderAudio->startAsync(err))
238-
{
239-
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't start audio decoder: %s", err);
240-
return;
241-
}
236+
mDecoderAudio->startAsync();
242237

243238
AVStream* stream_video = mMovieFile->streams[mVideoStreamIndex];
244239
mDecoderVideo = new DecoderVideo(stream_video);
245-
if(!mDecoderVideo->startAsync(err))
246-
{
247-
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't start video decoder: %s", err);
248-
return;
249-
}
250-
240+
mDecoderVideo->startAsync();
241+
251242
mCurrentState = MEDIA_PLAYER_STARTED;
252243
__android_log_print(ANDROID_LOG_INFO, TAG, "playing %ix%i", mVideoWidth, mVideoHeight);
253244
while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&

jni/libmediaplayer/thread.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
class Thread
77
{
88
public:
9-
Thread();
10-
~Thread();
11-
129
void start();
1310
void startAsync();
1411
int wait();

0 commit comments

Comments
 (0)