Chromium Code Reviews| 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 | |
| 17 window.navigator.presentation.defaultRequest = startSessionRequest; | |
| 18 window.navigator.presentation.defaultRequest.onconnectionavailable = function(e) | |
| 19 { | |
| 20 defaultRequestSessionId = e.connection.id; | |
| 21 }; | |
| 22 | 13 |
| 23 /** | 14 /** |
| 24 * Waits until one device is available. | 15 * Initialize Cast APIs. |
| 25 */ | 16 */ |
| 26 function waitUntilDeviceAvailable() { | 17 function initialize() { |
| 27 startSessionRequest.getAvailability(presentationUrl).then( | 18 // Load Cast APIs |
| 28 function(availability) { | 19 if (!chrome.cast || !chrome.cast.isAvailable) { |
|
mark a. foltz
2016/04/04 20:25:07
Use the API-provided callback to start initializat
Lei Lei
2016/04/13 00:22:43
Done.
| |
| 29 console.log('availability ' + availability.value + '\n'); | 20 setTimeout(startSession, 1000); |
|
mark a. foltz
2016/04/04 20:25:06
startSession is no longer defined?
Lei Lei
2016/04/13 00:22:43
Done.
| |
| 30 if (availability.value) { | 21 return; |
| 31 console.log('device available'); | 22 } else { |
| 32 } else { | 23 console.info('Initializing API'); |
| 33 availability.onchange = function(newAvailability) { | 24 var sessionRequest = new chrome.cast.SessionRequest( |
| 34 if (newAvailability) | 25 chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID); |
| 35 console.log('got new availability'); | 26 var apiConfig = new chrome.cast.ApiConfig( |
| 36 } | 27 sessionRequest, |
| 37 } | 28 null, // session listener |
| 38 }).catch(function(){ | 29 function(availability) { // receiver listener |
| 39 console.log('error'); | 30 console.info('Recevier listener: ' + JSON.stringify(availability)); |
| 31 initialized = true; | |
| 32 }); | |
| 33 chrome.cast.initialize( | |
| 34 apiConfig, | |
| 35 function() { // Successful callback | |
| 36 console.info('Initialize successfully'); | |
| 37 }, | |
| 38 function(error) { // Error callback | |
| 39 console.info('Initialize failed, error: ' + JSON.stringify(error)); | |
| 40 }); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * Start a new session for flinging scenario. | |
| 46 */ | |
| 47 function startFlingingSession() { | |
| 48 console.info('Starting Session'); | |
| 49 chrome.cast.requestSession( | |
| 50 function(session) { // Request session successful callback | |
| 51 console.info('Request session successfully'); | |
| 52 currentSession = session; | |
| 53 }, | |
| 54 function(error) { // Request session Error callback | |
| 55 console.info('Request session failed, error: ' + JSON.stringify(error)); | |
| 40 }); | 56 }); |
| 41 } | 57 } |
| 42 | 58 |
| 43 /** | 59 /** |
| 44 * Starts session. | 60 * Loads the specific video on Chromecast. |
| 45 */ | 61 */ |
| 46 function startSession() { | 62 function loadMedia(mediaUrl) { |
| 47 startSessionPromise = startSessionRequest.start(); | 63 if (!currentSession) { |
| 48 console.log('start session'); | 64 console.warn('Cannot load media without a live session'); |
| 65 } | |
| 66 console.info('loading ' + mediaUrl); | |
| 67 var mediaInfo = new chrome.cast.media.MediaInfo(mediaUrl, 'video/mp4'); | |
| 68 var request = new chrome.cast.media.LoadRequest(mediaInfo); | |
| 69 request.autoplay = true; | |
| 70 request.currentTime = 0; | |
| 71 currentSession.loadMedia(request, | |
| 72 function(media) { | |
| 73 console.info('Load media successfully'); | |
| 74 currentMedia = media; | |
| 75 }, | |
| 76 function(error) { // Error callback | |
| 77 console.info('Load media failed, error: ' + JSON.stringify(error)); | |
| 78 }); | |
| 49 } | 79 } |
| 50 | 80 |
| 51 /** | 81 /** |
| 52 * Checks if the session has been started successfully. | 82 * Stops current session. |
| 53 */ | 83 */ |
| 54 function checkSession() { | 84 function stopSession() { |
| 55 if (!startSessionPromise) { | 85 if (currentSession) { |
| 56 sendResult(false, 'Did not attempt to start session'); | 86 currentSession.stop( |
| 57 } else { | 87 function() { |
| 58 startSessionPromise.then(function(session) { | 88 console.info('Stop session successfully'); |
| 59 if(!session) { | 89 currentSession = null; |
| 60 console.log('Failed to start session'); | 90 }, |
| 61 } else { | 91 function(error) { // Error callback |
| 62 // set the new session | 92 console.info('Stop session failed, error: ' + JSON.stringify(error)); |
| 63 startedSession = session; | 93 }); |
| 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 } | 94 } |
| 72 } | 95 } |
| 73 | |
| 74 /** | |
| 75 * Terminates current session. | |
| 76 */ | |
| 77 function terminateSession() { | |
| 78 if (startedSession) { | |
| 79 startedSession.terminate(); | |
| 80 } | |
| 81 } | |
| 82 | |
| OLD | NEW |