| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('media', function() { |
| 6 |
| 7 var audioStreams = new media.ItemStore; |
| 8 var audioStreamDiv; |
| 9 |
| 10 /** |
| 11 * Write the set of audio streams to the DOM. |
| 12 */ |
| 13 printAudioStreams = function() { |
| 14 |
| 15 /** |
| 16 * Render a single stream as a <li>. |
| 17 * |
| 18 * @param {Object} stream The stream to render. |
| 19 * @return {HTMLElement} A <li> containing the stream information. |
| 20 */ |
| 21 printStream = function(stream) { |
| 22 var out = document.createElement('li'); |
| 23 out.id = stream.id; |
| 24 out.className = 'audio-stream'; |
| 25 out.setAttribute('status', stream.status); |
| 26 |
| 27 out.innerHTML += 'Audio stream ' + stream.id.split('.')[1]; |
| 28 out.innerHTML += ' is ' + (stream.playing ? 'playing' : 'paused'); |
| 29 out.innerHTML += ' at ' + Math.round(stream.volume * 100) + '% volume.'; |
| 30 return out; |
| 31 }; |
| 32 |
| 33 var out = document.createElement('ul'); |
| 34 audioStreams.map(printStream).forEach(function(s) { out.appendChild(s) }); |
| 35 |
| 36 if (audioStreamDiv.firstChild) |
| 37 audioStreamDiv.removeChild(audioStreamDiv.firstChild); |
| 38 audioStreamDiv.appendChild(out); |
| 39 }; |
| 40 |
| 41 /** |
| 42 * Initialize variables and ask MediaInternals for all its data. |
| 43 */ |
| 44 initialize = function() { |
| 45 audioStreamDiv = document.getElementById('audio-streams'); |
| 46 // Get information about all currently active media. |
| 47 chrome.send('getEverything'); |
| 48 }; |
| 49 |
| 50 return { |
| 51 printAudioStreams: printAudioStreams, |
| 52 audioStreams: audioStreams, |
| 53 initialize: initialize |
| 54 }; |
| 55 |
| 56 }); |
| 57 |
| 58 /** |
| 59 * Receiving data for an audio stream. |
| 60 * Add it to audioStreams and refresh. |
| 61 * |
| 62 * @param {Object} stream JSON representation of an audio stream. |
| 63 */ |
| 64 onAudioUpdate = function(stream) { |
| 65 media.audioStreams.addItem(stream); |
| 66 media.printAudioStreams(); |
| 67 }; |
| 68 |
| 69 /** |
| 70 * Receiving all data. |
| 71 * Add it all to the appropriate stores and refresh. |
| 72 * |
| 73 * @param {Object} stuff JSON containing lists of data. |
| 74 * @param {Object} stuff.audio_streams A dictionary of audio streams. |
| 75 */ |
| 76 onReceiveEverything = function(stuff) { |
| 77 media.audioStreams.addItems(stuff.audio_streams); |
| 78 media.printAudioStreams(); |
| 79 }; |
| 80 |
| 81 /** |
| 82 * Removing an item from the appropriate store. |
| 83 * |
| 84 * @param {string} id The id of the item to be removed, in the format |
| 85 * "item_type.identifying_info". |
| 86 */ |
| 87 onItemDeleted = function(id) { |
| 88 var type = id.split('.')[0]; |
| 89 switch (type) { |
| 90 case 'audio_streams': |
| 91 media.audioStreams.removeItem(id); |
| 92 media.printAudioStreams(); |
| 93 break; |
| 94 } |
| 95 }; |
| 96 |
| 97 /** |
| 98 * Initialize everything once we have access to the DOM. |
| 99 */ |
| 100 window.onload = function() { |
| 101 media.initialize(); |
| 102 }; |
| OLD | NEW |