Skip to content

Commit

Permalink
MythMediaCodecInterop: Add SurfaceTextureListener implementation
Browse files Browse the repository at this point in the history
- note: the java class is part of the packaging repo
  • Loading branch information
mark-kendall committed Mar 5, 2019
1 parent 5ee9fac commit d545b67
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
1 change: 0 additions & 1 deletion mythtv/libs/libmythtv/mythmediacodeccontext.cpp
Expand Up @@ -211,7 +211,6 @@ int MythMediaCodecContext::GetBuffer(struct AVCodecContext *Context, AVFrame *Fr

// Frame->buf[0] contains the release method. Take another reference to ensure the frame
// is not released before it is displayed.
LOG(VB_GENERAL, LOG_INFO, LOC + QString("Buffer ref count %1").arg(av_buffer_get_ref_count(Frame->buf[0])));
videoframe->priv[0] = reinterpret_cast<unsigned char*>(av_buffer_ref(Frame->buf[0]));

// Retrieve and set the interop class
Expand Down
40 changes: 19 additions & 21 deletions mythtv/libs/libmythtv/mythmediacodecinterop.cpp
Expand Up @@ -30,7 +30,8 @@ MythMediaCodecInterop::MythMediaCodecInterop(MythRenderOpenGL* Context)
m_frameWaitLock(),
m_colourSpaceInitialised(false),
m_surface(),
m_surfaceTexture()
m_surfaceTexture(),
m_surfaceListener()
{
}

Expand All @@ -43,6 +44,13 @@ void* MythMediaCodecInterop::GetSurface(void)
return m_surface.object();
}

void Java_org_mythtv_android_SurfaceTextureListener_frameAvailable(JNIEnv*, jobject, jlong Wait, jobject)
{
QWaitCondition *wait = reinterpret_cast<QWaitCondition*>(Wait);
if (wait)
wait->wakeAll();
}

bool MythMediaCodecInterop::Initialise(QSize Size)
{
if (!m_context)
Expand Down Expand Up @@ -73,12 +81,13 @@ bool MythMediaCodecInterop::Initialise(QSize Size)

// Create surface
m_surfaceTexture = QAndroidJniObject("android/graphics/SurfaceTexture", "(I)V", texture->m_textureId);
if (m_surfaceTexture.isValid())
//N.B. org/mythtv/android/SurfaceTextureListener is found in the packaging repo
m_surfaceListener = QAndroidJniObject("org/mythtv/android/SurfaceTextureListener", "(J)V", jlong(&m_frameWait));
if (m_surfaceTexture.isValid() && m_surfaceListener.isValid())
{
//m_surfaceTexture.callMethod<void>("setOnFrameAvailableListener",
// "(Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;)V",
// QAndroidJniObject("com/mythtv/android/SurfaceTextureListener",
// "(J)V", jlong(&m_frameWait)).object());
m_surfaceTexture.callMethod<void>("setOnFrameAvailableListener",
"(Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;)V",
m_surfaceListener.object());
m_surface = QAndroidJniObject("android/view/Surface",
"(Landroid/graphics/SurfaceTexture;)V",
m_surfaceTexture.object());
Expand All @@ -95,19 +104,9 @@ bool MythMediaCodecInterop::Initialise(QSize Size)
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to create Android SurfaceTexture");
m_context->glDeleteTextures(1, &(texture->m_textureId));
m_context->DeleteTexture(texture);

return false;
}


//extern "C" void Java_com_mythtv_android_SurfaceTextureListener_frameAvailable(JNIEnv*, jobject, jlong Wait, jobject)
//{
// LOG(VB_GENERAL, LOG_INFO, LOC + "frameAvailable");
// QWaitCondition *wait = reinterpret_cast<QWaitCondition*>(Wait);
// if (wait)
// wait->wakeAll();
//}

vector<MythGLTexture*> MythMediaCodecInterop::Acquire(MythRenderOpenGL *Context,
VideoColourSpace *ColourSpace,
VideoFrame *Frame,
Expand Down Expand Up @@ -143,15 +142,14 @@ vector<MythGLTexture*> MythMediaCodecInterop::Acquire(MythRenderOpenGL *Context,
}

// Render...
LOG(VB_GENERAL, LOG_INFO, LOC + "Release to render...");
m_frameWaitLock.lock();
if (av_mediacodec_release_buffer(buffer, 1) < 0)
LOG(VB_GENERAL, LOG_ERR , LOC + "av_mediacodec_release_buffer failed");

// Wait for completion
//m_frameWaitLock.lock();
//if (!m_frameWait.wait(&m_frameWaitLock, 50))
// LOG(VB_GENERAL, LOG_WARNING, LOC + "Timed out waiting for frame update");
//m_frameWaitLock.unlock();
if (!m_frameWait.wait(&m_frameWaitLock, 10))
LOG(VB_GENERAL, LOG_WARNING, LOC + "Timed out waiting for frame update");
m_frameWaitLock.unlock();

// Ensure we don't try and release it again
Frame->buf = nullptr;
Expand Down
3 changes: 3 additions & 0 deletions mythtv/libs/libmythtv/mythmediacodecinterop.h
Expand Up @@ -9,6 +9,8 @@
// MythTV
#include "mythopenglinterop.h"

extern "C" MTV_PUBLIC void Java_org_mythtv_android_SurfaceTextureListener_frameAvailable(JNIEnv*, jobject, jlong Wait, jobject);

class MythMediaCodecInterop : public MythOpenGLInterop
{
public:
Expand All @@ -29,6 +31,7 @@ class MythMediaCodecInterop : public MythOpenGLInterop
bool m_colourSpaceInitialised;
QAndroidJniObject m_surface;
QAndroidJniObject m_surfaceTexture;
QAndroidJniObject m_surfaceListener;
};

#endif // MYTHMEDIACODECINTEROP_H

0 comments on commit d545b67

Please sign in to comment.