Skip to content

Commit ce60158

Browse files
committed
move all playback stuff to mediaplayer class
1 parent aade92e commit ce60158

File tree

6 files changed

+71
-25
lines changed

6 files changed

+71
-25
lines changed

jni/libmediaplayer/decoder_audio.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <android/log.h>
22
#include "decoder_audio.h"
33

4-
#include "output.h"
5-
64
#define TAG "FFMpegAudioDecoder"
75

86
DecoderAudio::DecoderAudio(AVStream* stream) : IDecoder(stream)
@@ -20,30 +18,16 @@ bool DecoderAudio::prepare()
2018
if(mSamples == NULL) {
2119
return false;
2220
}
23-
24-
if(Output::AudioDriver_set(MUSIC,
25-
mStream->codec->sample_rate,
26-
PCM_16_BIT,
27-
(mStream->codec->channels == 2) ?
28-
CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO) != ANDROID_AUDIOTRACK_RESULT_SUCCESS)
29-
{
30-
//err = "Couldnt' set audio track";
31-
return false;
32-
}
33-
if(Output::AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
34-
//err = "Couldnt' start audio track";
35-
return false;
36-
}
3721
return true;
3822
}
3923

4024
bool DecoderAudio::process(AVPacket *packet)
4125
{
4226
int size = mSamplesSize;
4327
int len = avcodec_decode_audio3(mStream->codec, mSamples, &size, packet);
44-
if(Output::AudioDriver_write(mSamples, size) <= 0) {
45-
return false;
46-
}
28+
29+
onDecode(mSamples, size);
30+
4731
return true;
4832
}
4933

@@ -71,8 +55,6 @@ bool DecoderAudio::decode(void* ptr)
7155

7256
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding audio ended");
7357

74-
Output::AudioDriver_unregister();
75-
7658
// Free audio samples buffer
7759
av_free(mSamples);
7860
return true;

jni/libmediaplayer/decoder_audio.h

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

44
#include "decoder.h"
55

6+
typedef void (*AudioDecodingHandler) (int16_t*,int);
7+
68
class DecoderAudio : public IDecoder
79
{
810
public:
911
DecoderAudio(AVStream* stream);
1012

1113
~DecoderAudio();
1214

15+
AudioDecodingHandler onDecode;
16+
1317
private:
1418
int16_t* mSamples;
1519
int mSamplesSize;

jni/libmediaplayer/decoder_video.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool DecoderVideo::process(AVPacket *packet)
9494
{
9595
int completed;
9696
int pts = 0;
97-
97+
9898
// Decode video frame
9999
avcodec_decode_video(mStream->codec,
100100
mTempFrame,
@@ -114,6 +114,9 @@ bool DecoderVideo::process(AVPacket *packet)
114114

115115
if (completed) {
116116
pts = synchronize(mTempFrame, pts);
117+
118+
//onDecode(frame, pts);
119+
117120
// Convert the image from its native format to RGB
118121
sws_scale(mConvertCtx,
119122
mTempFrame->data,

jni/libmediaplayer/decoder_video.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
#include "decoder.h"
55

6+
typedef void (*VideoDecodingHandler) (AVFrame*,double);
7+
68
class DecoderVideo : public IDecoder
79
{
810
public:
911
DecoderVideo(AVStream* stream);
1012
~DecoderVideo();
1113

14+
VideoDecodingHandler onDecode;
15+
1216
private:
1317
AVFrame* mFrame;
1418
AVFrame* mTempFrame;

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ status_t MediaPlayer::prepareAudio()
6666
if (mAudioStreamIndex == -1) {
6767
return INVALID_OPERATION;
6868
}
69-
69+
70+
AVStream* stream = mMovieFile->streams[mAudioStreamIndex];
7071
// Get a pointer to the codec context for the video stream
71-
AVCodecContext* codec_ctx = mMovieFile->streams[mAudioStreamIndex]->codec;
72+
AVCodecContext* codec_ctx = stream->codec;
7273
AVCodec* codec = avcodec_find_decoder(codec_ctx->codec_id);
7374
if (codec == NULL) {
7475
return INVALID_OPERATION;
@@ -78,6 +79,20 @@ status_t MediaPlayer::prepareAudio()
7879
if (avcodec_open(codec_ctx, codec) < 0) {
7980
return INVALID_OPERATION;
8081
}
82+
83+
// prepare os output
84+
if (Output::AudioDriver_set(MUSIC,
85+
stream->codec->sample_rate,
86+
PCM_16_BIT,
87+
(stream->codec->channels == 2) ? CHANNEL_OUT_STEREO
88+
: CHANNEL_OUT_MONO) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
89+
return INVALID_OPERATION;
90+
}
91+
92+
if (Output::AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
93+
return INVALID_OPERATION;
94+
}
95+
8196
return NO_ERROR;
8297
}
8398

@@ -179,6 +194,9 @@ status_t MediaPlayer::suspend() {
179194

180195
// Close the video file
181196
av_close_input_file(mMovieFile);
197+
198+
Output::AudioDriver_unregister();
199+
182200
return NO_ERROR;
183201
}
184202

@@ -223,18 +241,49 @@ bool MediaPlayer::shouldCancel(PacketQueue* queue)
223241
frames = 0;
224242
}
225243
frames++;
226-
*/
244+
245+
*/
246+
247+
void MediaPlayer::decode(AVFrame* frame, double pts)
248+
{
249+
/*
250+
AVFrame* buffFrame = avcodec_alloc_frame();
251+
if (frame == NULL) {
252+
return false;
253+
}
254+
255+
// Convert the image from its native format to RGB
256+
sws_scale(mConvertCtx,
257+
frame->data,
258+
frame->linesize,
259+
0,
260+
mStream->codec->height,
261+
buffFrame->data,
262+
buffFrame->linesize);
263+
264+
Output::VideoDriver_updateSurface();
265+
*/
266+
}
267+
268+
void MediaPlayer::decode(int16_t* buffer, int buffer_size)
269+
{
270+
if(Output::AudioDriver_write(buffer, buffer_size) <= 0) {
271+
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't write samples to audio track");
272+
}
273+
}
227274

228275
void MediaPlayer::decodeMovie(void* ptr)
229276
{
230277
AVPacket pPacket;
231278

232279
AVStream* stream_audio = mMovieFile->streams[mAudioStreamIndex];
233280
mDecoderAudio = new DecoderAudio(stream_audio);
281+
mDecoderAudio->onDecode = decode;
234282
mDecoderAudio->startAsync();
235283

236284
AVStream* stream_video = mMovieFile->streams[mVideoStreamIndex];
237285
mDecoderVideo = new DecoderVideo(stream_video);
286+
mDecoderVideo->onDecode = decode;
238287
mDecoderVideo->startAsync();
239288

240289
mCurrentState = MEDIA_PLAYER_STARTED;

jni/libmediaplayer/mediaplayer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ class MediaPlayer
147147
bool shouldCancel(PacketQueue* queue);
148148
static void ffmpegNotify(void* ptr, int level, const char* fmt, va_list vl);
149149
static void* startPlayer(void* ptr);
150+
151+
static void decode(AVFrame* frame, double pts);
152+
static void decode(int16_t* buffer, int buffer_size);
153+
150154
void decodeMovie(void* ptr);
151155

152156
double mTime;

0 commit comments

Comments
 (0)