| 1 | /*  This file is part of the KDE project.
 | 
|---|
| 2 | 
 | 
|---|
| 3 |     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|---|
| 4 | 
 | 
|---|
| 5 |     This library is free software: you can redistribute it and/or modify
 | 
|---|
| 6 |     it under the terms of the GNU Lesser General Public License as published by
 | 
|---|
| 7 |     the Free Software Foundation, either version 2.1 or 3 of the License.
 | 
|---|
| 8 | 
 | 
|---|
| 9 |     This library is distributed in the hope that it will be useful,
 | 
|---|
| 10 |     but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 11 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 12 |     GNU Lesser General Public License for more details.
 | 
|---|
| 13 | 
 | 
|---|
| 14 |     You should have received a copy of the GNU Lesser General Public License
 | 
|---|
| 15 |     along with this library.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 16 | */
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <gst/interfaces/propertyprobe.h>
 | 
|---|
| 19 | #include <gst/gst.h>
 | 
|---|
| 20 | #include "common.h"
 | 
|---|
| 21 | #include "gsthelper.h"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include <QtCore/QList>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | QT_BEGIN_NAMESPACE
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace Phonon
 | 
|---|
| 28 | {
 | 
|---|
| 29 | namespace Gstreamer
 | 
|---|
| 30 | {
 | 
|---|
| 31 | 
 | 
|---|
| 32 | /**
 | 
|---|
| 33 |  * Probes a gstElement for a list of settable string-property values
 | 
|---|
| 34 |  *
 | 
|---|
| 35 |  * @return a QStringList containing a list of allwed string values for the given
 | 
|---|
| 36 |  *           element
 | 
|---|
| 37 |  */
 | 
|---|
| 38 | QList<QByteArray> GstHelper::extractProperties(GstElement *elem, const QByteArray &value)
 | 
|---|
| 39 | {
 | 
|---|
| 40 |     Q_ASSERT(elem);
 | 
|---|
| 41 |     QList<QByteArray> list;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |     if (GST_IS_PROPERTY_PROBE(elem)) {
 | 
|---|
| 44 |         GstPropertyProbe *probe = GST_PROPERTY_PROBE(elem);
 | 
|---|
| 45 |         const GParamSpec *devspec = 0;
 | 
|---|
| 46 |         GValueArray *array = NULL;
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         if ((devspec = gst_property_probe_get_property (probe, value))) {
 | 
|---|
| 49 |             if ((array = gst_property_probe_probe_and_get_values (probe, devspec))) {
 | 
|---|
| 50 |                 for (unsigned int device = 0; device < array->n_values; device++) {
 | 
|---|
| 51 |                     GValue *deviceId = g_value_array_get_nth (array, device);
 | 
|---|
| 52 |                     list.append(g_value_get_string(deviceId));
 | 
|---|
| 53 |                 }
 | 
|---|
| 54 |             }
 | 
|---|
| 55 |             if (array)
 | 
|---|
| 56 |                 g_value_array_free (array);
 | 
|---|
| 57 |         }
 | 
|---|
| 58 |     }
 | 
|---|
| 59 |     return list;
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /**
 | 
|---|
| 63 |  * Sets the string value of a GstElement's property
 | 
|---|
| 64 |  *
 | 
|---|
| 65 |  * @return false if the value could not be set.
 | 
|---|
| 66 |  */
 | 
|---|
| 67 | bool GstHelper::setProperty(GstElement *elem, const char *propertyName, const QByteArray &propertyValue)
 | 
|---|
| 68 | {
 | 
|---|
| 69 |     Q_ASSERT(elem);
 | 
|---|
| 70 |     Q_ASSERT(propertyName && strlen(propertyName));
 | 
|---|
| 71 | 
 | 
|---|
| 72 |     if (GST_IS_PROPERTY_PROBE(elem) && gst_property_probe_get_property( GST_PROPERTY_PROBE( elem), propertyName ) ) {
 | 
|---|
| 73 |         g_object_set(G_OBJECT(elem), propertyName, propertyValue.constData(), (const char*)NULL);
 | 
|---|
| 74 |         return true;
 | 
|---|
| 75 |     }
 | 
|---|
| 76 |     return false;
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /**
 | 
|---|
| 80 |  * Queries an element for the value of an object property
 | 
|---|
| 81 |  */
 | 
|---|
| 82 | QByteArray GstHelper::property(GstElement *elem, const char *propertyName)
 | 
|---|
| 83 | {
 | 
|---|
| 84 |     Q_ASSERT(elem);
 | 
|---|
| 85 |     Q_ASSERT(propertyName && strlen(propertyName));
 | 
|---|
| 86 |     QByteArray retVal;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |     if (GST_IS_PROPERTY_PROBE(elem) && gst_property_probe_get_property( GST_PROPERTY_PROBE(elem), propertyName)) {
 | 
|---|
| 89 |         gchar *value = NULL;
 | 
|---|
| 90 |         g_object_get (G_OBJECT(elem), propertyName, &value, (const char*)NULL);
 | 
|---|
| 91 |         retVal = QByteArray(value);
 | 
|---|
| 92 |         g_free (value);
 | 
|---|
| 93 |     }
 | 
|---|
| 94 |     return retVal;
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | /**
 | 
|---|
| 98 |  * Queries a GstObject for it's name
 | 
|---|
| 99 |  */
 | 
|---|
| 100 | QByteArray GstHelper::name(GstObject *obj)
 | 
|---|
| 101 | {
 | 
|---|
| 102 |     Q_ASSERT(obj);
 | 
|---|
| 103 |     QByteArray retVal;
 | 
|---|
| 104 |     gchar *value = NULL;
 | 
|---|
| 105 |     if ((value = gst_object_get_name (obj))) {
 | 
|---|
| 106 |         retVal = QByteArray(value);
 | 
|---|
| 107 |         g_free (value);
 | 
|---|
| 108 |     }
 | 
|---|
| 109 |     return retVal;
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | 
 | 
|---|
| 113 | /***
 | 
|---|
| 114 |  * Creates an instance of a playbin with "audio-src" and
 | 
|---|
| 115 |  * "video-src" ghost pads to allow redirected output streams.
 | 
|---|
| 116 |  *
 | 
|---|
| 117 |  * ### This function is probably not required now that MediaObject is based
 | 
|---|
| 118 |  *     on decodebin directly.
 | 
|---|
| 119 |  */
 | 
|---|
| 120 | GstElement* GstHelper::createPluggablePlaybin()
 | 
|---|
| 121 | {
 | 
|---|
| 122 |     GstElement *playbin = 0;
 | 
|---|
| 123 |     //init playbin and add to our pipeline
 | 
|---|
| 124 |     playbin = gst_element_factory_make("playbin2", NULL);
 | 
|---|
| 125 | 
 | 
|---|
| 126 |     //Create an identity element to redirect sound
 | 
|---|
| 127 |     GstElement *audioSinkBin =  gst_bin_new (NULL);
 | 
|---|
| 128 |     GstElement *audioPipe = gst_element_factory_make("identity", NULL);
 | 
|---|
| 129 |     gst_bin_add(GST_BIN(audioSinkBin), audioPipe);
 | 
|---|
| 130 | 
 | 
|---|
| 131 |     //Create a sinkpad on the identity
 | 
|---|
| 132 |     GstPad *audiopad = gst_element_get_pad (audioPipe, "sink");
 | 
|---|
| 133 |     gst_element_add_pad (audioSinkBin, gst_ghost_pad_new ("sink", audiopad));
 | 
|---|
| 134 |     gst_object_unref (audiopad);
 | 
|---|
| 135 | 
 | 
|---|
| 136 |     //Create an "audio_src" source pad on the playbin
 | 
|---|
| 137 |     GstPad *audioPlaypad = gst_element_get_pad (audioPipe, "src");
 | 
|---|
| 138 |     gst_element_add_pad (playbin, gst_ghost_pad_new ("audio_src", audioPlaypad));
 | 
|---|
| 139 |     gst_object_unref (audioPlaypad);
 | 
|---|
| 140 | 
 | 
|---|
| 141 |     //Done with our audio redirection
 | 
|---|
| 142 |     g_object_set (G_OBJECT(playbin), "audio-sink", audioSinkBin, (const char*)NULL);
 | 
|---|
| 143 | 
 | 
|---|
| 144 |     // * * Redirect video to "video_src" pad : * *
 | 
|---|
| 145 | 
 | 
|---|
| 146 |     //Create an identity element to redirect sound
 | 
|---|
| 147 |     GstElement *videoSinkBin =  gst_bin_new (NULL);
 | 
|---|
| 148 |     GstElement *videoPipe = gst_element_factory_make("identity", NULL);
 | 
|---|
| 149 |     gst_bin_add(GST_BIN(videoSinkBin), videoPipe);
 | 
|---|
| 150 | 
 | 
|---|
| 151 |     //Create a sinkpad on the identity
 | 
|---|
| 152 |     GstPad *videopad = gst_element_get_pad (videoPipe, "sink");
 | 
|---|
| 153 |     gst_element_add_pad (videoSinkBin, gst_ghost_pad_new ("sink", videopad));
 | 
|---|
| 154 |     gst_object_unref (videopad);
 | 
|---|
| 155 | 
 | 
|---|
| 156 |     //Create an "audio_src" source pad on the playbin
 | 
|---|
| 157 |     GstPad *videoPlaypad = gst_element_get_pad (videoPipe, "src");
 | 
|---|
| 158 |     gst_element_add_pad (playbin, gst_ghost_pad_new ("video_src", videoPlaypad));
 | 
|---|
| 159 |     gst_object_unref (videoPlaypad);
 | 
|---|
| 160 | 
 | 
|---|
| 161 |     //Done with our video redirection
 | 
|---|
| 162 |     g_object_set (G_OBJECT(playbin), "video-sink", videoSinkBin, (const char*)NULL);
 | 
|---|
| 163 |     return playbin;
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | 
 | 
|---|
| 167 | } //namespace Gstreamer
 | 
|---|
| 168 | } //namespace Phonon
 | 
|---|
| 169 | 
 | 
|---|
| 170 | QT_END_NAMESPACE
 | 
|---|