plugins/multimedia/gstreamer/mediacapture/qgstreameraudioencode.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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.h"
       
    43 #include "qgstreamercapturesession.h"
       
    44 #include "qgstreamermediacontainercontrol.h"
       
    45 
       
    46 #include <QtCore/qdebug.h>
       
    47 
       
    48 #include <math.h>
       
    49 
       
    50 QGstreamerAudioEncode::QGstreamerAudioEncode(QObject *parent)
       
    51     :QAudioEncoderControl(parent)
       
    52 {
       
    53     QList<QByteArray> codecCandidates;
       
    54 
       
    55 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
    56     codecCandidates << "audio/PCM"; //<< "audio/AMR" << "audio/AMR-WB" << "audio/speex";
       
    57 #else
       
    58     codecCandidates << "audio/mpeg" << "audio/vorbis" << "audio/speex" << "audio/GSM"
       
    59                     << "audio/PCM" << "audio/AMR" << "audio/AMR-WB";
       
    60 #endif
       
    61 
       
    62 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
    63     m_elementNames["audio/PCM"] = "audioresample";
       
    64     m_elementNames["audio/AMR"] = "nokiaamrnbenc";
       
    65     m_elementNames["audio/AMR-WB"] = "nokiaamrwbenc";
       
    66     m_elementNames["audio/speex"] = "speexenc";
       
    67 #else
       
    68     m_elementNames["audio/mpeg"] = "lamemp3enc";
       
    69     m_elementNames["audio/vorbis"] = "vorbisenc";
       
    70     m_elementNames["audio/speex"] = "speexenc";
       
    71     m_elementNames["audio/GSM"] = "gsmenc";
       
    72     m_elementNames["audio/PCM"] = "audioresample";
       
    73     m_elementNames["audio/AMR"] = "amrnbenc";
       
    74     m_elementNames["audio/AMR-WB"] = "amrwbenc";
       
    75 #endif
       
    76 
       
    77     m_codecOptions["audio/vorbis"] = QStringList() << "min-bitrate" << "max-bitrate";
       
    78     m_codecOptions["audio/mpeg"] = QStringList() << "mode";
       
    79     m_codecOptions["audio/speex"] = QStringList() << "mode" << "vbr" << "vad" << "dtx";
       
    80     m_codecOptions["audio/GSM"] = QStringList();
       
    81     m_codecOptions["audio/PCM"] = QStringList();
       
    82     m_codecOptions["audio/AMR"] = QStringList();
       
    83     m_codecOptions["audio/AMR-WB"] = QStringList();
       
    84 
       
    85     foreach( const QByteArray& codecName, codecCandidates ) {
       
    86         QByteArray elementName = m_elementNames[codecName];
       
    87         GstElementFactory *factory = gst_element_factory_find(elementName.constData());
       
    88 
       
    89         if (factory) {
       
    90             m_codecs.append(codecName);
       
    91             const gchar *descr = gst_element_factory_get_description(factory);
       
    92 
       
    93             if (codecName == QByteArray("audio/PCM"))
       
    94                 m_codecDescriptions.insert(codecName, tr("Raw PCM audio"));
       
    95             else
       
    96                 m_codecDescriptions.insert(codecName, QString::fromUtf8(descr));
       
    97 
       
    98             m_streamTypes.insert(codecName,
       
    99                                  QGstreamerMediaContainerControl::supportedStreamTypes(factory, GST_PAD_SRC));
       
   100 
       
   101             gst_object_unref(GST_OBJECT(factory));
       
   102         }
       
   103     }
       
   104 
       
   105     //if (!m_codecs.isEmpty())
       
   106     //    m_audioSettings.setCodec(m_codecs[0]);
       
   107 }
       
   108 
       
   109 QGstreamerAudioEncode::~QGstreamerAudioEncode()
       
   110 {
       
   111 }
       
   112 
       
   113 QStringList QGstreamerAudioEncode::supportedAudioCodecs() const
       
   114 {
       
   115     return m_codecs;
       
   116 }
       
   117 
       
   118 QString QGstreamerAudioEncode::codecDescription(const QString &codecName) const
       
   119 {
       
   120     return m_codecDescriptions.value(codecName);
       
   121 }
       
   122 
       
   123 QStringList QGstreamerAudioEncode::supportedEncodingOptions(const QString &codec) const
       
   124 {
       
   125     return m_codecOptions.value(codec);
       
   126 }
       
   127 
       
   128 QVariant QGstreamerAudioEncode::encodingOption(
       
   129         const QString &codec, const QString &name) const
       
   130 {
       
   131     return m_options[codec].value(name);
       
   132 }
       
   133 
       
   134 void QGstreamerAudioEncode::setEncodingOption(
       
   135         const QString &codec, const QString &name, const QVariant &value)
       
   136 {
       
   137     m_options[codec][name] = value;
       
   138 }
       
   139 
       
   140 QList<int> QGstreamerAudioEncode::supportedSampleRates(const QAudioEncoderSettings &, bool *) const
       
   141 {
       
   142     //TODO check element caps to find actual values
       
   143 
       
   144     return QList<int>();
       
   145 }
       
   146 
       
   147 QAudioEncoderSettings QGstreamerAudioEncode::audioSettings() const
       
   148 {
       
   149     return m_audioSettings;
       
   150 }
       
   151 
       
   152 void QGstreamerAudioEncode::setAudioSettings(const QAudioEncoderSettings &settings)
       
   153 {
       
   154     m_audioSettings = settings;
       
   155 }
       
   156 
       
   157 
       
   158 
       
   159 GstElement *QGstreamerAudioEncode::createEncoder()
       
   160 {
       
   161     QString codec = m_audioSettings.codec();
       
   162 
       
   163     GstBin * encoderBin = GST_BIN(gst_bin_new("audio-encoder-bin"));
       
   164     Q_ASSERT(encoderBin);
       
   165 
       
   166     GstElement *capsFilter = gst_element_factory_make("capsfilter", NULL);
       
   167     GstElement *encoderElement = gst_element_factory_make(m_elementNames.value(codec).constData(), NULL);
       
   168 
       
   169     Q_ASSERT(encoderElement);
       
   170 
       
   171     gst_bin_add(encoderBin, capsFilter);
       
   172     gst_bin_add(encoderBin, encoderElement);
       
   173     gst_element_link(capsFilter, encoderElement);
       
   174 
       
   175     // add ghostpads
       
   176     GstPad *pad = gst_element_get_static_pad(capsFilter, "sink");
       
   177     gst_element_add_pad(GST_ELEMENT(encoderBin), gst_ghost_pad_new("sink", pad));
       
   178     gst_object_unref(GST_OBJECT(pad));
       
   179 
       
   180     pad = gst_element_get_static_pad(encoderElement, "src");
       
   181     gst_element_add_pad(GST_ELEMENT(encoderBin), gst_ghost_pad_new("src", pad));
       
   182     gst_object_unref(GST_OBJECT(pad));
       
   183 
       
   184     if (m_audioSettings.sampleRate() > 0 || m_audioSettings.channelCount() > 0) {
       
   185         GstCaps *caps = gst_caps_new_empty();
       
   186         GstStructure *structure = gst_structure_new("audio/x-raw-int", NULL);
       
   187 
       
   188         if (m_audioSettings.sampleRate() > 0)
       
   189             gst_structure_set(structure, "rate", G_TYPE_INT, m_audioSettings.sampleRate(), NULL );
       
   190 
       
   191         if (m_audioSettings.channelCount() > 0)
       
   192             gst_structure_set(structure, "channels", G_TYPE_INT, m_audioSettings.channelCount(), NULL );
       
   193 
       
   194         gst_caps_append_structure(caps,structure);
       
   195 
       
   196         //qDebug() << "set caps filter:" << gst_caps_to_string(caps);
       
   197 
       
   198         g_object_set(G_OBJECT(capsFilter), "caps", caps, NULL);
       
   199     }
       
   200 
       
   201     if (encoderElement) {
       
   202         if (m_audioSettings.encodingMode() == QtMultimediaKit::ConstantQualityEncoding) {
       
   203             QtMultimediaKit::EncodingQuality qualityValue = m_audioSettings.quality();
       
   204 
       
   205             if (codec == QLatin1String("audio/vorbis")) {
       
   206                 double qualityTable[] = {
       
   207                     0.1, //VeryLow
       
   208                     0.3, //Low
       
   209                     0.5, //Normal
       
   210                     0.7, //High
       
   211                     1.0 //VeryHigh
       
   212                 };
       
   213                 g_object_set(G_OBJECT(encoderElement), "quality", qualityTable[qualityValue], NULL);
       
   214             } else if (codec == QLatin1String("audio/mpeg")) {
       
   215                 g_object_set(G_OBJECT(encoderElement), "target", 0, NULL); //constant quality mode
       
   216                 qreal quality[] = {
       
   217                     1, //VeryLow
       
   218                     3, //Low
       
   219                     5, //Normal
       
   220                     7, //High
       
   221                     9 //VeryHigh
       
   222                 };
       
   223                 g_object_set(G_OBJECT(encoderElement), "quality", quality[qualityValue], NULL);
       
   224             } else if (codec == QLatin1String("audio/speex")) {
       
   225                 //0-10 range with default 8
       
   226                 double qualityTable[] = {
       
   227                     2, //VeryLow
       
   228                     5, //Low
       
   229                     8, //Normal
       
   230                     9, //High
       
   231                     10 //VeryHigh
       
   232                 };
       
   233                 g_object_set(G_OBJECT(encoderElement), "quality", qualityTable[qualityValue], NULL);
       
   234             } else if (codec.startsWith("audio/AMR")) {
       
   235                 int band[] = {
       
   236                     0, //VeryLow
       
   237                     2, //Low
       
   238                     4, //Normal
       
   239                     6, //High
       
   240                     7  //VeryHigh
       
   241                 };
       
   242 
       
   243                 g_object_set(G_OBJECT(encoderElement), "band-mode", band[qualityValue], NULL);
       
   244             }
       
   245         } else {
       
   246             int bitrate = m_audioSettings.bitRate();
       
   247             if (bitrate > 0) {
       
   248                 if (codec == QLatin1String("audio/mpeg")) {
       
   249                     g_object_set(G_OBJECT(encoderElement), "target", 1, NULL); //constant bitrate mode
       
   250                 }
       
   251                 g_object_set(G_OBJECT(encoderElement), "bitrate", bitrate, NULL);
       
   252             }
       
   253         }
       
   254 
       
   255         QMap<QString, QVariant> options = m_options.value(codec);
       
   256         QMapIterator<QString,QVariant> it(options);
       
   257         while (it.hasNext()) {
       
   258             it.next();
       
   259             QString option = it.key();
       
   260             QVariant value = it.value();
       
   261 
       
   262             switch (value.type()) {
       
   263             case QVariant::Int:
       
   264                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toInt(), NULL);
       
   265                 break;
       
   266             case QVariant::Bool:
       
   267                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toBool(), NULL);
       
   268                 break;
       
   269             case QVariant::Double:
       
   270                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toDouble(), NULL);
       
   271                 break;
       
   272             case QVariant::String:
       
   273                 g_object_set(G_OBJECT(encoderElement), option.toAscii(), value.toString().toUtf8().constData(), NULL);
       
   274                 break;
       
   275             default:
       
   276                 qWarning() << "unsupported option type:" << option << value;
       
   277                 break;
       
   278             }
       
   279 
       
   280         }
       
   281     }
       
   282 
       
   283     return GST_ELEMENT(encoderBin);
       
   284 }
       
   285 
       
   286 
       
   287 QSet<QString> QGstreamerAudioEncode::supportedStreamTypes(const QString &codecName) const
       
   288 {
       
   289     return m_streamTypes.value(codecName);
       
   290 }