qtmobility/plugins/multimedia/gstreamer/mediacapture/maemo/qgstreameraudioencode_maemo.cpp
changeset 4 90517678cc4f
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qgstreameraudioencode_maemo.h"
       
    43 #include "qgstreamermediacontainercontrol_maemo.h"
       
    44 
       
    45 #include <QtCore/qdebug.h>
       
    46 
       
    47 QGstreamerAudioEncode::QGstreamerAudioEncode(QObject *parent)
       
    48     :QAudioEncoderControl(parent)
       
    49 {
       
    50     QList<QByteArray> codecCandidates;
       
    51     codecCandidates << "audio/mpeg" << "audio/PCM" << "audio/AMR" << "audio/AMR-WB" << "audio/speex";
       
    52 
       
    53     m_elementNames["audio/mpeg"] = "nokiaaacenc";
       
    54     m_elementNames["audio/speex"] = "speexenc";
       
    55     m_elementNames["audio/PCM"] = "audioresample";
       
    56     m_elementNames["audio/AMR"] = "nokiaamrnbenc";
       
    57     m_elementNames["audio/AMR-WB"] = "nokiaamrwbenc";
       
    58 
       
    59     foreach( const QByteArray& codecName, codecCandidates ) {
       
    60         QByteArray elementName = m_elementNames[codecName];
       
    61         GstElementFactory *factory = gst_element_factory_find(elementName.constData());
       
    62 
       
    63         if (factory) {
       
    64             m_codecs.append(codecName);
       
    65             const gchar *descr = gst_element_factory_get_description(factory);
       
    66 
       
    67             if (codecName == QByteArray("audio/PCM"))
       
    68                 m_codecDescriptions.insert(codecName, tr("Raw PCM audio"));
       
    69             else
       
    70                 m_codecDescriptions.insert(codecName, QString::fromUtf8(descr));
       
    71 
       
    72             m_streamTypes.insert(codecName,
       
    73                                  QGstreamerMediaContainerControl::supportedStreamTypes(factory, GST_PAD_SRC));
       
    74 
       
    75             gst_object_unref(GST_OBJECT(factory));
       
    76         }
       
    77     }
       
    78 
       
    79     if (!m_codecs.isEmpty())
       
    80         m_audioSettings.setCodec(m_codecs[0]);
       
    81 }
       
    82 
       
    83 QGstreamerAudioEncode::~QGstreamerAudioEncode()
       
    84 {
       
    85 }
       
    86 
       
    87 QStringList QGstreamerAudioEncode::supportedAudioCodecs() const
       
    88 {
       
    89     return m_codecs;
       
    90 }
       
    91 
       
    92 QString QGstreamerAudioEncode::codecDescription(const QString &codecName) const
       
    93 {
       
    94     return m_codecDescriptions.value(codecName);
       
    95 }
       
    96 
       
    97 QStringList QGstreamerAudioEncode::supportedEncodingOptions(const QString &codec) const
       
    98 {
       
    99     return m_codecOptions.value(codec);
       
   100 }
       
   101 
       
   102 QVariant QGstreamerAudioEncode::encodingOption(
       
   103         const QString &codec, const QString &name) const
       
   104 {
       
   105     return m_options[codec].value(name);
       
   106 }
       
   107 
       
   108 void QGstreamerAudioEncode::setEncodingOption(
       
   109         const QString &codec, const QString &name, const QVariant &value)
       
   110 {
       
   111     m_options[codec][name] = value;
       
   112 }
       
   113 
       
   114 QList<int> QGstreamerAudioEncode::supportedSampleRates(const QAudioEncoderSettings &, bool *) const
       
   115 {
       
   116     //TODO check element caps to find actual values
       
   117 
       
   118     return QList<int>();
       
   119 }
       
   120 
       
   121 QAudioEncoderSettings QGstreamerAudioEncode::audioSettings() const
       
   122 {
       
   123     return m_audioSettings;
       
   124 }
       
   125 
       
   126 void QGstreamerAudioEncode::setAudioSettings(const QAudioEncoderSettings &settings)
       
   127 {
       
   128     m_audioSettings = settings;
       
   129 }
       
   130 
       
   131 GstElement *QGstreamerAudioEncode::createEncoder()
       
   132 {
       
   133     QString codec = m_audioSettings.codec();
       
   134 
       
   135     GstBin * encoderBin = GST_BIN(gst_bin_new("audio-encoder-bin"));
       
   136     Q_ASSERT(encoderBin);
       
   137 
       
   138     GstElement *capsFilter = gst_element_factory_make("capsfilter", NULL);
       
   139     GstElement *encoderElement = gst_element_factory_make(m_elementNames.value(codec).constData(), NULL);
       
   140 
       
   141     Q_ASSERT(encoderElement);
       
   142 
       
   143     gst_bin_add(encoderBin, capsFilter);
       
   144     gst_bin_add(encoderBin, encoderElement);
       
   145     gst_element_link(capsFilter, encoderElement);
       
   146 
       
   147     // add ghostpads
       
   148     GstPad *pad = gst_element_get_static_pad(capsFilter, "sink");
       
   149     gst_element_add_pad(GST_ELEMENT(encoderBin), gst_ghost_pad_new("sink", pad));
       
   150     gst_object_unref(GST_OBJECT(pad));
       
   151 
       
   152     pad = gst_element_get_static_pad(encoderElement, "src");
       
   153     gst_element_add_pad(GST_ELEMENT(encoderBin), gst_ghost_pad_new("src", pad));
       
   154     gst_object_unref(GST_OBJECT(pad));
       
   155 
       
   156     if (m_audioSettings.sampleRate() > 0 || m_audioSettings.channelCount() > 0) {
       
   157         GstCaps *caps = gst_caps_new_empty();
       
   158         GstStructure *structure = gst_structure_new("audio/x-raw-int", NULL);
       
   159 
       
   160         if (m_audioSettings.sampleRate() > 0)
       
   161             gst_structure_set(structure, "rate", G_TYPE_INT, m_audioSettings.sampleRate(), NULL );
       
   162 
       
   163         if (m_audioSettings.channelCount() > 0)
       
   164             gst_structure_set(structure, "channels", G_TYPE_INT, m_audioSettings.channelCount(), NULL );
       
   165 
       
   166         gst_caps_append_structure(caps,structure);
       
   167 
       
   168         g_object_set(G_OBJECT(capsFilter), "caps", caps, NULL);
       
   169     }
       
   170 
       
   171     if (encoderElement) {
       
   172         if (m_audioSettings.encodingMode() == QtMedia::ConstantQualityEncoding) {
       
   173             QtMedia::EncodingQuality qualityValue = m_audioSettings.quality();
       
   174 
       
   175             if (codec == QLatin1String("audio/mpeg")) {
       
   176                 g_object_set(G_OBJECT(encoderElement), "target", 0, NULL); //constant quality mode
       
   177                 qreal quality[] = {
       
   178                     8000, //VeryLow
       
   179                     64000, //Low
       
   180                     128000, //Normal
       
   181                     192000, //High
       
   182                     320000 //VeryHigh
       
   183                 };
       
   184                 g_object_set(G_OBJECT(encoderElement), "bitrate", quality[qualityValue], NULL);
       
   185             } else if (codec == QLatin1String("audio/speex")) {
       
   186                 //0-10 range with default 8
       
   187                 double qualityTable[] = {
       
   188                     2, //VeryLow
       
   189                     5, //Low
       
   190                     8, //Normal
       
   191                     9, //High
       
   192                     10 //VeryHigh
       
   193                 };
       
   194                 g_object_set(G_OBJECT(encoderElement), "quality", qualityTable[qualityValue], NULL);
       
   195             } else if (codec.startsWith("audio/AMR")) {
       
   196                 int band[] = {
       
   197                     0, //VeryLow
       
   198                     2, //Low
       
   199                     4, //Normal
       
   200                     6, //High
       
   201                     7  //VeryHigh
       
   202                 };
       
   203 
       
   204                 g_object_set(G_OBJECT(encoderElement), "band-mode", band[qualityValue], NULL);
       
   205             }
       
   206         } else {
       
   207             int bitrate = m_audioSettings.bitRate();
       
   208             if (bitrate > 0) {
       
   209                 g_object_set(G_OBJECT(encoderElement), "bitrate", bitrate, NULL);
       
   210             }
       
   211         }
       
   212 
       
   213         QMap<QString, QVariant> options = m_options.value(codec);
       
   214         QMapIterator<QString,QVariant> it(options);
       
   215         while (it.hasNext()) {
       
   216             it.next();
       
   217             QString option = it.key();
       
   218             QVariant value = it.value();
       
   219 
       
   220             switch (value.type()) {
       
   221             case QVariant::Int:
       
   222                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toInt(), NULL);
       
   223                 break;
       
   224             case QVariant::Bool:
       
   225                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toBool(), NULL);
       
   226                 break;
       
   227             case QVariant::Double:
       
   228                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toDouble(), NULL);
       
   229                 break;
       
   230             case QVariant::String:
       
   231                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toString().toUtf8().constData(), NULL);
       
   232                 break;
       
   233             default:
       
   234                 qWarning() << "unsupported option type:" << option << value;
       
   235                 break;
       
   236             }
       
   237 
       
   238         }
       
   239     }
       
   240 
       
   241     return GST_ELEMENT(encoderBin);
       
   242 
       
   243 }
       
   244 
       
   245 
       
   246 QSet<QString> QGstreamerAudioEncode::supportedStreamTypes(const QString &codecName) const
       
   247 {
       
   248     return m_streamTypes.value(codecName);
       
   249 }