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() {
98 m_bin = gst_element_factory_make ("playbin2", "VideoPlayerBin");
100 qmlInfo(this) << "Failed to create playbin2";
104 g_object_set (m_bin, "flags", 99, NULL);
106 GstElement *elem = gst_element_factory_make("pulsesink", "VideoPlayerPulseSink");
108 qmlInfo(this) << "Failed to create pulsesink";
111 // TODO: properties on sink
112 g_object_set (m_bin, "audio-sink", elem, NULL);
115 GstBus *bus = gst_element_get_bus(m_bin);
116 gst_bus_add_watch(bus, bus_call, this);
117 gst_object_unref(bus);
120 QUrl VideoPlayer::source() const {
124 void VideoPlayer::setSource(const QUrl& source) {
125 if (m_url != source) {
127 emit sourceChanged();
131 CameraConfig *VideoPlayer::cameraConfig() const {
135 void VideoPlayer::setCameraConfig(CameraConfig *config) {
136 if (m_config && m_config != config) {
137 qmlInfo(this) << "Cannot reset CameraConfig";
142 qmlInfo(this) << "CameraConfig cannot be empty";
146 if (m_config != config) {
148 emit cameraConfigChanged();
152 qint64 VideoPlayer::duration() const {
157 GstFormat format = GST_FORMAT_TIME;
159 if (!gst_element_query_duration(m_bin, &format, &dur)) {
160 qmlInfo(this) << "Failed to query pipeline duration";
164 if (format != GST_FORMAT_TIME) {
165 qmlInfo(this) << "Pipeline format is not time";
174 qint64 VideoPlayer::position() {
179 GstFormat format = GST_FORMAT_TIME;
181 if (!gst_element_query_position(m_bin, &format, &pos)) {
182 qmlInfo(this) << "Failed to query pipeline position";
186 if (format != GST_FORMAT_TIME) {
187 qmlInfo(this) << "Pipeline format is not time";
198 void VideoPlayer::setPosition(qint64 position) {
202 bool VideoPlayer::pause() {
203 return setState(VideoPlayer::StatePaused);
206 bool VideoPlayer::play() {
207 return setState(VideoPlayer::StatePlaying);
210 bool VideoPlayer::seek(qint64 offset) {
212 qmlInfo(this) << "no playbin2";
220 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE);
221 gboolean ret = gst_element_seek_simple (m_bin, GST_FORMAT_TIME,
233 bool VideoPlayer::stop() {
234 return setState(VideoPlayer::StateStopped);
237 void VideoPlayer::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
239 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
241 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
245 m_renderer->resize(newGeometry.size());
250 void VideoPlayer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
256 painter->fillRect(boundingRect(), Qt::black);
259 void VideoPlayer::paint(QPainter *painter) {
260 painter->fillRect(contentsBoundingRect(), Qt::black);
268 bool needsNativePainting = m_renderer->needsNativePainting();
270 if (needsNativePainting) {
271 painter->beginNativePainting();
274 m_renderer->paint(QMatrix4x4(painter->combinedTransform()), painter->viewport());
276 if (needsNativePainting) {
277 painter->endNativePainting();
281 VideoPlayer::State VideoPlayer::state() const {
285 bool VideoPlayer::setState(const VideoPlayer::State& state) {
286 if (state == m_state) {
291 qmlInfo(this) << "no playbin2";
295 if (state == VideoPlayer::StatePaused) {
298 // Set uri if needed:
299 if (m_state == VideoPlayer::StateStopped) {
300 const char *uri = m_url.toString().toUtf8().constData();
301 g_object_set(m_bin, "uri", uri, NULL);
304 if (gst_element_set_state(m_bin, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
305 qmlInfo(this) << "error setting pipeline to PAUSED";
310 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
311 == GST_STATE_CHANGE_FAILURE) {
312 qmlInfo(this) << "setting pipeline to PAUSED failed";
316 if (st != GST_STATE_PAUSED) {
317 qmlInfo(this) << "pipeline failed to transition to to PAUSED state";
326 else if (state == VideoPlayer::StatePlaying) {
327 // Set uri if needed:
328 if (m_state == VideoPlayer::StateStopped) {
329 const char *uri = m_url.toString().toUtf8().constData();
330 g_object_set(m_bin, "uri", uri, NULL);
333 if (gst_element_set_state(m_bin, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
334 qmlInfo(this) << "error setting pipeline to PLAYING";
339 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
340 == GST_STATE_CHANGE_FAILURE) {
341 qmlInfo(this) << "setting pipeline to PLAYING failed";
345 if (st != GST_STATE_PLAYING) {
346 qmlInfo(this) << "pipeline failed to transition to to PLAYING state";
353 emit durationChanged();
354 emit positionChanged();
363 if (gst_element_set_state(m_bin, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
364 qmlInfo(this) << "error setting pipeline to NULL";
369 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
370 == GST_STATE_CHANGE_FAILURE) {
371 qmlInfo(this) << "setting pipeline to NULL failed";
375 if (st != GST_STATE_NULL) {
376 qmlInfo(this) << "pipeline failed to transition to to NULL state";
383 emit durationChanged();
384 emit positionChanged();
390 gboolean VideoPlayer::bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
393 VideoPlayer *that = (VideoPlayer *) data;
398 switch (GST_MESSAGE_TYPE(msg)) {
399 case GST_MESSAGE_EOS:
403 case GST_MESSAGE_ERROR:
404 gst_message_parse_error (msg, &err, &debug);
406 emit that->error(err->message, err->code, debug);
425 void VideoPlayer::updateRequested() {