How to use FrameProducer method in devicefarmer-stf

Best JavaScript code snippet using devicefarmer-stf

stream.js

Source:stream.js Github

copy

Full Screen

...25 .dependency(require('./options'))26 .define(function(options, adb, minicap, display, screenOptions) {27 var log = logger.createLogger('device:plugins:screen:stream')28 var frameStore = new FrameStore();29 function FrameProducer(config) {30 EventEmitter.call(this)31 this.actionQueue = []32 this.runningState = FrameProducer.STATE_STOPPED33 this.desiredState = new StateQueue()34 this.output = null35 this.socket = null36 this.pid = -137 this.banner = null38 this.parser = null39 this.frameConfig = config40 this.readable = false41 this.needsReadable = false42 this.failCounter = new FailCounter(3, 10000)43 this.failCounter.on('exceedLimit', this._failLimitExceeded.bind(this))44 this.failed = false45 this.readableListener = this._readableListener.bind(this)46 }47 util.inherits(FrameProducer, EventEmitter)48 FrameProducer.STATE_STOPPED = 149 FrameProducer.STATE_STARTING = 250 FrameProducer.STATE_STARTED = 351 FrameProducer.STATE_STOPPING = 452 FrameProducer.prototype._ensureState = function() {53 if (this.desiredState.empty()) {54 return55 }56 if (this.failed) {57 log.warn('Will not apply desired state due to too many failures')58 return59 }60 switch (this.runningState) {61 case FrameProducer.STATE_STARTING:62 case FrameProducer.STATE_STOPPING:63 // Just wait.64 break65 case FrameProducer.STATE_STOPPED:66 if (this.desiredState.next() === FrameProducer.STATE_STARTED) {67 this.runningState = FrameProducer.STATE_STARTING68 this._startService().bind(this)69 .then(function(out) {70 this.output = new RiskyStream(out)71 .on('unexpectedEnd', this._outputEnded.bind(this))72 return this._readOutput(this.output.stream)73 })74 .then(function() {75 return this._waitForPid()76 })77 .then(function() {78 return this._connectService()79 })80 .then(function(socket) {81 this.parser = new FrameParser()82 this.socket = new RiskyStream(socket)83 .on('unexpectedEnd', this._socketEnded.bind(this))84 return this._readBanner(this.socket.stream)85 })86 .then(function(banner) {87 this.banner = banner88 return this._readFrames(this.socket.stream)89 })90 .then(function() {91 this.runningState = FrameProducer.STATE_STARTED92 this.emit('start')93 })94 .catch(Promise.CancellationError, function() {95 return this._stop()96 })97 .catch(function(err) {98 return this._stop().finally(function() {99 this.failCounter.inc()100 this.emit('error', err)101 })102 })103 .finally(function() {104 this._ensureState()105 })106 }107 else {108 setImmediate(this._ensureState.bind(this))109 }110 break111 case FrameProducer.STATE_STARTED:112 if (this.desiredState.next() === FrameProducer.STATE_STOPPED) {113 this.runningState = FrameProducer.STATE_STOPPING114 this._stop().finally(function() {115 this._ensureState()116 })117 }118 else {119 setImmediate(this._ensureState.bind(this))120 }121 break122 }123 }124 FrameProducer.prototype.start = function() {125 log.info('Requesting frame producer to start')126 this.desiredState.push(FrameProducer.STATE_STARTED)127 this._ensureState()128 }129 FrameProducer.prototype.stop = function() {130 log.info('Requesting frame producer to stop')131 this.desiredState.push(FrameProducer.STATE_STOPPED)132 this._ensureState()133 }134 FrameProducer.prototype.restart = function() {135 switch (this.runningState) {136 case FrameProducer.STATE_STARTED:137 case FrameProducer.STATE_STARTING:138 this.desiredState.push(FrameProducer.STATE_STOPPED)139 this.desiredState.push(FrameProducer.STATE_STARTED)140 this._ensureState()141 break142 }143 }144 FrameProducer.prototype.updateRotation = function(rotation) {145 if (this.frameConfig.rotation === rotation) {146 log.info('Keeping %d as current frame producer rotation', rotation)147 return148 }149 log.info('Setting frame producer rotation to %d', rotation)150 this.frameConfig.rotation = rotation151 this._configChanged()152 }153 FrameProducer.prototype.updateProjection = function(width, height) {154 if (this.frameConfig.virtualWidth === width &&155 this.frameConfig.virtualHeight === height) {156 log.info(157 'Keeping %dx%d as current frame producer projection', width, height)158 return159 }160 log.info('Setting frame producer projection to %dx%d', width, height)161 this.frameConfig.virtualWidth = width162 this.frameConfig.virtualHeight = height163 this._configChanged()164 }165 FrameProducer.prototype.setStaticProjection = function() {166 if (this.frameConfig.virtualWidth === FINAL_DEVICE_CAPTURE_WIDTH &&167 this.frameConfig.virtualHeight === FINAL_DEVICE_CAPTURE_HEIGHT) {168 log.info('Keeping %dx%d as current frame producer projection',169 FINAL_DEVICE_CAPTURE_WIDTH, FINAL_DEVICE_CAPTURE_HEIGHT)170 return171 }172 log.info('Setting frame producer projection to %dx%d',173 FINAL_DEVICE_CAPTURE_WIDTH,174 FINAL_DEVICE_CAPTURE_HEIGHT)175 this.frameConfig.virtualWidth = FINAL_DEVICE_CAPTURE_WIDTH176 this.frameConfig.virtualHeight = FINAL_DEVICE_CAPTURE_HEIGHT177 this._configChanged()178 }179 FrameProducer.prototype.nextFrame = function() {180 var frame = null181 var chunk182 if (this.parser) {183 while ((frame = this.parser.nextFrame()) === null) {184 chunk = this.socket.stream.read()185 if (chunk) {186 this.parser.push(chunk)187 }188 else {189 this.readable = false190 break191 }192 }193 }194 return frame195 }196 FrameProducer.prototype.needFrame = function() {197 this.needsReadable = true198 this._maybeEmitReadable()199 }200 FrameProducer.prototype._configChanged = function() {201 this.restart()202 }203 FrameProducer.prototype._socketEnded = function() {204 log.warn('Connection to minicap ended unexpectedly')205 this.failCounter.inc()206 this.restart()207 }208 FrameProducer.prototype._outputEnded = function() {209 log.warn('Shell keeping minicap running ended unexpectedly')210 this.failCounter.inc()211 this.restart()212 }213 FrameProducer.prototype._failLimitExceeded = function(limit, time) {214 this._stop()215 this.failed = true216 this.emit('error', new Error(util.format(217 'Failed more than %d times in %dms'218 , limit219 , time220 )))221 }222 FrameProducer.prototype._startService = function() {223 log.info('Launching screen service')224 return minicap.run(util.format('-S -P %s', this.frameConfig.toString()))225 .timeout(10000)226 }227 FrameProducer.prototype._readOutput = function(out) {228 out.pipe(split()).on('data', function(line) {229 var trimmed = line.toString().trim()230 if (trimmed === '') {231 return232 }233 if (/ERROR/.test(line)) {234 log.fatal('minicap error: "%s"', line)235 return lifecycle.fatal()236 }237 var match = /^PID: (\d+)$/.exec(line)238 if (match) {239 this.pid = Number(match[1])240 this.emit('pid', this.pid)241 }242 log.info('minicap says: "%s"', line)243 }.bind(this))244 }245 FrameProducer.prototype._waitForPid = function() {246 if (this.pid > 0) {247 return Promise.resolve(this.pid)248 }249 var pidListener250 return new Promise(function(resolve) {251 this.on('pid', pidListener = resolve)252 }.bind(this)).bind(this)253 .timeout(2000)254 .finally(function() {255 this.removeListener('pid', pidListener)256 })257 }258 FrameProducer.prototype._connectService = function() {259 function tryConnect(times, delay) {260 return adb.openLocal(options.serial, 'localabstract:minicap')261 .timeout(10000)262 .then(function(out) {263 return out264 })265 .catch(function(err) {266 if (/closed/.test(err.message) && times > 1) {267 return Promise.delay(delay)268 .then(function() {269 return tryConnect(times - 1, delay * 2)270 })271 }272 return Promise.reject(err)273 })274 }275 log.info('Connecting to minicap service')276 return tryConnect(5, 100)277 }278 FrameProducer.prototype._stop = function() {279 return this._disconnectService(this.socket).bind(this)280 .timeout(2000)281 .then(function() {282 return this._stopService(this.output).timeout(10000)283 })284 .then(function() {285 this.runningState = FrameProducer.STATE_STOPPED286 this.emit('stop')287 })288 .catch(function(err) {289 // In practice we _should_ never get here due to _stopService()290 // being quite aggressive. But if we do, well... assume it291 // stopped anyway for now.292 this.runningState = FrameProducer.STATE_STOPPED293 this.emit('error', err)294 this.emit('stop')295 })296 .finally(function() {297 this.output = null298 this.socket = null299 this.pid = -1300 this.banner = null301 this.parser = null302 })303 }304 FrameProducer.prototype._disconnectService = function(socket) {305 log.info('Disconnecting from minicap service')306 if (!socket || socket.ended) {307 return Promise.resolve(true)308 }309 socket.stream.removeListener('readable', this.readableListener)310 var endListener311 return new Promise(function(resolve) {312 socket.on('end', endListener = function() {313 resolve(true)314 })315 socket.stream.resume()316 socket.end()317 })318 .finally(function() {319 socket.removeListener('end', endListener)320 })321 }322 FrameProducer.prototype._stopService = function(output) {323 log.info('Stopping minicap service')324 if (!output || output.ended) {325 return Promise.resolve(true)326 }327 var pid = this.pid328 function kill(signal) {329 if (pid <= 0) {330 return Promise.reject(new Error('Minicap service pid is unknown'))331 }332 var signum = {333 SIGTERM: -15334 , SIGKILL: -9335 }[signal]336 log.info('Sending %s to minicap', signal)337 return Promise.all([338 output.waitForEnd()339 , adb.shell(options.serial, ['kill', signum, pid])340 .then(adbkit.util.readAll)341 .return(true)342 ])343 .timeout(2000)344 }345 function kindKill() {346 return kill('SIGTERM')347 }348 function forceKill() {349 return kill('SIGKILL')350 }351 function forceEnd() {352 log.info('Ending minicap I/O as a last resort')353 output.end()354 return Promise.resolve(true)355 }356 return kindKill()357 .catch(Promise.TimeoutError, forceKill)358 .catch(forceEnd)359 }360 FrameProducer.prototype._readBanner = function(socket) {361 log.info('Reading minicap banner')362 return bannerutil.read(socket).timeout(2000)363 }364 FrameProducer.prototype._readFrames = function(socket) {365 this.needsReadable = true366 socket.on('readable', this.readableListener)367 // We may already have data pending. Let the user know they should368 // at least attempt to read frames now.369 this.readableListener()370 }371 FrameProducer.prototype._maybeEmitReadable = function() {372 if (this.readable && this.needsReadable) {373 this.needsReadable = false374 this.emit('readable')375 }376 }377 FrameProducer.prototype._readableListener = function() {378 this.readable = true379 this._maybeEmitReadable()380 }381 function createServer() {382 log.info('Starting WebSocket server on port %d', screenOptions.publicPort)383 var wss = new WebSocket.Server({384 port: screenOptions.publicPort385 , perMessageDeflate: false386 })387 var listeningListener, errorListener388 return new Promise(function(resolve, reject) {389 listeningListener = function() {390 return resolve(wss)391 }392 errorListener = function(err) {393 return reject(err)394 }395 wss.on('listening', listeningListener)396 wss.on('error', errorListener)397 })398 .finally(function() {399 wss.removeListener('listening', listeningListener)400 wss.removeListener('error', errorListener)401 })402 }403 return createServer()404 .then(function(wss) {405 var frameProducer = new FrameProducer(406 new FrameConfig(display.properties, display.properties))407 var broadcastSet = frameProducer.broadcastSet = new BroadcastSet()408 broadcastSet.on('nonempty', function() {409 frameProducer.start()410 })411 broadcastSet.on('empty', function() {412 frameProducer.stop()413 })414 broadcastSet.on('insert', function(id) {415 // If two clients join a session in the middle, one of them416 // may not release the initial size because the projection417 // doesn't necessarily change, and the producer doesn't Getting418 // restarted. Therefore we have to call onStart() manually419 // if the producer is already up and running....

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var FrameProducer = require('devicefarmer-stf').FrameProducer;2var FrameConsumer = require('devicefarmer-stf').FrameConsumer;3var fs = require('fs');4var path = require('path');5var producer = new FrameProducer();6var consumer = new FrameConsumer();7consumer.on('frame', function(frame) {8 console.log('Received frame');9 fs.writeFileSync(path.join(__dirname, 'frame.png'), frame);10});11producer.on('connected', function() {12 console.log('Producer connected');13 producer.start(0);14});15producer.on('disconnected', function() {16 console.log('Producer disconnected');17 process.exit(0);18});19producer.on('error', function(err) {20 console.log('Producer error: ' + err);21});22consumer.on('error', function(err) {23 console.log('Consumer error: ' + err);24});25{26 "scripts": {27 },28 "dependencies": {29 }30}

Full Screen

Using AI Code Generation

copy

Full Screen

1var FrameProducer = require('devicefarmer-stf').FrameProducer;2producer.on('frame', function(frame) {3 console.log(frame);4});5producer.start();6var FrameConsumer = require('devicefarmer-stf').FrameConsumer;7consumer.on('frame', function(frame) {8 console.log(frame);9});10consumer.start();11var FrameProducer = require('devicefarmer-stf').FrameProducer;12producer.on('frame', function(frame) {13 console.log(frame);14});15producer.start();16var FrameConsumer = require('devicefarmer-stf').FrameConsumer;17consumer.on('frame', function(frame) {18 console.log(frame);19});20consumer.start();21var FrameProducer = require('devicefarmer-stf').FrameProducer;22producer.on('frame', function(frame) {23 console.log(frame);24});25producer.start();26var FrameConsumer = require('devicefarmer-stf').FrameConsumer;27consumer.on('frame', function(frame) {28 console.log(frame);29});30consumer.start();31var FrameProducer = require('devicefarmer-stf').FrameProducer;32producer.on('frame', function(frame) {33 console.log(frame);34});35producer.start();36var FrameConsumer = require('devicefarmer-stf').FrameConsumer;

Full Screen

Using AI Code Generation

copy

Full Screen

1var FrameProducer = require('devicefarmer-stf-client').FrameProducer;2var frameProducer = new FrameProducer();3frameProducer.on('frame', function(frame) {4 console.log(frame);5});6frameProducer.on('error', function(err) {7 console.log(err);8});9frameProducer.on('connect', function() {10 console.log('connected');11});12frameProducer.on('disconnect', function() {13 console.log('disconnected');14});15frameProducer.on('ready', function() {16 console.log('ready');17});18frameProducer.on('end', function() {19 console.log('end');20});21frameProducer.on('close', function() {22 console.log('close');23});24frameProducer.on('message', function(message) {25 console.log('message');26});27frameProducer.on('open', function() {28 console.log('open');29});30frameProducer.on('upgrade', function() {31 console.log('upgrade');32});33frameProducer.on('ping', function() {34 console.log('ping');35});36frameProducer.on('pong', function() {37 console.log('pong');38});39frameProducer.on('unexpected-response', function() {40 console.log('unexpected-response');41});42var FrameConsumer = require('devicefarmer-stf-client').FrameConsumer;43var frameConsumer = new FrameConsumer();44frameConsumer.on('frame', function(frame) {45 console.log(frame);46});47frameConsumer.on('error', function(err) {48 console.log(err);49});50frameConsumer.on('connect', function() {51 console.log('connected');52});53frameConsumer.on('disconnect', function() {54 console.log('disconnected');55});56frameConsumer.on('ready', function() {57 console.log('ready');58});59frameConsumer.on('end', function() {60 console.log('end');61});62frameConsumer.on('close', function() {63 console.log('close');64});65frameConsumer.on('message', function(message) {66 console.log('message');67});68frameConsumer.on('open', function() {69 console.log('open');70});71frameConsumer.on('upgrade', function() {72 console.log('upgrade');73});74frameConsumer.on('ping', function() {75 console.log('ping');76});77frameConsumer.on('pong', function() {78 console.log('pong');79});80frameConsumer.on('unexpected-response

Full Screen

Using AI Code Generation

copy

Full Screen

1var frameProducer = require('devicefarmer-stf-client').FrameProducer;2var frameProducer = new FrameProducer();3frameProducer.start();4frameProducer.on('frame', function(frame) {5});6frameProducer.stop();7var frameConsumer = require('devicefarmer-stf-client').FrameConsumer;8var frameConsumer = new FrameConsumer();9frameConsumer.start();10frameConsumer.on('frame', function(frame) {11});12frameConsumer.stop();

Full Screen

Using AI Code Generation

copy

Full Screen

1var FrameProducer = require('devicefarmer-stf').FrameProducer;2var frameProducer = new FrameProducer('your-device-id');3frameProducer.start(function(err) {4 if (err) {5 console.error(err);6 process.exit(1);7 }8 console.log('FrameProducer started');9});10frameProducer.on('frame', function(frame) {11 console.log('Got new frame', frame);12});13frameProducer.on('end', function() {14 console.log('FrameProducer ended');15});16var FrameConsumer = require('devicefarmer-stf').FrameConsumer;17var frameConsumer = new FrameConsumer('your-device-id');18frameConsumer.start(function(err) {19 if (err) {20 console.error(err);21 process.exit(1);22 }23 console.log('FrameConsumer started');24});25frameConsumer.on('frame', function(frame) {26 console.log('Got new frame', frame);27});28frameConsumer.on('end', function() {29 console.log('FrameConsumer ended');30});31var FrameConsumer = require('devicefarmer-stf').FrameConsumer;32var frameConsumer = new FrameConsumer('your-device-id');33frameConsumer.start(function(err) {34 if (err) {35 console.error(err);36 process.exit(1);37 }38 console.log('FrameConsumer started');39});40frameConsumer.on('frame', function(frame) {41 console.log('Got new frame', frame);42});43frameConsumer.on('end', function() {44 console.log('FrameConsumer ended');45});46var FrameConsumer = require('devicefarmer-stf').FrameConsumer;47var frameConsumer = new FrameConsumer('your-device-id');48frameConsumer.start(function(err) {49 if (err) {50 console.error(err);51 process.exit(1);52 }53 console.log('FrameConsumer started');54});55frameConsumer.on('frame', function(frame) {56 console.log('Got new frame', frame);57});58frameConsumer.on('end', function() {59 console.log('FrameConsumer ended');60});61var FrameConsumer = require('devicefarmer-stf').FrameConsumer

Full Screen

Using AI Code Generation

copy

Full Screen

1const FrameProducer = require('devicefarmer-stf/lib/FrameProducer')2var frameProducer = new FrameProducer('device_id', 'frame.png')3frameProducer.getFrame()4 .then(function() {5 console.log('got a frame')6 })7 .catch(function(err) {8 console.log('error getting frame: ' + err)9 })

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run devicefarmer-stf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful