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"
33 VideoPlayer::VideoPlayer(QDeclarativeItem *parent) :
34 QDeclarativeItem(parent),
36 VideoPlayer::VideoPlayer(QQuickItem *parent) :
37 QQuickPaintedItem(parent),
42 m_state(VideoPlayer::StateStopped),
43 m_timer(new QTimer(this)),
46 m_timer->setSingleShot(false);
47 m_timer->setInterval(500);
48 QObject::connect(m_timer, SIGNAL(timeout()), this, SIGNAL(positionChanged()));
51 setFlag(QGraphicsItem::ItemHasNoContents, false);
55 setRenderTarget(QQuickPaintedItem::FramebufferObject);
57 setAntialiasing(false);
61 VideoPlayer::~VideoPlayer() {
65 gst_object_unref(m_bin);
70 void VideoPlayer::componentComplete() {
72 QDeclarativeItem::componentComplete();
74 QQuickPaintedItem::componentComplete();
78 qmlInfo(this) << "CameraConfig not set";
82 m_renderer = QtCamViewfinderRenderer::create(m_config->config(), this);
84 qmlInfo(this) << "Failed to create viewfinder renderer";
88 m_renderer->resize(QSizeF(width(), height()));
89 QObject::connect(m_renderer, SIGNAL(updateRequested()), this, SLOT(updateRequested()));
92 g_object_set(m_bin, "video-sink", m_renderer->sinkElement(), NULL);
96 void VideoPlayer::classBegin() {
97 m_bin = gst_element_factory_make ("playbin2", "VideoPlayerBin");
99 qmlInfo(this) << "Failed to create playbin2";
103 g_object_set (m_bin, "flags", 99, NULL);
105 GstElement *elem = gst_element_factory_make("pulsesink", "VideoPlayerPulseSink");
107 qmlInfo(this) << "Failed to create pulsesink";
110 // TODO: properties on sink
111 g_object_set (m_bin, "audio-sink", elem, NULL);
114 GstBus *bus = gst_element_get_bus(m_bin);
115 gst_bus_add_watch(bus, bus_call, this);
116 gst_object_unref(bus);
119 QUrl VideoPlayer::source() const {
123 void VideoPlayer::setSource(const QUrl& source) {
124 if (m_url != source) {
126 emit sourceChanged();
130 CameraConfig *VideoPlayer::cameraConfig() const {
134 void VideoPlayer::setCameraConfig(CameraConfig *config) {
135 if (m_config && m_config != config) {
136 qmlInfo(this) << "Cannot reset CameraConfig";
141 qmlInfo(this) << "CameraConfig cannot be empty";
145 if (m_config != config) {
147 emit cameraConfigChanged();
151 qint64 VideoPlayer::duration() const {
156 GstFormat format = GST_FORMAT_TIME;
158 if (!gst_element_query_duration(m_bin, &format, &dur)) {
159 qmlInfo(this) << "Failed to query pipeline duration";
163 if (format != GST_FORMAT_TIME) {
164 qmlInfo(this) << "Pipeline format is not time";
173 qint64 VideoPlayer::position() {
178 GstFormat format = GST_FORMAT_TIME;
180 if (!gst_element_query_position(m_bin, &format, &pos)) {
181 qmlInfo(this) << "Failed to query pipeline position";
185 if (format != GST_FORMAT_TIME) {
186 qmlInfo(this) << "Pipeline format is not time";
197 void VideoPlayer::setPosition(qint64 position) {
201 bool VideoPlayer::pause() {
202 return setState(VideoPlayer::StatePaused);
205 bool VideoPlayer::play() {
206 return setState(VideoPlayer::StatePlaying);
209 bool VideoPlayer::seek(qint64 offset) {
211 qmlInfo(this) << "no playbin2";
219 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE);
220 gboolean ret = gst_element_seek_simple (m_bin, GST_FORMAT_TIME,
232 bool VideoPlayer::stop() {
233 return setState(VideoPlayer::StateStopped);
236 void VideoPlayer::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
238 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
240 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
244 m_renderer->resize(newGeometry.size());
249 void VideoPlayer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
255 painter->fillRect(boundingRect(), Qt::black);
258 void VideoPlayer::paint(QPainter *painter) {
259 painter->fillRect(contentsBoundingRect(), Qt::black);
267 bool needsNativePainting = m_renderer->needsNativePainting();
269 if (needsNativePainting) {
270 painter->beginNativePainting();
273 m_renderer->paint(QMatrix4x4(painter->combinedTransform()), painter->viewport());
275 if (needsNativePainting) {
276 painter->endNativePainting();
280 VideoPlayer::State VideoPlayer::state() const {
284 bool VideoPlayer::setState(const VideoPlayer::State& state) {
285 if (state == m_state) {
290 qmlInfo(this) << "no playbin2";
294 if (state == VideoPlayer::StatePaused) {
297 // Set uri if needed:
298 if (m_state == VideoPlayer::StateStopped) {
299 const char *uri = m_url.toString().toUtf8().constData();
300 g_object_set(m_bin, "uri", uri, NULL);
303 if (gst_element_set_state(m_bin, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
304 qmlInfo(this) << "error setting pipeline to PAUSED";
309 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
310 == GST_STATE_CHANGE_FAILURE) {
311 qmlInfo(this) << "setting pipeline to PAUSED failed";
315 if (st != GST_STATE_PAUSED) {
316 qmlInfo(this) << "pipeline failed to transition to to PAUSED state";
325 else if (state == VideoPlayer::StatePlaying) {
326 // Set uri if needed:
327 if (m_state == VideoPlayer::StateStopped) {
328 const char *uri = m_url.toString().toUtf8().constData();
329 g_object_set(m_bin, "uri", uri, NULL);
332 if (gst_element_set_state(m_bin, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
333 qmlInfo(this) << "error setting pipeline to PLAYING";
338 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
339 == GST_STATE_CHANGE_FAILURE) {
340 qmlInfo(this) << "setting pipeline to PLAYING failed";
344 if (st != GST_STATE_PLAYING) {
345 qmlInfo(this) << "pipeline failed to transition to to PLAYING state";
352 emit durationChanged();
353 emit positionChanged();
362 if (gst_element_set_state(m_bin, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
363 qmlInfo(this) << "error setting pipeline to NULL";
368 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
369 == GST_STATE_CHANGE_FAILURE) {
370 qmlInfo(this) << "setting pipeline to NULL failed";
374 if (st != GST_STATE_NULL) {
375 qmlInfo(this) << "pipeline failed to transition to to NULL state";
382 emit durationChanged();
383 emit positionChanged();
389 gboolean VideoPlayer::bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
392 VideoPlayer *that = (VideoPlayer *) data;
397 switch (GST_MESSAGE_TYPE(msg)) {
398 case GST_MESSAGE_EOS:
402 case GST_MESSAGE_ERROR:
403 gst_message_parse_error (msg, &err, &debug);
405 emit that->error(err->message, err->code, debug);
424 void VideoPlayer::updateRequested() {