Skip to content

Commit 975ced8

Browse files
committed
refactor(ovkmplayer): Switch to OOP
1 parent 72741d6 commit 975ced8

13 files changed

+568
-53
lines changed

app/app.iml

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@
146146
<content url="file://$MODULE_DIR$/../ndk-modules/ovkmplayer">
147147
<sourceFolder url="file://$MODULE_DIR$/../ndk-modules/ovkmplayer" isTestSource="false" />
148148
</content>
149+
<content url="file://$MODULE_DIR$/../ndk-modules/ovkmplayer/decoders">
150+
<sourceFolder url="file://$MODULE_DIR$/../ndk-modules/ovkmplayer/decoders" isTestSource="false" />
151+
</content>
152+
<content url="file://$MODULE_DIR$/../ndk-modules/ovkmplayer/utils">
153+
<sourceFolder url="file://$MODULE_DIR$/../ndk-modules/ovkmplayer/utils" isTestSource="false" />
154+
</content>
149155
<orderEntry type="jdk" jdkName="Android API 29 Platform" jdkType="Android SDK" />
150156
<orderEntry type="sourceFolder" forTests="false" />
151157
<orderEntry type="library" exported="" name="Gradle: com.reginald.swiperefresh:library-1.1.2" level="project" />

ndk-modules/ovkmplayer/Android.mk

