mmlibs/mmfw/src/Client/Video/mmfsubtitleutility.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include "mmfsubtitleutility.h"
       
    22 
       
    23 
       
    24 CMMFSubtitleUtility* CMMFSubtitleUtility::NewL(RMMFController& aController, RWsSession &aWs)
       
    25 	{
       
    26 	CMMFSubtitleUtility* self = new(ELeave) CMMFSubtitleUtility(aController);
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL(aWs);
       
    29 	CleanupStack::Pop();
       
    30 	return self;	
       
    31 	}
       
    32 
       
    33 CMMFSubtitleUtility::CMMFSubtitleUtility(RMMFController& aController): iSubtitleSupportCustomCommands(aController)
       
    34 	{
       
    35 	}
       
    36 
       
    37 void CMMFSubtitleUtility::ConstructL(RWsSession &aWs)
       
    38 	{
       
    39 	iDevice = new (ELeave) CWsScreenDevice(aWs);
       
    40 	User::LeaveIfError(iDevice->Construct());
       
    41 	iSubtitleGc = new (ELeave) CWindowGc(iDevice);
       
    42 	User::LeaveIfError(iSubtitleGc->Construct());
       
    43 	}
       
    44 
       
    45 CMMFSubtitleUtility::~CMMFSubtitleUtility()
       
    46 	{
       
    47 	delete iSubtitleGc;
       
    48 	delete iDevice;
       
    49 	iSubtitleLanguages.Close();
       
    50 	iCrpDataArray.Close();
       
    51 	}
       
    52 
       
    53 // Add subtitle related config to controller
       
    54 TInt CMMFSubtitleUtility::AddSubtitleConfig(const TMMFSubtitleWindowConfig& aConfig)
       
    55 	{
       
    56 	TCrpData crpData(aConfig.iWindowId);
       
    57 	// Add the crp data before calling custom commands in case append fails
       
    58 	TInt err = iCrpDataArray.Append(crpData);
       
    59 	if (KErrNone == err)
       
    60 		{
       
    61 		err = iSubtitleSupportCustomCommands.AddSubtitleConfig(aConfig);
       
    62 	
       
    63 		if (KErrNone != err)
       
    64 			{
       
    65 			// removed the added crp data on error
       
    66 			iCrpDataArray.Remove(iCrpDataArray.Count()-1);
       
    67 			}
       
    68 		}
       
    69 	
       
    70 	return err;
       
    71 	}
       
    72 
       
    73 // Found the index of the subtitle data in iCrpDataArray given aWindowId
       
    74 TInt CMMFSubtitleUtility::FindCrpArrayIndex(TInt aWindowId)
       
    75 	{
       
    76 	TInt ret = KErrNotFound;
       
    77 	for (TInt i = iCrpDataArray.Count(); --i >= 0; )
       
    78 		{
       
    79 		if (iCrpDataArray[i].iWindowId == aWindowId)
       
    80 			{
       
    81 			ret = i;
       
    82 			break;
       
    83 			}
       
    84 		}
       
    85 	return ret;
       
    86 	}
       
    87 
       
    88 // Remove subtitle related config from controller
       
    89 TInt CMMFSubtitleUtility::RemoveSubtitleConfig(TInt aWindowId)
       
    90 	{
       
    91 	TInt ret = FindCrpArrayIndex(aWindowId);
       
    92 	if (ret >= 0)
       
    93 		{		
       
    94 		iCrpDataArray.Remove(ret);
       
    95 		ret = iSubtitleSupportCustomCommands.RemoveSubtitleConfig(aWindowId);
       
    96 		}
       
    97 	return ret;
       
    98 	}
       
    99 
       
   100 // Update subtitle related config from controller and clear the subtitle region
       
   101 TInt CMMFSubtitleUtility::UpdateSubtitleConfig(const TMMFSubtitleWindowConfig& aConfig)
       
   102 	{
       
   103 	TInt ret = FindCrpArrayIndex(aConfig.iWindowId);
       
   104 	if (ret >= 0)
       
   105 		{
       
   106 		// clear the CRP id
       
   107 		iCrpDataArray[ret].iCrpId.Set(0);
       
   108 
       
   109 		ret = iSubtitleSupportCustomCommands.UpdateSubtitleConfig(aConfig);
       
   110 		}
       
   111 	return ret;
       
   112 	}
       
   113 
       
   114 // Return if subtitle is available
       
   115 TBool CMMFSubtitleUtility::SubtitlesAvailable(RMMFController& aController)
       
   116 	{
       
   117 	TBool available = EFalse;
       
   118 	RMMFVideoPlaySubtitleSupportCustomCommands subtitleSupportCustomCommands(aController);
       
   119 	subtitleSupportCustomCommands.GetSubtitlesAvailable(available);
       
   120 	return available;
       
   121 	}
       
   122 
       
   123 TInt CMMFSubtitleUtility::EnableSubtitles()
       
   124 	{
       
   125 	return iSubtitleSupportCustomCommands.EnableSubtitles();
       
   126 	}
       
   127 
       
   128 void CMMFSubtitleUtility::DisableSubtitles()
       
   129 	{
       
   130 	for (TInt i = iCrpDataArray.Count(); --i >= 0; )
       
   131 		{
       
   132 		iSubtitleSupportCustomCommands.RemoveSubtitleConfig(iCrpDataArray[i].iWindowId);
       
   133 		}
       
   134 
       
   135 	iCrpDataArray.Reset();
       
   136 	
       
   137 	// still need to send disable command to controller event if array count was 0
       
   138 	// because window may have been removed after subtitle is enabled
       
   139 	iSubtitleSupportCustomCommands.DisableSubtitles();
       
   140 	}
       
   141 
       
   142 // Helper function to draw CRP
       
   143 void CMMFSubtitleUtility::DrawCrp(RWindow& aWindow, TInt aCrpIdx, TBool aCallBeginRedraw)
       
   144 	{
       
   145 	TCrpData& crpData = iCrpDataArray[aCrpIdx];
       
   146 
       
   147 	if (crpData.iCrpId.Id() != 0)
       
   148 		{
       
   149 
       
   150 		if (aCallBeginRedraw)
       
   151 			{
       
   152 			// CRP is ready to be drawn
       
   153 			aWindow.Invalidate(crpData.iCrpRect);
       
   154 			aWindow.BeginRedraw(crpData.iCrpRect);
       
   155 			}
       
   156 		
       
   157 		iSubtitleGc->Activate(aWindow);
       
   158 		iSubtitleGc->DrawWsGraphic(crpData.iCrpId, crpData.iCrpRect);
       
   159 		iSubtitleGc->Deactivate();
       
   160 		
       
   161 		if (aCallBeginRedraw)
       
   162 			{
       
   163 			aWindow.EndRedraw();
       
   164 			}
       
   165 		}
       
   166 	}
       
   167 
       
   168 // Handle CRP ready event from controller and draw the CRP
       
   169 void CMMFSubtitleUtility::HandleCrpReady(RWindow& aWindow)
       
   170 	{
       
   171 	TInt index = FindCrpArrayIndex(aWindow.WsHandle());
       
   172 	if (index >= 0)
       
   173 		{
       
   174 		TCrpData& crpData = iCrpDataArray[index];
       
   175 		TInt err = iSubtitleSupportCustomCommands.GetCrpParameters(aWindow.WsHandle(), 
       
   176 																	crpData.iCrpId, 
       
   177 																	crpData.iCrpRect);
       
   178 
       
   179 		if (KErrNone == err)
       
   180 			{
       
   181 			DrawCrp(aWindow, index, ETrue);
       
   182 			}
       
   183 		else
       
   184 			{
       
   185 			// Window may have been removed before the event was received, ignore event
       
   186 			RDebug::Print(_L("CMMFSubtitleUtility::HandleCrpReady aWindowId=%d, err==%d"), aWindow.WsHandle(), err);
       
   187 			}
       
   188 		}
       
   189 	}
       
   190 
       
   191 // Redraw subtitle CRP if redraw rect intersect with subtitle region
       
   192 void CMMFSubtitleUtility::RedrawSubtitle(RWindow& aWindow, const TRect& aRedrawRect)
       
   193 	{
       
   194 	TInt index = FindCrpArrayIndex(aWindow.WsHandle());
       
   195 	if (index >= 0 && aRedrawRect.Intersects(iCrpDataArray[index].iCrpRect))
       
   196 		{
       
   197 		DrawCrp(aWindow, index, EFalse);
       
   198 		}
       
   199 	}
       
   200 
       
   201 TInt CMMFSubtitleUtility::SetSubtitleLanguage(TLanguage aSubtitleLanguage)
       
   202 	{
       
   203 	return iSubtitleSupportCustomCommands.SetSubtitleLanguage(aSubtitleLanguage);
       
   204 	}
       
   205 
       
   206 TArray<TLanguage> CMMFSubtitleUtility::SupportedSubtitleLanguagesL()
       
   207 	{
       
   208 	TRAPD(err, iSubtitleSupportCustomCommands.GetSupportedSubtitleLanguagesL(iSubtitleLanguages));
       
   209 	
       
   210 	// Do not propagate KErrNotSupported; return empty list of languages.
       
   211 	if (KErrNone != err && KErrNotSupported != err)
       
   212 		{
       
   213 		User::Leave(err);
       
   214 		}
       
   215 	return iSubtitleLanguages.Array();
       
   216 	}
       
   217 
       
   218 TLanguage CMMFSubtitleUtility::SubtitleLanguage()
       
   219 	{
       
   220 	TLanguage language = ELangNone;
       
   221 	
       
   222 	// ignore returned value, language parameter is unchanged on error
       
   223 	iSubtitleSupportCustomCommands.GetSubtitleLanguage(language);
       
   224 
       
   225 	return language;
       
   226 	}