plugins/multimedia/symbian/mmf/radio/s60radiotunercontrol_31.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 "s60radiotunercontrol_31.h"
       
    43 #include "s60radiotunerservice.h"
       
    44 
       
    45 #include <QtCore/qdebug.h>
       
    46 #include <QFile>
       
    47 
       
    48 // from AudioPreference.h
       
    49 const TInt KAudioPriorityFMRadio = 79;
       
    50 const TUint KAudioPrefRadioAudioEvent = 0x03000001;
       
    51 
       
    52 S60RadioTunerControl::S60RadioTunerControl(QObject *parent)
       
    53     : QRadioTunerControl(parent)
       
    54     , m_error(0)
       
    55     , m_tunerState(0)
       
    56     , m_apiTunerState(QRadioTuner::StoppedState)
       
    57     , m_audioInitializationComplete(false)
       
    58     , m_radioError(QRadioTuner::NoError)
       
    59     , m_muted(false)
       
    60     , m_isStereo(true)
       
    61     , m_stereoMode(QRadioTuner::Auto)
       
    62     , m_signal(0)
       
    63     , m_currentBand(QRadioTuner::FM)
       
    64     , m_currentFreq(87500000)
       
    65     , m_scanning(false)
       
    66     , m_vol(50)
       
    67 {
       
    68     initRadio();   
       
    69 }
       
    70 
       
    71 S60RadioTunerControl::~S60RadioTunerControl()
       
    72 {
       
    73 	if (m_tunerUtility) {
       
    74 	    m_tunerUtility->Close();
       
    75 		m_tunerUtility->CancelNotifyChange();
       
    76 		m_tunerUtility->CancelNotifySignalStrength();
       
    77 		m_tunerUtility->CancelNotifyStereoChange();
       
    78 		delete m_tunerUtility;
       
    79 	}
       
    80 	if (m_audioPlayerUtility) {
       
    81 		m_audioPlayerUtility = NULL;
       
    82 	}
       
    83 }
       
    84 
       
    85 bool S60RadioTunerControl::initRadio()
       
    86 {
       
    87 	m_available = false;
       
    88 
       
    89 	TRAPD(tunerError, m_tunerUtility = CMMTunerUtility::NewL(*this, CMMTunerUtility::ETunerBandFm, 1, 
       
    90 												CMMTunerUtility::ETunerAccessPriorityNormal));
       
    91 	if (tunerError != KErrNone) {
       
    92         m_radioError = QRadioTuner::OpenError;
       
    93         return m_available;
       
    94     }
       
    95 	
       
    96 	TRAPD(playerError, m_audioPlayerUtility = m_tunerUtility->TunerPlayerUtilityL(*this));
       
    97 	if (playerError != KErrNone) {
       
    98 		m_radioError = QRadioTuner::OpenError;
       
    99 		return m_available;
       
   100 	}
       
   101 	
       
   102 	TRAPD(initializeError, m_audioPlayerUtility->InitializeL(KAudioPriorityFMRadio, 
       
   103 												TMdaPriorityPreference(KAudioPrefRadioAudioEvent)));
       
   104 	if (initializeError != KErrNone) {
       
   105 		m_radioError = QRadioTuner::OpenError;
       
   106 		return m_available;
       
   107 	}
       
   108 		
       
   109 	m_tunerUtility->NotifyChange(*this);
       
   110 	m_tunerUtility->NotifyStereoChange(*this);
       
   111 	m_tunerUtility->NotifySignalStrength(*this);
       
   112 	
       
   113 	TFrequency freq(m_currentFreq);
       
   114 	m_tunerUtility->Tune(freq);
       
   115 		
       
   116 	m_available = true;
       
   117 	
       
   118     return m_available;
       
   119 }
       
   120 
       
   121 void S60RadioTunerControl::start()
       
   122 {
       
   123 	if (!m_audioInitializationComplete) {
       
   124 		TFrequency freq(m_currentFreq);
       
   125 		m_tunerUtility->Tune(freq);
       
   126 	} else {
       
   127 		m_audioPlayerUtility->Play();
       
   128 	}
       
   129 
       
   130 	m_apiTunerState = QRadioTuner::ActiveState;
       
   131 	emit stateChanged(m_apiTunerState);
       
   132 }
       
   133 
       
   134 void S60RadioTunerControl::stop()
       
   135 {
       
   136     if (m_audioPlayerUtility) {
       
   137 		m_audioPlayerUtility->Stop();
       
   138 		m_apiTunerState = QRadioTuner::StoppedState;
       
   139 		emit stateChanged(m_apiTunerState);
       
   140     }		
       
   141 }
       
   142 
       
   143 QRadioTuner::State S60RadioTunerControl::state() const
       
   144 {
       
   145     return m_apiTunerState;
       
   146 }
       
   147 
       
   148 QRadioTuner::Band S60RadioTunerControl::band() const
       
   149 {
       
   150     return m_currentBand;
       
   151 }
       
   152 
       
   153 bool S60RadioTunerControl::isBandSupported(QRadioTuner::Band b) const
       
   154 {
       
   155 	if(b == QRadioTuner::FM)
       
   156 		return true;
       
   157 	else if(b == QRadioTuner::LW)
       
   158 		return false;
       
   159 	else if(b == QRadioTuner::AM)
       
   160 		return true;
       
   161 	else if(b == QRadioTuner::SW)
       
   162 		return false;
       
   163 	else
       
   164 		return false;
       
   165 }
       
   166 
       
   167 void S60RadioTunerControl::setBand(QRadioTuner::Band b)
       
   168 {
       
   169     QRadioTuner::Band tempBand = b; 
       
   170     if (tempBand != m_currentBand) {
       
   171         m_currentBand = b;        
       
   172         emit bandChanged(m_currentBand);
       
   173     }
       
   174 }
       
   175 
       
   176 int S60RadioTunerControl::frequency() const
       
   177 {
       
   178     return m_currentFreq;
       
   179 }
       
   180 
       
   181 void S60RadioTunerControl::setFrequency(int frequency)
       
   182 {
       
   183     m_currentFreq = frequency;
       
   184     TFrequency freq(m_currentFreq);
       
   185     m_tunerUtility->Tune(freq);
       
   186 }
       
   187 
       
   188 int S60RadioTunerControl::frequencyStep(QRadioTuner::Band b) const
       
   189 {
       
   190     int step = 0;
       
   191 
       
   192     if(b == QRadioTuner::FM)
       
   193         step = 100000; // 100kHz steps
       
   194     else if(b == QRadioTuner::LW)
       
   195         step = 1000; // 1kHz steps
       
   196     else if(b == QRadioTuner::AM)
       
   197         step = 1000; // 1kHz steps
       
   198     else if(b == QRadioTuner::SW)
       
   199         step = 500; // 500Hz steps
       
   200 
       
   201     return step;
       
   202 }
       
   203 
       
   204 QPair<int,int> S60RadioTunerControl::frequencyRange(QRadioTuner::Band band) const
       
   205 {
       
   206 	TFrequency bottomFreq;
       
   207 	TFrequency topFreq;
       
   208 	int bandError = KErrNone;
       
   209    
       
   210 	if (m_tunerUtility){
       
   211 		bandError = m_tunerUtility->GetFrequencyBandRange(bottomFreq, topFreq);	   
       
   212 		if (!bandError) {
       
   213 			return qMakePair<int,int>(bottomFreq.iFrequency, topFreq.iFrequency);
       
   214 		}
       
   215 	}  
       
   216    return qMakePair<int,int>(0,0);
       
   217 }
       
   218 
       
   219 CMMTunerUtility::TTunerBand S60RadioTunerControl::getNativeBand(QRadioTuner::Band b) const
       
   220 {
       
   221     // api match to native s60 bands    
       
   222     if (b == QRadioTuner::AM)
       
   223         return CMMTunerUtility::ETunerBandAm;
       
   224     else if (b == QRadioTuner::FM)
       
   225         return CMMTunerUtility::ETunerBandFm;
       
   226     else if (b == QRadioTuner::LW)
       
   227         return CMMTunerUtility::ETunerBandLw;
       
   228     else
       
   229         return CMMTunerUtility::ETunerNoBand;
       
   230 }
       
   231 
       
   232 bool S60RadioTunerControl::isStereo() const
       
   233 {
       
   234     return m_isStereo;
       
   235 }
       
   236 
       
   237 QRadioTuner::StereoMode S60RadioTunerControl::stereoMode() const
       
   238 {
       
   239     return m_stereoMode;
       
   240 }
       
   241 
       
   242 void S60RadioTunerControl::setStereoMode(QRadioTuner::StereoMode mode)
       
   243 {
       
   244 	m_stereoMode = mode;
       
   245 	if (m_tunerUtility) {	    
       
   246 	    if (QRadioTuner::ForceMono == mode)
       
   247 	        m_tunerUtility->ForceMonoReception(true);
       
   248 	    else 
       
   249 	        m_tunerUtility->ForceMonoReception(false);
       
   250 	}	
       
   251 }
       
   252 
       
   253 int S60RadioTunerControl::signalStrength() const
       
   254 {
       
   255     // return value is a percentage value
       
   256     if (m_tunerUtility) {       
       
   257         TInt maxSignalStrength;
       
   258         TInt currentSignalStrength;
       
   259         m_error = m_tunerUtility->GetMaxSignalStrength(maxSignalStrength);       
       
   260         if (m_error == KErrNone) {
       
   261             m_error = m_tunerUtility->GetSignalStrength(currentSignalStrength);
       
   262             if (m_error == KErrNone) {
       
   263 				if (maxSignalStrength == 0 || currentSignalStrength == 0) {
       
   264 					return 0;
       
   265 				}
       
   266 				m_signal = (currentSignalStrength/maxSignalStrength)*100; 
       
   267             }           
       
   268         }
       
   269     }
       
   270     return m_signal;
       
   271 }
       
   272 
       
   273 int S60RadioTunerControl::volume() const
       
   274 {
       
   275     return m_vol;
       
   276 }
       
   277 
       
   278 void S60RadioTunerControl::setVolume(int volume)
       
   279 {
       
   280     if (m_audioPlayerUtility) {
       
   281 		m_vol = volume;
       
   282 		TInt error = m_audioPlayerUtility->SetVolume(volume/10);
       
   283 		emit volumeChanged(m_vol);
       
   284     }
       
   285 }
       
   286 
       
   287 bool S60RadioTunerControl::isMuted() const
       
   288 {
       
   289     return m_muted;
       
   290 }
       
   291 
       
   292 void S60RadioTunerControl::setMuted(bool muted)
       
   293 {
       
   294     if (m_audioPlayerUtility && m_audioInitializationComplete) {
       
   295         m_muted = muted;
       
   296         m_audioPlayerUtility->Mute(m_muted);
       
   297         emit mutedChanged(m_muted);           
       
   298     }
       
   299 }
       
   300 
       
   301 bool S60RadioTunerControl::isSearching() const
       
   302 {
       
   303     if (m_tunerUtility) {
       
   304     	TUint32 tempState;
       
   305     	m_tunerUtility->GetState(tempState);
       
   306     	if (tempState == CMMTunerUtility::ETunerStateRetuning || m_scanning) {
       
   307 			return true;
       
   308     	} else
       
   309     		return false;
       
   310     }
       
   311     return true;
       
   312 }
       
   313 
       
   314 void S60RadioTunerControl::cancelSearch()
       
   315 {
       
   316 	m_tunerUtility->CancelRetune();
       
   317 	m_scanning = false;
       
   318 	emit searchingChanged(false);
       
   319 }
       
   320 
       
   321 void S60RadioTunerControl::searchForward()
       
   322 {
       
   323 	m_scanning = true;
       
   324 	setVolume(m_vol);
       
   325 	m_tunerUtility->StationSeek(CMMTunerUtility::ESearchDirectionUp);
       
   326 	emit searchingChanged(true);
       
   327 }
       
   328 
       
   329 void S60RadioTunerControl::searchBackward()
       
   330 {
       
   331 	m_scanning = true;
       
   332 	setVolume(m_vol);
       
   333 	m_tunerUtility->StationSeek(CMMTunerUtility::ESearchDirectionDown);
       
   334 	emit searchingChanged(true);
       
   335 }
       
   336 
       
   337 bool S60RadioTunerControl::isValid() const
       
   338 {
       
   339     return m_available;
       
   340 }
       
   341 
       
   342 bool S60RadioTunerControl::isAvailable() const
       
   343 {
       
   344     return m_available;
       
   345 }
       
   346 
       
   347 QtMultimediaKit::AvailabilityError S60RadioTunerControl::availabilityError() const
       
   348 {
       
   349     if (m_available)
       
   350         return QtMultimediaKit::NoError;
       
   351     else
       
   352         return QtMultimediaKit::ResourceError;
       
   353 }
       
   354 
       
   355 QRadioTuner::Error S60RadioTunerControl::error() const
       
   356 {
       
   357     return m_radioError;
       
   358 }
       
   359 
       
   360 QString S60RadioTunerControl::errorString() const
       
   361 {
       
   362 	return m_errorString;
       
   363 }
       
   364 
       
   365 void S60RadioTunerControl::MToTuneComplete(TInt aError)
       
   366 {
       
   367 	if (aError == KErrNone) {
       
   368 		m_scanning = false;
       
   369 		m_audioPlayerUtility->Play();
       
   370 		if (!m_audioInitializationComplete) {
       
   371 		TRAPD(initializeError, m_audioPlayerUtility->InitializeL(KAudioPriorityFMRadio, 
       
   372                                                         TMdaPriorityPreference(KAudioPrefRadioAudioEvent)));
       
   373 			if (initializeError != KErrNone) {
       
   374 				m_radioError = QRadioTuner::OpenError;
       
   375 			}
       
   376 		}
       
   377 	}
       
   378 }
       
   379 
       
   380 void S60RadioTunerControl::MTcoFrequencyChanged(const TFrequency& aOldFrequency, const TFrequency& aNewFrequency)
       
   381 {
       
   382 	m_currentFreq = aNewFrequency.iFrequency;
       
   383 	m_scanning = false;
       
   384 	emit frequencyChanged(m_currentFreq);
       
   385 }
       
   386 
       
   387 void S60RadioTunerControl::MTcoStateChanged(const TUint32& aOldState, const TUint32& aNewState)
       
   388 {
       
   389 	if (aNewState == CMMTunerUtility::ETunerStateActive) {
       
   390 		m_apiTunerState = QRadioTuner::ActiveState;
       
   391 	}
       
   392 	if (aNewState == CMMTunerUtility::ETunerStatePlaying) {
       
   393 		m_apiTunerState = QRadioTuner::ActiveState;
       
   394 	}	
       
   395 	if (aOldState != aNewState){
       
   396 		emit stateChanged(m_apiTunerState);
       
   397 	}
       
   398 }
       
   399 
       
   400 void S60RadioTunerControl::MTcoAntennaDetached()
       
   401 {
       
   402 	// no actions
       
   403 }
       
   404 
       
   405 void S60RadioTunerControl::MTcoAntennaAttached()
       
   406 {
       
   407 	// no actions
       
   408 }
       
   409 
       
   410 void S60RadioTunerControl::FlightModeChanged(TBool aFlightMode)
       
   411 {
       
   412 	// no actions
       
   413 }
       
   414 
       
   415 void S60RadioTunerControl::MTsoStereoReceptionChanged(TBool aStereo)
       
   416 {
       
   417 	m_isStereo = aStereo;
       
   418 	emit stereoStatusChanged(aStereo);
       
   419 }
       
   420 
       
   421 void S60RadioTunerControl::MTsoForcedMonoChanged(TBool aForcedMono)
       
   422 {
       
   423 	if (aForcedMono) {
       
   424 		m_stereoMode = QRadioTuner::ForceMono;
       
   425 	}
       
   426 }
       
   427 
       
   428 void S60RadioTunerControl::MssoSignalStrengthChanged(TInt aNewSignalStrength)
       
   429 {
       
   430 	m_signal = aNewSignalStrength;
       
   431 	emit signalStrengthChanged(m_signal);
       
   432 }
       
   433 
       
   434 void S60RadioTunerControl::MTapoInitializeComplete(TInt aError)
       
   435 {
       
   436 	if (aError == KErrNone) {
       
   437 		m_audioInitializationComplete = true;
       
   438 		m_available = true;
       
   439 		m_audioPlayerUtility->Play();
       
   440 		m_apiTunerState = QRadioTuner::ActiveState;
       
   441 		emit stateChanged(m_apiTunerState);
       
   442 	} else if (aError != KErrNone) {
       
   443 		m_radioError = QRadioTuner::OpenError;
       
   444 	}
       
   445 }
       
   446 
       
   447 void S60RadioTunerControl::MTapoPlayEvent(TEventType aEvent, TInt aError, TAny* aAdditionalInfo)
       
   448 {
       
   449 	// no actions
       
   450 }
       
   451 
       
   452 
       
   453