{"version":3,"file":"encoder-Ba4kA31m.mjs","names":[],"sources":["../src/video/encoder/encoder.ts"],"sourcesContent":["/* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD */\n/* SPDX-License-Identifier: Apache-2.0 */\n\n/**\n * Video encoder service module for JS apps — wraps `brookesia_service_video` encoder RPC.\n *\n * @example\n * import encoder from '@brookesia/service/video/encoder';\n * if (encoder.available()) {\n *   encoder.open({ sinks: [{ format: 'H264', width: 320, height: 240, fps: 30 }] });\n *   encoder.start();\n *   encoder.fetchFrame(0);\n *   encoder.stop();\n *   encoder.close();\n * }\n */\n\nimport {\n  closeEncoder,\n  closeVideoEncoder,\n  fetchEncoderFrame,\n  isVideoEncoderAvailable,\n  openEncoder,\n  openVideoEncoder,\n  startEncoder,\n  stopEncoder,\n  type VideoEncoderClientOptions,\n  type VideoEncoderHandle,\n} from './encoder-client.ts';\nimport type {\n  VideoEncoderConfig,\n  VideoEncoderDisplayConfig,\n  VideoEncoderSinkFormat,\n  VideoEncoderSinkInfo,\n  VideoEncoderSourceConfig,\n  VideoEncoderState,\n} from './encoder-types.ts';\n\nexport type {\n  VideoEncoderConfig,\n  VideoEncoderDisplayConfig,\n  VideoEncoderSinkFormat,\n  VideoEncoderSinkInfo,\n  VideoEncoderSourceConfig,\n  VideoEncoderState,\n};\n\nexport type VideoEncoderModuleOptions = VideoEncoderClientOptions;\n\ntype State = {\n  handle: VideoEncoderHandle | null;\n  options: VideoEncoderModuleOptions;\n};\n\nconst state: State = { handle: null, options: {} };\n\nfunction handle(): VideoEncoderHandle {\n  if (state.handle !== null) {\n    return state.handle;\n  }\n  if (!isVideoEncoderAvailable(state.options)) {\n    throw new Error('Video encoder service is not available');\n  }\n  state.handle = openVideoEncoder(state.options);\n  return state.handle;\n}\n\n/**\n * Inject a mock RPC host and reset the cached service handle (vitest / Node only).\n *\n * @param options - Client options, typically `{ host }` from `@brookesia/service/hosts/video-encoder`\n */\nexport function configure(options: VideoEncoderModuleOptions): void {\n  if (state.handle !== null) {\n    closeVideoEncoder(state.handle, state.options);\n  }\n  state.handle = null;\n  state.options = options;\n}\n\nconst encoder = {\n  /** @returns `true` when video encoder service is registered */\n  available(): boolean {\n    return isVideoEncoderAvailable(state.options);\n  },\n\n  /** Release the video encoder service binding. */\n  close(): void {\n    if (state.handle === null) {\n      return;\n    }\n    closeVideoEncoder(state.handle, state.options);\n    state.handle = null;\n  },\n\n  /**\n   * Open an encoding session with one or more sinks.\n   *\n   * @param config - `{ sinks: [{ format: 'H264' | 'MJPEG', width, height, fps }] }`\n   */\n  open(config: VideoEncoderConfig): void {\n    openEncoder(handle(), config, state.options);\n  },\n\n  /** Close the active encoding session (keeps service handle). */\n  closeEncoder(): void {\n    closeEncoder(handle(), state.options);\n  },\n\n  /** Start frame capture / encoding. */\n  start(): void {\n    startEncoder(handle(), state.options);\n  },\n\n  /** Stop frame capture / encoding. */\n  stop(): void {\n    stopEncoder(handle(), state.options);\n  },\n\n  /**\n   * Pull one encoded frame from a sink.\n   *\n   * @param sinkIndex - Sink index from {@link open} config (default `0`)\n   */\n  fetchFrame(sinkIndex = 0): void {\n    fetchEncoderFrame(handle(), sinkIndex, state.options);\n  },\n};\n\nexport default encoder;\n"],"mappings":";;;;;;;;;;;;;;;AAsDA,MAAM,QAAe;CAAE,QAAQ;CAAM,SAAS,CAAC;AAAE;AAEjD,SAAS,SAA6B;CACpC,IAAI,MAAM,WAAW,MACnB,OAAO,MAAM;CAEf,IAAI,CAAC,wBAAwB,MAAM,OAAO,GACxC,MAAM,IAAI,MAAM,wCAAwC;CAE1D,MAAM,SAAS,iBAAiB,MAAM,OAAO;CAC7C,OAAO,MAAM;AACf;;;;;;AAOA,SAAgB,UAAU,SAA0C;CAClE,IAAI,MAAM,WAAW,MACnB,kBAAkB,MAAM,QAAQ,MAAM,OAAO;CAE/C,MAAM,SAAS;CACf,MAAM,UAAU;AAClB;AAEA,MAAM,UAAU;;CAEd,YAAqB;EACnB,OAAO,wBAAwB,MAAM,OAAO;CAC9C;;CAGA,QAAc;EACZ,IAAI,MAAM,WAAW,MACnB;EAEF,kBAAkB,MAAM,QAAQ,MAAM,OAAO;EAC7C,MAAM,SAAS;CACjB;;;;;;CAOA,KAAK,QAAkC;EACrC,YAAY,OAAO,GAAG,QAAQ,MAAM,OAAO;CAC7C;;CAGA,eAAqB;EACnB,aAAa,OAAO,GAAG,MAAM,OAAO;CACtC;;CAGA,QAAc;EACZ,aAAa,OAAO,GAAG,MAAM,OAAO;CACtC;;CAGA,OAAa;EACX,YAAY,OAAO,GAAG,MAAM,OAAO;CACrC;;;;;;CAOA,WAAW,YAAY,GAAS;EAC9B,kBAAkB,OAAO,GAAG,WAAW,MAAM,OAAO;CACtD;AACF"}