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"
35 VideoPlayer::VideoPlayer(QDeclarativeItem *parent) :
36 QDeclarativeItem(parent),
38 VideoPlayer::VideoPlayer(QQuickItem *parent) :
39 QQuickPaintedItem(parent),
44 m_state(VideoPlayer::StateStopped),
45 m_timer(new QTimer(this)),
48 m_timer->setSingleShot(false);
49 m_timer->setInterval(500);
50 QObject::connect(m_timer, SIGNAL(timeout()), this, SIGNAL(positionChanged()));
53 setFlag(QGraphicsItem::ItemHasNoContents, false);
57 setRenderTarget(QQuickPaintedItem::FramebufferObject);
59 setAntialiasing(false);
63 VideoPlayer::~VideoPlayer() {
67 gst_object_unref(m_bin);
72 void VideoPlayer::componentComplete() {
74 QDeclarativeItem::componentComplete();
76 QQuickPaintedItem::componentComplete();
80 qmlInfo(this) << "CameraConfig not set";
84 m_renderer = QtCamViewfinderRenderer::create(m_config->config(), this);
86 qmlInfo(this) << "Failed to create viewfinder renderer";
90 m_renderer->resize(QSizeF(width(), height()));
91 QObject::connect(m_renderer, SIGNAL(updateRequested()), this, SLOT(updateRequested()));
94 g_object_set(m_bin, "video-sink", m_renderer->sinkElement(), NULL);
98 void VideoPlayer::classBegin() {
100 QDeclarativeItem::classBegin();
102 QQuickPaintedItem::classBegin();
105 m_bin = gst_element_factory_make ("playbin2", "VideoPlayerBin");
107 qmlInfo(this) << "Failed to create playbin2";
111 g_signal_connect (G_OBJECT (m_bin), "notify::volume", G_CALLBACK (on_volume_changed), this);
112 g_object_set (m_bin, "flags", 99, NULL);
114 GstElement *elem = gst_element_factory_make("pulsesink", "VideoPlayerPulseSink");
116 qmlInfo(this) << "Failed to create pulsesink";
119 g_object_set (m_bin, "audio-sink", elem, NULL);
122 GstBus *bus = gst_element_get_bus(m_bin);
123 gst_bus_add_watch(bus, bus_call, this);
124 gst_object_unref(bus);
127 QUrl VideoPlayer::source() const {
131 void VideoPlayer::setSource(const QUrl& source) {
132 if (m_url != source) {
134 emit sourceChanged();
138 CameraConfig *VideoPlayer::cameraConfig() const {
142 void VideoPlayer::setCameraConfig(CameraConfig *config) {
143 if (m_config && m_config != config) {
144 qmlInfo(this) << "Cannot reset CameraConfig";
149 qmlInfo(this) << "CameraConfig cannot be empty";
153 if (m_config != config) {
155 emit cameraConfigChanged();
159 qint64 VideoPlayer::duration() const {
164 GstFormat format = GST_FORMAT_TIME;
166 if (!gst_element_query_duration(m_bin, &format, &dur)) {
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)) {
191 if (format != GST_FORMAT_TIME) {
192 qmlInfo(this) << "Pipeline format is not time";
203 void VideoPlayer::setPosition(qint64 position) {
207 bool VideoPlayer::pause() {
208 return setState(VideoPlayer::StatePaused);
211 bool VideoPlayer::play() {
212 return setState(VideoPlayer::StatePlaying);
215 bool VideoPlayer::seek(qint64 offset) {
217 qmlInfo(this) << "no playbin2";
225 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE);
226 gboolean ret = gst_element_seek_simple (m_bin, GST_FORMAT_TIME,
238 bool VideoPlayer::stop() {
239 return setState(VideoPlayer::StateStopped);
242 void VideoPlayer::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
244 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
246 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
250 m_renderer->resize(newGeometry.size());
255 void VideoPlayer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
261 painter->fillRect(boundingRect(), Qt::black);
264 void VideoPlayer::paint(QPainter *painter) {
265 painter->fillRect(contentsBoundingRect(), Qt::black);
273 bool needsNativePainting = m_renderer->needsNativePainting();
275 if (needsNativePainting) {
276 painter->beginNativePainting();
279 m_renderer->paint(QMatrix4x4(painter->combinedTransform()), painter->viewport());
281 if (needsNativePainting) {
282 painter->endNativePainting();
286 VideoPlayer::State VideoPlayer::state() const {
290 bool VideoPlayer::setState(const VideoPlayer::State& state) {
291 if (state == m_state) {
296 qmlInfo(this) << "no playbin2";
300 if (state == VideoPlayer::StatePaused) {
303 // Set uri if needed:
304 if (m_state == VideoPlayer::StateStopped) {
305 const char *uri = m_url.toString().toUtf8().constData();
306 g_object_set(m_bin, "uri", uri, NULL);
309 if (gst_element_set_state(m_bin, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
310 qmlInfo(this) << "error setting pipeline to PAUSED";
315 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
316 == GST_STATE_CHANGE_FAILURE) {
317 qmlInfo(this) << "setting pipeline to PAUSED failed";
321 if (st != GST_STATE_PAUSED) {
322 qmlInfo(this) << "pipeline failed to transition to to PAUSED state";
331 else if (state == VideoPlayer::StatePlaying) {
332 // Set uri if needed:
333 if (m_state == VideoPlayer::StateStopped) {
334 const char *uri = m_url.toString().toUtf8().constData();
335 g_object_set(m_bin, "uri", uri, NULL);
338 if (gst_element_set_state(m_bin, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
339 qmlInfo(this) << "error setting pipeline to PLAYING";
344 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
345 == GST_STATE_CHANGE_FAILURE) {
346 qmlInfo(this) << "setting pipeline to PLAYING failed";
350 if (st != GST_STATE_PLAYING) {
351 qmlInfo(this) << "pipeline failed to transition to to PLAYING state";
358 emit durationChanged();
359 emit positionChanged();
368 if (gst_element_set_state(m_bin, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
369 qmlInfo(this) << "error setting pipeline to NULL";
374 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
375 == GST_STATE_CHANGE_FAILURE) {
376 qmlInfo(this) << "setting pipeline to NULL failed";
380 if (st != GST_STATE_NULL) {
381 qmlInfo(this) << "pipeline failed to transition to to NULL state";
388 emit durationChanged();
389 emit positionChanged();
395 gboolean VideoPlayer::bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
398 VideoPlayer *that = (VideoPlayer *) data;
403 switch (GST_MESSAGE_TYPE(msg)) {
404 case GST_MESSAGE_EOS:
408 case GST_MESSAGE_ERROR:
409 gst_message_parse_error (msg, &err, &debug);
411 emit that->error(err->message, err->code, debug);
430 void VideoPlayer::updateRequested() {
434 quint32 VideoPlayer::volume() {
436 g_object_get (m_bin, "volume", &vol, NULL);
438 qint32 res = (int)round(vol * 100.0);
443 void VideoPlayer::setVolume(quint32 volume) {
444 if (VideoPlayer::volume() != volume) {
445 double vol = volume / 100.0;
446 g_object_set (m_bin, "volume", vol, NULL);
447 emit volumeChanged();
451 void VideoPlayer::on_volume_changed(GObject *object, GParamSpec *pspec, gpointer user_data) {
455 VideoPlayer *player = (VideoPlayer *) user_data;
457 QMetaObject::invokeMethod(player, "volumeChanged", Qt::QueuedConnection);