src/multimedia/qaudiocapturesource.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 "qmediaobject_p.h"
       
    43 #include <qaudiocapturesource.h>
       
    44 #include "qaudioendpointselector.h"
       
    45 
       
    46 QT_BEGIN_NAMESPACE
       
    47 
       
    48 /*!
       
    49     \class QAudioCaptureSource
       
    50     \brief The QAudioCaptureSource class provides an interface to query and select an audio input endpoint.
       
    51     \ingroup multimedia
       
    52 
       
    53     \preliminary
       
    54 
       
    55     QAudioCaptureSource provides access to the audio inputs available on your system.
       
    56 
       
    57     You can query these inputs and select one to use.
       
    58 
       
    59     A typical implementation example:
       
    60     \code
       
    61         QAudioCaptureSource* audiocapturesource = new QAudioCaptureSource;
       
    62         QMediaRecorder* capture = new QMediaRecorder(audiocapturesource);
       
    63     \endcode
       
    64 
       
    65     The audiocapturesource interface is then used to:
       
    66 
       
    67     - Get and Set the audio input to use.
       
    68 
       
    69     The capture interface is then used to:
       
    70 
       
    71     - Set the destination using setOutputLocation()
       
    72 
       
    73     - Set the format parameters using setAudioCodec(),
       
    74 
       
    75     - Control the recording using record(),stop()
       
    76 
       
    77     \sa QMediaRecorder
       
    78 */
       
    79 
       
    80 class QAudioCaptureSourcePrivate : public QMediaObjectPrivate
       
    81 {
       
    82 public:
       
    83     Q_DECLARE_PUBLIC(QAudioCaptureSource)
       
    84 
       
    85     void initControls()
       
    86     {
       
    87         Q_Q(QAudioCaptureSource);
       
    88 
       
    89         if (service != 0)
       
    90             audioEndpointSelector = qobject_cast<QAudioEndpointSelector*>(service->requestControl(QAudioEndpointSelector_iid));
       
    91 
       
    92         if (audioEndpointSelector) {
       
    93             q->connect(audioEndpointSelector, SIGNAL(activeEndpointChanged(const QString&)),
       
    94                        SIGNAL(activeAudioInputChanged(const QString&)));
       
    95             q->connect(audioEndpointSelector, SIGNAL(availableEndpointsChanged()),
       
    96                        SIGNAL(availableAudioInputsChanged()));
       
    97             q->connect(audioEndpointSelector, SIGNAL(availableEndpointsChanged()),
       
    98                        SLOT(statusChanged()));
       
    99             errorState = QtMultimediaKit::NoError;
       
   100         }
       
   101     }
       
   102 
       
   103     QAudioCaptureSourcePrivate():provider(0), audioEndpointSelector(0), errorState(QtMultimediaKit::ServiceMissingError) {}
       
   104     QMediaServiceProvider *provider;
       
   105     QAudioEndpointSelector   *audioEndpointSelector;
       
   106     QtMultimediaKit::AvailabilityError errorState;
       
   107 };
       
   108 
       
   109 /*!
       
   110     Construct a QAudioCaptureSource using the QMediaService from \a provider, with \a parent.
       
   111 */
       
   112 
       
   113 QAudioCaptureSource::QAudioCaptureSource(QObject *parent, QMediaServiceProvider *provider):
       
   114     QMediaObject(*new QAudioCaptureSourcePrivate, parent, provider->requestService(Q_MEDIASERVICE_AUDIOSOURCE))
       
   115 {
       
   116     Q_D(QAudioCaptureSource);
       
   117 
       
   118     d->provider = provider;
       
   119     d->initControls();
       
   120 }
       
   121 
       
   122 /*!
       
   123     Destroys the audiocapturesource object.
       
   124 */
       
   125 
       
   126 QAudioCaptureSource::~QAudioCaptureSource()
       
   127 {
       
   128     Q_D(QAudioCaptureSource);
       
   129 
       
   130     if (d->service && d->audioEndpointSelector)
       
   131         d->service->releaseControl(d->audioEndpointSelector);
       
   132 
       
   133     if (d->provider)
       
   134         d->provider->releaseService(d->service);
       
   135 }
       
   136 
       
   137 /*!
       
   138     Returns the error state of the audio capture service.
       
   139 */
       
   140 
       
   141 QtMultimediaKit::AvailabilityError QAudioCaptureSource::availabilityError() const
       
   142 {
       
   143     Q_D(const QAudioCaptureSource);
       
   144 
       
   145     return d->errorState;
       
   146 }
       
   147 
       
   148 /*!
       
   149     Returns true if the audio capture service is available, otherwise returns false.
       
   150 */
       
   151 bool QAudioCaptureSource::isAvailable() const
       
   152 {
       
   153     Q_D(const QAudioCaptureSource);
       
   154 
       
   155     if (d->service != NULL) {
       
   156         if (d->audioEndpointSelector && d->audioEndpointSelector->availableEndpoints().size() > 0)
       
   157             return true;
       
   158     }
       
   159     return false;
       
   160 }
       
   161 
       
   162 
       
   163 /*!
       
   164     Returns a list of available audio inputs
       
   165 */
       
   166 
       
   167 QList<QString> QAudioCaptureSource::audioInputs() const
       
   168 {
       
   169     Q_D(const QAudioCaptureSource);
       
   170 
       
   171     QList<QString> list;
       
   172     if (d && d->audioEndpointSelector)
       
   173         list <<d->audioEndpointSelector->availableEndpoints();
       
   174 
       
   175     return list;
       
   176 }
       
   177 
       
   178 /*!
       
   179     Returns the description of the audio input device with \a name.
       
   180 */
       
   181 
       
   182 QString QAudioCaptureSource::audioDescription(const QString& name) const
       
   183 {
       
   184     Q_D(const QAudioCaptureSource);
       
   185 
       
   186     if(d->audioEndpointSelector)
       
   187         return d->audioEndpointSelector->endpointDescription(name);
       
   188     else
       
   189         return QString();
       
   190 }
       
   191 
       
   192 /*!
       
   193     Returns the default audio input name.
       
   194 */
       
   195 
       
   196 QString QAudioCaptureSource::defaultAudioInput() const
       
   197 {
       
   198     Q_D(const QAudioCaptureSource);
       
   199 
       
   200     if(d->audioEndpointSelector)
       
   201         return d->audioEndpointSelector->defaultEndpoint();
       
   202     else
       
   203         return QString();
       
   204 }
       
   205 
       
   206 /*!
       
   207     Returns the active audio input name.
       
   208 */
       
   209 
       
   210 QString QAudioCaptureSource::activeAudioInput() const
       
   211 {
       
   212     Q_D(const QAudioCaptureSource);
       
   213 
       
   214     if(d->audioEndpointSelector)
       
   215         return d->audioEndpointSelector->activeEndpoint();
       
   216     else
       
   217         return QString();
       
   218 }
       
   219 
       
   220 /*!
       
   221     Set the active audio input to \a name.
       
   222 */
       
   223 
       
   224 void QAudioCaptureSource::setAudioInput(const QString& name)
       
   225 {
       
   226     Q_D(const QAudioCaptureSource);
       
   227 
       
   228     if(d->audioEndpointSelector)
       
   229         return d->audioEndpointSelector->setActiveEndpoint(name);
       
   230 }
       
   231 
       
   232 /*!
       
   233     \fn QAudioCaptureSource::activeAudioInputChanged(const QString& name)
       
   234 
       
   235     Signal emitted when active audio input changes to \a name.
       
   236 */
       
   237 
       
   238 /*!
       
   239     \fn QAudioCaptureSource::availableAudioInputsChanged()
       
   240 
       
   241     Signal is emitted when the available audio inputs change.
       
   242 */
       
   243 
       
   244 /*!
       
   245   \internal
       
   246 */
       
   247 void QAudioCaptureSource::statusChanged()
       
   248 {
       
   249     Q_D(QAudioCaptureSource);
       
   250 
       
   251     if (d->audioEndpointSelector) {
       
   252         if (d->audioEndpointSelector->availableEndpoints().size() > 0) {
       
   253             d->errorState = QtMultimediaKit::NoError;
       
   254             emit availabilityChanged(true);
       
   255         } else {
       
   256             d->errorState = QtMultimediaKit::BusyError;
       
   257             emit availabilityChanged(false);
       
   258         }
       
   259     } else {
       
   260         d->errorState = QtMultimediaKit::ServiceMissingError;
       
   261         emit availabilityChanged(false);
       
   262     }
       
   263 }
       
   264 
       
   265 #include "moc_qaudiocapturesource.cpp"
       
   266 QT_END_NAMESPACE
       
   267