Menu

[r43]: / trunk / libs / OSMF / org / osmf / net / drm / DRMServices.as  Maximize  Restore  History

Download this file

531 lines (483 with data), 15.9 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*****************************************************
*
* 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.net.drm
{
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.StatusEvent;
import flash.utils.ByteArray;
import org.osmf.events.DRMEvent;
import org.osmf.events.MediaError;
import org.osmf.events.MediaErrorCodes;
import org.osmf.traits.DRMState;
import org.osmf.utils.OSMFStrings;
CONFIG::FLASH_10_1
{
import flash.events.DRMAuthenticationCompleteEvent;
import flash.events.DRMAuthenticationErrorEvent;
import flash.events.DRMErrorEvent;
import flash.events.DRMStatusEvent;
import flash.net.drm.AuthenticationMethod;
import flash.net.drm.DRMContentData;
import flash.net.drm.DRMManager;
import flash.net.drm.DRMVoucher;
import flash.net.drm.LoadVoucherSetting;
import flash.net.drm.VoucherAccessInfo;
import flash.system.SystemUpdater;
import flash.system.SystemUpdaterType;
}
[ExcludeClass]
/**
* Dispatched when either anonymous or credential-based authentication is needed in order
* to playback the media.
*
* @eventType org.osmf.events.DRMEvent.DRM_STATE_CHANGE
*/
[Event(name='drmStateChange', type='org.osmf.events.DRMEvent')]
/**
* @private
*
* The DRMServices class is a utility class to adapt the Flash Player's DRM
* to the OSMF-style DRM API. DRMServices handles triggering updates to
* the DRM subsystem, as well as triggering the appropriate events when
* authentication is needed, complete, or failed.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
internal class DRMServices extends EventDispatcher
{
CONFIG::FLASH_10_1
{
/**
* Constructor.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*
*/
public function DRMServices()
{
drmManager = DRMManager.getDRMManager();
}
/**
* The current state of the DRM for this media. The states are explained
* in the DRMState enumeration in the org.osmf.drm package.
* @see DRMState
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get drmState():String
{
return _drmState;
}
/**
* The metadata property is specific to the DRM for the Flash Player. Once set, authentication
* and voucher retrieval is started. This method may trigger an update to the DRM subsystem. Metadata
* forms the basis for content data.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function set drmMetadata(value:Object):void
{
lastToken = null;
if (value is DRMContentData)
{
drmContentData = value as DRMContentData;
retrieveVoucher();
}
else
{
try
{
drmContentData = new DRMContentData(value as ByteArray);
retrieveVoucher();
}
catch (argError:ArgumentError) // DRM ContentData is invalid
{
updateDRMState
( DRMState.AUTHENTICATION_ERROR,
new MediaError(argError.errorID, "DRMContentData invalid")
);
}
catch (error:IllegalOperationError)
{
function onComplete(event:Event):void
{
updater.removeEventListener(Event.COMPLETE, onComplete);
drmMetadata = value;
}
update(SystemUpdaterType.DRM);
updater.addEventListener(Event.COMPLETE, onComplete);
}
}
}
public function get drmMetadata():Object
{
return drmContentData;
}
/**
* Authenticates the media. Can be used for both anonymous and credential-based
* authentication.
*
* @param username The username. Should be null for anonymous authentication.
* @param password The password. Should be null for anonymous authentication.
*
* @throws IllegalOperationError If the metadata hasn't been set.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function authenticate(username:String = null, password:String = null):void
{
if (drmContentData == null)
{
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.DRM_METADATA_NOT_SET));
}
drmManager.addEventListener(DRMAuthenticationErrorEvent.AUTHENTICATION_ERROR, authError);
drmManager.addEventListener(DRMAuthenticationCompleteEvent.AUTHENTICATION_COMPLETE, authComplete);
if (password == null && username == null)
{
retrieveVoucher();
}
else
{
drmManager.authenticate(drmContentData.serverURL, drmContentData.domain, username, password);
}
}
/**
* Authenticates the media using an object which serves as a token. Can be used
* for both anonymous and credential-based authentication.
*
* @param token The token to use for authentication.
*
* @throws IllegalOperationError If the metadata hasn't been set.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function authenticateWithToken(token:Object):void
{
if (drmContentData == null)
{
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.DRM_METADATA_NOT_SET));
}
drmManager.setAuthenticationToken(drmContentData.serverURL, drmContentData.domain, token as ByteArray);
retrieveVoucher();
}
/**
* Returns the start date for the playback window. Returns null if authentication
* hasn't taken place.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get startDate():Date
{
if (voucher != null)
{
return voucher.playbackTimeWindow ? voucher.playbackTimeWindow.startDate : voucher.voucherStartDate;
}
else
{
return null;
}
}
/**
* Returns the end date for the playback window. Returns null if authentication
* hasn't taken place.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get endDate():Date
{
if (voucher != null)
{
return voucher.playbackTimeWindow ? voucher.playbackTimeWindow.endDate : voucher.voucherEndDate;
}
else
{
return null;
}
}
/**
* Returns the length of the playback window, in seconds. Returns NaN if
* authentication hasn't taken place.
*
* Note that this property will generally be the difference between startDate
* and endDate, but is included as a property because there may be times where
* the duration is known up front, but the start or end dates are not (e.g. a
* one week rental).
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get period():Number
{
if (voucher != null)
{
return voucher.playbackTimeWindow ? voucher.playbackTimeWindow.period : (voucher.voucherEndDate && voucher.voucherStartDate) ? (voucher.voucherEndDate.time - voucher.voucherStartDate.time)/1000 : 0;
}
else
{
return NaN;
}
}
/**
* @private
*
* Signals failures from the DRMsubsystem not captured though the
* DRMServices class.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function inlineDRMFailed(error:MediaError):void
{
updateDRMState(DRMState.AUTHENTICATION_ERROR, error);
}
/**
* @private
* Signals DRM is available, taken from the inline netstream APIs.
* Assumes the voucher is available.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function inlineOnVoucher(event:DRMStatusEvent):void
{
drmContentData = event.contentData;
onVoucherLoaded(event);
}
/**
* @private
*
* Triggers and update of the DRM subsystem.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function update(type:String):SystemUpdater
{
updateDRMState(DRMState.DRM_SYSTEM_UPDATING);
if (updater == null) //An update hasn't been triggered
{
updater = new SystemUpdater();
//If there is an update already happening, just wait for it to finish.
toggleErrorListeners(updater, true);
updater.update(type);
}
else
{
//If there is an update already happening, just wait for it to finish.
toggleErrorListeners(updater, true);
}
return updater;
}
// Internals
//
/**
* Downloads the voucher for the metadata specified.
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function retrieveVoucher():void
{
updateDRMState(DRMState.AUTHENTICATING);
drmManager.addEventListener(DRMErrorEvent.DRM_ERROR, onDRMError);
drmManager.addEventListener(DRMStatusEvent.DRM_STATUS, onVoucherLoaded);
drmManager.loadVoucher(drmContentData, LoadVoucherSetting.ALLOW_SERVER);
}
private function onVoucherLoaded(event:DRMStatusEvent):void
{
if (event.contentData == drmContentData)
{
var now:Date = new Date();
if (event.voucher &&
(
( event.voucher.voucherEndDate == null
|| event.voucher.voucherEndDate.time >= now.time)
&& ( event.voucher.voucherStartDate == null
|| event.voucher.voucherStartDate.time <= now.time)
)
)
{
this.voucher = event.voucher;
removeEventListeners();
if (voucher.playbackTimeWindow == null)
{
updateDRMState(DRMState.AUTHENTICATION_COMPLETE, null, voucher.voucherStartDate, voucher.voucherEndDate, period, lastToken);
}
else
{
updateDRMState(DRMState.AUTHENTICATION_COMPLETE, null, voucher.playbackTimeWindow.startDate, voucher.playbackTimeWindow.endDate, voucher.playbackTimeWindow.period, lastToken);
}
}
else //Only force refresh if voucher was good, and has expired (local voucher).
{
forceRefreshVoucher();
}
}
}
private function forceRefreshVoucher():void
{
drmManager.loadVoucher(drmContentData, LoadVoucherSetting.FORCE_REFRESH);
}
private function onDRMError(event:DRMErrorEvent):void
{
if (event.contentData == drmContentData) //Ensure this event is for our data.
{
switch(event.errorID)
{
case DRM_CONTENT_NOT_YET_VALID:
forceRefreshVoucher();
break;
case DRM_NEEDS_AUTHENTICATION:
updateDRMState(DRMState.AUTHENTICATION_NEEDED, null, null, null, 0, null, event.contentData.serverURL );
break;
default:
removeEventListeners();
updateDRMState(DRMState.AUTHENTICATION_ERROR, new MediaError(event.errorID, event.text));
break;
}
}
}
private function removeEventListeners():void
{
drmManager.removeEventListener(DRMErrorEvent.DRM_ERROR, onDRMError);
drmManager.removeEventListener(DRMStatusEvent.DRM_STATUS, onVoucherLoaded);
}
private function authComplete(event:DRMAuthenticationCompleteEvent):void
{
drmManager.removeEventListener(DRMAuthenticationErrorEvent.AUTHENTICATION_ERROR, authError);
drmManager.removeEventListener(DRMAuthenticationCompleteEvent.AUTHENTICATION_COMPLETE, authComplete);
lastToken = event.token;
retrieveVoucher();
}
private function authError(event:DRMAuthenticationErrorEvent):void
{
drmManager.removeEventListener(DRMAuthenticationErrorEvent.AUTHENTICATION_ERROR, authError);
drmManager.removeEventListener(DRMAuthenticationCompleteEvent.AUTHENTICATION_COMPLETE, authComplete);
updateDRMState(DRMState.AUTHENTICATION_ERROR, new MediaError(event.errorID, event.toString()));
}
private function toggleErrorListeners(updater:SystemUpdater, on:Boolean):void
{
if (on)
{
updater.addEventListener(Event.COMPLETE, onUpdateComplete);
updater.addEventListener(Event.CANCEL, onUpdateComplete);
updater.addEventListener(IOErrorEvent.IO_ERROR, onUpdateError);
updater.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUpdateError);
updater.addEventListener(StatusEvent.STATUS, onUpdateError);
}
else
{
updater.removeEventListener(Event.COMPLETE, onUpdateComplete);
updater.removeEventListener(Event.CANCEL, onUpdateComplete);
updater.removeEventListener(IOErrorEvent.IO_ERROR, onUpdateError);
updater.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onUpdateError);
updater.removeEventListener(StatusEvent.STATUS, onUpdateError);
}
}
private function onUpdateComplete(event:Event):void
{
toggleErrorListeners(updater, false);
}
private function onUpdateError(event:Event):void
{
toggleErrorListeners(updater, false);
updateDRMState(DRMState.AUTHENTICATION_ERROR, new MediaError(MediaErrorCodes.DRM_SYSTEM_UPDATE_ERROR, event.toString()));
}
private function updateDRMState(newState:String, error:MediaError = null, start:Date = null, end:Date = null, period:Number = 0 , token:Object=null, prompt:String = null ):void
{
_drmState = newState;
dispatchEvent
( new DRMEvent
( DRMEvent.DRM_STATE_CHANGE,
newState,
false,
false,
start,
end,
period,
prompt,
token,
error
)
);
}
// The flash player's internal DRM error codes
/**
* @private
*/
private static const DRM_AUTHENTICATION_FAILED:int = 3301;
/**
* @private
*/
private static const DRM_NEEDS_AUTHENTICATION:int = 3330;
/**
* @private
*/
private static const DRM_CONTENT_NOT_YET_VALID:int = 3331;
private var _drmState:String = DRMState.UNINITIALIZED;
private var lastToken:ByteArray
private var drmContentData:DRMContentData;
private var voucher:DRMVoucher;
private var drmManager:DRMManager;
//this is static, since the SystemUpdater needs to be trated as a singleton. Only one update at a time.
private static var updater:SystemUpdater;
}
}
}
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.