2012-09-25 00:35:50 +00:00
|
|
|
#include "videoplayer.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include "../mwbase/windowmanager.hpp"
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
|
|
|
|
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-09-25 00:35:50 +00:00
|
|
|
namespace MWRender
|
|
|
|
{
|
|
|
|
|
|
|
|
int OgreResource_Read(void *opaque, uint8_t *buf, int buf_size)
|
|
|
|
{
|
2012-12-11 22:06:06 +00:00
|
|
|
Ogre::DataStreamPtr stream = static_cast<VideoState*>(opaque)->stream;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
int num_read = stream->size() - stream->tell();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
if (num_read > buf_size)
|
|
|
|
num_read = buf_size;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
stream->read(buf, num_read);
|
|
|
|
return num_read;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int OgreResource_Write(void *opaque, uint8_t *buf, int buf_size)
|
|
|
|
{
|
2012-12-11 22:06:06 +00:00
|
|
|
Ogre::DataStreamPtr stream = static_cast<VideoState*>(opaque)->stream;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
int num_write = stream->size() - stream->tell();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
if (num_write > buf_size)
|
|
|
|
num_write = buf_size;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
stream->write (buf, num_write);
|
|
|
|
return num_write;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t OgreResource_Seek(void *opaque, int64_t offset, int whence)
|
|
|
|
{
|
2012-12-11 22:06:06 +00:00
|
|
|
Ogre::DataStreamPtr stream = static_cast<VideoState*>(opaque)->stream;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
switch (whence)
|
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
case SEEK_SET:
|
2012-12-11 22:06:06 +00:00
|
|
|
stream->seek(offset);
|
2012-12-11 21:49:31 +00:00
|
|
|
case SEEK_CUR:
|
2012-12-11 22:06:06 +00:00
|
|
|
stream->seek(stream->tell() + offset);
|
2012-12-11 21:49:31 +00:00
|
|
|
case SEEK_END:
|
2012-12-11 22:06:06 +00:00
|
|
|
stream->seek(stream->size() + offset);
|
2012-12-11 21:49:31 +00:00
|
|
|
case AVSEEK_SIZE:
|
2012-12-11 22:06:06 +00:00
|
|
|
return stream->size();
|
2012-12-11 21:49:31 +00:00
|
|
|
default:
|
2012-12-11 22:06:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
return stream->tell();
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
/* Since we only have one decoding thread, the Big Struct
|
|
|
|
can be global in case we need it. */
|
|
|
|
VideoState *global_video_state;
|
|
|
|
|
|
|
|
void packet_queue_init(PacketQueue *q) {
|
|
|
|
memset(q, 0, sizeof(PacketQueue));
|
|
|
|
}
|
|
|
|
int packet_queue_put(PacketQueue *q, AVPacket *pkt) {
|
|
|
|
AVPacketList *pkt1;
|
|
|
|
if(av_dup_packet(pkt) < 0) {
|
2012-12-03 16:33:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pkt1 = (AVPacketList*)av_malloc(sizeof(AVPacketList));
|
2012-12-11 21:49:31 +00:00
|
|
|
if (!pkt1)
|
|
|
|
return -1;
|
2012-12-03 16:33:02 +00:00
|
|
|
pkt1->pkt = *pkt;
|
|
|
|
pkt1->next = NULL;
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
q->mutex.lock ();
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if (!q->last_pkt)
|
|
|
|
q->first_pkt = pkt1;
|
|
|
|
else
|
|
|
|
q->last_pkt->next = pkt1;
|
|
|
|
q->last_pkt = pkt1;
|
|
|
|
q->nb_packets++;
|
|
|
|
q->size += pkt1->pkt.size;
|
|
|
|
q->cond.notify_one();
|
|
|
|
q->mutex.unlock ();
|
2012-12-03 16:33:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) {
|
|
|
|
AVPacketList *pkt1;
|
|
|
|
int ret;
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
boost::unique_lock<boost::mutex> lock(q->mutex);
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
for(;;) {
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if(global_video_state->quit) {
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
pkt1 = q->first_pkt;
|
|
|
|
if (pkt1) {
|
|
|
|
q->first_pkt = pkt1->next;
|
|
|
|
if (!q->first_pkt)
|
2012-12-11 22:06:06 +00:00
|
|
|
q->last_pkt = NULL;
|
2012-12-11 21:49:31 +00:00
|
|
|
q->nb_packets--;
|
|
|
|
q->size -= pkt1->pkt.size;
|
2012-12-03 16:33:02 +00:00
|
|
|
*pkt = pkt1->pkt;
|
|
|
|
av_free(pkt1);
|
2012-12-11 21:49:31 +00:00
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
} else if (!block) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
q->cond.wait(lock);
|
2012-12-03 16:33:02 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
static void packet_queue_flush(PacketQueue *q) {
|
|
|
|
AVPacketList *pkt, *pkt1;
|
|
|
|
|
|
|
|
q->mutex.lock();
|
|
|
|
for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
|
|
|
|
pkt1 = pkt->next;
|
|
|
|
av_free_packet(&pkt->pkt);
|
|
|
|
av_freep(&pkt);
|
|
|
|
}
|
|
|
|
q->last_pkt = NULL;
|
|
|
|
q->first_pkt = NULL;
|
|
|
|
q->nb_packets = 0;
|
|
|
|
q->size = 0;
|
|
|
|
q->mutex.unlock ();
|
|
|
|
}
|
|
|
|
double get_audio_clock(VideoState *is) {
|
|
|
|
double pts;
|
|
|
|
|
|
|
|
pts = is->audio_clock; /* maintained in the audio thread */
|
|
|
|
if(is->audio_st) {
|
2012-12-12 03:32:10 +00:00
|
|
|
int n = is->audio_st->codec->channels * 2;
|
|
|
|
int bytes_per_sec = is->audio_st->codec->sample_rate * n;
|
|
|
|
int hw_buf_size = is->audio_buf_size - is->audio_buf_index;
|
2012-12-11 21:49:31 +00:00
|
|
|
pts -= (double)hw_buf_size / bytes_per_sec;
|
|
|
|
}
|
|
|
|
return pts;
|
|
|
|
}
|
|
|
|
double get_video_clock(VideoState *is) {
|
|
|
|
double delta;
|
|
|
|
|
|
|
|
delta = (av_gettime() - is->video_current_pts_time) / 1000000.0;
|
|
|
|
return is->video_current_pts + delta;
|
|
|
|
}
|
|
|
|
double get_external_clock(VideoState *is) {
|
|
|
|
return av_gettime() / 1000000.0;
|
|
|
|
}
|
|
|
|
double get_master_clock(VideoState *is) {
|
|
|
|
if(is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
|
|
|
|
return get_video_clock(is);
|
|
|
|
} else if(is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
|
|
|
|
return get_audio_clock(is);
|
|
|
|
} else {
|
|
|
|
return get_external_clock(is);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Add or subtract samples to get a better sync, return new
|
|
|
|
audio buffer size */
|
|
|
|
int synchronize_audio(VideoState *is, short *samples,
|
|
|
|
int samples_size, double pts) {
|
|
|
|
int n;
|
|
|
|
double ref_clock;
|
|
|
|
|
|
|
|
n = 2 * is->audio_st->codec->channels;
|
|
|
|
|
|
|
|
if(is->av_sync_type != AV_SYNC_AUDIO_MASTER) {
|
|
|
|
double diff, avg_diff;
|
|
|
|
int wanted_size, min_size, max_size;
|
|
|
|
// int nb_samples;
|
|
|
|
|
|
|
|
ref_clock = get_master_clock(is);
|
|
|
|
diff = get_audio_clock(is) - ref_clock;
|
|
|
|
if(diff < AV_NOSYNC_THRESHOLD) {
|
|
|
|
// accumulate the diffs
|
|
|
|
is->audio_diff_cum = diff + is->audio_diff_avg_coef
|
|
|
|
* is->audio_diff_cum;
|
|
|
|
if(is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
|
|
|
|
is->audio_diff_avg_count++;
|
|
|
|
} else {
|
|
|
|
avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
|
|
|
|
if(fabs(avg_diff) >= is->audio_diff_threshold) {
|
|
|
|
wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);
|
|
|
|
min_size = samples_size * ((100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100);
|
|
|
|
max_size = samples_size * ((100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100);
|
|
|
|
if(wanted_size < min_size) {
|
|
|
|
wanted_size = min_size;
|
|
|
|
} else if (wanted_size > max_size) {
|
|
|
|
wanted_size = max_size;
|
2012-12-03 16:33:02 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
if(wanted_size < samples_size) {
|
|
|
|
/* remove samples */
|
|
|
|
samples_size = wanted_size;
|
|
|
|
} else if(wanted_size > samples_size) {
|
|
|
|
uint8_t *samples_end, *q;
|
|
|
|
int nb;
|
|
|
|
/* add samples by copying final sample*/
|
|
|
|
nb = (samples_size - wanted_size);
|
|
|
|
samples_end = (uint8_t *)samples + samples_size - n;
|
|
|
|
q = samples_end + n;
|
|
|
|
while(nb > 0) {
|
|
|
|
memcpy(q, samples_end, n);
|
|
|
|
q += n;
|
|
|
|
nb -= n;
|
|
|
|
}
|
|
|
|
samples_size = wanted_size;
|
2012-12-03 16:33:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* difference is TOO big; reset diff stuff */
|
|
|
|
is->audio_diff_avg_count = 0;
|
|
|
|
is->audio_diff_cum = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return samples_size;
|
2012-12-03 16:33:02 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
int audio_decode_frame(VideoState *is, uint8_t *audio_buf, int buf_size, double *pts_ptr) {
|
|
|
|
int len1, data_size, n;
|
|
|
|
AVPacket *pkt = &is->audio_pkt;
|
|
|
|
double pts;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
while(is->audio_pkt_size > 0) {
|
|
|
|
data_size = buf_size;
|
|
|
|
len1 = avcodec_decode_audio3(is->audio_st->codec,
|
|
|
|
(int16_t *)audio_buf, &data_size, pkt);
|
|
|
|
|
|
|
|
|
|
|
|
if(len1 < 0) {
|
2012-12-11 22:06:06 +00:00
|
|
|
/* if error, skip frame */
|
|
|
|
is->audio_pkt_size = 0;
|
|
|
|
break;
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
is->audio_pkt_data += len1;
|
|
|
|
is->audio_pkt_size -= len1;
|
|
|
|
if(data_size <= 0) {
|
|
|
|
/* No data yet, get more frames */
|
2012-12-11 22:06:06 +00:00
|
|
|
continue;
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
pts = is->audio_clock;
|
|
|
|
*pts_ptr = pts;
|
|
|
|
n = 2 * is->audio_st->codec->channels;
|
|
|
|
is->audio_clock += (double)data_size /
|
2012-12-11 22:06:06 +00:00
|
|
|
(double)(n * is->audio_st->codec->sample_rate);
|
2012-12-11 21:49:31 +00:00
|
|
|
|
|
|
|
/* We have data, return it and come back for more later */
|
|
|
|
return data_size;
|
|
|
|
}
|
|
|
|
if(pkt->data)
|
|
|
|
av_free_packet(pkt);
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if(is->quit) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* next packet */
|
|
|
|
if(packet_queue_get(&is->audioq, pkt, 1) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
is->audio_pkt_data = pkt->data;
|
|
|
|
is->audio_pkt_size = pkt->size;
|
|
|
|
/* if update, update the audio clock w/pts */
|
2012-12-12 03:43:07 +00:00
|
|
|
if(pkt->pts != (int64_t)AV_NOPTS_VALUE) {
|
2012-12-11 21:49:31 +00:00
|
|
|
is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
void audio_callback(void *userdata, Uint8 *stream, int len) {
|
|
|
|
VideoState *is = (VideoState *)userdata;
|
|
|
|
int len1, audio_size;
|
|
|
|
double pts;
|
|
|
|
|
|
|
|
while(len > 0) {
|
|
|
|
if(is->audio_buf_index >= is->audio_buf_size) {
|
|
|
|
/* We have already sent all our data; get more */
|
|
|
|
audio_size = audio_decode_frame(is, is->audio_buf, sizeof(is->audio_buf), &pts);
|
|
|
|
if(audio_size < 0) {
|
2012-12-11 22:06:06 +00:00
|
|
|
/* If error, output silence */
|
|
|
|
is->audio_buf_size = 1024;
|
|
|
|
memset(is->audio_buf, 0, is->audio_buf_size);
|
2012-12-11 21:49:31 +00:00
|
|
|
} else {
|
2012-12-11 22:06:06 +00:00
|
|
|
audio_size = synchronize_audio(is, (int16_t *)is->audio_buf,
|
|
|
|
audio_size, pts);
|
|
|
|
is->audio_buf_size = audio_size;
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
is->audio_buf_index = 0;
|
|
|
|
}
|
|
|
|
len1 = is->audio_buf_size - is->audio_buf_index;
|
|
|
|
if(len1 > len)
|
|
|
|
len1 = len;
|
|
|
|
memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
|
|
|
|
len -= len1;
|
|
|
|
stream += len1;
|
|
|
|
is->audio_buf_index += len1;
|
|
|
|
}
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
/*
|
|
|
|
static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque) {
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = FF_REFRESH_EVENT;
|
|
|
|
event.user.data1 = opaque;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
return 0; // 0 means stop timer
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
*/
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
void timer_callback (int delay, VideoState* is)
|
2012-09-25 00:35:50 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
boost::this_thread::sleep (boost::posix_time::milliseconds(delay));
|
|
|
|
is->refresh++;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
/* schedule a video refresh in 'delay' ms */
|
|
|
|
static void schedule_refresh(VideoState *is, int delay)
|
2012-09-25 00:35:50 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
//SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
|
|
|
|
//is->refresh_queue.push_back (delay);
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
boost::thread (boost::bind(&timer_callback, delay, is));
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
void video_display(VideoState *is)
|
|
|
|
{
|
|
|
|
VideoPicture *vp;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
vp = &is->pictq[is->pictq_rindex];
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if (is->video_st->codec->width != 0 && is->video_st->codec->height != 0)
|
|
|
|
{
|
|
|
|
Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton ().getByName("VideoTexture");
|
2012-12-12 03:43:07 +00:00
|
|
|
if (texture.isNull () || texture->getWidth() != (size_t)is->video_st->codec->width ||
|
|
|
|
texture->getHeight() != (size_t)is->video_st->codec->height)
|
2012-12-11 21:49:31 +00:00
|
|
|
{
|
|
|
|
Ogre::TextureManager::getSingleton ().remove ("VideoTexture");
|
|
|
|
texture = Ogre::TextureManager::getSingleton().createManual(
|
|
|
|
"VideoTexture",
|
|
|
|
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
|
|
|
Ogre::TEX_TYPE_2D,
|
|
|
|
is->video_st->codec->width, is->video_st->codec->height,
|
|
|
|
0,
|
|
|
|
Ogre::PF_BYTE_RGBA,
|
|
|
|
Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
|
|
|
|
}
|
|
|
|
Ogre::PixelBox pb(is->video_st->codec->width, is->video_st->codec->height, 1, Ogre::PF_BYTE_RGBA, vp->data);
|
|
|
|
Ogre::HardwarePixelBufferSharedPtr buffer = texture->getBuffer();
|
|
|
|
buffer->blitFromMemory(pb);
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
free(vp->data);
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
void video_refresh_timer(void *userdata) {
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
VideoState *is = (VideoState *)userdata;
|
|
|
|
VideoPicture *vp;
|
|
|
|
double actual_delay, delay, sync_threshold, ref_clock, diff;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if(is->video_st) {
|
|
|
|
if(is->pictq_size == 0) {
|
|
|
|
schedule_refresh(is, 1);
|
|
|
|
} else {
|
|
|
|
vp = &is->pictq[is->pictq_rindex];
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
is->video_current_pts = vp->pts;
|
|
|
|
is->video_current_pts_time = av_gettime();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
delay = vp->pts - is->frame_last_pts; /* the pts from last time */
|
|
|
|
if(delay <= 0 || delay >= 1.0) {
|
2012-12-11 22:06:06 +00:00
|
|
|
/* if incorrect delay, use previous one */
|
|
|
|
delay = is->frame_last_delay;
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
/* save for next time */
|
|
|
|
is->frame_last_delay = delay;
|
|
|
|
is->frame_last_pts = vp->pts;
|
|
|
|
|
|
|
|
/* update delay to sync to audio if not master source */
|
|
|
|
if(is->av_sync_type != AV_SYNC_VIDEO_MASTER) {
|
2012-12-11 22:06:06 +00:00
|
|
|
ref_clock = get_master_clock(is);
|
|
|
|
diff = vp->pts - ref_clock;
|
|
|
|
|
|
|
|
/* Skip or repeat the frame. Take delay into account
|
|
|
|
FFPlay still doesn't "know if this is the best guess." */
|
|
|
|
sync_threshold = (delay > AV_SYNC_THRESHOLD) ? delay : AV_SYNC_THRESHOLD;
|
|
|
|
if(fabs(diff) < AV_NOSYNC_THRESHOLD) {
|
|
|
|
if(diff <= -sync_threshold) {
|
|
|
|
delay = 0;
|
|
|
|
} else if(diff >= sync_threshold) {
|
|
|
|
delay = 2 * delay;
|
|
|
|
}
|
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
is->frame_timer += delay;
|
|
|
|
/* computer the REAL delay */
|
|
|
|
actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
|
|
|
|
if(actual_delay < 0.010) {
|
2012-12-11 22:06:06 +00:00
|
|
|
/* Really it should skip the picture instead */
|
|
|
|
actual_delay = 0.010;
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
schedule_refresh(is, (int)(actual_delay * 1000 + 0.5));
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
/* show the picture! */
|
|
|
|
video_display(is);
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
/* update queue for next picture! */
|
|
|
|
if(++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) {
|
2012-12-11 22:06:06 +00:00
|
|
|
is->pictq_rindex = 0;
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
is->pictq_mutex.lock();
|
|
|
|
is->pictq_size--;
|
|
|
|
is->pictq_cond.notify_one ();
|
|
|
|
is->pictq_mutex.unlock ();
|
|
|
|
}
|
2012-12-11 22:06:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-12-11 21:49:31 +00:00
|
|
|
schedule_refresh(is, 100);
|
|
|
|
}
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
int queue_picture(VideoState *is, AVFrame *pFrame, double pts) {
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
VideoPicture *vp;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
/* wait until we have a new pic */
|
2012-09-25 00:35:50 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
boost::unique_lock<boost::mutex> lock(is->pictq_mutex);
|
|
|
|
while(is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
|
|
|
|
!is->quit) {
|
|
|
|
is->pictq_cond.timed_wait(lock, boost::posix_time::milliseconds(1));
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if(is->quit)
|
|
|
|
return -1;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
// windex is set to 0 initially
|
|
|
|
vp = &is->pictq[is->pictq_windex];
|
|
|
|
|
|
|
|
// Convert the image into YUV format that SDL uses
|
|
|
|
if(is->sws_context == NULL) {
|
|
|
|
int w = is->video_st->codec->width;
|
|
|
|
int h = is->video_st->codec->height;
|
|
|
|
is->sws_context = sws_getContext(w, h,
|
|
|
|
is->video_st->codec->pix_fmt, w, h,
|
|
|
|
PIX_FMT_RGBA, SWS_BICUBIC, NULL, NULL, NULL);
|
|
|
|
if(is->sws_context == NULL)
|
|
|
|
throw std::runtime_error("Cannot initialize the conversion context!\n");
|
|
|
|
}
|
2012-12-03 15:44:41 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
vp->data =(uint8_t*) malloc(is->video_st->codec->width * is->video_st->codec->height * 4);
|
2012-12-03 15:44:41 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
sws_scale(is->sws_context, pFrame->data, pFrame->linesize,
|
|
|
|
0, is->video_st->codec->height, &vp->data, is->rgbaFrame->linesize);
|
2012-12-03 15:44:41 +00:00
|
|
|
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
vp->pts = pts;
|
2012-12-03 15:44:41 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
// now we inform our display thread that we have a pic ready
|
|
|
|
if(++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE) {
|
|
|
|
is->pictq_windex = 0;
|
2012-12-03 16:33:02 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
is->pictq_mutex.lock();
|
|
|
|
is->pictq_size++;
|
|
|
|
is->pictq_mutex.unlock();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-03 15:44:41 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
double synchronize_video(VideoState *is, AVFrame *src_frame, double pts) {
|
2012-12-03 15:44:41 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
double frame_delay;
|
|
|
|
|
|
|
|
if(pts != 0) {
|
|
|
|
/* if we have pts, set video clock to it */
|
|
|
|
is->video_clock = pts;
|
|
|
|
} else {
|
|
|
|
/* if we aren't given a pts, set it to the clock */
|
|
|
|
pts = is->video_clock;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
/* update the video clock */
|
|
|
|
frame_delay = av_q2d(is->video_st->codec->time_base);
|
|
|
|
/* if we are repeating a frame, adjust clock accordingly */
|
|
|
|
frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
|
|
|
|
is->video_clock += frame_delay;
|
|
|
|
return pts;
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
uint64_t global_video_pkt_pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
/* These are called whenever we allocate a frame
|
|
|
|
* buffer. We use this to store the global_pts in
|
|
|
|
* a frame at the time it is allocated.
|
|
|
|
*/
|
|
|
|
int our_get_buffer(struct AVCodecContext *c, AVFrame *pic) {
|
|
|
|
int ret = avcodec_default_get_buffer(c, pic);
|
|
|
|
uint64_t *pts = (uint64_t*)av_malloc(sizeof(uint64_t));
|
|
|
|
*pts = global_video_pkt_pts;
|
|
|
|
pic->opaque = pts;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void our_release_buffer(struct AVCodecContext *c, AVFrame *pic) {
|
|
|
|
if(pic) av_freep(&pic->opaque);
|
|
|
|
avcodec_default_release_buffer(c, pic);
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
int video_thread(void *arg) {
|
|
|
|
VideoState *is = (VideoState *)arg;
|
|
|
|
AVPacket pkt1, *packet = &pkt1;
|
|
|
|
int len1, frameFinished;
|
|
|
|
AVFrame *pFrame;
|
|
|
|
double pts;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
pFrame = avcodec_alloc_frame();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
is->rgbaFrame = avcodec_alloc_frame();
|
|
|
|
avpicture_alloc ((AVPicture *)is->rgbaFrame, PIX_FMT_RGBA, is->video_st->codec->width, is->video_st->codec->height);
|
2012-09-25 00:35:50 +00:00
|
|
|
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
for(;;) {
|
|
|
|
if(packet_queue_get(&is->videoq, packet, 1) < 0) {
|
|
|
|
// means we quit getting packets
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pts = 0;
|
|
|
|
|
|
|
|
// Save global pts to be stored in pFrame
|
|
|
|
global_video_pkt_pts = packet->pts;
|
|
|
|
// Decode video frame
|
|
|
|
len1 = avcodec_decode_video2(is->video_st->codec, pFrame, &frameFinished,
|
|
|
|
packet);
|
2012-12-12 03:43:07 +00:00
|
|
|
if(packet->dts == (int64_t)AV_NOPTS_VALUE
|
2012-12-11 21:49:31 +00:00
|
|
|
&& pFrame->opaque && *(uint64_t*)pFrame->opaque != AV_NOPTS_VALUE) {
|
|
|
|
pts = *(uint64_t *)pFrame->opaque;
|
2012-12-12 03:43:07 +00:00
|
|
|
} else if(packet->dts != (int64_t)AV_NOPTS_VALUE) {
|
2012-12-11 21:49:31 +00:00
|
|
|
pts = packet->dts;
|
|
|
|
} else {
|
|
|
|
pts = 0;
|
|
|
|
}
|
|
|
|
pts *= av_q2d(is->video_st->time_base);
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-01 10:30:51 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
// Did we get a video frame?
|
|
|
|
if(frameFinished) {
|
|
|
|
pts = synchronize_video(is, pFrame, pts);
|
|
|
|
if(queue_picture(is, pFrame, pts) < 0) {
|
2012-12-11 22:06:06 +00:00
|
|
|
break;
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
av_free_packet(packet);
|
2012-12-01 10:30:51 +00:00
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
SDL_CloseAudio();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
av_free(pFrame);
|
2012-09-25 00:54:29 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
avpicture_free((AVPicture *)is->rgbaFrame);
|
|
|
|
av_free(is->rgbaFrame);
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
return 0;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
int stream_component_open(VideoState *is, int stream_index, AVFormatContext *pFormatCtx)
|
2012-09-25 00:35:50 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
AVCodecContext *codecCtx;
|
|
|
|
AVCodec *codec;
|
|
|
|
SDL_AudioSpec wanted_spec, spec;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-12 03:43:07 +00:00
|
|
|
if(stream_index < 0 || (unsigned int)stream_index >= pFormatCtx->nb_streams) {
|
2012-12-11 21:49:31 +00:00
|
|
|
return -1;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
|
|
|
|
// Get a pointer to the codec context for the video stream
|
|
|
|
codecCtx = pFormatCtx->streams[stream_index]->codec;
|
|
|
|
|
|
|
|
if(codecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
// Set audio settings from codec info
|
|
|
|
wanted_spec.freq = codecCtx->sample_rate;
|
|
|
|
wanted_spec.format = AUDIO_S16SYS;
|
|
|
|
wanted_spec.channels = codecCtx->channels;
|
|
|
|
wanted_spec.silence = 0;
|
|
|
|
wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
|
|
|
|
wanted_spec.callback = audio_callback;
|
|
|
|
wanted_spec.userdata = is;
|
|
|
|
|
|
|
|
if(SDL_OpenAudio(&wanted_spec, &spec) < 0) {
|
|
|
|
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
is->audio_hw_buf_size = spec.size;
|
|
|
|
}
|
|
|
|
codec = avcodec_find_decoder(codecCtx->codec_id);
|
|
|
|
if(!codec || (avcodec_open2(codecCtx, codec, NULL) < 0)) {
|
|
|
|
fprintf(stderr, "Unsupported codec!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(codecCtx->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
is->audioStream = stream_index;
|
|
|
|
is->audio_st = pFormatCtx->streams[stream_index];
|
|
|
|
is->audio_buf_size = 0;
|
|
|
|
is->audio_buf_index = 0;
|
|
|
|
|
|
|
|
/* averaging filter for audio sync */
|
|
|
|
is->audio_diff_avg_coef = exp(log(0.01 / AUDIO_DIFF_AVG_NB));
|
|
|
|
is->audio_diff_avg_count = 0;
|
|
|
|
/* Correct audio only if larger error than this */
|
|
|
|
is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / codecCtx->sample_rate;
|
|
|
|
|
|
|
|
memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
|
|
|
|
packet_queue_init(&is->audioq);
|
|
|
|
SDL_PauseAudio(0);
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
is->videoStream = stream_index;
|
|
|
|
is->video_st = pFormatCtx->streams[stream_index];
|
|
|
|
|
|
|
|
is->frame_timer = (double)av_gettime() / 1000000.0;
|
|
|
|
is->frame_last_delay = 40e-3;
|
|
|
|
is->video_current_pts_time = av_gettime();
|
|
|
|
|
|
|
|
packet_queue_init(&is->videoq);
|
|
|
|
is->video_thread = boost::thread(video_thread, is);
|
|
|
|
codecCtx->get_buffer = our_get_buffer;
|
|
|
|
codecCtx->release_buffer = our_release_buffer;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-12 03:36:04 +00:00
|
|
|
return 0;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
int decode_interrupt_cb(void) {
|
|
|
|
return (global_video_state && global_video_state->quit);
|
|
|
|
}
|
|
|
|
|
|
|
|
int decode_thread(void *arg) {
|
|
|
|
|
|
|
|
VideoState *is = (VideoState *)arg;
|
|
|
|
AVFormatContext *pFormatCtx = avformat_alloc_context ();
|
|
|
|
AVPacket pkt1, *packet = &pkt1;
|
|
|
|
|
|
|
|
int video_index = -1;
|
|
|
|
int audio_index = -1;
|
2012-12-12 03:43:07 +00:00
|
|
|
unsigned int i;
|
2012-12-11 21:49:31 +00:00
|
|
|
|
|
|
|
is->videoStream=-1;
|
|
|
|
is->audioStream=-1;
|
|
|
|
is->quit = 0;
|
|
|
|
|
|
|
|
Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton ().openResource (is->resourceName);
|
|
|
|
if(stream.isNull ())
|
|
|
|
throw std::runtime_error("Failed to open video resource");
|
2012-12-11 22:06:06 +00:00
|
|
|
is->stream = stream;
|
2012-12-11 21:49:31 +00:00
|
|
|
|
|
|
|
AVIOContext *ioContext = 0;
|
|
|
|
|
2012-12-11 22:06:06 +00:00
|
|
|
ioContext = avio_alloc_context(NULL, 0, 0, is, OgreResource_Read, OgreResource_Write, OgreResource_Seek);
|
2012-12-11 21:49:31 +00:00
|
|
|
if (!ioContext)
|
|
|
|
throw std::runtime_error("Failed to allocate ioContext ");
|
|
|
|
|
|
|
|
pFormatCtx->pb = ioContext;
|
|
|
|
|
|
|
|
global_video_state = is;
|
|
|
|
// will interrupt blocking functions if we quit!
|
|
|
|
//url_set_interrupt_cb(decode_interrupt_cb);
|
|
|
|
|
|
|
|
// Open video file
|
|
|
|
/// \todo leak here, ffmpeg or valgrind bug ?
|
|
|
|
if (avformat_open_input(&pFormatCtx, is->resourceName.c_str(), NULL, NULL))
|
|
|
|
throw std::runtime_error("Failed to open video input");
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
// Retrieve stream information
|
|
|
|
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
|
|
|
|
throw std::runtime_error("Failed to retrieve stream information");
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
// Dump information about file onto standard error
|
|
|
|
av_dump_format(pFormatCtx, 0, is->resourceName.c_str(), 0);
|
|
|
|
|
|
|
|
for(i=0; i<pFormatCtx->nb_streams; i++) {
|
|
|
|
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO &&
|
|
|
|
video_index < 0) {
|
|
|
|
video_index=i;
|
|
|
|
}
|
|
|
|
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
|
|
|
|
audio_index < 0) {
|
|
|
|
audio_index=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(audio_index >= 0) {
|
|
|
|
stream_component_open(is, audio_index, pFormatCtx);
|
|
|
|
}
|
|
|
|
if(video_index >= 0) {
|
|
|
|
stream_component_open(is, video_index, pFormatCtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is->videoStream >= 0 /*|| is->audioStream < 0*/)
|
2012-12-03 16:33:02 +00:00
|
|
|
{
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
// main decode loop
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if(is->quit) {
|
2012-12-03 16:33:02 +00:00
|
|
|
break;
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
2012-12-11 21:49:31 +00:00
|
|
|
if( (is->audioStream >= 0 && is->audioq.size > MAX_AUDIOQ_SIZE) ||
|
|
|
|
is->videoq.size > MAX_VIDEOQ_SIZE) {
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(av_read_frame(pFormatCtx, packet) < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Is this a packet from the video stream?
|
|
|
|
if(packet->stream_index == is->videoStream) {
|
|
|
|
packet_queue_put(&is->videoq, packet);
|
|
|
|
} else if(packet->stream_index == is->audioStream) {
|
|
|
|
packet_queue_put(&is->audioq, packet);
|
|
|
|
} else {
|
|
|
|
av_free_packet(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* all done - wait for it */
|
|
|
|
while(!is->quit) {
|
|
|
|
// EOF reached, all packets processed, we can exit now
|
|
|
|
if (is->audioq.nb_packets == 0 && is->videoq.nb_packets == 0)
|
|
|
|
break;
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
is->quit = 1;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
is->audioq.cond.notify_one ();
|
|
|
|
is->videoq.cond.notify_one ();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
is->video_thread.join();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if (is->audioStream >= 0)
|
|
|
|
avcodec_close(is->audio_st->codec);
|
|
|
|
if (is->videoStream >= 0)
|
|
|
|
avcodec_close(is->video_st->codec);
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
sws_freeContext (is->sws_context);
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
av_close_input_file(pFormatCtx);
|
|
|
|
pFormatCtx = NULL;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
av_free(ioContext);
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
VideoPlayer::VideoPlayer(Ogre::SceneManager* sceneMgr)
|
|
|
|
: mState(NULL)
|
|
|
|
, mSceneMgr(sceneMgr)
|
|
|
|
{
|
|
|
|
mVideoMaterial = Ogre::MaterialManager::getSingleton ().create("VideoMaterial", "General");
|
|
|
|
mVideoMaterial->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
|
|
|
|
mVideoMaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
|
|
|
|
mVideoMaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
|
|
|
|
mVideoMaterial->getTechnique(0)->getPass(0)->createTextureUnitState();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
mRectangle = new Ogre::Rectangle2D(true);
|
|
|
|
mRectangle->setCorners(-1.0, 1.0, 1.0, -1.0);
|
|
|
|
mRectangle->setMaterial("VideoMaterial");
|
|
|
|
mRectangle->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY+1);
|
|
|
|
// Use infinite AAB to always stay visible
|
|
|
|
Ogre::AxisAlignedBox aabInf;
|
|
|
|
aabInf.setInfinite();
|
|
|
|
mRectangle->setBoundingBox(aabInf);
|
|
|
|
// Attach background to the scene
|
|
|
|
Ogre::SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode();
|
|
|
|
node->attachObject(mRectangle);
|
|
|
|
mRectangle->setVisible(false);
|
|
|
|
mRectangle->setVisibilityFlags (0x1);
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
VideoPlayer::~VideoPlayer ()
|
2012-09-25 00:35:50 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
if (mState)
|
|
|
|
close();
|
2012-09-25 00:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
void VideoPlayer::playVideo (const std::string &resourceName)
|
2012-09-25 00:35:50 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
if (mState)
|
|
|
|
close();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
mRectangle->setVisible(true);
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
MWBase::Environment::get().getWindowManager ()->pushGuiMode (MWGui::GM_Video);
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-12 00:30:34 +00:00
|
|
|
// Turn off rendering except the GUI
|
|
|
|
mSceneMgr->clearSpecialCaseRenderQueues();
|
|
|
|
// SCRQM_INCLUDE with RENDER_QUEUE_OVERLAY does not work.
|
|
|
|
for (int i = 0; i < Ogre::RENDER_QUEUE_MAX; ++i)
|
|
|
|
{
|
|
|
|
if (i > 0 && i < 96)
|
|
|
|
mSceneMgr->addSpecialCaseRenderQueue(i);
|
|
|
|
}
|
|
|
|
mSceneMgr->setSpecialCaseRenderQueueMode(Ogre::SceneManager::SCRQM_EXCLUDE);
|
|
|
|
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
mState = new VideoState;
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
// Register all formats and codecs
|
|
|
|
av_register_all();
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if(SDL_Init(SDL_INIT_AUDIO)) {
|
|
|
|
throw std::runtime_error("Failed to initialize SDL");
|
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
mState->refresh = 0;
|
|
|
|
mState->resourceName = resourceName;
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
schedule_refresh(mState, 40);
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
mState->av_sync_type = DEFAULT_AV_SYNC_TYPE;
|
|
|
|
mState->parse_thread = boost::thread(decode_thread, mState);
|
|
|
|
}
|
2012-12-03 16:33:02 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
void VideoPlayer::update ()
|
2012-12-03 16:33:02 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
if (mState && mState->refresh)
|
2012-12-03 16:33:02 +00:00
|
|
|
{
|
2012-12-11 21:49:31 +00:00
|
|
|
video_refresh_timer (mState);
|
|
|
|
mState->refresh--;
|
|
|
|
}
|
|
|
|
if (mState && mState->quit)
|
|
|
|
{
|
|
|
|
close();
|
2012-12-03 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
if (!Ogre::TextureManager::getSingleton ().getByName ("VideoTexture").isNull ())
|
2012-12-11 22:06:06 +00:00
|
|
|
mVideoMaterial->getTechnique(0)->getPass(0)->getTextureUnitState (0)->setTextureName ("VideoTexture");
|
2012-12-11 21:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoPlayer::close()
|
|
|
|
{
|
|
|
|
mState->quit = 1;
|
|
|
|
|
|
|
|
mState->parse_thread.join ();
|
|
|
|
|
|
|
|
delete mState;
|
|
|
|
mState = NULL;
|
|
|
|
|
|
|
|
mRectangle->setVisible (false);
|
|
|
|
MWBase::Environment::get().getWindowManager ()->removeGuiMode (MWGui::GM_Video);
|
2012-12-12 00:30:34 +00:00
|
|
|
|
|
|
|
mSceneMgr->clearSpecialCaseRenderQueues();
|
|
|
|
mSceneMgr->setSpecialCaseRenderQueueMode(Ogre::SceneManager::SCRQM_EXCLUDE);
|
2012-12-03 16:33:02 +00:00
|
|
|
}
|
2012-09-25 00:35:50 +00:00
|
|
|
|
2012-12-11 21:49:31 +00:00
|
|
|
bool VideoPlayer::isPlaying ()
|
|
|
|
{
|
|
|
|
return mState != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|