Skip to content

Commit 7dd4e74

Browse files
committed
add output class which handles access to system audio and video drivers
1 parent 67a901e commit 7dd4e74

File tree

7 files changed

+127
-74
lines changed

7 files changed

+127
-74
lines changed

jni/include/drivers_map.h

Lines changed: 0 additions & 52 deletions
This file was deleted.

jni/libmediaplayer/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LOCAL_C_INCLUDES += \
1010
LOCAL_SRC_FILES += \
1111
mediaplayer.cpp \
1212
packetqueue.cpp \
13+
output.cpp \
1314
decoder.cpp \
1415
decoder_audio.cpp \
1516
decoder_video.cpp

jni/libmediaplayer/decoder_audio.cpp

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

4-
#include <drivers_map.h>
4+
#include "output.h"
55

66
#define TAG "FFMpegAudioDecoder"
77

@@ -29,14 +29,14 @@ bool DecoderAudio::prepare(const char *err)
2929
mSamplesSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
3030
mSamples = (int16_t *) av_malloc(mSamplesSize);
3131

32-
if(AudioDriver_set(mConfig->streamType,
32+
if(Output::AudioDriver_set(mConfig->streamType,
3333
mConfig->sampleRate,
3434
mConfig->format,
3535
mConfig->channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
3636
err = "Couldnt' set audio track";
3737
return false;
3838
}
39-
if(AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
39+
if(Output::AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
4040
err = "Couldnt' start audio track";
4141
return false;
4242
}
@@ -47,7 +47,7 @@ bool DecoderAudio::process(AVPacket *packet)
4747
{
4848
int size = mSamplesSize;
4949
int len = avcodec_decode_audio3(mCodecCtx, mSamples, &size, packet);
50-
if(AudioDriver_write(mSamples, size) <= 0) {
50+
if(Output::AudioDriver_write(mSamples, size) <= 0) {
5151
return false;
5252
}
5353
return true;
@@ -78,7 +78,7 @@ bool DecoderAudio::decode(void* ptr)
7878

7979
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding audio ended");
8080

81-
AudioDriver_unregister();
81+
Output::AudioDriver_unregister();
8282

8383
// Free audio samples buffer
8484
av_free(mSamples);

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ extern "C" {
2222

2323
#include <android/log.h>
2424

25-
// map system drivers methods
26-
#include <drivers_map.h>
27-
2825
#include "mediaplayer.h"
26+
#include "output.h"
2927

3028
static MediaPlayer* sPlayer;
3129

@@ -188,7 +186,7 @@ AVFrame* MediaPlayer::createAndroidFrame()
188186
return NULL;
189187
}
190188

191-
if(VideoDriver_getPixels(mVideoWidth,
189+
if(Output::VideoDriver_getPixels(mVideoWidth,
192190
mVideoHeight,
193191
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
194192
return NULL;
@@ -237,10 +235,10 @@ status_t MediaPlayer::resume() {
237235

238236
status_t MediaPlayer::setVideoSurface(JNIEnv* env, jobject jsurface)
239237
{
240-
if(VideoDriver_register(env, jsurface) != ANDROID_SURFACE_RESULT_SUCCESS) {
238+
if(Output::VideoDriver_register(env, jsurface) != ANDROID_SURFACE_RESULT_SUCCESS) {
241239
return INVALID_OPERATION;
242240
}
243-
if(AudioDriver_register() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
241+
if(Output::AudioDriver_register() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
244242
return INVALID_OPERATION;
245243
}
246244
return NO_ERROR;
@@ -267,7 +265,7 @@ status_t MediaPlayer::processVideo(AVPacket *packet, AVFrame *pFrame)
267265
pFrame->data,
268266
pFrame->linesize);
269267

270-
VideoDriver_updateSurface();
268+
Output::VideoDriver_updateSurface();
271269
return NO_ERROR;
272270
}
273271
return INVALID_OPERATION;
@@ -277,7 +275,7 @@ status_t MediaPlayer::processAudio(AVPacket *packet, int16_t *samples, int sampl
277275
{
278276
int size = samples_size;
279277
int len = avcodec_decode_audio3(mFFmpegStorage.audio.codec_ctx, samples, &size, packet);
280-
if(AudioDriver_write(samples, size) <= 0) {
278+
if(Output::AudioDriver_write(samples, size) <= 0) {
281279
return INVALID_OPERATION;
282280
}
283281
return NO_ERROR;
@@ -352,7 +350,7 @@ void MediaPlayer::decodeVideo(void* ptr)
352350

353351
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video ended");
354352

355-
VideoDriver_unregister();
353+
Output::VideoDriver_unregister();
356354

357355
// Free the RGB image
358356
av_free(pFrameRGB);
@@ -374,13 +372,13 @@ void MediaPlayer::decodeAudio(void* ptr)
374372
int sampleRate = mFFmpegStorage.audio.codec_ctx->sample_rate;
375373
int format = PCM_16_BIT;
376374
int channels = (mFFmpegStorage.audio.codec_ctx->channels == 2) ? CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO;
377-
if(AudioDriver_set(streamType,
375+
if(Output::AudioDriver_set(streamType,
378376
sampleRate,
379377
format,
380378
channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
381379
goto end;
382380
}
383-
if(AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
381+
if(Output::AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
384382
goto end;
385383
}
386384

@@ -422,7 +420,7 @@ void MediaPlayer::decodeAudio(void* ptr)
422420
end:
423421
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding audio ended");
424422

425-
AudioDriver_unregister();
423+
Output::AudioDriver_unregister();
426424

427425
// Free audio samples buffer
428426
av_free(pAudioSamples);
@@ -436,19 +434,19 @@ void MediaPlayer::decodeMovie(void* ptr)
436434
pthread_create(&mVideoThread, NULL, startVideoDecoding, NULL);
437435
pthread_create(&mAudioThread, NULL, startAudioDecoding, NULL);
438436

439-
/*
437+
/*
440438
DecoderAudioConfig cfg;
441439
cfg.streamType = MUSIC;
442440
cfg.sampleRate = mFFmpegStorage.audio.codec_ctx->sample_rate;
443441
cfg.format = PCM_16_BIT;
444442
cfg.channels = (mFFmpegStorage.audio.codec_ctx->channels == 2) ? CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO;
445-
mDecoderAudio = new DecoderAudio(mAudioQueue, mFFmpegStorage.audio.codec_ctx, &cfg);
443+
mDecoderAudio = new DecoderAudio(mFFmpegStorage.audio.codec_ctx, &cfg);
446444
if(!mDecoderAudio->startAsync(err))
447445
{
448446
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't start audio decoder: %s", err);
449447
return;
450448
}
451-
*/
449+
*/
452450

453451
mCurrentState = MEDIA_PLAYER_STARTED;
454452
__android_log_print(ANDROID_LOG_INFO, TAG, "playing %ix%i", mVideoWidth, mVideoHeight);

jni/libmediaplayer/mediaplayer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <android/Errors.h>
88

99
#include "packetqueue.h"
10-
//#include "decoder_audio.h"
10+
#include "decoder_audio.h"
11+
//#include "decoder_video.h"
1112

1213
#define FFMPEG_PLAYER_MAX_QUEUE_SIZE 10
1314

@@ -190,7 +191,8 @@ class MediaPlayer
190191
//Mutex mNotifyLock;
191192
//Condition mSignal;
192193
MediaPlayerListener* mListener;
193-
//DecoderAudio* mDecoderAudio;
194+
DecoderAudio* mDecoderAudio;
195+
//DecoderVideo* mDecoderVideo;
194196
void* mCookie;
195197
media_player_states mCurrentState;
196198
int mDuration;

jni/libmediaplayer/output.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <android/log.h>
2+
#include "output.h"
3+
4+
#define TAG "FFMpegOutput"
5+
6+
//-------------------- Audio driver --------------------
7+
8+
int Output::AudioDriver_register()
9+
{
10+
return AndroidAudioTrack_register();
11+
}
12+
13+
int Output::AudioDriver_unregister()
14+
{
15+
return AndroidAudioTrack_unregister();
16+
}
17+
18+
int Output::AudioDriver_start()
19+
{
20+
return AndroidAudioTrack_start();
21+
}
22+
23+
int Output::AudioDriver_set(int streamType,
24+
uint32_t sampleRate,
25+
int format,
26+
int channels)
27+
{
28+
return AndroidAudioTrack_set(streamType,
29+
sampleRate,
30+
format,
31+
channels);
32+
}
33+
34+
int Output::AudioDriver_flush()
35+
{
36+
return AndroidAudioTrack_flush();
37+
}
38+
39+
int Output::AudioDriver_stop()
40+
{
41+
return AndroidAudioTrack_stop();
42+
}
43+
44+
int Output::AudioDriver_reload()
45+
{
46+
return AndroidAudioTrack_reload();
47+
}
48+
49+
int Output::AudioDriver_write(void *buffer, int buffer_size)
50+
{
51+
return AndroidAudioTrack_write(buffer, buffer_size);
52+
}
53+
54+
//-------------------- Video driver --------------------
55+
56+
int Output::VideoDriver_register(JNIEnv* env, jobject jsurface)
57+
{
58+
return AndroidSurface_register(env, jsurface);
59+
}
60+
61+
int Output::VideoDriver_unregister()
62+
{
63+
return AndroidSurface_unregister();
64+
}
65+
66+
int Output::VideoDriver_getPixels(int width, int height, void** pixels)
67+
{
68+
return AndroidSurface_getPixels(width, height, pixels);
69+
}
70+
71+
int Output::VideoDriver_updateSurface()
72+
{
73+
return AndroidSurface_updateSurface();
74+
}

jni/libmediaplayer/output.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef FFMPEG_OUTPUT_H
2+
#define FFMPEG_OUTPUT_H
3+
4+
#include <jni.h>
5+
6+
#include <android/audiotrack.h>
7+
#include <android/surface.h>
8+
9+
class Output
10+
{
11+
public:
12+
static int AudioDriver_register();
13+
static int AudioDriver_set(int streamType,
14+
uint32_t sampleRate,
15+
int format,
16+
int channels);
17+
static int AudioDriver_start();
18+
static int AudioDriver_flush();
19+
static int AudioDriver_stop();
20+
static int AudioDriver_reload();
21+
static int AudioDriver_write(void *buffer, int buffer_size);
22+
static int AudioDriver_unregister();
23+
24+
static int VideoDriver_register(JNIEnv* env, jobject jsurface);
25+
static int VideoDriver_getPixels(int width, int height, void** pixels);
26+
static int VideoDriver_updateSurface();
27+
static int VideoDriver_unregister();
28+
};
29+
30+
#endif //FFMPEG_DECODER_H

0 commit comments

Comments
 (0)