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 qmlInfo(this) << "CameraConfig not set";
76 m_renderer = QtCamViewfinderRenderer::create(m_config->config(), this);
78 qmlInfo(this) << "Failed to create viewfinder renderer";
82 m_renderer->resize(QSizeF(width(), height()));
83 QObject::connect(m_renderer, SIGNAL(updateRequested()), this, SLOT(updateRequested()));
86 g_object_set(m_bin, "video-sink", m_renderer->sinkElement(), NULL);
90 void VideoPlayer::classBegin() {
91 m_bin = gst_element_factory_make ("playbin2", "VideoPlayerBin");
93 qmlInfo(this) << "Failed to create playbin2";
97 g_object_set (m_bin, "flags", 99, NULL);
99 GstElement *elem = gst_element_factory_make("pulsesink", "VideoPlayerPulseSink");
101 qmlInfo(this) << "Failed to create pulsesink";
104 // TODO: properties on sink
105 g_object_set (m_bin, "audio-sink", elem, NULL);
108 GstBus *bus = gst_element_get_bus(m_bin);
109 gst_bus_add_watch(bus, bus_call, this);
110 gst_object_unref(bus);
113 QUrl VideoPlayer::source() const {
117 void VideoPlayer::setSource(const QUrl& source) {
118 if (m_url != source) {
120 emit sourceChanged();
124 CameraConfig *VideoPlayer::cameraConfig() const {
128 void VideoPlayer::setCameraConfig(CameraConfig *config) {
129 if (m_config && m_config != config) {
130 qmlInfo(this) << "Cannot reset CameraConfig";
135 qmlInfo(this) << "CameraConfig cannot be empty";
139 if (m_config != config) {
141 emit cameraConfigChanged();
145 qint64 VideoPlayer::duration() const {
150 GstFormat format = GST_FORMAT_TIME;
152 if (!gst_element_query_duration(m_bin, &format, &dur)) {
153 qmlInfo(this) << "Failed to query pipeline duration";
157 if (format != GST_FORMAT_TIME) {
158 qmlInfo(this) << "Pipeline format is not time";
167 qint64 VideoPlayer::position() {
172 GstFormat format = GST_FORMAT_TIME;
174 if (!gst_element_query_position(m_bin, &format, &pos)) {
175 qmlInfo(this) << "Failed to query pipeline position";
179 if (format != GST_FORMAT_TIME) {
180 qmlInfo(this) << "Pipeline format is not time";
191 void VideoPlayer::setPosition(qint64 position) {
195 bool VideoPlayer::pause() {
196 return setState(VideoPlayer::StatePaused);
199 bool VideoPlayer::play() {
200 return setState(VideoPlayer::StatePlaying);
203 bool VideoPlayer::seek(qint64 offset) {
205 qmlInfo(this) << "no playbin2";
213 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE);
214 gboolean ret = gst_element_seek_simple (m_bin, GST_FORMAT_TIME,
226 bool VideoPlayer::stop() {
227 return setState(VideoPlayer::StateStopped);
230 void VideoPlayer::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
232 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
234 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
238 m_renderer->resize(newGeometry.size());
243 void VideoPlayer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
249 painter->fillRect(boundingRect(), Qt::black);
252 void VideoPlayer::paint(QPainter *painter) {
253 painter->fillRect(contentsBoundingRect(), Qt::black);
261 m_renderer->paint(painter);
264 VideoPlayer::State VideoPlayer::state() const {
268 bool VideoPlayer::setState(const VideoPlayer::State& state) {
269 if (state == m_state) {
274 qmlInfo(this) << "no playbin2";
278 if (state == VideoPlayer::StatePaused) {
281 // Set uri if needed:
282 if (m_state == VideoPlayer::StateStopped) {
283 const char *uri = m_url.toString().toUtf8().constData();
284 g_object_set(m_bin, "uri", uri, NULL);
287 if (gst_element_set_state(m_bin, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
288 qmlInfo(this) << "error setting pipeline to PAUSED";
293 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
294 == GST_STATE_CHANGE_FAILURE) {
295 qmlInfo(this) << "setting pipeline to PAUSED failed";
299 if (st != GST_STATE_PAUSED) {
300 qmlInfo(this) << "pipeline failed to transition to to PAUSED state";
309 else if (state == VideoPlayer::StatePlaying) {
310 // Set uri if needed:
311 if (m_state == VideoPlayer::StateStopped) {
312 const char *uri = m_url.toString().toUtf8().constData();
313 g_object_set(m_bin, "uri", uri, NULL);
316 if (gst_element_set_state(m_bin, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
317 qmlInfo(this) << "error setting pipeline to PLAYING";
322 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
323 == GST_STATE_CHANGE_FAILURE) {
324 qmlInfo(this) << "setting pipeline to PLAYING failed";
328 if (st != GST_STATE_PLAYING) {
329 qmlInfo(this) << "pipeline failed to transition to to PLAYING state";
336 emit durationChanged();
337 emit positionChanged();
346 if (gst_element_set_state(m_bin, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
347 qmlInfo(this) << "error setting pipeline to NULL";
352 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
353 == GST_STATE_CHANGE_FAILURE) {
354 qmlInfo(this) << "setting pipeline to NULL failed";
358 if (st != GST_STATE_NULL) {
359 qmlInfo(this) << "pipeline failed to transition to to NULL state";
366 emit durationChanged();
367 emit positionChanged();
373 gboolean VideoPlayer::bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
376 VideoPlayer *that = (VideoPlayer *) data;
381 switch (GST_MESSAGE_TYPE(msg)) {
382 case GST_MESSAGE_EOS:
386 case GST_MESSAGE_ERROR:
387 gst_message_parse_error (msg, &err, &debug);
389 emit that->error(err->message, err->code, debug);
408 void VideoPlayer::updateRequested() {