| 1 | /*  This file is part of the KDE project.
 | 
|---|
| 2 | 
 | 
|---|
| 3 | Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|---|
| 4 | 
 | 
|---|
| 5 | This library is free software: you can redistribute it and/or modify
 | 
|---|
| 6 | it under the terms of the GNU Lesser General Public License as published by
 | 
|---|
| 7 | the Free Software Foundation, either version 2.1 or 3 of the License.
 | 
|---|
| 8 | 
 | 
|---|
| 9 | This library is distributed in the hope that it will be useful,
 | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 12 | GNU Lesser General Public License for more details.
 | 
|---|
| 13 | 
 | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License
 | 
|---|
| 15 | along with this library.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 16 | 
 | 
|---|
| 17 | */
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "mediaobject.h"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "abstractaudioeffect.h"
 | 
|---|
| 22 | #include "audioplayer.h"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | QT_BEGIN_NAMESPACE
 | 
|---|
| 25 | 
 | 
|---|
| 26 | using namespace Phonon;
 | 
|---|
| 27 | using namespace Phonon::MMF;
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /*! \class MMF::AbstractAudioEffect
 | 
|---|
| 30 |   \internal
 | 
|---|
| 31 | */
 | 
|---|
| 32 | 
 | 
|---|
| 33 | /*! \namespace Phonon::MMF
 | 
|---|
| 34 |   \internal
 | 
|---|
| 35 | */
 | 
|---|
| 36 | 
 | 
|---|
| 37 | AbstractAudioEffect::AbstractAudioEffect(QObject *parent,
 | 
|---|
| 38 |                                          const QList<EffectParameter> ¶ms)
 | 
|---|
| 39 |     :   MediaNode(parent)
 | 
|---|
| 40 |     ,   m_params(params)
 | 
|---|
| 41 |     ,   m_player(0)
 | 
