Webcam.js

import CaptureBridge from './CaptureBridge.js'
import Camera from './Camera.js'
import { BASE_URL } from './Common.js'

/**
 * Webcam
 * @classdesc Convenience class for instantiating a Webcam Object.
 * Inherits all methods on the {@link Camera} class.
 * @extends Camera
 * @see {@link Camera}
 */
class Webcam extends Camera {
  /**
   * Instantiate a Webcam
   * @constructor
   * @param {string} pluginId - The Id of the desired plugin to use for this
   * webcam instance. The plugin must support the `start_feed`, `stop_feed`,
   * `devices`, and `capture` methods.
   * @param {string} [baseURL] - Protocol, domain, and port for the service.
   * @example
   * const webcam = new Webcam('capture_webcam_windows')
   */
  constructor (pluginId, baseUrl = BASE_URL) {
    super(pluginId, baseUrl)
  }
}

export default Webcam

/**
 * Instantiate this Capture Bridge instance's Webcam plugin
 * @constructor
 * @param {string} [baseURL] - Protocol, domain, and port for the service.
 * @example
 * const webcam = await WebcamFactory()
 */
export const WebcamFactory = async (baseUrl = BASE_URL) => {
  const cb = new CaptureBridge(baseUrl)
  const plugins = await cb.plugins()
  const plugin = plugins.find(p => p.id.includes('capture_webcam_'))
  if (!plugin) {
    throw new Error('No webcam plugin found')
  }
  return new Webcam(plugin, baseUrl)
}