qtmobility/plugins/multimedia/gstreamer/mediacapture/maemo/qgstreamerrecordercontrol_maemo.cpp
changeset 4 90517678cc4f
child 11 06b8e2af4411
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 "qgstreamerrecordercontrol_maemo.h"
       
    43 #include "qgstreameraudioencode_maemo.h"
       
    44 #include "qgstreamervideoencode_maemo.h"
       
    45 #include "qgstreamermediacontainercontrol_maemo.h"
       
    46 #include <QtCore/QDebug>
       
    47 
       
    48 QGstreamerRecorderControl::QGstreamerRecorderControl(QGstreamerCaptureSession *session)
       
    49     :QMediaRecorderControl(session), m_session(session), m_state(QMediaRecorder::StoppedState)
       
    50 {
       
    51     connect(m_session, SIGNAL(stateChanged(QGstreamerCaptureSession::State)), SLOT(updateState()));
       
    52     connect(m_session, SIGNAL(error(int,QString)), SIGNAL(error(int,QString)));
       
    53     connect(m_session, SIGNAL(durationChanged(qint64)), SIGNAL(durationChanged(qint64)));
       
    54     m_hasPreviewState = m_session->captureMode() != QGstreamerCaptureSession::Audio;
       
    55 }
       
    56 
       
    57 QGstreamerRecorderControl::~QGstreamerRecorderControl()
       
    58 {
       
    59 }
       
    60 
       
    61 QUrl QGstreamerRecorderControl::outputLocation() const
       
    62 {
       
    63     return m_session->outputLocation();
       
    64 }
       
    65 
       
    66 bool QGstreamerRecorderControl::setOutputLocation(const QUrl &sink)
       
    67 {
       
    68     m_session->setOutputLocation(sink);
       
    69     return true;
       
    70 }
       
    71 
       
    72 
       
    73 QMediaRecorder::State QGstreamerRecorderControl::state() const
       
    74 {
       
    75     switch ( m_session->state() ) {
       
    76         case QGstreamerCaptureSession::RecordingState:
       
    77             return QMediaRecorder::RecordingState;
       
    78         case QGstreamerCaptureSession::PausedState:
       
    79             return QMediaRecorder::PausedState;
       
    80         case QGstreamerCaptureSession::PreviewState:
       
    81         case QGstreamerCaptureSession::StoppedState:
       
    82             return QMediaRecorder::StoppedState;
       
    83     }
       
    84 
       
    85     return QMediaRecorder::StoppedState;
       
    86 
       
    87 }
       
    88 
       
    89 void QGstreamerRecorderControl::updateState()
       
    90 {
       
    91     QMediaRecorder::State newState = state();
       
    92     if (m_state != newState) {
       
    93         m_state = newState;
       
    94         emit stateChanged(m_state);
       
    95     }
       
    96 }
       
    97 
       
    98 qint64 QGstreamerRecorderControl::duration() const
       
    99 {
       
   100     return m_session->duration();
       
   101 }
       
   102 
       
   103 void QGstreamerRecorderControl::record()
       
   104 {
       
   105     if (!m_hasPreviewState || m_session->state() != QGstreamerCaptureSession::StoppedState) {
       
   106         m_session->setState(QGstreamerCaptureSession::RecordingState);
       
   107     } else
       
   108         emit error(QMediaRecorder::ResourceError, tr("Service has not been started"));
       
   109 }
       
   110 
       
   111 void QGstreamerRecorderControl::pause()
       
   112 {
       
   113     if (!m_hasPreviewState || m_session->state() != QGstreamerCaptureSession::StoppedState) {
       
   114         m_session->setState(QGstreamerCaptureSession::PausedState);
       
   115     } else
       
   116         emit error(QMediaRecorder::ResourceError, tr("Service has not been started"));
       
   117 }
       
   118 
       
   119 void QGstreamerRecorderControl::stop()
       
   120 {
       
   121     if (!m_hasPreviewState) {
       
   122         m_session->setState(QGstreamerCaptureSession::StoppedState);
       
   123     } else {
       
   124         if (m_session->state() != QGstreamerCaptureSession::StoppedState)
       
   125             m_session->setState(QGstreamerCaptureSession::PreviewState);
       
   126     }
       
   127 }
       
   128 
       
   129 void QGstreamerRecorderControl::applySettings()
       
   130 {
       
   131     //Check the codecs are compatible with container,
       
   132     //and choose the compatible codecs/container if omitted
       
   133     QGstreamerAudioEncode *audioEncodeControl = m_session->audioEncodeControl();
       
   134     QGstreamerVideoEncode *videoEncodeControl = m_session->videoEncodeControl();
       
   135     QGstreamerMediaContainerControl *mediaContainerControl = m_session->mediaContainerControl();
       
   136 
       
   137     bool needAudio = m_session->captureMode() & QGstreamerCaptureSession::Audio;
       
   138     bool needVideo = m_session->captureMode() & QGstreamerCaptureSession::Video;
       
   139 
       
   140     QStringList containerCandidates;
       
   141     if (mediaContainerControl->containerMimeType().isEmpty())
       
   142         containerCandidates = mediaContainerControl->supportedContainers();
       
   143     else
       
   144         containerCandidates << mediaContainerControl->containerMimeType();
       
   145 
       
   146 
       
   147     QStringList audioCandidates;
       
   148     if (needAudio) {
       
   149         QAudioEncoderSettings audioSettings = audioEncodeControl->audioSettings();
       
   150         if (audioSettings.codec().isEmpty())
       
   151             audioCandidates = audioEncodeControl->supportedAudioCodecs();
       
   152         else
       
   153             audioCandidates << audioSettings.codec();
       
   154     }
       
   155 
       
   156     QStringList videoCandidates;
       
   157     if (needVideo) {
       
   158         QVideoEncoderSettings videoSettings = videoEncodeControl->videoSettings();
       
   159         if (videoSettings.codec().isEmpty())
       
   160             videoCandidates = videoEncodeControl->supportedVideoCodecs();
       
   161         else
       
   162             videoCandidates << videoSettings.codec();
       
   163     }
       
   164 
       
   165     QString container;
       
   166     QString audioCodec;
       
   167     QString videoCodec;
       
   168 
       
   169     foreach (const QString &containerCandidate, containerCandidates) {
       
   170         QSet<QString> supportedTypes = mediaContainerControl->supportedStreamTypes(containerCandidate);
       
   171 
       
   172         audioCodec.clear();
       
   173         videoCodec.clear();
       
   174 
       
   175         if (needAudio) {
       
   176             bool found = false;
       
   177             foreach (const QString &audioCandidate, audioCandidates) {
       
   178                 QSet<QString> audioTypes = audioEncodeControl->supportedStreamTypes(audioCandidate);
       
   179                 if (!audioTypes.intersect(supportedTypes).isEmpty()) {
       
   180                     found = true;
       
   181                     audioCodec = audioCandidate;
       
   182                     break;
       
   183                 }
       
   184             }
       
   185             if (!found)
       
   186                 continue;
       
   187         }
       
   188 
       
   189         if (needVideo) {
       
   190             bool found = false;
       
   191             foreach (const QString &videoCandidate, videoCandidates) {
       
   192                 QSet<QString> videoTypes = videoEncodeControl->supportedStreamTypes(videoCandidate);
       
   193                 if (!videoTypes.intersect(supportedTypes).isEmpty()) {
       
   194                     found = true;
       
   195                     videoCodec = videoCandidate;
       
   196                     break;
       
   197                 }
       
   198             }
       
   199             if (!found)
       
   200                 continue;
       
   201         }
       
   202 
       
   203         container = containerCandidate;
       
   204         break;
       
   205     }
       
   206 
       
   207     if (container.isEmpty()) {
       
   208         emit error(QMediaRecorder::FormatError, tr("Not compatible codecs and container format."));
       
   209     } else {
       
   210         mediaContainerControl->setContainerMimeType(container);
       
   211 
       
   212         if (needAudio) {
       
   213             QAudioEncoderSettings audioSettings = audioEncodeControl->audioSettings();
       
   214             audioSettings.setCodec(audioCodec);
       
   215             audioEncodeControl->setAudioSettings(audioSettings);
       
   216         }
       
   217 
       
   218         if (needVideo) {
       
   219             QVideoEncoderSettings videoSettings = videoEncodeControl->videoSettings();
       
   220             videoSettings.setCodec(videoCodec);
       
   221             videoEncodeControl->setVideoSettings(videoSettings);
       
   222         }
       
   223     }
       
   224 }