Skip to content

Commit

Permalink
add methods to renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
havlenapetr committed Aug 3, 2010
1 parent 9ee92bd commit bb8d6b6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
73 changes: 73 additions & 0 deletions jni/libmediaplayer/renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,89 @@
#include <android/log.h>

extern "C" {

#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"

}

#include "output.h"
#include "renderer.h"

#define TAG "FFMpegRenderer"

Renderer::Renderer()
{
mQueue = new PacketQueue();
}

Renderer::~Renderer()
{
}

bool Renderer::init(JNIEnv *env, jobject jsurface)
{
if(Output::VideoDriver_register(env, jsurface) != ANDROID_SURFACE_RESULT_SUCCESS) {
return false;
}
if(Output::AudioDriver_register() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
return false;
}
return true;
}

PacketQueue* Renderer::queue()
{
return mQueue;
}

bool Renderer::startAsync(const char* err)
{
pthread_create(&mThread, NULL, startRendering, this);
return true;
}

void* Renderer::startRendering(void* ptr)
{
__android_log_print(ANDROID_LOG_INFO, TAG, "starting renderer thread");
Renderer* renderer = (Renderer *) ptr;
renderer->mRendering = true;
renderer->render(ptr);
renderer->mRendering = false;
__android_log_print(ANDROID_LOG_INFO, TAG, "decoder renderer ended");
}

bool Renderer::render(void* ptr)
{
AVPacket pPacket;

while(mRendering)
{
if(mQueue->get(&pPacket, true) < 0)
{
mRendering = false;
return false;
}

// Free the packet that was allocated by av_read_frame
av_free_packet(&pPacket);
}

return true;
}

int Renderer::wait()
{
return pthread_join(mThread, NULL);
}

void Renderer::stop()
{
mQueue->abort();
__android_log_print(ANDROID_LOG_INFO, TAG, "waiting on end of renderer thread");
int ret = -1;
if((ret = wait()) != 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel renderer: %i", ret);
return;
}
}
15 changes: 15 additions & 0 deletions jni/libmediaplayer/renderer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef FFMPEG_RENDERER_H
#define FFMPEG_RENDERER_H

#include <jni.h>
#include <pthread.h>

#include "packetqueue.h"
Expand All @@ -10,6 +11,20 @@ class Renderer
public:
Renderer();
~Renderer();

bool init(JNIEnv *env, jobject jsurface);
PacketQueue* queue();
bool startAsync(const char* err);
int wait();
void stop();

private:
PacketQueue* mQueue;
pthread_t mThread;
bool mRendering;

bool render(void* ptr);
static void* startRendering(void* ptr);
};

#endif //FFMPEG_DECODER_H

0 comments on commit bb8d6b6

Please sign in to comment.