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