Player APIs (BrightScript & Ja...
...
JavaScript APIs
BrightSign Asset Pool
BrightSign Asset Pool Example
1 min
this script shows how the brightsign asset pool can be used from javascript it works from both nodejs and chromium it is not intended as a full application and it doesn’t do anything with the assets that are downloaded // mit license // // copyright (c) 2020 brightsign // // permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "software"), to deal // in the software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the software, and to permit persons to whom the software is // furnished to do so, subject to the following conditions // // the above copyright notice and this permission notice shall be included in all // copies or substantial portions of the software // // the software is provided "as is", without warranty of any kind, express or // implied, including but not limited to the warranties of merchantability, // fitness for a particular purpose and noninfringement in no event shall the // authors or copyright holders be liable for any claim, damages or other // liability, whether in an action of contract, tort or otherwise, arising from, // out of or in connection with the software or the use or other dealings in the // software // customise these settings for your environment const storagepath = "/storage/sd/"; const poolpath = storagepath + "examplepool"; const serverprefix = "https //github com/brightsign/javascript assetpoolfetcher/blob/video training/"; const fs = require('fs'); const assetpool = require("@brightsign/assetpool"); const assetpoolfiles = require("@brightsign/assetpoolfiles"); const assetfetcher = require("@brightsign/assetfetcher"); const assetrealizer = require("@brightsign/assetrealizer"); // create the asset collection array to be used by the rest of the // program the asset collection will usually come either directly or // indirectly from a server somewhere rather than being generated in // code like this function makeassetcollection() { // assets must have a name and a link everything else is // optional you can add your own properties if required for // example, the "osupdate" property here is only used by this // script let videoasset1 = { 	name "brightlink mp4", 	hash { method "sha256", 	 hex "82e5e800ff4b2608ae996b6c9fce78c0d84b82cba0da270251846324f48fb076" }, 	link serverprefix + "brightlink%201min mp4?raw=true", 	size 17219989, }; let videoasset2 = { 	name "cmscontrol mp4", 	hash { method "sha512", 	 hex "d476296b114fda612917b1568a9f7ffe6f54f3f927b2d1ecc932816e912619eeef980e1adc16d8b1d7914d3f172cd6a2ef17172ea3251ddcdbb3d151faa6818f" }, 	link serverprefix + "cms%2bcontrol%201%20min mp4?raw=true", 	size 5633877, }; // we don't want to risk actually changing the os version on the // brightsign running this example cript, so this is not a real // brightsignos file it will be realized into the root of the // storage device, but the os will fail to find the expected // header and rename it to placeholder bsfw invalid let osupdateasset = { 	name "placeholder bsfw", 	hash { method "sha1", hex "20e89c9ba0491590e3c34bc704171c0c02e643c3" }, 	link serverprefix + "placeholder bsfw?raw=true", 	size 43, 	osupdate true, }; let assetcollection = \[ 	videoasset1, 	videoasset2, 	osupdateasset, ]; return assetcollection; } // convert a progress event to a useful string for reporting function progressstring(event) { if (event currentfiletotal === undefined) { 	// if the size of the asset was not specified in the asset collection, then the total size may not be reported 	// during the fetch 	return event currentfiletransferred tostring() + " of unknown"; } else { 	return event currentfiletransferred tostring() + " of " + event currentfiletotal tostring() + " " 	 \+ (100 event currentfiletransferred / event currentfiletotal) tofixed(0) + "%"; } } // download any assets that aren't already in the pool into the pool // whilst reporting progress async function fetchassets(assetpool, assetcollection) { console log("fetch " + json stringify(assetcollection map(asset => asset name))); let assetfetcher = new assetfetcher(assetpool); assetfetcher addeventlistener("fileevent", (event) => { 	// this is called each time the fetcher has finished trying to 	// download an asset, whether successful or not it is not 	// called for any assets that are already in the pool 	console log("asset \[" + (event index + 1) tostring() + "] " 	 \+ event filename + " complete " + event responsecode tostring() + " " + event error); }); assetfetcher addeventlistener("progressevent", (event) => { 	// this is called at approximately the progress interval 	// specified in the options to indicate how far through the 	// download 	console log("asset \[" + (event index + 1) tostring() + "/" + event total tostring() + "] " + event filename 	 \+ " progress " + progressstring(event)); }); const fetchoptions = { 	// receive asset progress events about every five seconds 	progressinterval 5, 	// try to download each asset three times before giving up 	fileretrycount 3, 	// give up if we fail to download at least 1024 bytes in each 	// ten second period 	minimumtransferrate { bytespersecond 1024, periodinseconds 10 }, }; try { 	await assetfetcher start(assetcollection, fetchoptions); } catch (err) { 	console log("fetch failed " + err message); 	throw(err); } } // in order to make use of an asset from the pool you need to look up // its pool filename so you can refer to it there async function useassets(assetpool, assetcollection) { let files = new assetpoolfiles(assetpool, assetcollection); for (const filename of \[ 'brightlink mp4', 'cmscontrol mp4' ]) { 	const path = await files getpath(filename); 	console log("asset " + filename + " is at " + path); } } // some files need to appear in the filesystem outside the pool for // example, brightsignos update files must be written to the root of // a storage device for them to be found realizing will copy files, // so can be slow on large files async function realizeassets(assetpool, assetcollection) { let realizer = new assetrealizer(assetpool, storagepath); // we only want to realize the files that we have to const assetstorealize = assetcollection filter(asset => asset osupdate); console log("realize " + json stringify(assetstorealize map(asset => asset name))); await realizer realize(assetstorealize); } function ensuredirectoryexists(path) { try { 	fs mkdirsync(path); } catch (err) { 	if (err code != 'eexist') 	 throw(err); } } function exceptiontostring(err) { if (err instanceof error) return err name + " " + err message; else if (typeof(err) === "string") return err; else return json stringify(err); } async function runexample() { console log("start"); ensuredirectoryexists(poolpath); // only one assetpool instance should be created for a given pool // path having multiple instances risks them disagreeing over // which assets are protected during pruning let assetpool = new assetpool(poolpath); // don't let the pool grow any larger than 500mib await assetpool setmaximumpoolsize(500 1024 1024); // don't let free space on the storage device fall below 100mib await assetpool reservestorage(100 1024 1024); const assetcollection = makeassetcollection(); // we need to stop the fetcher from pruning any of the assets we // currently care about in order to make space for fetching new // assets or realizing existing ones assets are protected until // the assetpool instance is destroyed or unprotectassets is // called for the same name await assetpool protectassets("collection1", assetcollection); await fetchassets(assetpool, assetcollection); // in this case any failure to fetch the assets will cause // fetchassets to have thrown an exception, so we won't get this // far however, in a larger script it may be more convenient to // call areassetsready to determine whether the asset collection // is ready for use if (await assetpool areassetsready(assetcollection)) { 	await useassets(assetpool, assetcollection); 	await realizeassets(assetpool, assetcollection); } else { 	console log("assets were not downloaded successfully"); } } runexample() then(() => { 	console log("complete"); 	process exit(0); }) catch((err) => { 	console log("failed " + exceptiontostring(err)); 	process exit(1); });