Skip to content

Commit

Permalink
renderer prepared but in use not yet
Browse files Browse the repository at this point in the history
  • Loading branch information
havlenapetr committed Aug 4, 2010
1 parent e23b13f commit 132ba26
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 7 deletions.
85 changes: 85 additions & 0 deletions jni/libmediaplayer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern "C" {

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

}

Expand Down Expand Up @@ -35,6 +36,8 @@ bool Renderer::init(JNIEnv *env, jobject jsurface)

bool Renderer::prepare(const char* err)
{
void* pixels;

if (Output::AudioDriver_set(MUSIC, mAudioStream->codec->sample_rate, PCM_16_BIT,
(mAudioStream->codec->channels == 2) ? CHANNEL_OUT_STEREO
: CHANNEL_OUT_MONO) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
Expand All @@ -45,6 +48,66 @@ bool Renderer::prepare(const char* err)
err = "Couldnt' start audio track";
return false;
}

mConvertCtx = sws_getContext(mVideoStream->codec->width,
mVideoStream->codec->height,
mVideoStream->codec->pix_fmt,
mVideoStream->codec->width,
mVideoStream->codec->height,
PIX_FMT_RGB565,
SWS_POINT,
NULL,
NULL,
NULL);

if (mConvertCtx == NULL) {
//err = "Couldn't allocate mConvertCtx";
return false;
}

if (Output::VideoDriver_getPixels(mVideoStream->codec->width,
mVideoStream->codec->height,
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
//err = "Couldn't get pixels from android surface wrapper";
return false;
}

// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *) mFrame, (uint8_t *) pixels, PIX_FMT_RGB565,
mVideoStream->codec->width, mVideoStream->codec->height);

return true;
}

void Renderer::enqueue(Event* event)
{
mEventBuffer.add(event);
notify();
}

bool Renderer::processAudio(Event* event)
{
AudioEvent* e = (AudioEvent *) event;
if (Output::AudioDriver_write(e->samples, e->samples_size) <= 0) {
return false;
}
return true;
}

bool Renderer::processVideo(Event* event)
{
VideoEvent* e = (VideoEvent *) event;
sws_scale(mConvertCtx,
e->frame->data,
e->frame->linesize,
0,
mVideoStream->codec->height,
mFrame->data,
mFrame->linesize);

Output::VideoDriver_updateSurface();
return true;
}

Expand All @@ -54,11 +117,33 @@ void Renderer::handleRun(void* ptr)

while(true)
{
waitOnNotify();

int length = mEventBuffer.size();
Event** events = mEventBuffer.editArray();
mEventBuffer.clear();

// execute this events
for (int i = 0; i < length; i++) {
Event *e = events[i];
if(e->type == EVENT_TYPE_VIDEO) {
processVideo(e);
}
else if(e->type == EVENT_TYPE_AUDIO) {
processAudio(e);
}
else {
__android_log_print(ANDROID_LOG_ERROR, TAG,
"Failed to encode event type: %i", e->type);
}
free(e);
}
}
}

void Renderer::stop()
{
notify();
__android_log_print(ANDROID_LOG_INFO, TAG, "waiting on end of renderer thread");
int ret = -1;
if((ret = wait()) != 0) {
Expand Down
27 changes: 22 additions & 5 deletions jni/libmediaplayer/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

using namespace android;

#define EVENT_TYPE_VIDEO 1;
#define EVENT_TYPE_AUDIO 2;
#define EVENT_TYPE_VIDEO 1
#define EVENT_TYPE_AUDIO 2

class Renderer : public Thread
{
Expand All @@ -25,17 +25,29 @@ class Renderer : public Thread

class VideoEvent : public Event
{
VideoEvent()
public:
AVFrame* frame;
double pts;

VideoEvent(AVFrame* f, double p)
{
type = EVENT_TYPE_VIDEO;
frame = f;
pts = p;
}
};

class AudioEvent : public Event
{
AudioEvent()
public:
int16_t* samples;
int samples_size;

AudioEvent(int16_t* data, int data_size)
{
type = EVENT_TYPE_AUDIO;
samples = data;
samples_size = data_size;
}
};

Expand All @@ -53,7 +65,12 @@ class Renderer : public Thread
AVStream* mVideoStream;
Vector<Event*> mEventBuffer;

AVFrame* mFrame;
struct SwsContext* mConvertCtx;

bool processAudio(Event* event);
bool processVideo(Event* event);
void handleRun(void* ptr);
};

#endif //FFMPEG_DECODER_H
#endif //FFMPEG_RENDERER_H
24 changes: 24 additions & 0 deletions jni/libmediaplayer/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

#define TAG "FFMpegThread"

Thread::Thread()
{
pthread_mutex_init(&mLock, NULL);
pthread_cond_init(&mCondition, NULL);
}

Thread::~Thread()
{
}

void Thread::start()
{
handleRun(NULL);
Expand Down Expand Up @@ -36,6 +46,20 @@ void* Thread::startThread(void* ptr)
__android_log_print(ANDROID_LOG_INFO, TAG, "thread ended");
}

void Thread::waitOnNotify()
{
pthread_mutex_lock(&mLock);
pthread_cond_wait(&mCondition, &mLock);
pthread_mutex_unlock(&mLock);
}

void Thread::notify()
{
pthread_mutex_lock(&mLock);
pthread_cond_signal(&mCondition);
pthread_mutex_unlock(&mLock);
}

void Thread::handleRun(void* ptr)
{
}
13 changes: 11 additions & 2 deletions jni/libmediaplayer/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@
class Thread
{
public:
Thread();
~Thread();

void start();
void startAsync();
int wait();

void waitOnNotify();
void notify();
virtual void stop();

protected:
pthread_t mThread;
bool mRunning;
bool mRunning;

virtual void handleRun(void* ptr);

private:
pthread_t mThread;
pthread_mutex_t mLock;
pthread_cond_t mCondition;

static void* startThread(void* ptr);
};

Expand Down

0 comments on commit 132ba26

Please sign in to comment.