Skip to content

Commit

Permalink
Improve android media player, also fix #2101
Browse files Browse the repository at this point in the history
a. handle cbcr offset properly
b. handle output dim properly
  • Loading branch information
halx99 committed Aug 28, 2024
1 parent 5851603 commit 63f677c
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 134 deletions.
92 changes: 55 additions & 37 deletions core/media/AndroidMediaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,77 @@
# include "platform/android/jni/JniHelper.h"

extern "C" {
JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeHandleEvent(JNIEnv* env, jclass, jlong pME, int arg1)
JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeFireEvent(JNIEnv* env, jclass, jlong pME, int arg1)
{
auto mediaEngine = (ax::AndroidMediaEngine*)((uintptr_t)pME);
if (!mediaEngine)
return;

mediaEngine->fireMediaEvent((ax::MEMediaEventType)arg1);
mediaEngine->_fireMediaEvent((ax::MEMediaEventType)arg1);
}
JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeHandleVideoSample(JNIEnv* env,
jclass,
jlong pME,
jobject sampleBuffer,
int sampleLen,
int outputX,
int outputY,
int videoX,
int videoY,
int rotation,
int videoPF)

JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeStoreVideoMeta(JNIEnv* env,
jclass,
jlong pME,
int outputX,
int outputY,
int videoX,
int videoY,
int cbcrOffset,
int rotation,
int videoPF)
{
auto mediaEngine = (ax::AndroidMediaEngine*)((uintptr_t)pME);
if (!mediaEngine)
return;

mediaEngine->_storeVideoMeta(outputX, outputY, videoX, videoY, cbcrOffset, rotation, videoPF);
}

JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeStoreLastVideoSample(JNIEnv* env,
jclass,
jlong pME,
jobject sampleBuffer,
int sampleLen)
{
auto mediaEngine = (ax::AndroidMediaEngine*)((uintptr_t)pME);
if (!mediaEngine)
return;

auto sampleData = static_cast<uint8_t*>(env->GetDirectBufferAddress(sampleBuffer));

mediaEngine->handleVideoSample(sampleData, sampleLen, outputX, outputY, videoX, videoY, rotation, videoPF);
mediaEngine->_storeLastVideoSample(sampleData, sampleLen);
}

JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeSetDuration(JNIEnv* env,
jclass,
jlong pME,
double duration)
JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeStoreDuration(JNIEnv* env,
jclass,
jlong pME,
double duration)
{
auto mediaEngine = (ax::AndroidMediaEngine*)((uintptr_t)pME);
if (!mediaEngine)
return;

mediaEngine->updateDuration(duration);
mediaEngine->_storeDuration(duration);
}

JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeSetCurrentTime(JNIEnv* env,
jclass,
jlong pME,
double currentTime)
JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolMediaEngine_nativeStoreCurrentTime(JNIEnv* env,
jclass,
jlong pME,
double currentTime)
{
auto mediaEngine = (ax::AndroidMediaEngine*)((uintptr_t)pME);
if (!mediaEngine)
return;

mediaEngine->updateCurrentTime(currentTime);
mediaEngine->_storeCurrentTime(currentTime);
}
}

namespace ax
{

static const char* className = "org.axmol.lib.AxmolMediaEngine";
static const char* className = "org.axmol.lib.AxmolMediaEngine";

AndroidMediaEngine::AndroidMediaEngine()
{
Expand Down Expand Up @@ -162,8 +175,9 @@ bool AndroidMediaEngine::transferVideoFrame()

auto& buffer = _frameBuffer2;

ax::MEVideoFrame frame{buffer.data(), buffer.data() + _outputDim.x * _outputDim.y, buffer.size(),
ax::MEVideoPixelDesc{static_cast<ax::MEVideoPixelFormat>(_videoPF), _outputDim}, _videoDim};
ax::MEVideoFrame frame{buffer.data(), buffer.data() + _cbcrOffset, buffer.size(),
ax::MEVideoPixelDesc{static_cast<ax::MEVideoPixelFormat>(_videoPF), _outputDim},
_videoDim};
frame._vpd._rotation = _videoRotation;
assert(static_cast<int>(frame._dataLen) >= frame._vpd._dim.x * frame._vpd._dim.y * 3 / 2);
_onVideoFrame(frame);
Expand All @@ -174,23 +188,27 @@ bool AndroidMediaEngine::transferVideoFrame()
return false;
}

void AndroidMediaEngine::handleVideoSample(const uint8_t* buf,
size_t len,
int outputX,
int outputY,
int videoX,
int videoY,
int rotation,
int videoPF)
void AndroidMediaEngine::_storeVideoMeta(int outputX,
int outputY,
int videoX,
int videoY,
int cbcrOffset,
int rotation,
int videoPF)
{
std::unique_lock<std::mutex> lck(_frameBuffer1Mtx);
_frameBuffer1.assign(buf, buf + len);
_outputDim.set(outputX, outputY);
_videoDim.set(videoX, videoY);
_cbcrOffset = cbcrOffset;
_videoRotation = rotation;
_videoPF = videoPF;
}

void AndroidMediaEngine::_storeLastVideoSample(const uint8_t* buf, size_t len)
{
std::unique_lock<std::mutex> lck(_frameBuffer1Mtx);
_frameBuffer1.assign(buf, buf + len);
}

} // namespace ax

#endif
19 changes: 11 additions & 8 deletions core/media/AndroidMediaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ class AndroidMediaEngine : public MediaEngine
public:
AndroidMediaEngine();
~AndroidMediaEngine();
void fireMediaEvent(MEMediaEventType event)
{
if (_onMediaEvent)
_onMediaEvent(event);
}
void setCallbacks(std::function<void(MEMediaEventType)> onMediaEvent,
std::function<void(const MEVideoFrame&)> onVideoFrame) override
{
Expand All @@ -60,9 +55,16 @@ class AndroidMediaEngine : public MediaEngine
MEMediaState getState() const override;
bool transferVideoFrame() override;

void handleVideoSample(const uint8_t* buf, size_t len, int outputX, int outputY, int videoX, int videoY, int rotation, int videoPF);
void updateCurrentTime(double currentTime) { _currentTime = currentTime; }
void updateDuration(double duration) { _duration = duration; }
// internal stubs
void _fireMediaEvent(MEMediaEventType event)
{
if (_onMediaEvent)
_onMediaEvent(event);
}
void _storeDuration(double duration) { _duration = duration; }
void _storeCurrentTime(double currentTime) { _currentTime = currentTime; }
void _storeLastVideoSample(const uint8_t* buf, size_t len);
void _storeVideoMeta(int outputX, int outputY, int videoX, int videoY, int cbcrOffset, int rotation, int videoPF);

private:
void* context{}; // java object strong-refs
Expand All @@ -71,6 +73,7 @@ class AndroidMediaEngine : public MediaEngine

MEIntPoint _outputDim;
MEIntPoint _videoDim;
int _cbcrOffset{0};
int _videoRotation{0};
int _videoPF{-1};

Expand Down
Loading

0 comments on commit 63f677c

Please sign in to comment.