Menu

[r13]: / trunk / libs / OSMF / org / osmf / elements / compositeClasses / SerialSeekTrait.as  Maximize  Restore  History

Download this file

476 lines (418 with data), 15.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.elements.compositeClasses
{
import __AS3__.vec.Vector;
import org.osmf.events.SeekEvent;
import org.osmf.media.MediaElement;
import org.osmf.traits.MediaTraitType;
import org.osmf.traits.PlayState;
import org.osmf.traits.PlayTrait;
import org.osmf.traits.SeekTrait;
import org.osmf.traits.TimeTrait;
/**
* Implementation of SeekTrait which can be a composite media trait.
*
* In the serial case, the seek may:
* <ul> seek within the current child
* <ul> seek out of the current child and seek backward
* <ul> seek out of the current child and seek forward
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
internal class SerialSeekTrait extends CompositeSeekTrait
{
public function SerialSeekTrait(traitAggregator:TraitAggregator, owner:MediaElement)
{
super(traitAggregator, CompositionMode.SERIAL, owner);
this.owner = owner;
}
/**
* @private
*/
override protected function doSeek(seekOp:CompositeSeekOperationInfo):void
{
var serialSeek:SerialSeekOperationInfo = seekOp as SerialSeekOperationInfo;
var childSeekTrait:SeekTrait = serialSeek.fromChild.getTrait(MediaTraitType.SEEK) as SeekTrait;
if (serialSeek.fromChild == serialSeek.toChild)
{
readyToExitSeekingState = true;
// This is the case where the seek is within the current child.
childSeekTrait.seek(serialSeek.toChildTime);
}
else
{
// Now, the composite is going to do a seek that crosses multiple children.
// The order of operations is as follows:
// 1) Seek any required in-between children to the appropriate time.
// 2) Seek the targeted child to the appropriate time.
// 3) Seek the previous current child to the appropriate time.
// 4) Set the new current child to be the targeted child.
//
// The reason for this order is to ensure that we seek all non-current
// children *first*, and the current child *last*, so that we trigger
// events last.
//
// Note that we prevent ourselves from exiting the seeking state until
// we've completed all but the last operation.
crossChildrenSeeking = true;
allCrossChildrenSeeksInitiated = false;
readyToExitSeekingState = false;
// Remember the current state so that we can reapply it after the seek.
var previousPlayState:String = getCompositePlayState();
// Since the playhead seeks out of the child, the child needs to be stopped.
var childPlayTrait:PlayTrait = serialSeek.fromChild.getTrait(MediaTraitType.PLAY) as PlayTrait;
if (childPlayTrait != null)
{
childPlayTrait.stop();
}
var childSeekTargetTime:Number;
// Do the seek out of the current child first.
if (serialSeek.seekForward)
{
// Seeking forward means to move the playhead to the end.
var childTimeTrait:TimeTrait = serialSeek.fromChild.getTrait(MediaTraitType.TIME) as TimeTrait;
childSeekTargetTime = childTimeTrait.duration;
}
else
{
// Place all in-between children at the start.
for each (var inBetweenChild:SerialElementSegment in serialSeek.inBetweenChildren)
{
if (inBetweenChild.mediaElement.hasTrait(MediaTraitType.SEEK))
{
var inBetweenSeekTrait:SeekTrait = inBetweenChild.mediaElement.getTrait(MediaTraitType.SEEK) as SeekTrait;
if (inBetweenSeekTrait.canSeekTo(0))
{
inBetweenSeekTrait.seek(0);
}
}
}
// Seeking backward means to move the playhead to the beginning.
childSeekTargetTime = 0;
}
// Now the composite does the seek into another child, where the destiny of the seek is.
var nextSeekTrait:SeekTrait = serialSeek.toChild.getTrait(MediaTraitType.SEEK) as SeekTrait;
nextSeekTrait.seek(serialSeek.toChildTime);
// For forward seeks, set the current child before seeking the old
// current child. This ensures that a seek to the end doesn't trigger
// playback of the next child.
if (serialSeek.seekForward)
{
// Next, update our current child to the target child.
traitAggregator.listenedChild = serialSeek.toChild;
// We're not ready to exit the seeking state until we initiate
// the last operation. On a seek forward, the last operation
// is the last seek (see below).
readyToExitSeekingState = true;
}
// Now, seek the previously current child to the appropriate time.
allCrossChildrenSeeksInitiated = true;
childSeekTrait.seek(childSeekTargetTime);
if (serialSeek.seekForward == false)
{
// We're not ready to exit the seeking state until we initiate
// the last operation. On a seek back, the last operation
// is the setting of the new current child (see below).
readyToExitSeekingState = true;
traitAggregator.listenedChild = serialSeek.toChild;
}
if (!isNaN(exitSeekingStateTime))
{
setSeeking(false, exitSeekingStateTime);
exitSeekingStateTime = NaN;
}
var playTrait:PlayTrait = owner.getTrait(MediaTraitType.PLAY) as PlayTrait;
if (playTrait != null)
{
if (previousPlayState == PlayState.PLAYING)
{
playTrait.play();
}
else if (previousPlayState == PlayState.PAUSED && playTrait.canPause)
{
playTrait.pause();
}
}
}
}
/**
* @private
*/
override protected function prepareSeekOperationInfo(time:Number):CompositeSeekOperationInfo
{
var serialSegments:Vector.<SerialElementSegment> = prepareSerialSegments();
var seekToOp:SerialSeekOperationInfo;
var curChildIndex:int = traitAggregator.getChildIndex(traitAggregator.listenedChild);
if (curChildIndex < serialSegments.length)
{
var curChildSeekOperation:SerialElementSegment = serialSegments[curChildIndex];
// Check whether this is a seek within the current child
if (curChildSeekOperation.relativeStart <= time && time <= curChildSeekOperation.relativeEnd)
{
seekToOp = new SerialSeekOperationInfo();
seekToOp.canSeekTo = true;
seekToOp.fromChild = curChildSeekOperation.mediaElement;
seekToOp.toChild = curChildSeekOperation.mediaElement;
// Convert from the composite playhead position to child playhead position
seekToOp.toChildTime = time - curChildSeekOperation.relativeStart;
}
else if (time < curChildSeekOperation.relativeStart)
{
seekToOp = canSeekBackward(time, serialSegments, curChildIndex);
}
else
{
seekToOp = canSeekForward(time, serialSegments, curChildIndex);
}
}
else
{
// Seeking is not possible within the current child.
seekToOp = new SerialSeekOperationInfo();
seekToOp.canSeekTo = false;
}
return seekToOp;
}
/**
* @private
*/
override protected function checkSeeking():Boolean
{
// Serail composite seekable is only in seeking state when the current child is in
// seeking state.
return (traitOfCurrentChild != null) ? traitOfCurrentChild.seeking : false;
}
// Internals
//
private function prepareSerialSegments():Vector.<SerialElementSegment>
{
var serialSegments:Vector.<SerialElementSegment> = new Vector.<SerialElementSegment>();
var currentTime:Number = 0;
for (var i:int = 0; i < traitAggregator.numChildren; i++)
{
var mediaElement:MediaElement = traitAggregator.getChildAt(i);
var timeTrait:TimeTrait = mediaElement.getTrait(MediaTraitType.TIME) as TimeTrait;
// If a child has no duration, it will be skipped.
if (timeTrait != null && !isNaN(timeTrait.duration))
{
serialSegments.push
( new SerialElementSegment
( mediaElement
, currentTime
, currentTime + timeTrait.duration
, mediaElement.hasTrait(MediaTraitType.SEEK) == false
)
);
currentTime += timeTrait.duration;
}
else
{
// Push an empty segment so that we maintain one segment
// for each child.
serialSegments.push
( new SerialElementSegment
( mediaElement
, currentTime
, currentTime
, mediaElement.hasTrait(MediaTraitType.SEEK) == false
)
);
}
}
return serialSegments;
}
override protected function onSeekingChanged(event:SeekEvent):void
{
// If the composite is in the middle of a seek that crosses multiple children,
// the composite trait will expect to receive SeekingChangeEvents from its children.
// Once all children finish seeking, then the seek is considered complete.
if (crossChildrenSeeking)
{
// When executing a cross-child seek, we must wait until all of the seeks
// are initiated before checking to see if we should signal that the entire
// seek is complete, or else we'll end up with a false positive.
if (allCrossChildrenSeeksInitiated)
{
var child:MediaElement;
while (child = traitAggregator.getNextChildWithTrait(child, MediaTraitType.SEEK))
{
if ((child.getTrait(MediaTraitType.SEEK) as SeekTrait).seeking)
{
return; //return if any children haven't completed the seek.
}
}
crossChildrenSeeking = false;
if (readyToExitSeekingState)
{
// The child is exiting the seeking state, so we just
// update the composite seeking state.
setSeeking(false, timeTrait != null ? timeTrait.currentTime : 0);
}
else
{
// Save the time which we'll pass to the setSeeking method.
exitSeekingStateTime = timeTrait != null ? timeTrait.currentTime : 0;
}
}
}
else
{
var newSeekingState:Boolean = checkSeeking();
if (newSeekingState != seeking)
{
// At this point, we know that the change of the child seeking state will cause the
// composite seeking state to change.
if (newSeekingState)
{
// Do nothing.
}
else
{
// The child is exiting the seeking state, so we just
// update the composite seeking state.
setSeeking(false, event.time);
}
}
}
}
private function canSeekBackward
( time:Number
, serialSegments:Vector.<SerialElementSegment>
, curChildIndex:int):SerialSeekOperationInfo
{
var seekToOp:SerialSeekOperationInfo = new SerialSeekOperationInfo();
seekToOp.seekForward = false;
seekToOp.canSeekTo = false;
// If the current child is the first child of the serial element, and the
// composite trait still needs to seek to the left of the current child,
// it is definitely unseekable.
var index:int = curChildIndex - 1;
if (index < 0)
{
return seekToOp;
}
seekToOp.fromChild = serialSegments[curChildIndex].mediaElement;
var curSegment:SerialElementSegment = serialSegments[index];
while (curSegment != null)
{
var childSeekTrait:SeekTrait = curSegment.mediaElement.getTrait(MediaTraitType.SEEK) as SeekTrait;
if (time >= curSegment.relativeStart)
{
if (childSeekTrait == null)
{
curSegment = serialSegments[index+1];
seekToOp.toChild = curSegment.mediaElement;
seekToOp.toChildTime = 0;
}
else
{
seekToOp.toChild = curSegment.mediaElement;
seekToOp.toChildTime = time - curSegment.relativeStart;
}
seekToOp.canSeekTo = true;
break;
}
index--;
if (index < 0)
{
break;
}
else
{
seekToOp.inBetweenChildren.push(curSegment);
curSegment = serialSegments[index];
}
}
return seekToOp;
}
private function canSeekForward
( time:Number
, serialSegments:Vector.<SerialElementSegment>
, curChildIndex:int):SerialSeekOperationInfo
{
var seekToOp:SerialSeekOperationInfo = new SerialSeekOperationInfo();
seekToOp.seekForward = true;
seekToOp.canSeekTo = false;
// If the current child is the last child of the serial element, and the
// composite trait still needs to seek to the right of the current child,
// it is definitely unseekable.
var index:int = curChildIndex + 1;
if (index >= serialSegments.length)
{
return seekToOp;
}
seekToOp.fromChild = serialSegments[curChildIndex].mediaElement;
var curSegment:SerialElementSegment = serialSegments[index];
while (curSegment != null)
{
if (time <= curSegment.relativeEnd)
{
var childSeekTrait:SeekTrait = curSegment.mediaElement.getTrait(MediaTraitType.SEEK) as SeekTrait;
if (childSeekTrait == null)
{
// Not seekable.
break;
}
else
{
seekToOp.toChild = curSegment.mediaElement;
seekToOp.toChildTime = time - curSegment.relativeStart;
}
seekToOp.canSeekTo = true;
break;
}
index++;
if (index >= serialSegments.length || curSegment.unseekable)
{
// Not seekable.
break;
}
else
{
seekToOp.inBetweenChildren.push(curSegment);
curSegment = serialSegments[index];
}
}
return seekToOp;
}
private function get traitOfCurrentChild():SeekTrait
{
return traitAggregator.listenedChild
? traitAggregator.listenedChild.getTrait(MediaTraitType.SEEK) as SeekTrait
: null;
}
private function getCompositePlayState():String
{
var playTrait:PlayTrait = owner.getTrait(MediaTraitType.PLAY) as PlayTrait;
return (playTrait == null) ? null : playTrait.playState;
}
private var owner:MediaElement;
private var crossChildrenSeeking:Boolean;
private var allCrossChildrenSeeksInitiated:Boolean;
private var readyToExitSeekingState:Boolean = true;
private var exitSeekingStateTime:Number;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.