Skip to content

Commit 919aae7

Browse files
committed
fwat(ovkmplayer): Add AVPacket's synchronization time getter
1 parent dbae6b7 commit 919aae7

File tree

8 files changed

+76
-56
lines changed

8 files changed

+76
-56
lines changed

app/src/main/java/uk/openvk/android/legacy/utils/media/OvkMediaPlayer.java

+14-50
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.graphics.RectF;
3232
import android.media.AudioFormat;
3333
import android.media.AudioManager;
34+
import android.media.AudioRecord;
3435
import android.media.AudioTrack;
3536
import android.media.MediaPlayer;
3637
import android.os.Build;
@@ -74,6 +75,7 @@ public class OvkMediaPlayer extends MediaPlayer {
7475
private String dataSourceUrl;
7576
private ArrayList<OvkMediaTrack> tracks;
7677
private SurfaceHolder holder;
78+
private int minAudioBufferSize;
7779
private int minVideoBufferSize;
7880
private OnPreparedListener onPreparedListener;
7981
private OnErrorListener onErrorListener;
@@ -90,8 +92,8 @@ public class OvkMediaPlayer extends MediaPlayer {
9092
private native int naOpenFile(String filename);
9193
private native Object naGenerateTrackInfo(int type);
9294
// private native void naSetMinAudioBufferSize(int audioBufferSize);
93-
// private native void naDecodeVideoFromPacket();
94-
// private native void naDecodeAudioFromPacket(int aBuffLength);
95+
private native void naStartAudioDecoding();
96+
private native void naStartVideoDecoding();
9597
private native void naPlay();
9698
private native void naPause();
9799
private native void naStop();
@@ -219,7 +221,6 @@ public void prepare() throws IllegalStateException {
219221
onErrorListener.onError(this, -1);
220222
} else {
221223
getMediaInfo();
222-
223224
}
224225
}
225226

@@ -237,12 +238,7 @@ public void run() {
237238
@Override
238239
public void start() throws IllegalStateException {
239240
if(tracks != null) {
240-
new Thread(new Runnable() {
241-
@Override
242-
public void run() {
243-
naPlay();
244-
}
245-
}).start();
241+
naPlay();
246242
Log.d(MPLAY_TAG, "Playing...");
247243
OvkAudioTrack audio_track = null;
248244
OvkVideoTrack video_track = null;
@@ -255,41 +251,9 @@ public void run() {
255251
}
256252
final OvkAudioTrack finalAudioTrack = audio_track;
257253
final OvkVideoTrack finalVideoTrack = video_track;
258-
// new Thread(new Runnable() {
259-
// @Override
260-
// public void run() {
261-
// if(finalAudioTrack != null) {
262-
// int ch_config = finalAudioTrack.channels == 2 ?
263-
// AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
264-
// Log.d(MPLAY_TAG, "Decoding audio track...");
265-
// try {
266-
// naDecodeAudioFromPacket(AudioTrack.getMinBufferSize(
267-
// (int) finalAudioTrack.sample_rate, ch_config, AudioFormat.ENCODING_PCM_16BIT
268-
// ));
269-
// } catch (OutOfMemoryError oom) {
270-
// stop();
271-
// }
272-
// } else {
273-
// Log.e(MPLAY_TAG, "Audio stream not found. Skipping...");
274-
// }
275-
// }
276-
// }).start();
277-
278-
// new Thread(new Runnable() {
279-
// @Override
280-
// public void run() {
281-
// if(finalVideoTrack != null) {
282-
// Log.d(MPLAY_TAG, "Decoding video track...");
283-
// try {
284-
// naDecodeVideoFromPacket();
285-
// } catch (OutOfMemoryError oom) {
286-
// stop();
287-
// }
288-
// } else {
289-
// Log.e(MPLAY_TAG, "Video stream not found. Skipping...");
290-
// }
291-
// }
292-
// }).start();
254+
naStartAudioDecoding();
255+
256+
naStartVideoDecoding();
293257
}
294258
}
295259

@@ -300,6 +264,7 @@ private void renderAudio(final byte[] buffer, final int length) {
300264
Log.e(MPLAY_TAG, "Audio buffer is empty");
301265
return;
302266
}
267+
303268
if (!prepared_audio_buffer) {
304269
for (int tracks_index = 0; tracks_index < tracks.size(); tracks_index++) {
305270
if (tracks.get(tracks_index) instanceof OvkAudioTrack) {
@@ -317,6 +282,11 @@ private void renderAudio(final byte[] buffer, final int length) {
317282
ch_config,
318283
AudioFormat.ENCODING_PCM_16BIT, length * 2, AudioTrack.MODE_STREAM);
319284

285+
minAudioBufferSize = AudioRecord.getMinBufferSize(
286+
(int) (track.sample_rate / 2),
287+
ch_config,
288+
AudioFormat.ENCODING_PCM_16BIT);
289+
320290
audio_track.play();
321291
prepared_audio_buffer = true;
322292
}
@@ -388,11 +358,6 @@ private void renderVideo(final byte[] buffer, final int length) {
388358
oom.printStackTrace();
389359
stop();
390360
}
391-
try {
392-
Thread.sleep((long) (1000 / track.frame_rate));
393-
} catch (InterruptedException e) {
394-
e.printStackTrace();
395-
}
396361
}
397362
}
398363
}
@@ -423,7 +388,6 @@ private int getPlaybackState() {
423388

424389
public void onResult(int cmdId, int resultCode) {
425390
if(cmdId == FFMPEG_COMMAND_OPEN_CODECS) {
426-
427391
if(getMediaInfo() == null) {
428392
Log.e(MPLAY_TAG, String.format("Can't open file: %s", dataSourceUrl));
429393
Message msg = new Message();

ndk-modules/ovkmplayer/decoders/audiodec.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void *AudioDecoder::decodeInThread() {
5555
gCodecCtx->sample_fmt,
5656
1
5757
);
58+
5859
memcpy(gBuffer, pFrame->data[0], dataSize);
5960
gInterface->onStreamDecoding((uint8_t*)gBuffer, dataSize / 2, gStreamIndex);
6061
}
@@ -77,3 +78,7 @@ bool AudioDecoder::stop() {
7778
avcodec_close(gCodecCtx);
7879
return true;
7980
}
81+
82+
double AudioDecoder::getPacketTime(AVPacket avPkt) {
83+
return (avPkt.dts - gStream->start_time) * av_q2d(gStream->time_base);
84+
}

ndk-modules/ovkmplayer/decoders/audiodec.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class AudioDecoder {
7070
AVFormatContext *gFormatCtx;
7171
AVCodecContext *gCodecCtx;
7272
IFFmpegWrapper *gInterface;
73-
bool start();
74-
bool stop();
75-
void* decodeInThread();
73+
bool start();
74+
bool stop();
75+
void* decodeInThread();
76+
double getPacketTime(AVPacket avPkt);
7677
private:
7778
PacketQueue* gPktQueue;
7879
};

ndk-modules/ovkmplayer/decoders/videodec.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void *VideoDecoder::decodeInThread() {
6060
convertYuv2Rgb(pxf, gFrame, dataSize);
6161
tVideoFrames++;
6262
gInterface->onStreamDecoding((uint8_t*)gBuffer, dataSize, gStreamIndex);
63+
sleep((unsigned int)getPacketTime());
6364
}
6465
av_free(gFrame);
6566
// Free the packet that was allocated by av_read_frame
@@ -111,3 +112,7 @@ bool VideoDecoder::stop() {
111112
av_free(gBuffer);
112113
return true;
113114
}
115+
116+
double VideoDecoder::getPacketTime(AVPacket avPkt) {
117+
return (avPkt->dts - gStream->start_time) * av_q2d(gStream->time_base);
118+
}

ndk-modules/ovkmplayer/decoders/videodec.h

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C"{
3636
#endif
3737

3838
#include <../interfaces/ffwrap.h>
39+
#include <unistd.h>
3940

4041
// FFmpeg implementation headers (using LGPLv3.0 model)
4142
extern "C" {
@@ -76,6 +77,7 @@ class VideoDecoder {
7677
bool stop();
7778
void* decodeInThread();
7879
short* convertYuv2Rgb(AVPixelFormat pxf, AVFrame* frame, int length);
80+
double getPacketTime(AVPacket avPkt);
7981
private:
8082
PacketQueue* gPktQueue;
8183
};

ndk-modules/ovkmplayer/ovkmplay.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ JNIEXPORT void JNICALL naPlay(JNIEnv *env, jobject instance, int streamType) {
124124
gVMArgs.name = NULL;
125125
gVMArgs.group = NULL;
126126
gWrapper->setPlaybackState(FFMPEG_PLAYBACK_PLAYING);
127-
gWrapper->startDecoding();
127+
//gWrapper->startDecoding();
128+
}
129+
130+
JNIEXPORT void JNICALL naStartAudioDecoding(JNIEnv *env, jobject instance) {
131+
gWrapper->startDecoding(gWrapper->gAudioStreamIndex);
132+
}
133+
134+
JNIEXPORT void JNICALL naStartVideoDecoding(JNIEnv *env, jobject instance) {
135+
gWrapper->startDecoding(gWrapper->gVideoStreamIndex);
128136
}
129137

130138
JNIEXPORT void JNICALL naPause(JNIEnv *env, jobject instance) {
@@ -303,7 +311,7 @@ jint JNI_OnLoad(JavaVM* pVm, void* reserved) {
303311
if (pVm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
304312
return -1;
305313
}
306-
JNINativeMethod nm[9];
314+
JNINativeMethod nm[11];
307315

308316
nm[0].name = "naInit";
309317
nm[0].signature = "()V";
@@ -341,9 +349,17 @@ jint JNI_OnLoad(JavaVM* pVm, void* reserved) {
341349
nm[8].signature = "()V";
342350
nm[8].fnPtr = (void*)naStop;
343351

352+
nm[9].name = "naStartAudioDecoding";
353+
nm[9].signature = "()V";
354+
nm[9].fnPtr = (void*)naStartAudioDecoding;
355+
356+
nm[10].name = "naStartVideoDecoding";
357+
nm[10].signature = "()V";
358+
nm[10].fnPtr = (void*)naStartVideoDecoding;
359+
344360
jclass cls = env->FindClass("uk/openvk/android/legacy/utils/media/OvkMediaPlayer");
345361
//Register methods with env->RegisterNatives.
346-
env->RegisterNatives(cls, nm, 9);
362+
env->RegisterNatives(cls, nm, 11);
347363

348364
gVM = pVm;
349365

ndk-modules/ovkmplayer/utils/ffwrap.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,30 @@ void FFmpegWrapper::startDecoding() {
240240
pthread_create(&audioDecThread, NULL, &audioDecoderThread, (void*)audioDec);
241241
pthread_t videoDecThread;
242242
pthread_create(&videoDecThread, NULL, &videoDecoderThread, (void*)videoDec);
243+
}
244+
245+
void FFmpegWrapper::startDecoding(int pStreamIndex) {
246+
if(pStreamIndex == gAudioStreamIndex) {
247+
AudioDecoder *audioDec = new AudioDecoder(
248+
gFormatCtx,
249+
gAudioCodecCtx,
250+
getStream(gAudioStreamIndex),
251+
gAudioStreamIndex,
252+
gInterface
253+
);
254+
255+
pthread_t audioDecThread;
256+
pthread_create(&audioDecThread, NULL, &audioDecoderThread, (void*)audioDec);
257+
} else if(pStreamIndex == gVideoStreamIndex) {
258+
VideoDecoder *videoDec = new VideoDecoder(
259+
gFormatCtx,
260+
gVideoCodecCtx,
261+
getStream(gVideoStreamIndex),
262+
gVideoStreamIndex,
263+
gInterface
264+
);
265+
266+
pthread_t videoDecThread;
267+
pthread_create(&videoDecThread, NULL, &videoDecoderThread, (void*)videoDec);
268+
}
243269
}

ndk-modules/ovkmplayer/utils/ffwrap.h

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class FFmpegWrapper {
8888
AVStream* getStream(int index);
8989
void openCodecs();
9090
void startDecoding();
91+
void startDecoding(int pStreamIndex);
9192

9293
private:
9394
IFFmpegWrapper *gInterface;

0 commit comments

Comments
 (0)