|
1 /* |
|
2 * Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: This class implements FramePositioningControl for video player |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <jdebug.h> |
|
21 #include <e32base.h> |
|
22 #include <e32math.h> |
|
23 |
|
24 #include "cmmavideoframepositioningcontrol.h" |
|
25 |
|
26 namespace |
|
27 { |
|
28 const TInt32 KMMAFramePosMicroSecMultiplier = 1000000; |
|
29 } |
|
30 |
|
31 CMMAVideoFramePositioningControl* |
|
32 CMMAVideoFramePositioningControl::NewL(CMMAVideoPlayer* aPlayer) |
|
33 { |
|
34 CMMAVideoFramePositioningControl* self = |
|
35 new(ELeave) CMMAVideoFramePositioningControl(aPlayer); |
|
36 return self; |
|
37 |
|
38 } |
|
39 CMMAVideoFramePositioningControl:: |
|
40 CMMAVideoFramePositioningControl(CMMAVideoPlayer* aPlayer) |
|
41 : CMMAFramePositioningControl(aPlayer), iPlayer(aPlayer) |
|
42 { |
|
43 DEBUG("MMA:CMMAVideoFramePositioningControl::CMMAVideoFramePositioningControl"); |
|
44 } |
|
45 |
|
46 CMMAVideoFramePositioningControl::~CMMAVideoFramePositioningControl() |
|
47 { |
|
48 DEBUG("MMA:CMMAVideoFramePositioningControl::~CMMAVideoFramePositioningControl"); |
|
49 } |
|
50 |
|
51 TInt CMMAVideoFramePositioningControl::SeekL(TInt aFrameNumber) |
|
52 { |
|
53 DEBUG_INT("CMMAVideoFramePositioningControl::SeekL, aFrameNumber: %d", aFrameNumber); |
|
54 |
|
55 RMMFController& controller = iPlayer->Controller(); |
|
56 |
|
57 // Controller is paused before changing position to increase |
|
58 // accuracy |
|
59 TBool playerStarted = EFalse; |
|
60 if (iPlayer->State() == CMMAPlayer::EStarted) |
|
61 { |
|
62 playerStarted = ETrue; |
|
63 } |
|
64 |
|
65 if (playerStarted) |
|
66 { |
|
67 User::LeaveIfError(controller.Pause()); |
|
68 } |
|
69 |
|
70 // Frame number must be positive |
|
71 TInt frameNumber = aFrameNumber; |
|
72 if (frameNumber < 0) |
|
73 { |
|
74 frameNumber = 0; |
|
75 } |
|
76 |
|
77 // Find out framerate of video |
|
78 TReal32 frameRate = 0; |
|
79 GetAndCheckFrameRateL(frameRate); |
|
80 |
|
81 // Calculate new media time |
|
82 TReal tempReal = ((TReal32)frameNumber * |
|
83 KMMAFramePosMicroSecMultiplier) / frameRate; |
|
84 User::LeaveIfError(Math::Round(tempReal, tempReal, 0)); |
|
85 TInt64 newMediaTime = tempReal; |
|
86 |
|
87 // New media time is clamped to duration |
|
88 TInt err = ClampMediaTime(newMediaTime); |
|
89 // Clamp fails if duration of content is not known. |
|
90 // Two additional resorts are (in preferred order): |
|
91 // 1) Return current frame position if it can be calculated. |
|
92 // 2) Or seek to 0. |
|
93 |
|
94 // First resort |
|
95 if (err != KErrNone) |
|
96 { |
|
97 iPlayer->GetMediaTime(&newMediaTime); |
|
98 // Second resort |
|
99 if (newMediaTime < 0) |
|
100 { |
|
101 newMediaTime = 0; |
|
102 } |
|
103 } |
|
104 iPlayer->SetMediaTimeL(&newMediaTime); |
|
105 // Error condition |
|
106 if (newMediaTime < KErrNotFound) |
|
107 { |
|
108 User::Leave(KErrNotFound); |
|
109 } |
|
110 |
|
111 // Calculate actually set frame number |
|
112 tempReal = (((TReal32)newMediaTime / KMMAFramePosMicroSecMultiplier) * |
|
113 (TReal32) frameRate); |
|
114 User::LeaveIfError(Math::Round(tempReal, tempReal, 0)); |
|
115 |
|
116 // Restart controller |
|
117 if (playerStarted) |
|
118 { |
|
119 User::LeaveIfError(controller.Play()); |
|
120 } |
|
121 |
|
122 return tempReal; |
|
123 } |
|
124 |
|
125 TInt CMMAVideoFramePositioningControl::SkipL(TInt aFramesToSkip) |
|
126 { |
|
127 DEBUG_INT("CMMAVideoFramePositioningControl::SkipL, aFramesToSkip: %d", aFramesToSkip); |
|
128 |
|
129 RMMFController& controller = iPlayer->Controller(); |
|
130 |
|
131 // Controller is paused before changing position to increase |
|
132 // accuracy |
|
133 TBool playerStarted = EFalse; |
|
134 if (iPlayer->State() == CMMAPlayer::EStarted) |
|
135 { |
|
136 playerStarted = ETrue; |
|
137 } |
|
138 |
|
139 if (playerStarted) |
|
140 { |
|
141 User::LeaveIfError(controller.Pause()); |
|
142 } |
|
143 |
|
144 // Find out framerate of video |
|
145 TReal32 frameRate = 0; |
|
146 GetAndCheckFrameRateL(frameRate); |
|
147 |
|
148 // Find out current media time |
|
149 TInt64 currentMediaTime = 0; |
|
150 iPlayer->GetMediaTime(¤tMediaTime); |
|
151 |
|
152 // Error condition |
|
153 if (currentMediaTime < KErrNone) |
|
154 { |
|
155 User::Leave(KErrNotFound); |
|
156 } |
|
157 |
|
158 // Find out current frame number |
|
159 TReal tempReal = |
|
160 ((TReal32) currentMediaTime / KMMAFramePosMicroSecMultiplier) * |
|
161 frameRate; |
|
162 User::LeaveIfError(Math::Round(tempReal, tempReal, 0)); |
|
163 TInt currentFrameNumber = tempReal; |
|
164 |
|
165 // Calculate new media time |
|
166 TInt64 newMediaTime = currentMediaTime + |
|
167 (((TReal32) aFramesToSkip / frameRate) * KMMAFramePosMicroSecMultiplier); |
|
168 |
|
169 // New media time is clamped to duration |
|
170 TInt err = ClampMediaTime(newMediaTime); |
|
171 |
|
172 // If clamping fails, no frames are skipped |
|
173 TInt framesSkipped = 0; |
|
174 if (err == KErrNone) |
|
175 { |
|
176 iPlayer->SetMediaTimeL(&newMediaTime); |
|
177 // Error condition |
|
178 if (newMediaTime < KErrNone) |
|
179 { |
|
180 User::Leave(KErrNotFound); |
|
181 } |
|
182 |
|
183 // Calculate actual amount of frames skipped |
|
184 tempReal = ((((TReal32) newMediaTime / KMMAFramePosMicroSecMultiplier) * |
|
185 (TReal32) frameRate) - currentFrameNumber); |
|
186 |
|
187 User::LeaveIfError(Math::Round(tempReal, tempReal, 0)); |
|
188 framesSkipped = tempReal; |
|
189 } |
|
190 |
|
191 // Restart controller |
|
192 if (playerStarted) |
|
193 { |
|
194 User::LeaveIfError(controller.Play()); |
|
195 } |
|
196 |
|
197 return framesSkipped; |
|
198 } |
|
199 |
|
200 void CMMAVideoFramePositioningControl::MapFrameToTimeL(TInt aFrameNumber, TInt64* aMediaTime) |
|
201 { |
|
202 DEBUG_INT("CMMAVideoFramePositioningControl::MapFrameToTimeL, aFrameNumber: %d", aFrameNumber); |
|
203 // Find out framerate of video |
|
204 TReal32 frameRate = 0; |
|
205 GetAndCheckFrameRateL(frameRate); |
|
206 |
|
207 // Find out frame mediatime |
|
208 TInt64 frameMediaTime = |
|
209 ((TReal32)aFrameNumber / (TReal32)frameRate) * |
|
210 KMMAFramePosMicroSecMultiplier; |
|
211 |
|
212 TInt64 duration; |
|
213 iPlayer->GetDuration(&duration); |
|
214 |
|
215 if (frameMediaTime < 0) |
|
216 { |
|
217 frameMediaTime = KErrNotFound; |
|
218 } |
|
219 else if (frameMediaTime > duration) |
|
220 { |
|
221 frameMediaTime = KErrNotFound; |
|
222 // With some medias last frame media time is few hundred microsec's |
|
223 // over duration. This is because framerate is not accurate. |
|
224 // If given frame number is last frame and frameMediaTime > duration, |
|
225 // return duration |
|
226 // Find out last frame |
|
227 TReal lastFrame = (frameRate * (TReal32)duration) / |
|
228 KMMAFramePosMicroSecMultiplier; |
|
229 User::LeaveIfError(Math::Round(lastFrame, lastFrame, 0)); |
|
230 if (aFrameNumber == lastFrame) |
|
231 { |
|
232 frameMediaTime = duration; |
|
233 } |
|
234 } |
|
235 *aMediaTime = frameMediaTime; |
|
236 } |
|
237 |
|
238 TInt CMMAVideoFramePositioningControl::MapTimeToFrameL(TInt64* aMediaTime) |
|
239 { |
|
240 DEBUG_INT("CMMAVideoFramePositioningControl::MapTimeToFrameL, aMediaTime: %d", *aMediaTime); |
|
241 TInt64 mediaTime = *aMediaTime; |
|
242 |
|
243 // If given media time is < 0 or > duration, cannot map time to frame |
|
244 // conversion fails, -1 is returned |
|
245 TInt64 duration; |
|
246 iPlayer->GetDuration(&duration); |
|
247 if (mediaTime > duration || mediaTime < 0) |
|
248 { |
|
249 return KErrNotFound; |
|
250 } |
|
251 |
|
252 // Find out framerate of video |
|
253 TReal32 frameRate = 0; |
|
254 GetAndCheckFrameRateL(frameRate); |
|
255 |
|
256 // Calculate frame number from media time |
|
257 TReal tempReal = (TInt)((TReal64)(mediaTime * frameRate) / |
|
258 KMMAFramePosMicroSecMultiplier); |
|
259 User::LeaveIfError(Math::Round(tempReal, tempReal, 0)); |
|
260 |
|
261 return tempReal; |
|
262 } |
|
263 |
|
264 |
|
265 void CMMAVideoFramePositioningControl::GetAndCheckFrameRateL( |
|
266 TReal32& aFrameRate) |
|
267 { |
|
268 DEBUG("CMMAVideoFramePositioningControl::GetAndCheckFrameRateL"); |
|
269 RMMFVideoControllerCustomCommands customCommands = |
|
270 RMMFVideoControllerCustomCommands(iPlayer->Controller()); |
|
271 |
|
272 User::LeaveIfError(customCommands.GetFrameRate(aFrameRate)); |
|
273 DEBUG_INT("CMMAVideoFramePositioningControl::GetAndCheckFrameRateL, aFrameRate: %d", aFrameRate); |
|
274 if (aFrameRate <= 0) |
|
275 { |
|
276 // zero framerate is not accepted because cannot |
|
277 // divide by zero. Negative framerate is not |
|
278 // acceptable aswell. |
|
279 DEBUG("CMMAVideoFramePositioningControl::GetAndCheckFrameRateL: invalid framerate"); |
|
280 User::Leave(KErrNotFound); |
|
281 } |
|
282 } |
|
283 |
|
284 // END OF FILE |