qtmobility/plugins/multimedia/directshow/player/directshowplayercontrol.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 "directshowplayercontrol.h"
       
    43 
       
    44 #include "directshowplayerservice.h"
       
    45 
       
    46 #include <QtCore/qcoreapplication.h>
       
    47 #include <QtCore/qmath.h>
       
    48 
       
    49 static int volumeToDecibels(int volume)
       
    50 {
       
    51     if (volume == 0) {
       
    52         return -10000;
       
    53     } else if (volume == 100) {
       
    54         return 0;
       
    55 #ifdef QT_USE_MATH_H_FLOATS
       
    56     } else if (sizeof(qreal) == sizeof(float)) {
       
    57         return qRound(::log10f(float(volume) / 100) * 5000);
       
    58 #endif
       
    59     } else {
       
    60         return qRound(::log10(qreal(volume) / 100) * 5000);
       
    61     }
       
    62 }
       
    63 
       
    64 static int decibelsToVolume(int dB)
       
    65 {
       
    66     if (dB == -10000) {
       
    67         return 0;
       
    68     } else if (dB == 0) {
       
    69         return 100;
       
    70     } else {
       
    71         return qRound(100 * qPow(10, qreal(dB) / 5000));
       
    72     }
       
    73 }
       
    74 
       
    75 DirectShowPlayerControl::DirectShowPlayerControl(DirectShowPlayerService *service, QObject *parent)
       
    76     : QMediaPlayerControl(parent)
       
    77     , m_service(service)
       
    78     , m_audio(0)
       
    79     , m_updateProperties(0)
       
    80     , m_state(QMediaPlayer::StoppedState)
       
    81     , m_status(QMediaPlayer::UnknownMediaStatus)
       
    82     , m_streamTypes(0)
       
    83     , m_muteVolume(-1)
       
    84     , m_duration(0)
       
    85     , m_playbackRate(0)
       
    86     , m_seekable(false)
       
    87 {
       
    88 }
       
    89 
       
    90 DirectShowPlayerControl::~DirectShowPlayerControl()
       
    91 {
       
    92     if (m_audio)
       
    93         m_audio->Release();
       
    94 }
       
    95 
       
    96 QMediaPlayer::State DirectShowPlayerControl::state() const
       
    97 {
       
    98     return m_state;
       
    99 }
       
   100 
       
   101 QMediaPlayer::MediaStatus DirectShowPlayerControl::mediaStatus() const
       
   102 {
       
   103     return m_status;
       
   104 }
       
   105 
       
   106 qint64 DirectShowPlayerControl::duration() const
       
   107 {
       
   108     return m_duration;
       
   109 }
       
   110 
       
   111 qint64 DirectShowPlayerControl::position() const
       
   112 {
       
   113     return m_service->position();
       
   114 }
       
   115 
       
   116 void DirectShowPlayerControl::setPosition(qint64 position)
       
   117 {
       
   118     m_service->seek(position);
       
   119 }
       
   120 
       
   121 int DirectShowPlayerControl::volume() const
       
   122 {
       
   123     if (m_muteVolume >= 0) {
       
   124         return m_muteVolume;
       
   125     } else if (m_audio) {
       
   126         long dB = 0;
       
   127 
       
   128         m_audio->get_Volume(&dB);
       
   129 
       
   130         return decibelsToVolume(dB);
       
   131     } else {
       
   132         return 0;
       
   133     }
       
   134 }
       
   135 
       
   136 void DirectShowPlayerControl::setVolume(int volume)
       
   137 {
       
   138     int boundedVolume = qBound(0, volume, 100);
       
   139 
       
   140     if (m_muteVolume >= 0) {
       
   141         m_muteVolume = boundedVolume;
       
   142 
       
   143         emit volumeChanged(m_muteVolume);
       
   144     } else if (m_audio) {
       
   145         m_audio->put_Volume(volumeToDecibels(volume));
       
   146 
       
   147         emit volumeChanged(boundedVolume);
       
   148     }
       
   149 }
       
   150 
       
   151 bool DirectShowPlayerControl::isMuted() const
       
   152 {
       
   153     return m_muteVolume >= 0;
       
   154 }
       
   155 
       
   156 void DirectShowPlayerControl::setMuted(bool muted)
       
   157 {
       
   158     if (muted && m_muteVolume < 0) {
       
   159         if (m_audio) {
       
   160             long dB = 0;
       
   161 
       
   162             m_audio->get_Volume(&dB);
       
   163 
       
   164             m_muteVolume = decibelsToVolume(dB);
       
   165 
       
   166             m_audio->put_Volume(-10000);
       
   167         } else {
       
   168             m_muteVolume = 0;
       
   169         }
       
   170 
       
   171         emit mutedChanged(muted);
       
   172     } else if (!muted && m_muteVolume >= 0) {
       
   173         if (m_audio) {
       
   174             m_audio->put_Volume(volumeToDecibels(m_muteVolume));
       
   175         }
       
   176         m_muteVolume = -1;
       
   177 
       
   178         emit mutedChanged(muted);
       
   179     }
       
   180 }
       
   181 
       
   182 int DirectShowPlayerControl::bufferStatus() const
       
   183 {
       
   184     return m_service->bufferStatus();
       
   185 }
       
   186 
       
   187 bool DirectShowPlayerControl::isAudioAvailable() const
       
   188 {
       
   189     return m_streamTypes & DirectShowPlayerService::AudioStream;
       
   190 }
       
   191 
       
   192 bool DirectShowPlayerControl::isVideoAvailable() const
       
   193 {
       
   194     return m_streamTypes & DirectShowPlayerService::VideoStream;
       
   195 }
       
   196 
       
   197 bool DirectShowPlayerControl::isSeekable() const
       
   198 {
       
   199     return m_seekable;
       
   200 }
       
   201 
       
   202 QMediaTimeRange DirectShowPlayerControl::availablePlaybackRanges() const
       
   203 {
       
   204     return m_service->availablePlaybackRanges();
       
   205 }
       
   206 
       
   207 qreal DirectShowPlayerControl::playbackRate() const
       
   208 {
       
   209     return m_playbackRate;
       
   210 }
       
   211 
       
   212 void DirectShowPlayerControl::setPlaybackRate(qreal rate)
       
   213 {
       
   214     if (m_playbackRate != rate) {
       
   215         m_service->setRate(rate);
       
   216 
       
   217         emit playbackRateChanged(m_playbackRate = rate);
       
   218     }
       
   219 }
       
   220 
       
   221 QMediaContent DirectShowPlayerControl::media() const
       
   222 {
       
   223     return m_media;
       
   224 }
       
   225 
       
   226 const QIODevice *DirectShowPlayerControl::mediaStream() const
       
   227 {
       
   228     return m_stream;
       
   229 }
       
   230 
       
   231 void DirectShowPlayerControl::setMedia(const QMediaContent &media, QIODevice *stream)
       
   232 {
       
   233     m_media = media;
       
   234     m_stream = stream;
       
   235 
       
   236     m_updateProperties &= PlaybackRateProperty;
       
   237 
       
   238     m_service->load(media, stream);
       
   239 
       
   240     emit audioAvailableChanged(m_streamTypes & DirectShowPlayerService::AudioStream);
       
   241     emit videoAvailableChanged(m_streamTypes & DirectShowPlayerService::VideoStream);
       
   242     emit durationChanged(m_duration);
       
   243     emit seekableChanged(m_seekable);
       
   244     emit mediaStatusChanged(m_status);
       
   245     emit stateChanged(m_state);
       
   246 }
       
   247 
       
   248 void DirectShowPlayerControl::play()
       
   249 {
       
   250     m_service->play();
       
   251     emit stateChanged(m_state = QMediaPlayer::PlayingState);
       
   252 }
       
   253 
       
   254 void DirectShowPlayerControl::pause()
       
   255 {
       
   256     m_service->pause();
       
   257     emit stateChanged(m_state = QMediaPlayer::PausedState);
       
   258 }
       
   259 
       
   260 void DirectShowPlayerControl::stop()
       
   261 {
       
   262     m_service->stop();
       
   263     emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   264 }
       
   265 
       
   266 void DirectShowPlayerControl::customEvent(QEvent *event)
       
   267 {
       
   268     if (event->type() == QEvent::Type(PropertiesChanged)) {
       
   269         int properties = m_updateProperties;
       
   270         m_updateProperties = 0;
       
   271 
       
   272         if (properties & PlaybackRateProperty)
       
   273             emit playbackRateChanged(m_playbackRate);
       
   274 
       
   275         if (properties & StreamTypesProperty) {
       
   276             emit audioAvailableChanged(m_streamTypes & DirectShowPlayerService::AudioStream);
       
   277             emit videoAvailableChanged(m_streamTypes & DirectShowPlayerService::VideoStream);
       
   278         }
       
   279 
       
   280         if (properties & DurationProperty)
       
   281             emit durationChanged(m_duration);
       
   282 
       
   283         if (properties & SeekableProperty)
       
   284             emit seekableChanged(m_seekable);
       
   285 
       
   286         if (properties & StatusProperty)
       
   287             emit mediaStatusChanged(m_status);
       
   288 
       
   289         if (properties & StateProperty)
       
   290             emit stateChanged(m_state);
       
   291 
       
   292         event->accept();
       
   293     } else {
       
   294         QMediaPlayerControl::customEvent(event);
       
   295     }
       
   296 }
       
   297 
       
   298 void DirectShowPlayerControl::scheduleUpdate(int properties)
       
   299 {
       
   300     if (m_updateProperties == 0)
       
   301         QCoreApplication::postEvent(this, new QEvent(QEvent::Type(PropertiesChanged)));
       
   302 
       
   303     m_updateProperties |= properties;
       
   304 }
       
   305 
       
   306 void DirectShowPlayerControl::updateState(QMediaPlayer::State state)
       
   307 {
       
   308     if (m_state != state) {
       
   309         m_state = state;
       
   310 
       
   311         scheduleUpdate(StateProperty);
       
   312     }
       
   313 }
       
   314 
       
   315 void DirectShowPlayerControl::updateStatus(QMediaPlayer::MediaStatus status)
       
   316 {
       
   317     if (m_status != status) {
       
   318         m_status = status;
       
   319 
       
   320         scheduleUpdate(StatusProperty);
       
   321     }
       
   322 }
       
   323 
       
   324 void DirectShowPlayerControl::updateMediaInfo(qint64 duration, int streamTypes, bool seekable)
       
   325 {
       
   326     int properties = 0;
       
   327 
       
   328     if (m_duration != duration) {
       
   329         m_duration = duration;
       
   330 
       
   331         properties |= DurationProperty;
       
   332     }
       
   333     if (m_streamTypes != streamTypes) {
       
   334         m_streamTypes = streamTypes;
       
   335 
       
   336         properties |= StreamTypesProperty;
       
   337     }
       
   338 
       
   339     if (m_seekable != seekable) {
       
   340         m_seekable = seekable;
       
   341 
       
   342         properties |= SeekableProperty;
       
   343     }
       
   344 
       
   345     if (properties != 0)
       
   346         scheduleUpdate(properties);
       
   347 }
       
   348 
       
   349 void DirectShowPlayerControl::updatePlaybackRate(qreal rate)
       
   350 {
       
   351     if (m_playbackRate != rate) {
       
   352         m_playbackRate = rate;
       
   353 
       
   354         scheduleUpdate(PlaybackRateProperty);
       
   355     }
       
   356 }
       
   357 
       
   358 void DirectShowPlayerControl::updateAudioOutput(IBaseFilter *filter)
       
   359 {
       
   360     if (m_audio)
       
   361         m_audio->Release();
       
   362 
       
   363     m_audio = com_cast<IBasicAudio>(filter);
       
   364 }