+9-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ include $(PREBUILT_SHARED_LIBRARY)
3737
include $(CLEAR_VARS)
3838
LOCAL_ALLOW_UNDEFINED_SYMBOLS=false
3939
LOCAL_MODULE := ovkmplayer
40-
LOCAL_SRC_FILES := ovkmplay.cpp android.cpp
40+
LOCAL_SRC_FILES := ovkmplay.cpp \
41+
utils/android.cpp \
42+
decoders/audiodec.cpp \
43+
decoders/videodec.cpp \
44+
utils/pktqueue.cpp \
45+
utils/decthread.cpp
4146
LOCAL_C_INCLUDES := $(PROJECT_PATH)/ndk-modules/ovkmplayer/builder/ffmpeg-$(FFMPEG_VERSION)/android/$(TARGET_ARCH_ABI)/include
42-
LOCAL_C_INCLUDES += $(PROJECT_PATH)/ndk-modules/ovkmplayer/builder/ffmpeg-$(FFMPEG_VERSION)
47+
LOCAL_C_INCLUDES += $(PROJECT_PATH)/ndk-modules/ovkmplayer/builder/ffmpeg-$(FFMPEG_VERSION) \
48+
$(PROJECT_PATH)/ndk-modules/ovkmplayer/utils \
49+
$(PROJECT_PATH)/ndk-modules/ovkmplayer/decoders \
4350
LOCAL_CFLAGS += -std=c++98
4451
LOCAL_CPP_FEATURES := exceptions
4552
LOCAL_SHARED_LIBRARIES := ffmpeg-prebuilt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Created by tretdm on 20.04.2024.
3+
//
4+
5+
#include "audiodec.h"
6+
7+
#include <../utils/android.h>
8+
9+
#define LOG_TAG "OVK-MPLAY-LIB"
10+
#define LOG_LEVEL 10
11+
#define LOGD(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__);}
12+
#define LOGI(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__);}
13+
#define LOGW(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__);}
14+
#define LOGE(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);}
15+
16+
#define AV_MAX_AUDIO_FRAME_SIZE 192000;
17+
18+
AVStream* gStream;
19+
20+
AudioDecoder::AudioDecoder(AVStream* pStream, PacketQueue *pPktQueue) {
21+
gStream = pStream;
22+
gPktQueue = pPktQueue;
23+
}
24+
25+
bool AudioDecoder::prepare() {
26+
gBufferSize = AV_MAX_AUDIO_FRAME_SIZE;
27+
gBuffer = (short*) av_malloc(gBufferSize);
28+
return gBuffer != NULL;
29+
}
30+
31+
bool AudioDecoder::decode(void* ptr) {
32+
AVPacket avPkt;
33+
34+
while(gRunning) {
35+
if(gPktQueue->get(&avPkt, true) < 0) {
36+
gRunning = false;
37+
return gRunning;
38+
}
39+
/*if(!process()) {
40+
gRunning = false;
41+
return gRunning;
42+
}*/
43+
// Free the packet that was allocated by av_read_frame
44+
av_free_packet(&avPkt);
45+
}
46+
47+
av_free(gBuffer);
48+
49+
return true;
50+
51+
/*AVFrame *pFrame = av_frame_alloc();
52+
int AUDIO_INBUF_SIZE = 4096;
53+
int decodedDataSize = 0,
54+
packetSize = avPkt.size,
55+
status = 0,
56+
tAudioFrames = 0;
57+
/*jclass jmPlay = env->GetObjectClass(instance);
58+
jmethodID renderAudioMid = env->GetMethodID(jmPlay, "renderAudio", "([BI)V");
59+
short* buffer = (short*)malloc(aBuffLength);
60+
jbyteArray jBuffer = env->NewByteArray(aBuffLength);
61+
62+
pFrame = av_frame_alloc();
63+
64+
if (debug_mode) {
65+
LOGD(10, "[DEBUG] Starting audio decoder...");
66+
}*/
67+
68+
/*while (gPlaybackState == FFMPEG_PLAYBACK_PLAYING &&
69+
(status = av_read_frame(gFormatCtx, &avPkt)) >= 0) {
70+
int len = avcodec_decode_audio4(
71+
gAudioCodecCtx,
72+
pFrame,
73+
&status,
74+
&avPkt
75+
);
76+
77+
if (status) {
78+
int dataSize = av_samples_get_buffer_size(NULL,
79+
gAudioCodecCtx->channels,
80+
pFrame->nb_samples,
81+
gAudioCodecCtx->sample_fmt,
82+
1);
83+
84+
if (debug_mode) {
85+
LOGD(10, "[DEBUG] Decoding audio frame #%d... | Length: %d of %d",
86+
tAudioFrames + 1, dataSize, aBuffLength
87+
);
88+
}
89+
90+
buffer = (short *) pFrame->data[0];
91+
env->SetByteArrayRegion(jBuffer, 0, (jsize) dataSize / gAudioCodecCtx->channels, (jbyte *) buffer);
92+
env->CallVoidMethod(instance, renderAudioMid, jBuffer, dataSize / gAudioCodecCtx->channels);
93+
}
94+
95+
tAudioFrames++;
96+
97+
av_free_packet(&avPkt);
98+
av_packet_unref(&avPkt);
99+
}
100+
101+
while (gPlaybackState == FFMPEG_PLAYBACK_PLAYING &&
102+
(status = av_read_frame(gFormatCtx, &avPkt)) >= 0) {
103+
if(avPkt->stream_index == pStreamIndex) {
104+
105+
}
106+
}*/
107+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Created by tretdm on 20.04.2024.
3+
//
4+
5+
#ifndef MOBILE_ANDROID_LEGACY_AUDIODEC_H
6+
#define MOBILE_ANDROID_LEGACY_AUDIODEC_H
7+
8+
#include <../utils/pktqueue.h>
9+
10+
typedef void (*DecoderHandler) (short*, int);
11+
12+
// FFmpeg implementation headers (using LGPLv3.0 model)
13+
extern "C" {
14+
#define __STDC_CONSTANT_MACROS // workaround for compiler
15+
#include <libavutil/avstring.h>
16+
#include <libavutil/pixdesc.h>
17+
#include <libavutil/imgutils.h>
18+
#include <libavutil/samplefmt.h>
19+
#include <libavformat/avformat.h>
20+
#include <libavformat/url.h>
21+
#include <libavformat/avio.h>
22+
#include <libswscale/swscale.h>
23+
#include <libavcodec/avcodec.h>
24+
#include <libavcodec/avfft.h>
25+
#include <libavdevice/avdevice.h>
26+
#include <libswresample/swresample.h>
27+
}
28+
29+
class AudioDecoder {
30+
public:
31+
AudioDecoder(AVStream* pStream, PacketQueue *pPktQueue);
32+
bool prepare();
33+
bool process();
34+
bool decode(void *ptr);
35+
DecoderHandler onDecode;
36+
int gBufferSize;
37+
short* gBuffer;
38+
bool gRunning;
39+
private:
40+
PacketQueue* gPktQueue;
41+
};
42+
43+
44+
45+
#endif //MOBILE_ANDROID_LEGACY_AUDIODEC_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// Created by tretdm on 20.04.2024.
3+
//
4+
5+
#include "videodec.h"
6+
7+
VideoDecoder::VideoDecoder(AVStream* stream, PacketQueue *pPktQueue) {
8+
9+
}
10+
11+
bool VideoDecoder::prepare() {
12+
gFrame = avcodec_alloc_frame();
13+
return gFrame != NULL;
14+
}
15+
16+
bool VideoDecoder::process(AVPacket *avPkt) {
17+
int completed;
18+
int pts = 0;
19+
20+
// TODO: Put FFmpeg decoding method here.
21+
22+
if (avPkt->dts == AV_NOPTS_VALUE && gFrame->opaque
23+
&& *(uint64_t*) gFrame->opaque != AV_NOPTS_VALUE) {
24+
pts = *(uint64_t *) gFrame->opaque;
25+
} else if (avPkt->dts != AV_NOPTS_VALUE) {
26+
pts = avPkt->dts;
27+
} else {
28+
pts = 0;
29+
}
30+
pts *= av_q2d(gStream->time_base);
31+
32+
if (completed) {
33+
//pts = synchronize(gFrame, pts);
34+
//onDecode(gFrame, pts);
35+
}
36+
return completed;
37+
}
38+
39+
bool VideoDecoder::decode() {
40+
AVPacket avPkt;
41+
42+
while(gRunning) {
43+
if(gPktQueue->get(&avPkt, true) < 0) {
44+
gRunning = false;
45+
return gRunning;
46+
}
47+
if(!process(&avPkt)) {
48+
gRunning = false;
49+
return gRunning;
50+
}
51+
// Free the packet that was allocated by av_read_frame
52+
av_free_packet(&avPkt);
53+
}
54+
55+
// Free the RGB image
56+
av_free(gFrame);
57+
58+
return true;
59+
}
60+
61+
int VideoDecoder::getBuffer(struct AVCodecContext *pCodecCtx, AVFrame *pFrame) {
62+
int ret = avcodec_default_get_buffer(pCodecCtx, pFrame);
63+
uint64_t *pts = (uint64_t *)av_malloc(sizeof(uint64_t));
64+
*pts = gPktPts;
65+
pFrame->opaque = pts;
66+
return ret;
67+
}
68+
69+
void VideoDecoder::releaseBuffer(struct AVCodecContext *pCodecCtx, AVFrame *pFrame) {
70+
if(pFrame)
71+
av_freep(&pFrame->opaque);
72+
avcodec_default_release_buffer(pCodecCtx, pFrame);
73+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Created by tretdm on 20.04.2024.
3+
//
4+
5+
#ifndef MOBILE_ANDROID_LEGACY_VIDEODEC_H
6+
#define MOBILE_ANDROID_LEGACY_VIDEODEC_H
7+
8+
#include <../utils/pktqueue.h>
9+
10+
// FFmpeg implementation headers (using LGPLv3.0 model)
11+
extern "C" {
12+
#define __STDC_CONSTANT_MACROS // workaround for compiler
13+
#include <libavutil/avstring.h>
14+
#include <libavutil/pixdesc.h>
15+
#include <libavutil/imgutils.h>
16+
#include <libavutil/samplefmt.h>
17+
#include <libavformat/avformat.h>
18+
#include <libavformat/url.h>
19+
#include <libavformat/avio.h>
20+
#include <libswscale/swscale.h>
21+
#include <libavcodec/avcodec.h>
22+
#include <libavcodec/avfft.h>
23+
#include <libavdevice/avdevice.h>
24+
#include <libswresample/swresample.h>
25+
}
26+
27+
28+
class VideoDecoder {
29+
public:
30+
VideoDecoder(AVStream* pStream, PacketQueue *pPktQueue);
31+
AVFrame *gFrame;
32+
AVStream *gStream;
33+
bool gRunning;
34+
uint64_t gPktPts;
35+
bool prepare();
36+
bool process(AVPacket *avPkt);
37+
bool decode();
38+
int getBuffer(struct AVCodecContext *pCodecCtx, AVFrame *pFrame);
39+
void releaseBuffer(struct AVCodecContext *pCodecCtx, AVFrame *pFrame);
40+
private:
41+
PacketQueue* gPktQueue;
42+
};
43+
44+
#endif //MOBILE_ANDROID_LEGACY_VIDEODEC_H

ndk-modules/ovkmplayer/ovkmplay.cpp

+6-51
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include <assert.h>
3232
#include <pthread.h>
3333

34-
#include <android.h>
34+
#include <utils/android.h>
3535

3636
// Non-standard 'stdint' implementation
3737
#pragma clang diagnostic push
@@ -223,58 +223,12 @@ JNIEXPORT jint JNICALL naOpenFile(JNIEnv *env, jobject instance, jstring filenam
223223
JNIEXPORT void JNICALL naDecodeAudioFromPacket( // Decoding audio packets
224224
JNIEnv* env, jobject instance, int aBuffLength
225225
) {
226-
AVPacket avPkt;
227-
AVFrame *pFrame = av_frame_alloc();
228-
int AUDIO_INBUF_SIZE = 4096;
229-
int decodedDataSize = 0,
230-
packetSize = avPkt.size,
231-
status = 0,
232-
tAudioFrames = 0;
233-
jclass jmPlay = env->GetObjectClass(instance);
234-
jmethodID renderAudioMid = env->GetMethodID(jmPlay, "renderAudio", "([BI)V");
235-
short* buffer = (short*)malloc(aBuffLength);
236-
jbyteArray jBuffer = env->NewByteArray(aBuffLength);
237-
238-
pFrame = av_frame_alloc();
239226

240-
if (debug_mode) {
241-
LOGD(10, "[DEBUG] Starting audio decoder...");
242-
}
243-
244-
while (gPlaybackState == FFMPEG_PLAYBACK_PLAYING &&
245-
(status = av_read_frame(gFormatCtx, &avPkt)) >= 0) {
246-
int len = avcodec_decode_audio4(
247-
gAudioCodecCtx,
248-
pFrame,
249-
&status,
250-
&avPkt
251-
);
252-
253-
if (status) {
254-
int dataSize = av_samples_get_buffer_size(NULL,
255-
gAudioCodecCtx->channels,
256-
pFrame->nb_samples,
257-
gAudioCodecCtx->sample_fmt,
258-
1);
259-
260-
if (debug_mode) {
261-
LOGD(10, "[DEBUG] Decoding audio frame #%d... | Length: %d of %d",
262-
tAudioFrames + 1, dataSize, aBuffLength
263-
);
264-
}
265-
266-
buffer = (short *) pFrame->data[0];
267-
env->SetByteArrayRegion(jBuffer, 0, (jsize) dataSize / gAudioCodecCtx->channels, (jbyte *) buffer);
268-
env->CallVoidMethod(instance, renderAudioMid, jBuffer, dataSize / gAudioCodecCtx->channels);
269-
}
270-
271-
tAudioFrames++;
272-
273-
av_free_packet(&avPkt);
274-
av_packet_unref(&avPkt);
275-
}
276227
}
277228

229+
/*env->SetByteArrayRegion(jBuffer, 0, (jsize) dataSize / gAudioCodecCtx->channels, (jbyte *) buffer);
230+
env->CallVoidMethod(instance, renderAudioMid, jBuffer, dataSize / gAudioCodecCtx->channels);*/
231+
278232

279233
JNIEXPORT uint8_t* JNICALL convertYuv2Rgb(AVPixelFormat pxf, AVFrame* frame, int length) {
280234
uint8_t *buffer = (uint8_t*) malloc((size_t)length);
@@ -328,7 +282,7 @@ JNIEXPORT void JNICALL naDecodeVideoFromPacket(
328282
LOGD(10, "[DEBUG] Starting video decoder...");
329283
}
330284

331-
while (gPlaybackState == FFMPEG_PLAYBACK_PLAYING &&
285+
/*while (gPlaybackState == FFMPEG_PLAYBACK_PLAYING &&
332286
(status = av_read_frame(gFormatCtx, &avPkt)) >= 0) {
333287
if (avPkt.stream_index == gVideoStreamIndex) {
334288
AVFrame *pFrame = av_frame_alloc(),
@@ -385,6 +339,7 @@ JNIEXPORT void JNICALL naDecodeVideoFromPacket(
385339
av_free_packet(&avPkt);
386340
av_packet_unref(&avPkt);
387341
}
342+
*/
388343
}
389344

390345
JNIEXPORT void JNICALL naPlay(JNIEnv *env, jobject instance) {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)