2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "videoplayer.h"
23 #include <QDeclarativeInfo>
27 #include "cameraconfig.h"
29 #include "qtcamviewfinderrenderer.h"
34 VideoPlayer::VideoPlayer(QDeclarativeItem *parent) :
35 QDeclarativeItem(parent),
37 VideoPlayer::VideoPlayer(QQuickItem *parent) :
38 QQuickPaintedItem(parent),
43 m_state(VideoPlayer::StateStopped),
44 m_timer(new QTimer(this)),
47 m_timer->setSingleShot(false);
48 m_timer->setInterval(500);
49 QObject::connect(m_timer, SIGNAL(timeout()), this, SIGNAL(positionChanged()));
52 setFlag(QGraphicsItem::ItemHasNoContents, false);
56 setRenderTarget(QQuickPaintedItem::FramebufferObject);
58 setAntialiasing(false);
62 VideoPlayer::~VideoPlayer() {
66 gst_object_unref(m_bin);
71 void VideoPlayer::componentComplete() {
73 QDeclarativeItem::componentComplete();
75 QQuickPaintedItem::componentComplete();
79 qmlInfo(this) << "CameraConfig not set";
83 m_renderer = QtCamViewfinderRenderer::create(m_config->config(), this);
85 qmlInfo(this) << "Failed to create viewfinder renderer";
89 m_renderer->resize(QSizeF(width(), height()));
90 QObject::connect(m_renderer, SIGNAL(updateRequested()), this, SLOT(updateRequested()));
93 g_object_set(m_bin, "video-sink", m_renderer->sinkElement(), NULL);
97 void VideoPlayer::classBegin() {
99 QDeclarativeItem::classBegin();
101 QQuickPaintedItem::classBegin();
104 m_bin = gst_element_factory_make ("playbin2", "VideoPlayerBin");
106 qmlInfo(this) << "Failed to create playbin2";
110 g_object_set (m_bin, "flags", 99, NULL);
112 GstElement *elem = gst_element_factory_make("pulsesink", "VideoPlayerPulseSink");
114 qmlInfo(this) << "Failed to create pulsesink";
117 // TODO: properties on sink
118 g_object_set (m_bin, "audio-sink", elem, NULL);
121 GstBus *bus = gst_element_get_bus(m_bin);
122 gst_bus_add_watch(bus, bus_call, this);
123 gst_object_unref(bus);
126 QUrl VideoPlayer::source() const {
130 void VideoPlayer::setSource(const QUrl& source) {
131 if (m_url != source) {
133 emit sourceChanged();
137 CameraConfig *VideoPlayer::cameraConfig() const {
141 void VideoPlayer::setCameraConfig(CameraConfig *config) {
142 if (m_config && m_config != config) {
143 qmlInfo(this) << "Cannot reset CameraConfig";
148 qmlInfo(this) << "CameraConfig cannot be empty";
152 if (m_config != config) {
154 emit cameraConfigChanged();
158 qint64 VideoPlayer::duration() const {
163 GstFormat format = GST_FORMAT_TIME;
165 if (!gst_element_query_duration(m_bin, &format, &dur)) {
166 qmlInfo(this) << "Failed to query pipeline duration";
170 if (format != GST_FORMAT_TIME) {
171 qmlInfo(this) << "Pipeline format is not time";
180 qint64 VideoPlayer::position() {
185 GstFormat format = GST_FORMAT_TIME;
187 if (!gst_element_query_position(m_bin, &format, &pos)) {
188 qmlInfo(this) << "Failed to query pipeline position";
192 if (format != GST_FORMAT_TIME) {
193 qmlInfo(this) << "Pipeline format is not time";
204 void VideoPlayer::setPosition(qint64 position) {
208 bool VideoPlayer::pause() {
209 return setState(VideoPlayer::StatePaused);
212 bool VideoPlayer::play() {
213 return setState(VideoPlayer::StatePlaying);
216 bool VideoPlayer::seek(qint64 offset) {
218 qmlInfo(this) << "no playbin2";
226 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE);
227 gboolean ret = gst_element_seek_simple (m_bin, GST_FORMAT_TIME,
239 bool VideoPlayer::stop() {
240 return setState(VideoPlayer::StateStopped);
243 void VideoPlayer::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
245 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
247 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
251 m_renderer->resize(newGeometry.size());
256 void VideoPlayer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
262 painter->fillRect(boundingRect(), Qt::black);
265 void VideoPlayer::paint(QPainter *painter) {
266 painter->fillRect(contentsBoundingRect(), Qt::black);
274 bool needsNativePainting = m_renderer->needsNativePainting();
276 if (needsNativePainting) {
277 painter->beginNativePainting();
280 m_renderer->paint(QMatrix4x4(painter->combinedTransform()), painter->viewport());
282 if (needsNativePainting) {
283 painter->endNativePainting();
287 VideoPlayer::State VideoPlayer::state() const {
291 bool VideoPlayer::setState(const VideoPlayer::State& state) {
292 if (state == m_state) {
297 qmlInfo(this) << "no playbin2";
301 if (state == VideoPlayer::StatePaused) {
304 // Set uri if needed:
305 if (m_state == VideoPlayer::StateStopped) {
306 const char *uri = m_url.toString().toUtf8().constData();
307 g_object_set(m_bin, "uri", uri, NULL);
310 if (gst_element_set_state(m_bin, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
311 qmlInfo(this) << "error setting pipeline to PAUSED";
316 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
317 == GST_STATE_CHANGE_FAILURE) {
318 qmlInfo(this) << "setting pipeline to PAUSED failed";
322 if (st != GST_STATE_PAUSED) {
323 qmlInfo(this) << "pipeline failed to transition to to PAUSED state";
332 else if (state == VideoPlayer::StatePlaying) {
333 // Set uri if needed:
334 if (m_state == VideoPlayer::StateStopped) {
335 const char *uri = m_url.toString().toUtf8().constData();
336 g_object_set(m_bin, "uri", uri, NULL);
339 if (gst_element_set_state(m_bin, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
340 qmlInfo(this) << "error setting pipeline to PLAYING";
345 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
346 == GST_STATE_CHANGE_FAILURE) {
347 qmlInfo(this) << "setting pipeline to PLAYING failed";
351 if (st != GST_STATE_PLAYING) {
352 qmlInfo(this) << "pipeline failed to transition to to PLAYING state";
359 emit durationChanged();
360 emit positionChanged();
369 if (gst_element_set_state(m_bin, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
370 qmlInfo(this) << "error setting pipeline to NULL";
375 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
376 == GST_STATE_CHANGE_FAILURE) {
377 qmlInfo(this) << "setting pipeline to NULL failed";
381 if (st != GST_STATE_NULL) {
382 qmlInfo(this) << "pipeline failed to transition to to NULL state";
389 emit durationChanged();
390 emit positionChanged();
396 gboolean VideoPlayer::bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
399 VideoPlayer *that = (VideoPlayer *) data;
404 switch (GST_MESSAGE_TYPE(msg)) {
405 case GST_MESSAGE_EOS:
409 case GST_MESSAGE_ERROR:
410 gst_message_parse_error (msg, &err, &debug);
412 emit that->error(err->message, err->code, debug);
431 void VideoPlayer::updateRequested() {