| OLD | NEW |
| 1 /** | 1 /** |
| 2 * Copyright 2016 The Chromium Authors. All rights reserved. | 2 * Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 * | 5 * |
| 6 * @fileoverview Common APIs for presentation integration tests. | 6 * @fileoverview Common APIs for media router performance tests. |
| 7 * | 7 * |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 var startSessionPromise = null; | 10 var initialized = false; |
| 11 var startedSession = null; | 11 var currentSession = null; |
| 12 var reconnectedSession = null; | 12 var currentMedia = null; |
| 13 var presentationUrl = "http://www.google.com/#__testprovider__=true"; | |
| 14 var startSessionRequest = new PresentationRequest(presentationUrl); | |
| 15 var defaultRequestSessionId = null; | |
| 16 | 13 |
| 17 window.navigator.presentation.defaultRequest = startSessionRequest; | 14 |
| 18 window.navigator.presentation.defaultRequest.onconnectionavailable = function(e) | 15 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { |
| 19 { | 16 if (loaded) { |
| 20 defaultRequestSessionId = e.connection.id; | 17 initializeCastApi(); |
| 21 }; | 18 } else { |
| 19 console.log(errorInfo); |
| 20 } |
| 21 } |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Waits until one device is available. | 24 * Initialize Cast APIs. |
| 25 */ | 25 */ |
| 26 function waitUntilDeviceAvailable() { | 26 function initializeCastApi() { |
| 27 startSessionRequest.getAvailability(presentationUrl).then( | 27 // Load Cast APIs |
| 28 function(availability) { | 28 console.info('Initializing API'); |
| 29 console.log('availability ' + availability.value + '\n'); | 29 var sessionRequest = new chrome.cast.SessionRequest( |
| 30 if (availability.value) { | 30 chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID); |
| 31 console.log('device available'); | 31 var apiConfig = new chrome.cast.ApiConfig( |
| 32 } else { | 32 sessionRequest, |
| 33 availability.onchange = function(newAvailability) { | 33 null, // session listener |
| 34 if (newAvailability) | 34 function(availability) { // receiver listener |
| 35 console.log('got new availability'); | 35 console.info('Receiver listener: ' + JSON.stringify(availability)); |
| 36 } | 36 initialized = true; |
| 37 } | 37 }); |
| 38 }).catch(function(){ | 38 chrome.cast.initialize( |
| 39 console.log('error'); | 39 apiConfig, |
| 40 function() { // Successful callback |
| 41 console.info('Initialize successfully'); |
| 42 }, |
| 43 function(error) { // Error callback |
| 44 console.error('Initialize failed, error: ' + JSON.stringify(error)); |
| 40 }); | 45 }); |
| 41 } | 46 } |
| 42 | 47 |
| 43 /** | 48 /** |
| 44 * Starts session. | 49 * Start a new session for flinging scenario. |
| 45 */ | 50 */ |
| 46 function startSession() { | 51 function startFlingingSession() { |
| 47 startSessionPromise = startSessionRequest.start(); | 52 console.info('Starting Session'); |
| 48 console.log('start session'); | 53 chrome.cast.requestSession( |
| 54 function(session) { // Request session successful callback |
| 55 console.info('Request session successfully'); |
| 56 currentSession = session; |
| 57 }, |
| 58 function(error) { // Request session Error callback |
| 59 console.error('Request session failed, error: ' + JSON.stringify(error)); |
| 60 }); |
| 49 } | 61 } |
| 50 | 62 |
| 51 /** | 63 /** |
| 52 * Checks if the session has been started successfully. | 64 * Loads the specific video on Chromecast. |
| 65 * |
| 66 * @param {string} mediaUrl the url which points to a mp4 video. |
| 53 */ | 67 */ |
| 54 function checkSession() { | 68 function loadMedia(mediaUrl) { |
| 55 if (!startSessionPromise) { | 69 if (!currentSession) { |
| 56 sendResult(false, 'Did not attempt to start session'); | 70 console.warn('Cannot load media without a live session'); |
| 57 } else { | |
| 58 startSessionPromise.then(function(session) { | |
| 59 if(!session) { | |
| 60 console.log('Failed to start session'); | |
| 61 } else { | |
| 62 // set the new session | |
| 63 startedSession = session; | |
| 64 console.log('Session has been started'); | |
| 65 } | |
| 66 }).catch(function() { | |
| 67 // terminate old session if exists | |
| 68 terminateSession(); | |
| 69 console.log('Failed to start session'); | |
| 70 }) | |
| 71 } | 71 } |
| 72 console.info('loading ' + mediaUrl); |
| 73 var mediaInfo = new chrome.cast.media.MediaInfo(mediaUrl, 'video/mp4'); |
| 74 var request = new chrome.cast.media.LoadRequest(mediaInfo); |
| 75 request.autoplay = true; |
| 76 request.currentTime = 0; |
| 77 currentSession.loadMedia(request, |
| 78 function(media) { |
| 79 console.info('Load media successfully'); |
| 80 currentMedia = media; |
| 81 }, |
| 82 function(error) { // Error callback |
| 83 console.error('Load media failed, error: ' + JSON.stringify(error)); |
| 84 }); |
| 72 } | 85 } |
| 73 | 86 |
| 74 /** | 87 /** |
| 75 * Terminates current session. | 88 * Stops current session. |
| 76 */ | 89 */ |
| 77 function terminateSession() { | 90 function stopSession() { |
| 78 if (startedSession) { | 91 if (currentSession) { |
| 79 startedSession.terminate(); | 92 currentSession.stop( |
| 93 function() { |
| 94 console.info('Stop session successfully'); |
| 95 currentSession = null; |
| 96 }, |
| 97 function(error) { // Error callback |
| 98 console.error('Stop session failed, error: ' + JSON.stringify(error)); |
| 99 }); |
| 80 } | 100 } |
| 81 } | 101 } |
| 82 | |
| OLD | NEW |