|---|
| 42 | {
 | 
|---|
| 43 | 
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | QList<Phonon::EffectParameter> AbstractAudioEffect::parameters() const
 | 
|---|
| 47 | {
 | 
|---|
| 48 |     // Convert from QList<MMF::EffectParameter> to QList<Phonon::EffectParameter>
 | 
|---|
| 49 |     QList<Phonon::EffectParameter> result;
 | 
|---|
| 50 |     EffectParameter param;
 | 
|---|
| 51 |     foreach (param, m_params)
 | 
|---|
| 52 |         result += param;
 | 
|---|
| 53 |     return result;
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | QVariant AbstractAudioEffect::parameterValue(const Phonon::EffectParameter &queriedParam) const
 | 
|---|
| 57 | {
 | 
|---|
| 58 |     const QVariant &val = m_values.value(queriedParam.id());
 | 
|---|
| 59 | 
 | 
|---|
| 60 |     if (val.isNull())
 | 
|---|
| 61 |         return queriedParam.defaultValue();
 | 
|---|
| 62 |     else
 | 
|---|
| 63 |         return val;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | void AbstractAudioEffect::setParameterValue(const Phonon::EffectParameter ¶m,
 | 
|---|
| 67 |                                             const QVariant &newValue)
 | 
|---|
| 68 | {
 | 
|---|
| 69 |     m_values.insert(param.id(), newValue);
 | 
|---|
| 70 | 
 | 
|---|
| 71 |     if (m_effect.data()) {
 | 
|---|
| 72 |         const EffectParameter& internalParam = internalParameter(param.id());
 | 
|---|
| 73 |         int err = parameterChanged(internalParam, newValue);
 | 
|---|
| 74 |         // TODO: handle audio effect errors
 | 
|---|
| 75 |         Q_UNUSED(err);
 | 
|---|
| 76 |     }
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | void AbstractAudioEffect::abstractPlayerChanged(AbstractPlayer *player)
 | 
|---|
| 80 | {
 | 
|---|
| 81 |     m_player = qobject_cast<AbstractMediaPlayer *>(player);
 | 
|---|
| 82 |     m_effect.reset();
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | void AbstractAudioEffect::stateChanged(Phonon::State newState,
 | 
|---|
| 86 |                                        Phonon::State oldState)
 | 
|---|
| 87 | {
 | 
|---|
| 88 |     if (Phonon::LoadingState == oldState
 | 
|---|
| 89 |         && Phonon::LoadingState != newState)
 | 
|---|
| 90 |         createEffect();
 | 
|---|
| 91 | }
 | 
|---|
| 92 | 
 | 
|---|
| 93 | void AbstractAudioEffect::connectMediaObject(MediaObject *mediaObject)
 | 
|---|
| 94 | {
 | 
|---|
| 95 |     Q_ASSERT_X(!m_player, Q_FUNC_INFO, "Player already connected");
 | 
|---|
| 96 |     Q_ASSERT_X(!m_effect.data(), Q_FUNC_INFO, "Effect already created");
 | 
|---|
| 97 | 
 | 
|---|
| 98 |     abstractPlayerChanged(mediaObject->abstractPlayer());
 | 
|---|
| 99 | 
 | 
|---|
| 100 |     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
 | 
|---|
| 101 |             SLOT(stateChanged(Phonon::State, Phonon::State)));
 | 
|---|
| 102 | 
 | 
|---|
| 103 |     connect(mediaObject, SIGNAL(abstractPlayerChanged(AbstractPlayer *)),
 | 
|---|
| 104 |             SLOT(abstractPlayerChanged(AbstractPlayer *)));
 | 
|---|
| 105 | 
 | 
|---|
| 106 |     if (mediaObject->state() != Phonon::LoadingState)
 | 
|---|
| 107 |         createEffect();
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | void AbstractAudioEffect::disconnectMediaObject(MediaObject *mediaObject)
 | 
|---|
| 111 | {
 | 
|---|
| 112 |     mediaObject->disconnect(this);
 | 
|---|
| 113 |     abstractPlayerChanged(0);
 | 
|---|
| 114 | }
 | 
|---|
| 115 | 
 | 
|---|
| 116 | void AbstractAudioEffect::setEnabled(bool enabled)
 | 
|---|
| 117 | {
 | 
|---|
| 118 |     TInt err = KErrNone;
 | 
|---|
| 119 | 
 | 
|---|
| 120 |     if (enabled)
 | 
|---|
| 121 |         // TODO: handle audio effect errors
 | 
|---|
| 122 |         TRAP(err, m_effect->EnableL())
 | 
|---|
| 123 |     else
 | 
|---|
| 124 |         // TODO: handle audio effect errors
 | 
|---|
| 125 |         TRAP(err, m_effect->DisableL())
 | 
|---|
| 126 | 
 | 
|---|
| 127 |     Q_UNUSED(err);
 | 
|---|
| 128 | }
 | 
|---|
| 129 | 
 | 
|---|
| 130 | void AbstractAudioEffect::createEffect()
 | 
|---|
| 131 | {
 | 
|---|
| 132 |     Q_ASSERT_X(m_player, Q_FUNC_INFO, "Invalid media player pointer");
 | 
|---|
| 133 | 
 | 
|---|
| 134 |     if (AudioPlayer *audioPlayer = qobject_cast<AudioPlayer *>(m_player)) {
 | 
|---|
| 135 |         createEffect(audioPlayer->nativePlayer());
 | 
|---|
| 136 |     }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |     if (m_effect.data()) {
 | 
|---|
| 139 |         EffectParameter param;
 | 
|---|
| 140 |         int err = 0;
 | 
|---|
| 141 |         foreach (param, m_params) {
 | 
|---|
| 142 |             const QVariant value = parameterValue(param);
 | 
|---|
| 143 |             err = parameterChanged(param, value);
 | 
|---|
| 144 |         }
 | 
|---|
| 145 |         Q_UNUSED(err)
 | 
|---|
| 146 |     }
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | const MMF::EffectParameter& AbstractAudioEffect::internalParameter(int id) const
 | 
|---|
| 150 | {
 | 
|---|
| 151 |     const EffectParameter *result = 0;
 | 
|---|
| 152 |     for (int i=0; i<m_params.count() && !result; ++i) {
 | 
|---|
| 153 |         if (m_params[i].id() == id)
 | 
|---|
| 154 |             result = &m_params[i];
 | 
|---|
| 155 |     }
 | 
|---|
| 156 |     Q_ASSERT_X(result, Q_FUNC_INFO, "Parameter not found");
 | 
|---|
| 157 |     return *result;
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | int AbstractAudioEffect::parameterChanged(const EffectParameter ¶m,
 | 
|---|
| 161 |             const QVariant &value)
 | 
|---|
| 162 | {
 | 
|---|
| 163 |     int err = 0;
 | 
|---|
| 164 | 
 | 
|---|
| 165 |     switch (param.id()) {
 | 
|---|
| 166 |     case ParameterEnable:
 | 
|---|
| 167 |         setEnabled(value.toBool());
 | 
|---|
| 168 |         break;
 | 
|---|
| 169 |     default:
 | 
|---|
| 170 |         {
 | 
|---|
| 171 |         const EffectParameter& internalParam = internalParameter(param.id());
 | 
|---|
| 172 |         err = effectParameterChanged(internalParam, value);
 | 
|---|
| 173 |         }
 | 
|---|
| 174 |         break;
 | 
|---|
| 175 |     }
 | 
|---|
| 176 | 
 | 
|---|
| 177 |     if (!err)
 | 
|---|
| 178 |         TRAP(err, m_effect->ApplyL());
 | 
|---|
| 179 | 
 | 
|---|
| 180 |     return err;
 | 
|---|
| 181 | }
 | 
|---|
| 182 | 
 | 
|---|
| 183 | int AbstractAudioEffect::effectParameterChanged(
 | 
|---|
| 184 |     const EffectParameter ¶m, const QVariant &value)
 | 
|---|
| 185 | {
 | 
|---|
| 186 |     // Default implementation
 | 
|---|
| 187 |     Q_UNUSED(param)
 | 
|---|
| 188 |     Q_UNUSED(value)
 | 
|---|
| 189 |     Q_ASSERT_X(false, Q_FUNC_INFO, "Effect has no parameters");
 | 
|---|
| 190 |     return 0;
 | 
|---|
| 191 | }
 | 
|---|
| 192 | 
 | 
|---|
| 193 | 
 | 
|---|
| 194 | QT_END_NAMESPACE
 | 
|---|
| 195 | 
 | 
|---|