Skip to content

Commit 132ba26

Browse files
committed
renderer prepared but in use not yet
1 parent e23b13f commit 132ba26

File tree

4 files changed

+142
-7
lines changed

4 files changed

+142
-7
lines changed

jni/libmediaplayer/renderer.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern "C" {
44

55
#include "libavcodec/avcodec.h"
66
#include "libavformat/avformat.h"
7+
#include "libswscale/swscale.h"
78

89
}
910

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

3637
bool Renderer::prepare(const char* err)
3738
{
39+
void* pixels;
40+
3841
if (Output::AudioDriver_set(MUSIC, mAudioStream->codec->sample_rate, PCM_16_BIT,
3942
(mAudioStream->codec->channels == 2) ? CHANNEL_OUT_STEREO
4043
: CHANNEL_OUT_MONO) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
@@ -45,6 +48,66 @@ bool Renderer::prepare(const char* err)
4548
err = "Couldnt' start audio track";
4649
return false;
4750
}
51+
52+
mConvertCtx = sws_getContext(mVideoStream->codec->width,
53+
mVideoStream->codec->height,
54+
mVideoStream->codec->pix_fmt,
55+
mVideoStream->codec->width,
56+
mVideoStream->codec->height,
57+
PIX_FMT_RGB565,
58+
SWS_POINT,
59+
NULL,
60+
NULL,
61+
NULL);
62+
63+
if (mConvertCtx == NULL) {
64+
//err = "Couldn't allocate mConvertCtx";
65+
return false;
66+
}
67+
68+
if (Output::VideoDriver_getPixels(mVideoStream->codec->width,
69+
mVideoStream->codec->height,
70+
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
71+
//err = "Couldn't get pixels from android surface wrapper";
72+
return false;
73+
}
74+
75+
// Assign appropriate parts of buffer to image planes in pFrameRGB
76+
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
77+
// of AVPicture
78+
avpicture_fill((AVPicture *) mFrame, (uint8_t *) pixels, PIX_FMT_RGB565,
79+
mVideoStream->codec->width, mVideoStream->codec->height);
80+
81+
return true;
82+
}
83+
84+
void Renderer::enqueue(Event* event)
85+
{
86+
mEventBuffer.add(event);
87+
notify();
88+
}
89+
90+
bool Renderer::processAudio(Event* event)
91+
{
92+
AudioEvent* e = (AudioEvent *) event;
93+
if (Output::AudioDriver_write(e->samples, e->samples_size) <= 0) {
94+
return false;
95+
}
96+
return true;
97+
}
98+
99+
bool Renderer::processVideo(Event* event)
100+
{
101+
VideoEvent* e = (VideoEvent *) event;
102+
sws_scale(mConvertCtx,
103+
e->frame->data,
104+
e->frame->linesize,
105+
0,
106+
mVideoStream->codec->height,
107+
mFrame->data,
108+
mFrame->linesize);
109+
110+
Output::VideoDriver_updateSurface();
48111
return true;
49112
}
50113

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

55118
while(true)
56119
{
120+
waitOnNotify();
121+
122+
int length = mEventBuffer.size();
123+
Event** events = mEventBuffer.editArray();
124+
mEventBuffer.clear();
125+
126+
// execute this events
127+
for (int i = 0; i < length; i++) {
128+
Event *e = events[i];
129+
if(e->type == EVENT_TYPE_VIDEO) {
130+
processVideo(e);
131+
}
132+
else if(e->type == EVENT_TYPE_AUDIO) {
133+
processAudio(e);
134+
}
135+
else {
136+
__android_log_print(ANDROID_LOG_ERROR, TAG,
137+
"Failed to encode event type: %i", e->type);
138+
}
139+
free(e);
140+
}
57141
}
58142
}
59143

60144
void Renderer::stop()
61145
{
146+
notify();
62147
__android_log_print(ANDROID_LOG_INFO, TAG, "waiting on end of renderer thread");
63148
int ret = -1;
64149
if((ret = wait()) != 0) {

jni/libmediaplayer/renderer.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
using namespace android;
1212

13-
#define EVENT_TYPE_VIDEO 1;
14-
#define EVENT_TYPE_AUDIO 2;
13+
#define EVENT_TYPE_VIDEO 1
14+
#define EVENT_TYPE_AUDIO 2
1515

1616
class Renderer : public Thread
1717
{
@@ -25,17 +25,29 @@ class Renderer : public Thread
2525

2626
class VideoEvent : public Event
2727
{
28-
VideoEvent()
28+
public:
29+
AVFrame* frame;
30+
double pts;
31+
32+
VideoEvent(AVFrame* f, double p)
2933
{
3034
type = EVENT_TYPE_VIDEO;
35+
frame = f;
36+
pts = p;
3137
}
3238
};
3339

3440
class AudioEvent : public Event
3541
{
36-
AudioEvent()
42+
public:
43+
int16_t* samples;
44+
int samples_size;
45+
46+
AudioEvent(int16_t* data, int data_size)
3747
{
3848
type = EVENT_TYPE_AUDIO;
49+
samples = data;
50+
samples_size = data_size;
3951
}
4052
};
4153

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

68+
AVFrame* mFrame;
69+
struct SwsContext* mConvertCtx;
70+
71+
bool processAudio(Event* event);
72+
bool processVideo(Event* event);
5673
void handleRun(void* ptr);
5774
};
5875

59-
#endif //FFMPEG_DECODER_H
76+
#endif //FFMPEG_RENDERER_H

jni/libmediaplayer/thread.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33

44
#define TAG "FFMpegThread"
55

6+
Thread::Thread()
7+
{
8+
pthread_mutex_init(&mLock, NULL);
9+
pthread_cond_init(&mCondition, NULL);
10+
}
11+
12+
Thread::~Thread()
13+
{
14+
}
15+
616
void Thread::start()
717
{
818
handleRun(NULL);
@@ -36,6 +46,20 @@ void* Thread::startThread(void* ptr)
3646
__android_log_print(ANDROID_LOG_INFO, TAG, "thread ended");
3747
}
3848

49+
void Thread::waitOnNotify()
50+
{
51+
pthread_mutex_lock(&mLock);
52+
pthread_cond_wait(&mCondition, &mLock);
53+
pthread_mutex_unlock(&mLock);
54+
}
55+
56+
void Thread::notify()
57+
{
58+
pthread_mutex_lock(&mLock);
59+
pthread_cond_signal(&mCondition);
60+
pthread_mutex_unlock(&mLock);
61+
}
62+
3963
void Thread::handleRun(void* ptr)
4064
{
4165
}

jni/libmediaplayer/thread.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66
class Thread
77
{
88
public:
9+
Thread();
10+
~Thread();
11+
912
void start();
1013
void startAsync();
1114
int wait();
15+
16+
void waitOnNotify();
17+
void notify();
1218
virtual void stop();
1319

1420
protected:
15-
pthread_t mThread;
16-
bool mRunning;
21+
bool mRunning;
1722

1823
virtual void handleRun(void* ptr);
1924

2025
private:
26+
pthread_t mThread;
27+
pthread_mutex_t mLock;
28+
pthread_cond_t mCondition;
29+
2130
static void* startThread(void* ptr);
2231
};
2332

0 commit comments

Comments
 (0)