qtmobility/plugins/multimedia/symbian/mmf/radio/s60radiotunercontrol_since32.cpp
changeset 4 90517678cc4f
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_since32.h"
       
    43 #include "s60radiotunerservice.h"
       
    44 
       
    45 #include <QtCore/qdebug.h>
       
    46 #include <radiofmtunerutility.h>
       
    47 
       
    48 S60RadioTunerControl::S60RadioTunerControl(QObject *parent)
       
    49     : QRadioTunerControl(parent)
       
    50     , m_error(0)
       
    51     , m_radioUtility(NULL)   
       
    52     , m_fmTunerUtility(NULL)
       
    53     , m_playerUtility(NULL)
       
    54     , m_audioInitializationComplete(false)
       
    55     , m_muted(false)
       
    56     , m_isStereo(true)
       
    57     , m_vol(100)
       
    58     , m_signal(0)
       
    59     , m_radioError(QRadioTuner::NoError)
       
    60     , m_scanning(false)
       
    61     , m_currentBand(QRadioTuner::FM)
       
    62     , m_currentFreq(87500000)
       
    63     , m_stereoMode(QRadioTuner::Auto)
       
    64     , m_apiTunerState(QRadioTuner::StoppedState)
       
    65     , m_maxVolume(100)
       
    66 {   
       
    67     initRadio();
       
    68 }
       
    69 
       
    70 S60RadioTunerControl::~S60RadioTunerControl()
       
    71 {
       
    72 	if (m_fmTunerUtility) {
       
    73 		m_fmTunerUtility->Close();
       
    74 	}
       
    75 	
       
    76 	if(m_playerUtility) {
       
    77 		m_playerUtility->Close();
       
    78 	}
       
    79 	
       
    80 	delete m_radioUtility;
       
    81 }
       
    82 
       
    83 QRadioTuner::State S60RadioTunerControl::state() const
       
    84 {
       
    85     return m_apiTunerState;
       
    86 }
       
    87 
       
    88 QRadioTuner::Band S60RadioTunerControl::band() const
       
    89 {
       
    90     return m_currentBand;
       
    91 }
       
    92 
       
    93 bool S60RadioTunerControl::isBandSupported(QRadioTuner::Band b) const
       
    94 {
       
    95 	if(b == QRadioTuner::FM)
       
    96 		return true;
       
    97 	else if(b == QRadioTuner::LW)
       
    98 		return false;
       
    99 	else if(b == QRadioTuner::AM)
       
   100 		return true;
       
   101 	else if(b == QRadioTuner::SW)
       
   102 		return false;
       
   103 	else
       
   104 		return false;
       
   105 }
       
   106 
       
   107 void S60RadioTunerControl::setBand(QRadioTuner::Band b)
       
   108 {
       
   109     QRadioTuner::Band tempBand = b; 
       
   110     if (tempBand != m_currentBand) {
       
   111         m_currentBand = b;  
       
   112         emit bandChanged(m_currentBand);
       
   113     }   
       
   114 }
       
   115 
       
   116 int S60RadioTunerControl::frequency() const
       
   117 {
       
   118     return m_currentFreq;
       
   119 }
       
   120 
       
   121 void S60RadioTunerControl::setFrequency(int frequency)
       
   122 {
       
   123 	m_currentFreq = frequency;	
       
   124 	m_fmTunerUtility->SetFrequency(m_currentFreq);
       
   125 }
       
   126 int S60RadioTunerControl::frequencyStep(QRadioTuner::Band b) const
       
   127 {
       
   128     int step = 0;
       
   129     if(b == QRadioTuner::FM)
       
   130         step = 100000; // 100kHz steps
       
   131     else if(b == QRadioTuner::LW)
       
   132         step = 1000; // 1kHz steps
       
   133     else if(b == QRadioTuner::AM)
       
   134         step = 1000; // 1kHz steps
       
   135     else if(b == QRadioTuner::SW)
       
   136         step = 500; // 500Hz steps
       
   137 
       
   138     return step;
       
   139 }
       
   140 
       
   141 QPair<int,int> S60RadioTunerControl::frequencyRange(QRadioTuner::Band band) const
       
   142 {
       
   143     int bottomFreq;
       
   144     int topFreq;
       
   145     
       
   146     int bandError = KErrNone;
       
   147     TFmRadioFrequencyRange range;
       
   148     
       
   149     if (m_fmTunerUtility) {
       
   150         bandError = m_fmTunerUtility->GetFrequencyRange(range, bottomFreq, topFreq);
       
   151     }    
       
   152     if (!bandError) {
       
   153         return qMakePair<int,int>(bottomFreq, topFreq);
       
   154     }
       
   155     
       
   156     return qMakePair<int,int>(0,0);
       
   157 }
       
   158 
       
   159 bool S60RadioTunerControl::isStereo() const
       
   160 {
       
   161     return m_isStereo;
       
   162 }
       
   163 
       
   164 QRadioTuner::StereoMode S60RadioTunerControl::stereoMode() const
       
   165 {
       
   166     return m_stereoMode;
       
   167 }
       
   168 
       
   169 void S60RadioTunerControl::setStereoMode(QRadioTuner::StereoMode mode)
       
   170 {
       
   171 	if (m_fmTunerUtility) { 
       
   172 	    if (QRadioTuner::ForceMono == mode) {
       
   173 	    	m_fmTunerUtility->ForceMonoReception(true);
       
   174 	    	m_stereoMode = QRadioTuner::ForceMono;
       
   175 			m_isStereo = false;
       
   176 	    } else {
       
   177 	    	m_fmTunerUtility->ForceMonoReception(false);
       
   178 			m_isStereo = true;
       
   179 			m_stereoMode = QRadioTuner::ForceStereo;
       
   180 	    }
       
   181 	}
       
   182 }
       
   183 
       
   184 int S60RadioTunerControl::signalStrength() const
       
   185 {
       
   186     // return value is a percentage value
       
   187     if (m_fmTunerUtility) {    
       
   188         TInt maxSignalStrength;
       
   189         TInt currentSignalStrength;
       
   190         m_error = m_fmTunerUtility->GetMaxSignalStrength(maxSignalStrength);
       
   191         
       
   192         if (m_error == KErrNone) {
       
   193             m_error = m_fmTunerUtility->GetSignalStrength(currentSignalStrength);
       
   194             if (m_error == KErrNone) {
       
   195 				if (currentSignalStrength == 0 || maxSignalStrength == 0) {
       
   196 					return currentSignalStrength;
       
   197 				}
       
   198                 m_signal = currentSignalStrength / maxSignalStrength;                
       
   199             }           
       
   200         }
       
   201     }
       
   202     return m_signal;
       
   203 }
       
   204 
       
   205 int S60RadioTunerControl::volume() const
       
   206 {
       
   207     return m_vol;
       
   208 }
       
   209 
       
   210 void S60RadioTunerControl::setVolume(int volume)
       
   211 {
       
   212 	if (m_playerUtility) {
       
   213 		m_vol = volume;
       
   214 		m_playerUtility->SetVolume(volume);
       
   215 		emit volumeChanged(m_vol);
       
   216 	}
       
   217 }
       
   218 
       
   219 bool S60RadioTunerControl::isMuted() const
       
   220 {
       
   221     return m_muted;
       
   222 }
       
   223 
       
   224 void S60RadioTunerControl::setMuted(bool muted)
       
   225 {
       
   226 	if (m_playerUtility) {
       
   227 		m_muted = muted;
       
   228 		m_playerUtility->Mute(m_muted);  
       
   229 	} 
       
   230 }
       
   231 
       
   232 bool S60RadioTunerControl::isSearching() const
       
   233 {
       
   234 	return m_scanning;
       
   235 }
       
   236 
       
   237 void S60RadioTunerControl::cancelSearch()
       
   238 {
       
   239 	m_fmTunerUtility->CancelStationSeek();
       
   240 	m_scanning = false;
       
   241 	emit searchingChanged(false);
       
   242 }
       
   243 
       
   244 void S60RadioTunerControl::searchForward()
       
   245 {
       
   246 	m_fmTunerUtility->StationSeek(true);
       
   247 	m_scanning = true;
       
   248 	emit searchingChanged(true);
       
   249 }
       
   250 
       
   251 void S60RadioTunerControl::searchBackward()
       
   252 {
       
   253 	m_fmTunerUtility->StationSeek(false);
       
   254 	m_scanning = true;
       
   255 	emit searchingChanged(true);
       
   256 }
       
   257 
       
   258 bool S60RadioTunerControl::isValid() const
       
   259 {
       
   260     return m_available;
       
   261 }
       
   262 
       
   263 bool S60RadioTunerControl::initRadio()
       
   264 {
       
   265 	m_available = false;	
       
   266 	// create an instance of Radio Utility factory and indicate
       
   267 	// FM Radio is a primary client
       
   268 	TRAPD(utilityError, 
       
   269 		m_radioUtility = CRadioUtility::NewL(ETrue);
       
   270 		// Get a tuner utility
       
   271 		m_fmTunerUtility = &m_radioUtility->RadioFmTunerUtilityL(*this);
       
   272 		// Get a player utility
       
   273 		m_playerUtility = &m_radioUtility->RadioPlayerUtilityL(*this);
       
   274 	);
       
   275 	if (utilityError != KErrNone) {
       
   276         m_radioError = QRadioTuner::ResourceError;
       
   277 		return m_available;
       
   278 	}
       
   279 	
       
   280 	m_tunerControl = false;
       
   281 	
       
   282 	m_available = true;
       
   283 	return m_available;
       
   284 }
       
   285 
       
   286 bool S60RadioTunerControl::isAvailable() const
       
   287 {
       
   288 	return m_available;
       
   289 }
       
   290 
       
   291 QtMedia::AvailabilityError S60RadioTunerControl::availabilityError() const
       
   292 {
       
   293 	if (m_available)
       
   294 		return QtMedia::NoError;
       
   295 	else
       
   296 		return QtMedia::ResourceError;
       
   297 }
       
   298 
       
   299 void S60RadioTunerControl::start()
       
   300 {	
       
   301 	if (!m_tunerControl) {
       
   302 		m_fmTunerUtility->RequestTunerControl();
       
   303 		m_apiTunerState = QRadioTuner::ActiveState;
       
   304 		emit stateChanged(m_apiTunerState);
       
   305 	} else {
       
   306 		m_playerUtility->Play();
       
   307 		m_apiTunerState = QRadioTuner::ActiveState;
       
   308 		emit stateChanged(m_apiTunerState);
       
   309 	}
       
   310 		
       
   311 }
       
   312 
       
   313 void S60RadioTunerControl::stop()
       
   314 {
       
   315 	if (m_playerUtility) {
       
   316 		m_playerUtility->Stop();
       
   317 		m_apiTunerState = QRadioTuner::StoppedState;
       
   318 		emit stateChanged(m_apiTunerState);
       
   319     }
       
   320 }
       
   321 
       
   322 QRadioTuner::Error S60RadioTunerControl::error() const
       
   323 {
       
   324     return m_radioError;
       
   325 }
       
   326 QString S60RadioTunerControl::errorString() const
       
   327 {
       
   328 	return m_errorString;
       
   329 }
       
   330 
       
   331 void S60RadioTunerControl::MrpoStateChange(TPlayerState aState, TInt aError)
       
   332 {
       
   333 	if (aError == KErrNone){
       
   334 		m_radioError = QRadioTuner::NoError;
       
   335 		if (aState == ERadioPlayerIdle) {
       
   336 			m_apiTunerState = QRadioTuner::ActiveState;
       
   337 		} else if (aState == ERadioPlayerPlaying) {
       
   338 			m_apiTunerState = QRadioTuner::ActiveState;
       
   339 		}
       
   340 	}	
       
   341 	emit stateChanged(m_apiTunerState);
       
   342 }
       
   343 
       
   344 void S60RadioTunerControl::MrpoVolumeChange(TInt aVolume)
       
   345 {
       
   346 	m_vol = aVolume;
       
   347     emit volumeChanged(m_vol);
       
   348 }
       
   349 
       
   350 void S60RadioTunerControl::MrpoMuteChange(TBool aMute)
       
   351 {
       
   352 	m_muted = aMute;
       
   353 	emit mutedChanged(m_muted);
       
   354 }
       
   355 
       
   356 void S60RadioTunerControl::MrpoBalanceChange(TInt aLeftPercentage, TInt aRightPercentage)
       
   357 {
       
   358 	// no actions
       
   359 }
       
   360 
       
   361 void S60RadioTunerControl::MrftoRequestTunerControlComplete(TInt aError)
       
   362 {
       
   363 	if (aError == KErrNone) {
       
   364 		m_playerUtility->GetMaxVolume(m_maxVolume);
       
   365 		m_radioError = QRadioTuner::NoError;
       
   366 		m_tunerControl = true;
       
   367 		m_available = true;
       
   368 		m_fmTunerUtility->SetFrequency(m_currentFreq);
       
   369 		m_playerUtility->Play();
       
   370 		int signal = signalStrength();		
       
   371 		if (m_signal != signal) {
       
   372 			emit signalStrengthChanged(signal);
       
   373 			m_signal = signal;
       
   374 		}
       
   375 
       
   376 	} else if (aError == KFmRadioErrAntennaNotConnected) {
       
   377 		m_radioError = QRadioTuner::OpenError;
       
   378 	} else if (aError == KErrAlreadyExists){
       
   379 		m_radioError = QRadioTuner::ResourceError;
       
   380 	} else if (aError == KFmRadioErrFrequencyOutOfBandRange) {
       
   381 		m_radioError = QRadioTuner::OutOfRangeError;
       
   382 	}else{
       
   383 		m_radioError = QRadioTuner::OpenError;
       
   384 	}
       
   385 
       
   386 }
       
   387 
       
   388 void S60RadioTunerControl::MrftoSetFrequencyRangeComplete(TInt aError)
       
   389 {
       
   390 	if (aError == KFmRadioErrFrequencyOutOfBandRange || KFmRadioErrFrequencyNotValid) {
       
   391 		m_radioError = QRadioTuner::OutOfRangeError;
       
   392 	} else if (aError == KFmRadioErrHardwareFaulty || KFmRadioErrOfflineMode) {
       
   393 		m_radioError = QRadioTuner::OpenError;
       
   394 	}
       
   395 }
       
   396 
       
   397 void S60RadioTunerControl::MrftoSetFrequencyComplete(TInt aError)
       
   398 {
       
   399 	if (aError == KErrNone) {
       
   400 		m_radioError = QRadioTuner::NoError;
       
   401 	} else if (aError == KFmRadioErrFrequencyOutOfBandRange || KFmRadioErrFrequencyNotValid) {
       
   402 		m_radioError = QRadioTuner::OutOfRangeError;
       
   403 	} else if (aError == KFmRadioErrHardwareFaulty || KFmRadioErrOfflineMode) {
       
   404 		m_radioError = QRadioTuner::OpenError;
       
   405 	}
       
   406 }
       
   407 
       
   408 void S60RadioTunerControl::MrftoStationSeekComplete(TInt aError, TInt aFrequency)
       
   409 {
       
   410 	m_scanning = false;
       
   411 	if (aError == KErrNone) {
       
   412 		m_radioError = QRadioTuner::NoError;
       
   413 		m_currentFreq = aFrequency;
       
   414 	} else {
       
   415 		m_radioError = QRadioTuner::OpenError;
       
   416 	}
       
   417 }
       
   418 
       
   419 void S60RadioTunerControl::MrftoFmTransmitterStatusChange(TBool aActive)
       
   420 {
       
   421 	//no actions
       
   422 }
       
   423 
       
   424 void S60RadioTunerControl::MrftoAntennaStatusChange(TBool aAttached)
       
   425 {
       
   426 	if (aAttached && m_tunerControl) {
       
   427 		m_playerUtility->Play();
       
   428 	}
       
   429 }
       
   430 
       
   431 void S60RadioTunerControl::MrftoOfflineModeStatusChange(TBool /*aOfflineMode*/)
       
   432 {
       
   433 
       
   434 }
       
   435 
       
   436 void S60RadioTunerControl::MrftoFrequencyRangeChange(TFmRadioFrequencyRange aBand /*, TInt aMinFreq, TInt aMaxFreq*/)
       
   437 {
       
   438 	if (aBand == EFmRangeEuroAmerica) {
       
   439 		setBand(QRadioTuner::FM);
       
   440 	} 
       
   441 }
       
   442 
       
   443 void S60RadioTunerControl::MrftoFrequencyChange(TInt aNewFrequency)
       
   444 {
       
   445 	m_currentFreq = aNewFrequency;
       
   446 	emit frequencyChanged(m_currentFreq);
       
   447 
       
   448 	int signal = signalStrength();
       
   449 	if (m_signal != signal) {
       
   450 		emit signalStrengthChanged(signal);
       
   451 		m_signal = signal;
       
   452 	}
       
   453 }
       
   454 
       
   455 void S60RadioTunerControl::MrftoForcedMonoChange(TBool aForcedMono)
       
   456 {
       
   457 	if (aForcedMono) {
       
   458 		m_stereoMode = QRadioTuner::ForceMono;
       
   459 	} else {
       
   460 		m_stereoMode = QRadioTuner::ForceStereo;
       
   461 	}	
       
   462 	emit stereoStatusChanged(!aForcedMono);
       
   463 }
       
   464 
       
   465 void S60RadioTunerControl::MrftoSquelchChange(TBool aSquelch)
       
   466 {
       
   467 	// no actions
       
   468 }