Skip to content

Commit

Permalink
orientation support for opengl based renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Oct 22, 2014
1 parent 92a8d46 commit 0757255
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 10 deletions.
12 changes: 10 additions & 2 deletions examples/player/EventFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 39,7 @@

using namespace QtAV;

// TODO: watch main window
EventFilter::EventFilter(AVPlayer *player) :
QObject(player),
menu(0)
Expand Down Expand Up @@ -86,6 87,7 @@ void EventFilter::help()
emit helpRequested();
return;
static QString help = "<h4>" tr("Drag and drop a file to player\n") "</h4>"
"<p>" tr("A: switch aspect ratio") "</p>"
"<p>" tr("Double click to switch fullscreen") "</p>"
"<p>" tr("Shortcut:\n") "</p>"
"<p>" tr("Space: pause/continue\n") "</p>"
Expand All @@ -98,7 100,7 @@ void EventFilter::help()
"<p>" tr("P: replay\n") "</p>"
"<p>" tr("Q/ESC: quit\n") "</p>"
"<p>" tr("S: stop\n") "</p>"
"<p>" tr("R: switch aspect ratio") "</p>"
"<p>" tr("R: rotate 90") "</p>"
"<p>" tr("M: mute on/off\n") "</p>"
"<p>" tr("C: capture video") "</p>"
"<p>" tr("Up/Down: volume /-\n") "</p>"
Expand Down Expand Up @@ -233,12 235,18 @@ bool EventFilter::eventFilter(QObject *watched, QEvent *event)
player->audio()->setMute(!player->audio()->isMute());
}
break;
case Qt::Key_R: {
case Qt::Key_A: {
VideoRenderer* renderer = player->renderer();
VideoRenderer::OutAspectRatioMode r = renderer->outAspectRatioMode();
renderer->setOutAspectRatioMode(VideoRenderer::OutAspectRatioMode(((int)r 1)%2));
}
break;
case Qt::Key_R: {
VideoRenderer* renderer = player->renderer();
renderer->setOrientation(renderer->orientation() 90);
qDebug("orientation: %d", renderer->orientation());
}
break;
case Qt::Key_T: {
QWidget *w = qApp->activeWindow();
if (!w)
Expand Down
14 changes: 13 additions & 1 deletion examples/videographicsitem/videoplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 30,7 @@
#include <QLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QDial>

using namespace QtAV;

Expand All @@ -39,7 40,7 @@ VideoPlayer::VideoPlayer(QWidget *parent)
{
videoItem = new GraphicsItemRenderer;
videoItem->resizeRenderer(640, 360);
videoItem->setOutAspectRatioMode(VideoRenderer::RendererAspectRatio);
videoItem->setOutAspectRatioMode(VideoRenderer::VideoAspectRatio);

QGraphicsScene *scene = new QGraphicsScene(this);
scene->addItem(videoItem);
Expand All @@ -54,6 55,11 @@ VideoPlayer::VideoPlayer(QWidget *parent)
scaleSlider->setRange(0, 200);
scaleSlider->setValue(100);

QDial *orientation = new QDial();
orientation->setRange(0, 3);
orientation->setValue(0);

connect(orientation, SIGNAL(valueChanged(int)), SLOT(setOrientation(int)));
connect(rotateSlider, SIGNAL(valueChanged(int)), SLOT(rotateVideo(int)));
connect(scaleSlider, SIGNAL(valueChanged(int)), SLOT(scaleVideo(int)));
QPushButton *openBtn = new QPushButton;
Expand All @@ -67,6 73,7 @@ VideoPlayer::VideoPlayer(QWidget *parent)
QHBoxLayout *hb = new QHBoxLayout;
hb->addWidget(glBox);
hb->addWidget(openBtn);
hb->addWidget(orientation);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(rotateSlider);
Expand Down Expand Up @@ -101,6 108,11 @@ void VideoPlayer::setOpenGL(bool o)
#endif
}

void VideoPlayer::setOrientation(int value)
{
videoItem->setOrientation(value*90);
}

void VideoPlayer::rotateVideo(int angle)
{
//rotate around the center of video element
Expand Down
1 change: 1 addition & 0 deletions examples/videographicsitem/videoplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 41,7 @@ public slots:
void setOpenGL(bool o = true);

private slots:
void setOrientation(int value);
void rotateVideo(int angle);
void scaleVideo(int value);
void open();
Expand Down
1 change: 1 addition & 0 deletions src/QtAV/GLWidgetRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 60,7 @@ class Q_AV_EXPORT GLWidgetRenderer : public QGLWidget, public VideoRenderer
private:
virtual void onSetOutAspectRatioMode(OutAspectRatioMode mode);
virtual void onSetOutAspectRatio(qreal ratio);
virtual bool onSetOrientation(int value);
/*!
* \brief onSetBrightness
* only works for GLSL. otherwise return false, means that do nothing, brightness() does not change.
Expand Down
1 change: 1 addition & 0 deletions src/QtAV/GraphicsItemRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 77,7 @@ class Q_AV_EXPORT GraphicsItemRenderer : public GraphicsWidget, public QPainterR
private:
virtual void onSetOutAspectRatioMode(OutAspectRatioMode mode);
virtual void onSetOutAspectRatio(qreal ratio);
virtual bool onSetOrientation(int value);
virtual bool onSetBrightness(qreal b);
virtual bool onSetContrast(qreal c);
virtual bool onSetHue(qreal h);
Expand Down
1 change: 1 addition & 0 deletions src/QtAV/OpenGLRendererBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 60,7 @@ class Q_AV_EXPORT OpenGLRendererBase : public VideoRenderer, public QOpenGLFunct
private:
virtual void onSetOutAspectRatioMode(OutAspectRatioMode mode);
virtual void onSetOutAspectRatio(qreal ratio);
virtual bool onSetOrientation(int value);
virtual bool onSetBrightness(qreal b);
virtual bool onSetContrast(qreal c);
virtual bool onSetHue(qreal h);
Expand Down
13 changes: 11 additions & 2 deletions src/output/video/GLWidgetRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 200,10 @@ class GLWidgetRendererPrivate : public VideoRendererPrivate
}

void setupAspectRatio() {
mpv_matrix(0, 0) = (float)out_rect.width()/(float)renderer_width;
mpv_matrix(1, 1) = (float)out_rect.height()/(float)renderer_height;
mpv_matrix.setToIdentity();
mpv_matrix.scale((GLfloat)out_rect.width()/(GLfloat)renderer_width, (GLfloat)out_rect.height()/(GLfloat)renderer_height, 1);
if (orientation)
mpv_matrix.rotate(orientation, 0, 0, 1); // Z axis
}

class VideoMaterialType {};
Expand Down Expand Up @@ -1060,6 1062,13 @@ void GLWidgetRenderer::onSetOutAspectRatioMode(OutAspectRatioMode mode)
d_func().setupAspectRatio();
}

bool GLWidgetRenderer::onSetOrientation(int value)
{
Q_UNUSED(value);
d_func().setupAspectRatio();
return true;
}

bool GLWidgetRenderer::onSetBrightness(qreal b)
{
DPTR_D(GLWidgetRenderer);
Expand Down
13 changes: 11 additions & 2 deletions src/output/video/GraphicsItemRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 43,10 @@ class GraphicsItemRendererPrivate : public QPainterRendererPrivate
{}
virtual ~GraphicsItemRendererPrivate(){}
void setupAspectRatio() {
matrix(0, 0) = (GLfloat)out_rect.width()/(GLfloat)renderer_width;
matrix(1, 1) = (GLfloat)out_rect.height()/(GLfloat)renderer_height;
matrix.setToIdentity();
matrix.scale((GLfloat)out_rect.width()/(GLfloat)renderer_width, (GLfloat)out_rect.height()/(GLfloat)renderer_height, 1);
if (orientation)
matrix.rotate(orientation, 0, 0, 1); // Z axis
}
// return true if opengl is enabled and context is ready. may called by non-rendering thread
bool checkGL() {
Expand Down Expand Up @@ -197,6 199,13 @@ void GraphicsItemRenderer::onSetOutAspectRatio(qreal ratio)
d.setupAspectRatio();
}

bool GraphicsItemRenderer::onSetOrientation(int value)
{
Q_UNUSED(value);
d_func().setupAspectRatio();
return true;
}

void GraphicsItemRenderer::onSetOutAspectRatioMode(OutAspectRatioMode mode)
{
Q_UNUSED(mode);
Expand Down
16 changes: 13 additions & 3 deletions src/output/video/OpenGLRendererBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 50,12 @@ OpenGLRendererBasePrivate::~OpenGLRendererBasePrivate() {
}
}

void OpenGLRendererBasePrivate::setupAspectRatio() {
matrix(0, 0) = (GLfloat)out_rect.width()/(GLfloat)renderer_width;
matrix(1, 1) = (GLfloat)out_rect.height()/(GLfloat)renderer_height;
void OpenGLRendererBasePrivate::setupAspectRatio()
{
matrix.setToIdentity();
matrix.scale((GLfloat)out_rect.width()/(GLfloat)renderer_width, (GLfloat)out_rect.height()/(GLfloat)renderer_height, 1);
if (orientation)
matrix.rotate(orientation, 0, 0, 1); // Z axis
}

OpenGLRendererBase::OpenGLRendererBase(OpenGLRendererBasePrivate &d)
Expand Down Expand Up @@ -180,6 183,13 @@ void OpenGLRendererBase::onSetOutAspectRatioMode(OutAspectRatioMode mode)
d.setupAspectRatio();
}

bool OpenGLRendererBase::onSetOrientation(int value)
{
Q_UNUSED(value)
d_func().setupAspectRatio();
return true;
}

bool OpenGLRendererBase::onSetBrightness(qreal b)
{
d_func().glv.setBrightness(b);
Expand Down
1 change: 1 addition & 0 deletions src/output/video/VideoRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 272,7 @@ void VideoRenderer::setOrientation(int value)
d.orientation = old;
} else {
d.computeOutParameters(d.out_aspect_ratio);
onSetOutAspectRatio(outAspectRatio());
resizeFrame(d.out_rect.width(), d.out_rect.height());
}
}
Expand Down

0 comments on commit 0757255

Please sign in to comment.