qtmobility/src/multimedia/qaudiocapturesource.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     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 <qmediaobject_p.h>
       
    43 #include <qaudiocapturesource.h>
       
    44 #include <qaudioendpointselector.h>
       
    45 
       
    46 QTM_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->control(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 = QtMedia::NoError;
       
   100         }
       
   101     }
       
   102 
       
   103     QAudioCaptureSourcePrivate():provider(0), audioEndpointSelector(0), errorState(QtMedia::ServiceMissingError) {}
       
   104     QMediaServiceProvider *provider;
       
   105     QAudioEndpointSelector   *audioEndpointSelector;
       
   106     QtMedia::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     Construct a QAudioCaptureSource using the QMediaObject \a mediaObject, with \a parent.
       
   124 */
       
   125 
       
   126 QAudioCaptureSource::QAudioCaptureSource(QMediaObject *mediaObject, QObject *parent)
       
   127     :QMediaObject(*new QAudioCaptureSourcePrivate, parent, mediaObject->service())
       
   128 {
       
   129     Q_D(QAudioCaptureSource);
       
   130 
       
   131     d->provider = 0;
       
   132     d->initControls();
       
   133 }
       
   134 
       
   135 /*!
       
   136     Destroys the audiocapturesource object.
       
   137 */
       
   138 
       
   139 QAudioCaptureSource::~QAudioCaptureSource()
       
   140 {
       
   141     Q_D(QAudioCaptureSource);
       
   142 
       
   143     if (d->provider)
       
   144         d->provider->releaseService(d->service);
       
   145 }
       
   146 
       
   147 /*!
       
   148     Returns the error state of the audio capture service.
       
   149 */
       
   150 
       
   151 QtMedia::AvailabilityError QAudioCaptureSource::availabilityError() const
       
   152 {
       
   153     Q_D(const QAudioCaptureSource);
       
   154 
       
   155     return d->errorState;
       
   156 }
       
   157 
       
   158 /*!
       
   159     Returns true if the audio capture service is available, otherwise returns false.
       
   160 */
       
   161 bool QAudioCaptureSource::isAvailable() const
       
   162 {
       
   163     Q_D(const QAudioCaptureSource);
       
   164 
       
   165     if (d->service != NULL) {
       
   166         if (d->audioEndpointSelector && d->audioEndpointSelector->availableEndpoints().size() > 0)
       
   167             return true;
       
   168     }
       
   169     return false;
       
   170 }
       
   171 
       
   172 
       
   173 /*!
       
   174     Returns a list of available audio inputs
       
   175 */
       
   176 
       
   177 QList<QString> QAudioCaptureSource::audioInputs() const
       
   178 {
       
   179     Q_D(const QAudioCaptureSource);
       
   180 
       
   181     QList<QString> list;
       
   182     if (d && d->audioEndpointSelector)
       
   183         list <<d->audioEndpointSelector->availableEndpoints();
       
   184 
       
   185     return list;
       
   186 }
       
   187 
       
   188 /*!
       
   189     Returns the description of the audio input device with \a name.
       
   190 */
       
   191 
       
   192 QString QAudioCaptureSource::audioDescription(const QString& name) const
       
   193 {
       
   194     Q_D(const QAudioCaptureSource);
       
   195 
       
   196     if(d->audioEndpointSelector)
       
   197         return d->audioEndpointSelector->endpointDescription(name);
       
   198     else
       
   199         return QString();
       
   200 }
       
   201 
       
   202 /*!
       
   203     Returns the default audio input name.
       
   204 */
       
   205 
       
   206 QString QAudioCaptureSource::defaultAudioInput() const
       
   207 {
       
   208     Q_D(const QAudioCaptureSource);
       
   209 
       
   210     if(d->audioEndpointSelector)
       
   211         return d->audioEndpointSelector->defaultEndpoint();
       
   212     else
       
   213         return QString();
       
   214 }
       
   215 
       
   216 /*!
       
   217     Returns the active audio input name.
       
   218 */
       
   219 
       
   220 QString QAudioCaptureSource::activeAudioInput() const
       
   221 {
       
   222     Q_D(const QAudioCaptureSource);
       
   223 
       
   224     if(d->audioEndpointSelector)
       
   225         return d->audioEndpointSelector->activeEndpoint();
       
   226     else
       
   227         return QString();
       
   228 }
       
   229 
       
   230 /*!
       
   231     Set the active audio input to \a name.
       
   232 */
       
   233 
       
   234 void QAudioCaptureSource::setAudioInput(const QString& name)
       
   235 {
       
   236     Q_D(const QAudioCaptureSource);
       
   237 
       
   238     if(d->audioEndpointSelector)
       
   239         return d->audioEndpointSelector->setActiveEndpoint(name);
       
   240 }
       
   241 
       
   242 /*!
       
   243     \fn QAudioCaptureSource::activeAudioInputChanged(const QString& name)
       
   244 
       
   245     Signal emitted when active audio input changes to \a name.
       
   246 */
       
   247 
       
   248 /*!
       
   249     \fn QAudioCaptureSource::availableAudioInputsChanged()
       
   250 
       
   251     Signal is emitted when the available audio inputs change.
       
   252 */
       
   253 
       
   254 /*!
       
   255   \internal
       
   256 */
       
   257 void QAudioCaptureSource::statusChanged()
       
   258 {
       
   259     Q_D(QAudioCaptureSource);
       
   260 
       
   261     if (d->audioEndpointSelector) {
       
   262         if (d->audioEndpointSelector->availableEndpoints().size() > 0) {
       
   263             d->errorState = QtMedia::NoError;
       
   264             emit availabilityChanged(true);
       
   265         } else {
       
   266             d->errorState = QtMedia::BusyError;
       
   267             emit availabilityChanged(false);
       
   268         }
       
   269     } else {
       
   270         d->errorState = QtMedia::ServiceMissingError;
       
   271         emit availabilityChanged(false);
       
   272     }
       
   273 }
       
   274 
       
   275 #include "moc_qaudiocapturesource.cpp"
       
   276 QTM_END_NAMESPACE
       
   277