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 VideoPlayer::~VideoPlayer() {
59 gst_object_unref(m_bin);
64 void VideoPlayer::componentComplete() {
66 qmlInfo(this) << "CameraConfig not set";
70 m_renderer = QtCamViewfinderRenderer::create(m_config->config(), this);
72 qmlInfo(this) << "Failed to create viewfinder renderer";
76 m_renderer->resize(QSizeF(width(), height()));
77 QObject::connect(m_renderer, SIGNAL(updateRequested()), this, SLOT(updateRequested()));
80 g_object_set(m_bin, "video-sink", m_renderer->sinkElement(), NULL);
84 void VideoPlayer::classBegin() {
85 m_bin = gst_element_factory_make ("playbin2", "VideoPlayerBin");
87 qmlInfo(this) << "Failed to create playbin2";
91 g_object_set (m_bin, "flags", 99, NULL);
93 GstElement *elem = gst_element_factory_make("pulsesink", "VideoPlayerPulseSink");
95 qmlInfo(this) << "Failed to create pulsesink";
98 // TODO: properties on sink
99 g_object_set (m_bin, "audio-sink", elem, NULL);
102 GstBus *bus = gst_element_get_bus(m_bin);
103 gst_bus_add_watch(bus, bus_call, this);
104 gst_object_unref(bus);
107 QUrl VideoPlayer::source() const {
111 void VideoPlayer::setSource(const QUrl& source) {
112 if (m_url != source) {
114 emit sourceChanged();
118 CameraConfig *VideoPlayer::cameraConfig() const {
122 void VideoPlayer::setCameraConfig(CameraConfig *config) {
123 if (m_config && m_config != config) {
124 qmlInfo(this) << "Cannot reset CameraConfig";
129 qmlInfo(this) << "CameraConfig cannot be empty";
133 if (m_config != config) {
135 emit cameraConfigChanged();
139 qint64 VideoPlayer::duration() const {
144 GstFormat format = GST_FORMAT_TIME;
146 if (!gst_element_query_duration(m_bin, &format, &dur)) {
147 qmlInfo(this) << "Failed to query pipeline duration";
151 if (format != GST_FORMAT_TIME) {
152 qmlInfo(this) << "Pipeline format is not time";
161 qint64 VideoPlayer::position() {
166 GstFormat format = GST_FORMAT_TIME;
168 if (!gst_element_query_position(m_bin, &format, &pos)) {
169 qmlInfo(this) << "Failed to query pipeline position";
173 if (format != GST_FORMAT_TIME) {
174 qmlInfo(this) << "Pipeline format is not time";
185 void VideoPlayer::setPosition(qint64 position) {
189 bool VideoPlayer::pause() {
190 return setState(VideoPlayer::StatePaused);
193 bool VideoPlayer::play() {
194 return setState(VideoPlayer::StatePlaying);
197 bool VideoPlayer::seek(qint64 offset) {
199 qmlInfo(this) << "no playbin2";
207 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE);
208 gboolean ret = gst_element_seek_simple (m_bin, GST_FORMAT_TIME,
220 bool VideoPlayer::stop() {
221 return setState(VideoPlayer::StateStopped);
224 void VideoPlayer::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
226 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
228 QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
232 m_renderer->resize(newGeometry.size());
237 void VideoPlayer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
243 void VideoPlayer::paint(QPainter *painter) {
245 painter->fillRect(boundingRect(), Qt::black);
251 m_renderer->paint(painter);
254 VideoPlayer::State VideoPlayer::state() const {
258 bool VideoPlayer::setState(const VideoPlayer::State& state) {
259 if (state == m_state) {
264 qmlInfo(this) << "no playbin2";
268 if (state == VideoPlayer::StatePaused) {
271 // Set uri if needed:
272 if (m_state == VideoPlayer::StateStopped) {
273 const char *uri = m_url.toString().toUtf8().constData();
274 g_object_set(m_bin, "uri", uri, NULL);
277 if (gst_element_set_state(m_bin, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
278 qmlInfo(this) << "error setting pipeline to PAUSED";
283 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
284 == GST_STATE_CHANGE_FAILURE) {
285 qmlInfo(this) << "setting pipeline to PAUSED failed";
289 if (st != GST_STATE_PAUSED) {
290 qmlInfo(this) << "pipeline failed to transition to to PAUSED state";
299 else if (state == VideoPlayer::StatePlaying) {
300 // Set uri if needed:
301 if (m_state == VideoPlayer::StateStopped) {
302 const char *uri = m_url.toString().toUtf8().constData();
303 g_object_set(m_bin, "uri", uri, NULL);
306 if (gst_element_set_state(m_bin, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
307 qmlInfo(this) << "error setting pipeline to PLAYING";
312 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
313 == GST_STATE_CHANGE_FAILURE) {
314 qmlInfo(this) << "setting pipeline to PLAYING failed";
318 if (st != GST_STATE_PLAYING) {
319 qmlInfo(this) << "pipeline failed to transition to to PLAYING state";
326 emit durationChanged();
327 emit positionChanged();
336 if (gst_element_set_state(m_bin, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE) {
337 qmlInfo(this) << "error setting pipeline to NULL";
342 if (gst_element_get_state(m_bin, &st, NULL, GST_CLOCK_TIME_NONE)
343 == GST_STATE_CHANGE_FAILURE) {
344 qmlInfo(this) << "setting pipeline to NULL failed";
348 if (st != GST_STATE_NULL) {
349 qmlInfo(this) << "pipeline failed to transition to to NULL state";
356 emit durationChanged();
357 emit positionChanged();
363 gboolean VideoPlayer::bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
366 VideoPlayer *that = (VideoPlayer *) data;
371 switch (GST_MESSAGE_TYPE(msg)) {
372 case GST_MESSAGE_EOS:
376 case GST_MESSAGE_ERROR:
377 gst_message_parse_error (msg, &err, &debug);
379 emit that->error(err->message, err->code, debug);
398 void VideoPlayer::updateRequested() {