Menu

[r326]: / trunk / framework / OSMF / org / osmf / traits / DynamicStreamTrait.as  Maximize  Restore  History

Download this file

457 lines (417 with data), 13.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
/*****************************************************
*
* Copyright 2009 Akamai Technologies, Inc. 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 Akamai Technologies, Inc.
* Portions created by Akamai Technologies, Inc. are Copyright (C) 2009 Akamai
* Technologies, Inc. All Rights Reserved.
*
* Contributor(s): Adobe Systems Incorporated.
*
*****************************************************/
package org.osmf.traits
{
import flash.errors.IllegalOperationError;
import org.osmf.events.DynamicStreamEvent;
import org.osmf.utils.OSMFStrings;
/**
* Dispatched when a stream switch is requested, completed, or failed.
*
* @eventType org.osmf.events.DynamicStreamEvent.SWITCHING_CHANGE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="switchingChange",type="org.osmf.events.DynamicStreamEvent")]
/**
* Dispatched when the number of dynamic streams has changed.
*
* @eventType org.osmf.events.DynamicStreamEvent.NUM_DYNAMIC_STREAMS_CHANGE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="numDynamicStreamsChange",type="org.osmf.events.DynamicStreamEvent")]
/**
* Dispatched when the autoSwitch property changed.
*
* @eventType org.osmf.events.DynamicStreamEvent.AUTO_SWITCH_CHANGE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="autoSwitchChange",type="org.osmf.events.DynamicStreamEvent")]
/**
* DynamicStreamTrait defines the trait interface for media supporting dynamic stream
* switching. It can also be used as the base class for a more specific DynamicStreamTrait
* subclass.
*
* <p>Use the <code>MediaElement.hasTrait(MediaTraitType.DYNAMIC_STREAM)</code> method to query
* whether a media element has a trait of this type.
* If <code>hasTrait(MediaTraitType.DYNAMIC_STREAM)</code> returns <code>true</code>,
* use the <code>MediaElement.getTrait(MediaTraitType.DYNAMIC_STREAM)</code> method
* to get an object of this type.</p>
*
* @see org.osmf.media.MediaElement
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class DynamicStreamTrait extends MediaTraitBase
{
/**
* Constructor.
*
* @param autoSwitch The initial autoSwitch state for the trait. The default is true.
* @param currentIndex The initial stream index for the trait. The default is zero.
* @param numDynamicStreams The total number of dynamic streams.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function DynamicStreamTrait(autoSwitch:Boolean=true, currentIndex:int=0, numDynamicStreams:int=1)
{
super(MediaTraitType.DYNAMIC_STREAM);
_autoSwitch = autoSwitch;
_currentIndex = currentIndex;
_numDynamicStreams = numDynamicStreams;
_maxAllowedIndex = numDynamicStreams - 1;
_switching = false;
}
/**
* Defines whether or not the trait should be in manual
* or auto-switch mode. If in manual mode the <code>switchTo</code>
* method can be used to manually switch to a specific stream.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get autoSwitch():Boolean
{
return _autoSwitch;
}
public final function set autoSwitch(value:Boolean):void
{
if (autoSwitch != value)
{
autoSwitchChangeStart(value);
_autoSwitch = value;
autoSwitchChangeEnd();
}
}
/**
* The total number of dynamic streams.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get numDynamicStreams():int
{
return _numDynamicStreams;
}
/**
* The index of the current dynamic stream. Uses a zero-based index.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get currentIndex():int
{
return _currentIndex;
}
/**
* The maximum allowed index. This can be set at run-time to
* provide a ceiling for the switching profile, for example,
* to keep from switching up to a higher quality stream when
* the current video is too small to handle a higher quality stream.
*
* The default is the highest stream index.
*
* @throws RangeError If the specified index is less than zero or
* greater than the total number of dynamic streams.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get maxAllowedIndex():int
{
return _maxAllowedIndex;
}
public final function set maxAllowedIndex(value:int):void
{
if (value < 0 || value > _numDynamicStreams - 1)
{
throw new RangeError(OSMFStrings.getString(OSMFStrings.STREAMSWITCH_INVALID_INDEX));
}
if (maxAllowedIndex != value)
{
maxAllowedIndexChangeStart(value);
_maxAllowedIndex = value;
maxAllowedIndexChangeEnd();
}
}
/**
* Returns the associated bitrate, in kilobits per second, for the specified index.
*
* @throws RangeError If the specified index is less than zero or
* greater than the highest index available.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getBitrateForIndex(index:int):Number
{
if (index > _numDynamicStreams - 1 || index < 0)
{
throw new RangeError(OSMFStrings.getString(OSMFStrings.STREAMSWITCH_INVALID_INDEX));
}
return 0;
}
/**
* Indicates whether or not a switch is currently in progress.
* This property will return <code>true</code> while a switch has been
* requested and the switch has not yet been acknowledged and no switch failure
* has occurred. Once the switch request has been acknowledged or a
* failure occurs, the property will return <code>false</code>.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get switching():Boolean
{
return _switching;
}
/**
* Switch to a specific index. To switch up, use the <code>currentIndex</code>
* property, such as:<p>
* <code>
* obj.switchTo(obj.currentIndex + 1);
* </code>
* </p>
* @throws RangeError If the specified index is less than zero or
* greater than <code>maxAllowedIndex</code>.
* Note: If the media is paused, switching will not take place until after play resumes.
* @throws IllegalOperationError If the stream is not in manual switch mode.
*
* @see maxAllowedIndex
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function switchTo(index:int):void
{
if (autoSwitch)
{
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.STREAMSWITCH_STREAM_NOT_IN_MANUAL_MODE));
}
else if (index != currentIndex)
{
if (index < 0 || index > maxAllowedIndex)
{
throw new RangeError(OSMFStrings.getString(OSMFStrings.STREAMSWITCH_INVALID_INDEX));
}
// This method sets the switching state to true. The processing
// and completion of the switch are up to the implementing media.
setSwitching(true, index);
}
}
// Internals
//
/**
* Invoking this setter will result in the trait's numDynamicStreams
* property changing.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected final function setNumDynamicStreams(value:int):void
{
if (value != _numDynamicStreams)
{
_numDynamicStreams = value;
// Only adjust our maxAllowedIndex property if the old value
// is now out of range.
if (maxAllowedIndex >= _numDynamicStreams)
{
maxAllowedIndex = Math.max(0, _numDynamicStreams - 1);
}
dispatchEvent(new DynamicStreamEvent(DynamicStreamEvent.NUM_DYNAMIC_STREAMS_CHANGE));
}
}
/**
* Invoking this setter will result in the trait's currentIndex
* property changing.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected final function setCurrentIndex(value:int):void
{
_currentIndex = value;
}
/**
* Must be called by the implementing media on completing a switch.
*
* Calls the <code>switchingChangeStart()</code> and <code>switchingChangeEnd()</code>
* methods.
* @param newSwitching New <code>switching</code> value for the trait.
* @param index The index to which the switch shall (or did) occur.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected final function setSwitching(newSwitching:Boolean, index:int):void
{
if (newSwitching != _switching)
{
switchingChangeStart(newSwitching, index);
_switching = newSwitching;
// Update the index when a switch finishes.
if (newSwitching == false)
{
setCurrentIndex(index);
}
switchingChangeEnd(index);
}
}
/**
* Called immediately before the <code>autoSwitch</code> property is changed.
* <p>Subclasses can override this method to communicate the change to the media.</p>
* @param value New value for the <code>autoSwitch</code> property.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function autoSwitchChangeStart(value:Boolean):void
{
}
/**
* Called just after the <code>autoSwitch</code> property has changed.
* Dispatches the change event.
*
* <p>Subclasses that override should call this method to
* dispatch the change event.</p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function autoSwitchChangeEnd():void
{
dispatchEvent(new DynamicStreamEvent(DynamicStreamEvent.AUTO_SWITCH_CHANGE, false, false, false, _autoSwitch));
}
/**
* Called immediately before the <code>switching</code> property is changed.
* <p>Subclasses can override this method to communicate the change to the media.</p>
* @param newSwitching New value for the <code>switching</code> property.
* @param index The index of the stream to switch to.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function switchingChangeStart(newSwitching:Boolean, index:int):void
{
}
/**
* Called just after the <code>switching</code> property has changed.
* Dispatches the change event.
*
* <p>Subclasses that override should call this method to
* dispatch the change event.</p>
*
* @param index The index of the switched-to stream.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function switchingChangeEnd(index:int):void
{
dispatchEvent
( new DynamicStreamEvent
( DynamicStreamEvent.SWITCHING_CHANGE
, false
, false
, switching
)
);
}
/**
* Called immediately before the <code>maxAllowedIndex</code> property is changed.
* <p>Subclasses can override this method to communicate the change to the media.</p>
* @param newIndex New value for the <code>maxAllowedIndex</code> property.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function maxAllowedIndexChangeStart(newIndex:int):void
{
}
/**
* Called just after the <code>maxAllowedIndex</code> property has changed.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function maxAllowedIndexChangeEnd():void
{
}
private var _autoSwitch:Boolean;
private var _currentIndex:int = 0;
private var _maxAllowedIndex:int = 0;
private var _numDynamicStreams:int;
private var _switching:Boolean;
}
}
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.