2.0.0 • Published 4 years ago

@edgeros/jsre-medias v2.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Overview

The medias module can connect to multiple media sources at the same time. It consists of two parts:

Manager: Integrated onvif device search function, the searched device can be further identified as a camera device, and a media object can be created for the camera device; Manager provides device, camera, and Media objects Management interface.

Media: Integrates the WebMedia server and onvif.Camera objects, and provides a control interface for the camera media source.

The medias module realizes the search, connection and control functions of multiple camera devices at the same time.

Medias module depends on @edgeros/jsre-onvif and WebMedia modules.

User can use the following code to import the medias module.

var medias = require('@edgeros/jsre-medias');

Support

The following shows medias module APIs avaliable for each permissions.

 User ModePrivilege Mode
medias.Media
media.ser
media.source
media.enableMove
media.camLock
media.camHome
media.camMove
media.camStop
medias.Manager
medias.Manager.key
mgr.destroy
mgr.addCam
mgr.getCamUrl
mgr.removeCam
mgr.findCam
mgr.iterCam
mgr.addDev
mgr.removeDev
mgr.findDev
mgr.iterDev
mgr.createMedia
mgr.destroyMedia
mgr.findMedia
mgr.iterMedia

Media Class

new medias.Media(key, sid, app[, cam, timeout])

  • key {String} Media id.
  • sid {String} As the main part of media websocket server path. Data server path: /${sid}.media; Stream server path: /${sid}.
  • app {WebApp} Web app object.
  • cam {onvif.Camera} Onvif camera object. If cam unvalid, media.enableMove is false.
  • timeout {Integer} Media server timeout. When there is no client access to media server, it will be closed after timeout. default: 18000000s.

Media is an encapsulation of WebMedia.MediaServer, and Media also provides a move control interface for onvif.Camera.

Media objects are usually created by Manager.

Media Object

media.ser

  • {WebMedia.MediaServer} See WebMedia module.

media.source

  • {WebMedia.MediaSource} See WebMedia module.

media.enableMove

  • {Boolean} Whether it can control the movement of the camera.

NOTICE: some cameras support the ‘onvif.Camera interface, but do not have the movement function.

media.camLock

  • {Boolean} Whether to lock camera movement.

If the media is locked, the move operation is invalid

media.camHome(cb)

  • cb {Function} Callback. + err {Error} Error or null.

Call onvif.Camera.gotoHomePosition().

media.camMove(rx, ry, cb)

  • rx {Number} Moving direction(x): -1, 0 or 1.
    • rx {Number} Moving direction(y): -1, 0 or 1.
  • cb {Function} Callback. + err {Error} Error or null.

Call onvif.Camera.continuousMove().

media.camStop(cb)

  • cb {Function} Callback. + err {Error} Error or null.

Call onvif.Camera.stop().

Media Events

start

  • self {Media}

An start event is emitted whenmedia start.

stop

  • self {Media}

An stop event is emitted whenmedia stop.

open

  • self {Media}
  • client {WebMedia.MediaClient} Media client object.

A client connect to media.

close

  • self {Media}
  • client {WebMedia.MediaClient} Media client object.

A client disconnect.

Manager Class

new medias.Manager(app, MediaClass, opts, srcOptionHandle)

  • app {WebApp}
  • MediaClass {Media | null} Users can implement their own Media class which inherit to Media.
  • opts{Object} + nets {Array} Select networks to search onvif devices. default: {ifname: 'br5', localPort: 0}. + ifname {String} networks. + localPort {Integer} 0 - auto assign port. default: 0. + mediaTimeout {Integer} Media server timeout. When there is no client access to media server, it will be closed after timeout. default: 18000000s. + searchCycle {Integer} Onvif search device cycle, . default: 30000. + autoGetCamera {Boolean} true - Auto create camera object when searched a device. default: true.
  • srcOptionHandle {Function} This callback function is used to dynamically create the MediaSource construction selection. It receives a media options, and the returned object contains the inOpts, outOpts options, and the name of MediaSource . + opts {Object} Media options: + host {String} Media source hostname. + port {Integer} Media source port. + user {String} Media source user name. + pass {String} Media source password. + path {String} Media source path. + returns {Object} + source {String} Media source name. + inOpts {Object} Media source input options. + outPuts {Object} Media source output options.

medias.Manager.key(urlOpts)

  • urlOpts {Object} + hostname {String} Media source hostname. + port {Integer} Media source port. + path {String} Media source path.
  • returns {String} Media key.

Get media key from url options.

Manager Object

mgr.destroy()

Destroy Manager object.

mgr.addCam(uri, cam)

  • uri {String} Media uri.
  • cam {onvif.Camera} Onvif camera object.

Add a camera device to media manager.

getCamUrl(cam)

  • cam {onvif.Camera | String} Onvif camera object or camera uri.
  • returns {Object} + hostname {String} + port {Ingeger} + path {String}

Get camera device url patrs.

mgr.removeCam(cam)

  • cam {onvif.Camera | String} Onvif camera object or camera key.

Remove camera device.

mgr.findCam(key)

  • key {String} Camera key.

Find camera device by key.

mgr.iterCam(cb)

  • cb {Function} Iter callback. Arguments: + key {String} Camera key. + cam *{onvif.Camera} Camera object.

Iterative cameras.

mgr.addDev(dev)

  • dev {Object} Onvif searched device object.

Add onvif device..

mgr.removeDev(dev)

  • dev {onvif.device | String} Onvif device object or device key.

Remove onvif device.

mgr.findDev(key)

  • key {String} Onvif device key.

Find onvif device by key.

mgr.iterDev(cb)

  • cb {Function} Iter callback. Arguments: + key {String} Device key. + cam *{onvif.Camera} Device object.

Iterative devices.

mgr.createMedia(key, urlOpts, cam)

  • key {String} Media key.
  • urlOpts {Object} options: + hostname {String} Media source hostname. + port {Integer} Media source port. default: 554. + user {String} Media source user name. default: 'admin'. + pass {String} Media source password. default: 'admin'. + path {String} Media source path. default: '/'.
  • cam {Onvif.Camera} Onvif camera object.
  • cb {Function} Callback, arguments: media {Error | Media}

Create meida object.

mgr.destroyMedia(key, cb)

  • key {String} Media key.
  • cb {Function} Callback, arguments: ret {Boolean} Destroy success or not.

Destroy meida object.

mgr.findMedia(key)

  • key {String} Media key.
  • returns *{Media | undefined}

Find media. Return undefined if media not exits.

mgr.iterMedia(cb)

  • cb {Function} Iter callback. Arguments: + key {String} Media key. + media *{Media} Media object.

Iterative medias.

Manager Events

open

  • Media {Media}

A media object is created and started.

close

  • media {Media}

A media object stopped.

Example

function srcHandle(opts) {
	return {
		source: 'source-name', // User defined source.
		inOpts: { 
			// User source defined options.
		},
		outOpts: {
			// User source defined options.
		}
	}
}

var mediaMgr = new Manager(app, null, null, srcHandle);
mediaMgr.on('open', (media) => {
	media.on('open', (media, client) => {
		client.on('camera-move', (client, x, y, cb) => { // User defined event.
			media.camMove(x, y, (err) => {
				if (err) {
					cb({ result: false, msg: err.message });
				} else {
					cb({ result: true, msg: 'ok' });
				}
			});
		});

		client.on('camera-stop', (client, cb) => { // User defined event.
			media.camStop((err) => {
				if (err) {
					cb({ result: false, msg: err.message });
				} else {
					cb({ result: true, msg: 'ok' });
				}
			});
		});
	});
});