Skip to content

Commit fdf11fc

Browse files
committed
working all decoders properly
1 parent 3b3c53a commit fdf11fc

File tree

7 files changed

+55
-173
lines changed

7 files changed

+55
-173
lines changed

jni/libmediaplayer/decoder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class IDecoder
2828
protected:
2929
PacketQueue* mQueue;
3030
AVStream* mStream;
31-
AVCodecContext* mCodecCtx;
3231
bool mDecoding;
3332
pthread_t mThread;
3433

jni/libmediaplayer/decoder_audio.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
#define TAG "FFMpegAudioDecoder"
77

8-
DecoderAudio::DecoderAudio(AVStream* stream,
9-
struct DecoderAudioConfig* config) : IDecoder(stream)
8+
DecoderAudio::DecoderAudio(AVStream* stream) : IDecoder(stream)
109
{
11-
mConfig = config;
1210
}
1311

1412
DecoderAudio::~DecoderAudio()
@@ -24,10 +22,12 @@ bool DecoderAudio::prepare(const char *err)
2422
mSamplesSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
2523
mSamples = (int16_t *) av_malloc(mSamplesSize);
2624

27-
if(Output::AudioDriver_set(mConfig->streamType,
28-
mConfig->sampleRate,
29-
mConfig->format,
30-
mConfig->channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
25+
if(Output::AudioDriver_set(MUSIC,
26+
mStream->codec->sample_rate,
27+
PCM_16_BIT,
28+
(mStream->codec->channels == 2) ?
29+
CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO) != ANDROID_AUDIOTRACK_RESULT_SUCCESS)
30+
{
3131
err = "Couldnt' set audio track";
3232
return false;
3333
}

jni/libmediaplayer/decoder_audio.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@
33

44
#include "decoder.h"
55

6-
struct DecoderAudioConfig
7-
{
8-
int streamType;
9-
int sampleRate;
10-
int format;
11-
int channels;
12-
};
13-
146
class DecoderAudio : public IDecoder
157
{
168
public:
17-
DecoderAudio(AVStream* stream,
18-
struct DecoderAudioConfig* config);
9+
DecoderAudio(AVStream* stream);
1910

2011
~DecoderAudio();
2112

2213
private:
23-
struct DecoderAudioConfig* mConfig;
2414
int16_t* mSamples;
2515
int mSamplesSize;
2616

jni/libmediaplayer/decoder_video.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ extern "C" {
99

1010
#define TAG "FFMpegVideoDecoder"
1111

12-
DecoderVideo::DecoderVideo(AVStream* stream,
13-
struct DecoderVideoConfig* config) : IDecoder(stream)
12+
DecoderVideo::DecoderVideo(AVStream* stream) : IDecoder(stream)
1413
{
15-
mConfig = config;
1614
}
1715

1816
DecoderVideo::~DecoderVideo()
@@ -39,8 +37,23 @@ bool DecoderVideo::prepare(const char *err)
3937
return false;
4038
}
4139

42-
if(Output::VideoDriver_getPixels(mConfig->width,
43-
mConfig->height,
40+
mConvertCtx = sws_getContext(mStream->codec->width,
41+
mStream->codec->height,
42+
mStream->codec->pix_fmt,
43+
mStream->codec->width,
44+
mStream->codec->height,
45+
PIX_FMT_RGB565,
46+
SWS_POINT,
47+
NULL,
48+
NULL,
49+
NULL);
50+
if (mConvertCtx == NULL) {
51+
err = "Couldn't allocate mConvertCtx";
52+
return false;
53+
}
54+
55+
if(Output::VideoDriver_getPixels(mStream->codec->width,
56+
mStream->codec->height,
4457
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
4558
err = "Couldn't get pixels from android surface wrapper";
4659
return false;
@@ -52,8 +65,8 @@ bool DecoderVideo::prepare(const char *err)
5265
avpicture_fill((AVPicture *) mFrame,
5366
(uint8_t *)pixels,
5467
PIX_FMT_RGB565,
55-
mConfig->width,
56-
mConfig->height);
68+
mStream->codec->width,
69+
mStream->codec->height);
5770

5871
return true;
5972
}
@@ -63,22 +76,22 @@ bool DecoderVideo::process(AVPacket *packet)
6376
int completed;
6477

6578
// Decode video frame
66-
avcodec_decode_video(mCodecCtx,
79+
avcodec_decode_video(mStream->codec,
6780
mTempFrame,
6881
&completed,
6982
packet->data,
7083
packet->size);
7184

7285
if (completed) {
7386
// Convert the image from its native format to RGB
74-
sws_scale(mConfig->img_convert_ctx,
87+
sws_scale(mConvertCtx,
7588
mTempFrame->data,
7689
mTempFrame->linesize,
7790
0,
78-
mConfig->height,
91+
mStream->codec->height,
7992
mFrame->data,
8093
mFrame->linesize);
81-
94+
8295
Output::VideoDriver_updateSurface();
8396
return true;
8497
}

jni/libmediaplayer/decoder_video.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,17 @@
33

44
#include "decoder.h"
55

6-
struct DecoderVideoConfig
7-
{
8-
int width;
9-
int height;
10-
struct SwsContext* img_convert_ctx;
11-
};
12-
136
class DecoderVideo : public IDecoder
147
{
158
public:
16-
DecoderVideo(AVStream* stream,
17-
struct DecoderVideoConfig* config);
18-
9+
DecoderVideo(AVStream* stream);
1910
~DecoderVideo();
2011

2112
private:
22-
struct DecoderVideoConfig* mConfig;
2313
AVFrame* mFrame;
2414
AVFrame* mTempFrame;
15+
struct SwsContext* mConvertCtx;
16+
2517
bool prepare(const char *err);
2618
bool decode(void* ptr);
2719
bool process(AVPacket *packet);

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 18 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -174,42 +174,16 @@ status_t MediaPlayer::setDataSource(const char *url)
174174
return NO_ERROR;
175175
}
176176

177-
AVFrame* MediaPlayer::createAndroidFrame()
178-
{
179-
void* pixels;
180-
AVFrame* frame;
181-
182-
frame = avcodec_alloc_frame();
183-
if (frame == NULL) {
184-
return NULL;
185-
}
186-
187-
if(Output::VideoDriver_getPixels(mVideoWidth,
188-
mVideoHeight,
189-
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
190-
return NULL;
191-
}
192-
193-
// Assign appropriate parts of buffer to image planes in pFrameRGB
194-
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
195-
// of AVPicture
196-
avpicture_fill((AVPicture *) frame,
197-
(uint8_t *)pixels,
198-
PIX_FMT_RGB565,
199-
mVideoWidth,
200-
mVideoHeight);
201-
202-
return frame;
203-
}
204-
205177
status_t MediaPlayer::suspend() {
206178
__android_log_print(ANDROID_LOG_INFO, TAG, "suspend");
207179

208180
mCurrentState = MEDIA_PLAYER_STOPPED;
209-
if(mDecoderAudio != NULL)
210-
{
181+
if(mDecoderAudio != NULL) {
211182
mDecoderAudio->stop();
212183
}
184+
if(mDecoderVideo != NULL) {
185+
mDecoderVideo->stop();
186+
}
213187

214188
if(pthread_join(mPlayerThread, NULL) != 0) {
215189
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel player thread");
@@ -247,33 +221,6 @@ status_t MediaPlayer::setVideoSurface(JNIEnv* env, jobject jsurface)
247221
return NO_ERROR;
248222
}
249223

250-
status_t MediaPlayer::processVideo(AVPacket *packet, AVFrame *pFrame)
251-
{
252-
int completed;
253-
254-
// Decode video frame
255-
avcodec_decode_video(mFFmpegStorage.video.codec_ctx,
256-
mFFmpegStorage.pFrame,
257-
&completed,
258-
packet->data,
259-
packet->size);
260-
261-
if (completed) {
262-
// Convert the image from its native format to RGB
263-
sws_scale(mFFmpegStorage.img_convert_ctx,
264-
mFFmpegStorage.pFrame->data,
265-
mFFmpegStorage.pFrame->linesize,
266-
0,
267-
mVideoHeight,
268-
pFrame->data,
269-
pFrame->linesize);
270-
271-
Output::VideoDriver_updateSurface();
272-
return NO_ERROR;
273-
}
274-
return INVALID_OPERATION;
275-
}
276-
277224
bool MediaPlayer::shouldCancel(PacketQueue* queue)
278225
{
279226
return (mCurrentState == MEDIA_PLAYER_STATE_ERROR || mCurrentState == MEDIA_PLAYER_STOPPED ||
@@ -298,83 +245,34 @@ bool MediaPlayer::shouldCancel(PacketQueue* queue)
298245
}
299246
frames++;
300247
*/
301-
void MediaPlayer::decodeVideo(void* ptr)
302-
{
303-
AVPacket pPacket;
304-
AVFrame* pFrameRGB;
305-
bool run = true;
306-
307-
if((pFrameRGB = createAndroidFrame()) == NULL) {
308-
mCurrentState = MEDIA_PLAYER_STATE_ERROR;
309-
}
310-
311-
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video");
312-
313-
while (run)
314-
{
315-
if(mCurrentState == MEDIA_PLAYER_PAUSED) {
316-
usleep(50);
317-
continue;
318-
}
319-
if (shouldCancel(mVideoQueue)) {
320-
run = false;
321-
continue;
322-
}
323-
if(mVideoQueue->get(&pPacket, true) < 0)
324-
{
325-
mCurrentState = MEDIA_PLAYER_STATE_ERROR;
326-
}
327-
if(processVideo(&pPacket, pFrameRGB) != NO_ERROR)
328-
{
329-
mCurrentState = MEDIA_PLAYER_STATE_ERROR;
330-
}
331-
332-
AVStream* vs = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.video.stream];
333-
if(pPacket.dts != AV_NOPTS_VALUE) {
334-
mTime = pPacket.dts;
335-
} else {
336-
mTime = 0;
337-
}
338-
mTime *= av_q2d(vs->time_base);
339-
//__android_log_print(ANDROID_LOG_INFO, TAG, "time: %i", pts);
340-
// Free the packet that was allocated by av_read_frame
341-
av_free_packet(&pPacket);
342-
}
343-
344-
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video ended");
345-
346-
Output::VideoDriver_unregister();
347-
348-
// Free the RGB image
349-
av_free(pFrameRGB);
350-
}
351248

352249
void MediaPlayer::decodeMovie(void* ptr)
353250
{
354251
AVPacket pPacket;
355-
char err[256];
252+
char err[256];
356253

357-
pthread_create(&mVideoThread, NULL, startVideoDecoding, NULL);
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-
AVStream* stream = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.audio.stream];
365-
mDecoderAudio = new DecoderAudio(stream, &cfg);
254+
AVStream* stream_audio = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.audio.stream];
255+
mDecoderAudio = new DecoderAudio(stream_audio);
366256
if(!mDecoderAudio->startAsync(err))
367257
{
368258
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't start audio decoder: %s", err);
369259
return;
370260
}
371261

262+
AVStream* stream_video = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.video.stream];
263+
mDecoderVideo = new DecoderVideo(stream_video);
264+
if(!mDecoderVideo->startAsync(err))
265+
{
266+
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't start video decoder: %s", err);
267+
return;
268+
}
269+
372270
mCurrentState = MEDIA_PLAYER_STARTED;
373271
__android_log_print(ANDROID_LOG_INFO, TAG, "playing %ix%i", mVideoWidth, mVideoHeight);
374272
while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&
375273
mCurrentState != MEDIA_PLAYER_STATE_ERROR)
376274
{
377-
if (mVideoQueue->size() > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
275+
if (mDecoderVideo->packets()/*mVideoQueue->size()*/ > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
378276
mDecoderAudio->packets() > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
379277
usleep(200);
380278
continue;
@@ -387,7 +285,7 @@ void MediaPlayer::decodeMovie(void* ptr)
387285

388286
// Is this a packet from the video stream?
389287
if (pPacket.stream_index == mFFmpegStorage.video.stream) {
390-
mVideoQueue->put(&pPacket);
288+
mDecoderVideo->enqueue(&pPacket);
391289
}
392290
else if (pPacket.stream_index == mFFmpegStorage.audio.stream) {
393291
mDecoderAudio->enqueue(&pPacket);
@@ -401,7 +299,7 @@ void MediaPlayer::decodeMovie(void* ptr)
401299
//waits on end of video thread
402300
__android_log_print(ANDROID_LOG_ERROR, TAG, "waiting on video thread");
403301
int ret = -1;
404-
if((ret = pthread_join(mVideoThread, NULL)) != 0) {
302+
if((ret = mDecoderVideo->wait()) != 0) {
405303
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel video thread: %i", ret);
406304
}
407305

@@ -417,12 +315,6 @@ void MediaPlayer::decodeMovie(void* ptr)
417315
__android_log_print(ANDROID_LOG_INFO, TAG, "end of playing");
418316
}
419317

420-
void* MediaPlayer::startVideoDecoding(void* ptr)
421-
{
422-
__android_log_print(ANDROID_LOG_INFO, TAG, "starting video thread");
423-
sPlayer->decodeVideo(ptr);
424-
}
425-
426318
void* MediaPlayer::startPlayer(void* ptr)
427319
{
428320
__android_log_print(ANDROID_LOG_INFO, TAG, "starting main player thread");

0 commit comments

Comments
 (0)