@@ -98,6 +98,7 @@ var connectsdk = (function () {
98
98
statics : {
99
99
PlatformType : {
100
100
DEFAULT : "Default" ,
101
+ AIRPLAY : "AirPlay" ,
101
102
GOOGLE_CAST : "GoogleCast" ,
102
103
WEBOS_NATIVE : "WebOSNative" ,
103
104
WEBOS_WEB_APP : "WebOSWebApp"
@@ -181,7 +182,9 @@ var connectsdk = (function () {
181
182
var userAgent = navigator . userAgent . toLowerCase ( ) ;
182
183
this . platformType = ConnectManager . PlatformType . DEFAULT ;
183
184
184
- if ( userAgent . indexOf ( 'crkey' ) > 0 && cast != null )
185
+ if ( userAgent . indexOf ( 'iphone os' ) >= 0 )
186
+ this . platformType = ConnectManager . PlatformType . AIRPLAY ;
187
+ else if ( userAgent . indexOf ( 'crkey' ) > 0 && cast != null )
185
188
this . platformType = ConnectManager . PlatformType . GOOGLE_CAST ;
186
189
else if ( userAgent . indexOf ( 'tv' ) >= 0 && ( userAgent . indexOf ( 'webos' ) >= 0 ) || ( userAgent . indexOf ( 'web0s' ) >= 0 ) )
187
190
{
@@ -213,18 +216,9 @@ var connectsdk = (function () {
213
216
broadcastMessage : nop
214
217
} ;
215
218
216
- // webOS
217
- var WebOSCommon = {
218
- interactive : true ,
219
- init : function ( ) {
220
- window . addEventListener ( "keydown" , this . handleKeyDown . bind ( this ) ) ;
221
- this . on ( ConnectManager . EventType . STATUS , this . handleMediaStatusUpdate . bind ( this ) ) ;
219
+ // Base media player (JSON media playback & control commands)
222
220
223
- this . webOSAppChannels = new WebOSAppChannels ( ) ;
224
- this . webOSAppChannels . on ( 'message' , this . handleMessage . bind ( this ) ) ;
225
- this . webOSAppChannels . on ( 'ready' , this . handleReady . bind ( this ) ) ;
226
- this . webOSAppChannels . start ( ) ;
227
- } ,
221
+ var BaseMediaPlayer = {
228
222
229
223
onLoadImage : function ( image ) {
230
224
var imageElement = this . imageElement ;
@@ -248,101 +242,6 @@ var connectsdk = (function () {
248
242
}
249
243
} ,
250
244
251
- sendMessage : function ( to , message ) {
252
- this . webOSAppChannels . sendMessage ( to , message ) ;
253
- } ,
254
-
255
- broadcastMessage : function ( message ) {
256
- this . webOSAppChannels . broadcastMessage ( message ) ;
257
- } ,
258
-
259
- handleKeyDown : function ( evt ) {
260
- if ( ! this . mediaElement ) {
261
- return ;
262
- }
263
-
264
- switch ( evt . keyCode ) {
265
- case 415 : // PLAY
266
- console . log ( this . name + " :: play command received" ) ;
267
- this . mediaElement . play ( ) ;
268
- this . emit ( ConnectManager . EventType . PLAY ) ;
269
- break ;
270
-
271
- case 19 : // PAUSE
272
- console . log ( this . name + " :: pause command received" ) ;
273
- this . mediaElement . pause ( ) ;
274
- this . emit ( ConnectManager . EventType . PAUSE ) ;
275
- break ;
276
-
277
- case 413 : // STOP
278
- console . log ( this . name + " :: stop command received" ) ;
279
- this . emit ( ConnectManager . EventType . STOP ) ;
280
- break ;
281
- }
282
- } ,
283
-
284
- handleMessage : function ( msgData ) {
285
- var contentType = msgData . message . contentType ;
286
-
287
- switch ( contentType ) {
288
- case "connectsdk.mediaCommand" :
289
- this . handleMediaCommand ( msgData ) ;
290
- break ;
291
-
292
- case "connectsdk.serviceCommand" :
293
- this . handleServiceCommand ( msgData ) ;
294
- break ;
295
-
296
- default :
297
- this . emit ( ConnectManager . EventType . MESSAGE , msgData ) ;
298
- }
299
- } ,
300
-
301
- handleMediaCommand : function ( msgData ) {
302
- var mediaCommand = msgData . message . mediaCommand ;
303
- if ( ! mediaCommand ) {
304
- return ;
305
- }
306
-
307
- var commandType = mediaCommand . type ;
308
- console . log ( 'processing mediaCommand ' + JSON . stringify ( mediaCommand ) + ' of type ' + commandType ) ;
309
-
310
- switch ( commandType ) {
311
- case "displayImage" :
312
- this . handleDisplayImage ( msgData ) ;
313
- break ;
314
- case "getDuration" :
315
- this . handleGetDuration ( msgData ) ;
316
- break ;
317
- case "getPosition" :
318
- this . handleGetPosition ( msgData ) ;
319
- break ;
320
- case "playMedia" :
321
- this . handlePlayMedia ( msgData ) ;
322
- break ;
323
- case "seek" :
324
- this . handleSeek ( msgData ) ;
325
- break ;
326
- }
327
- } ,
328
-
329
- handleServiceCommand : function ( msgData ) {
330
- var serviceCommand = msgData . message . serviceCommand ;
331
- if ( ! serviceCommand ) {
332
- return ;
333
- }
334
-
335
- var commandType = serviceCommand . type ;
336
- console . log ( 'processing serviceCommand ' + JSON . stringify ( serviceCommand ) + ' of type ' + commandType ) ;
337
-
338
- switch ( commandType ) {
339
- case "close" :
340
- // this is a hack to circumvent the fact that window.close() doesn't work with the webOS app type
341
- window . open ( window . location , '_self' ) . close ( ) ;
342
- break ;
343
- }
344
- } ,
345
-
346
245
handleDisplayImage : function ( msgData ) {
347
246
var from = msgData . from ;
348
247
var mediaCommand = msgData . message . mediaCommand ;
@@ -409,7 +308,6 @@ var connectsdk = (function () {
409
308
if ( playState == null )
410
309
return ;
411
310
412
- // TODO: add to id here
413
311
this . broadcastMessage ( {
414
312
contentType : 'connectsdk.mediaEvent' ,
415
313
mediaEvent : {
@@ -441,16 +339,182 @@ var connectsdk = (function () {
441
339
} ,
442
340
443
341
handleSeek : function ( msgData ) {
444
- var position = msgData . message . mediaCommand . position ;
342
+ var from = msgData . from ;
343
+ var mediaCommand = msgData . message . mediaCommand ;
344
+ var commandType = mediaCommand . type ;
345
+ var requestId = mediaCommand . requestId ;
346
+ var position = mediaCommand . position ;
347
+
445
348
if ( position ) {
446
- var requestId = msgData . message . mediaCommand . requestId ;
447
349
var mediaElement = this . mediaElement ;
448
350
mediaElement && ( mediaElement . currentTime = position ) ;
449
- this . handleMediaStatusUpdate ( requestId ) ;
351
+ this . handleMediaStatusUpdate ( - 1 ) ;
352
+ }
353
+
354
+ this . sendMessage ( from , {
355
+ contentType : 'connectsdk.mediaCommandResponse' ,
356
+ mediaCommandResponse : {
357
+ type : commandType ,
358
+ requestId : requestId
359
+ }
360
+ } ) ;
361
+ } ,
362
+
363
+ handlePlay : function ( msgData ) {
364
+ var from = msgData . from ;
365
+ var mediaCommand = msgData . message . mediaCommand ;
366
+ var commandType = mediaCommand . type ;
367
+ var requestId = mediaCommand . requestId ;
368
+
369
+ var mediaElement = this . mediaElement ;
370
+ mediaElement && mediaElement . play ( ) ;
371
+
372
+ this . sendMessage ( from , {
373
+ contentType : 'connectsdk.mediaCommandResponse' ,
374
+ mediaCommandResponse : {
375
+ type : commandType ,
376
+ requestId : requestId
377
+ }
378
+ } ) ;
379
+ } ,
380
+
381
+ handlePause : function ( msgData ) {
382
+ var from = msgData . from ;
383
+ var mediaCommand = msgData . message . mediaCommand ;
384
+ var commandType = mediaCommand . type ;
385
+ var requestId = mediaCommand . requestId ;
386
+
387
+ var mediaElement = this . mediaElement ;
388
+ mediaElement && mediaElement . pause ( ) ;
389
+
390
+ this . sendMessage ( from , {
391
+ contentType : 'connectsdk.mediaCommandResponse' ,
392
+ mediaCommandResponse : {
393
+ type : commandType ,
394
+ requestId : requestId
395
+ }
396
+ } ) ;
397
+ } ,
398
+
399
+ handleMessage : function ( msgData ) {
400
+ var contentType = null ;
401
+
402
+ if ( msgData != null && msgData . message != null )
403
+ contentType = msgData . message . contentType ;
404
+
405
+ switch ( contentType ) {
406
+ case "connectsdk.mediaCommand" :
407
+ this . handleMediaCommand ( msgData ) ;
408
+ break ;
409
+
410
+ case "connectsdk.serviceCommand" :
411
+ this . handleServiceCommand ( msgData ) ;
412
+ break ;
413
+
414
+ default :
415
+ this . emit ( ConnectManager . EventType . MESSAGE , msgData ) ;
416
+ }
417
+ } ,
418
+
419
+ handleMediaCommand : function ( msgData ) {
420
+ var mediaCommand = msgData . message . mediaCommand ;
421
+ if ( ! mediaCommand ) {
422
+ return ;
423
+ }
424
+
425
+ var commandType = mediaCommand . type ;
426
+ console . log ( 'processing mediaCommand ' + JSON . stringify ( mediaCommand ) + ' of type ' + commandType ) ;
427
+
428
+ switch ( commandType ) {
429
+ case "displayImage" :
430
+ this . handleDisplayImage ( msgData ) ;
431
+ break ;
432
+ case "getDuration" :
433
+ this . handleGetDuration ( msgData ) ;
434
+ break ;
435
+ case "getPosition" :
436
+ this . handleGetPosition ( msgData ) ;
437
+ break ;
438
+ case "playMedia" :
439
+ this . handlePlayMedia ( msgData ) ;
440
+ break ;
441
+ case "seek" :
442
+ this . handleSeek ( msgData ) ;
443
+ break ;
444
+ case "play" :
445
+ this . handlePlay ( msgData ) ;
446
+ break ;
447
+ case "pause" :
448
+ this . handlePause ( msgData ) ;
449
+ break ;
450
+ }
451
+ } ,
452
+
453
+ handleServiceCommand : function ( msgData ) {
454
+ var serviceCommand = msgData . message . serviceCommand ;
455
+ if ( ! serviceCommand ) {
456
+ return ;
457
+ }
458
+
459
+ var commandType = serviceCommand . type ;
460
+ console . log ( 'processing serviceCommand ' + JSON . stringify ( serviceCommand ) + ' of type ' + commandType ) ;
461
+
462
+ switch ( commandType ) {
463
+ case "close" :
464
+ // this is a hack to circumvent the fact that window.close() doesn't work with the webOS app type
465
+ window . open ( window . location , '_self' ) . close ( ) ;
466
+ break ;
450
467
}
451
468
}
452
469
} ;
453
470
471
+ // webOS
472
+ var WebOSCommon = extend ( {
473
+ interactive : true ,
474
+ init : function ( ) {
475
+ window . addEventListener ( "keydown" , this . handleKeyDown . bind ( this ) ) ;
476
+ this . on ( ConnectManager . EventType . STATUS , this . handleMediaStatusUpdate . bind ( this ) ) ;
477
+
478
+ this . webOSAppChannels = new WebOSAppChannels ( ) ;
479
+ this . webOSAppChannels . on ( 'message' , this . handleMessage . bind ( this ) ) ;
480
+ this . webOSAppChannels . on ( 'ready' , this . handleReady . bind ( this ) ) ;
481
+ this . webOSAppChannels . start ( ) ;
482
+ } ,
483
+
484
+ sendMessage : function ( to , message ) {
485
+ this . webOSAppChannels . sendMessage ( to , message ) ;
486
+ } ,
487
+
488
+ broadcastMessage : function ( message ) {
489
+ this . webOSAppChannels . broadcastMessage ( message ) ;
490
+ } ,
491
+
492
+ handleKeyDown : function ( evt ) {
493
+ if ( ! this . mediaElement ) {
494
+ return ;
495
+ }
496
+
497
+ switch ( evt . keyCode ) {
498
+ case 415 : // PLAY
499
+ console . log ( this . name + " :: play command received" ) ;
500
+ this . mediaElement . play ( ) ;
501
+ this . emit ( ConnectManager . EventType . PLAY ) ;
502
+ break ;
503
+
504
+ case 19 : // PAUSE
505
+ console . log ( this . name + " :: pause command received" ) ;
506
+ this . mediaElement . pause ( ) ;
507
+ this . emit ( ConnectManager . EventType . PAUSE ) ;
508
+ break ;
509
+
510
+ case 413 : // STOP
511
+ console . log ( this . name + " :: stop command received" ) ;
512
+ this . emit ( ConnectManager . EventType . STOP ) ;
513
+ break ;
514
+ }
515
+ }
516
+ } , BaseMediaPlayer ) ;
517
+
454
518
platforms . WebOSNative = extend ( {
455
519
name : "webOS Native Web App"
456
520
} , WebOSCommon ) ;
@@ -459,6 +523,36 @@ var connectsdk = (function () {
459
523
name : "webOS Web App"
460
524
} , WebOSCommon ) ;
461
525
526
+ // AirPlay
527
+ platforms . AirPlay = extend ( {
528
+ name : "AirPlay" ,
529
+ interactive : true ,
530
+
531
+ init : function ( ) {
532
+ this . on ( ConnectManager . EventType . STATUS , this . handleMediaStatusUpdate . bind ( this ) ) ;
533
+ } ,
534
+
535
+ sendMessage : function ( to , message ) {
536
+ // AirPlay does not have p2p support, so we'll just 'broadcast' this message
537
+ this . broadcastMessage ( message ) ;
538
+ } ,
539
+
540
+ broadcastMessage : function ( message ) {
541
+ var messageString ;
542
+
543
+ if ( typeof message == 'string' )
544
+ messageString = message ;
545
+ else
546
+ messageString = JSON . stringify ( message ) ;
547
+
548
+ var iframe = document . createElement ( 'IFRAME' ) ;
549
+ iframe . setAttribute ( 'src' , 'connectsdk://' + messageString ) ;
550
+ document . documentElement . appendChild ( iframe ) ;
551
+ iframe . parentNode . removeChild ( iframe ) ;
552
+ iframe = null ;
553
+ }
554
+ } , BaseMediaPlayer ) ,
555
+
462
556
// Google Cast
463
557
platforms . GoogleCast = {
464
558
name : "Google Cast" ,
0 commit comments