From 8953f055c86ca74f145d7ac5ac93bb6104d73af9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 10 Jul 2023 16:08:53 +0200 Subject: Rename player embed api --- client/src/standalone/embed-player-api/.npmignore | 3 + client/src/standalone/embed-player-api/README.md | 3 + .../src/standalone/embed-player-api/definitions.ts | 25 +++ client/src/standalone/embed-player-api/events.ts | 48 +++++ .../src/standalone/embed-player-api/package.json | 28 +++ client/src/standalone/embed-player-api/player.ts | 236 +++++++++++++++++++++ .../src/standalone/embed-player-api/tsconfig.json | 19 ++ .../standalone/embed-player-api/webpack.config.js | 12 ++ 8 files changed, 374 insertions(+) create mode 100644 client/src/standalone/embed-player-api/.npmignore create mode 100644 client/src/standalone/embed-player-api/README.md create mode 100644 client/src/standalone/embed-player-api/definitions.ts create mode 100644 client/src/standalone/embed-player-api/events.ts create mode 100644 client/src/standalone/embed-player-api/package.json create mode 100644 client/src/standalone/embed-player-api/player.ts create mode 100644 client/src/standalone/embed-player-api/tsconfig.json create mode 100644 client/src/standalone/embed-player-api/webpack.config.js (limited to 'client/src/standalone/embed-player-api') diff --git a/client/src/standalone/embed-player-api/.npmignore b/client/src/standalone/embed-player-api/.npmignore new file mode 100644 index 000000000..870b6315b --- /dev/null +++ b/client/src/standalone/embed-player-api/.npmignore @@ -0,0 +1,3 @@ +tsconfig.json +*.ts +webpack.config.ts diff --git a/client/src/standalone/embed-player-api/README.md b/client/src/standalone/embed-player-api/README.md new file mode 100644 index 000000000..7b47e8f02 --- /dev/null +++ b/client/src/standalone/embed-player-api/README.md @@ -0,0 +1,3 @@ +# @peertube/embed-api + +See https://docs.joinpeertube.org/api/embed-player diff --git a/client/src/standalone/embed-player-api/definitions.ts b/client/src/standalone/embed-player-api/definitions.ts new file mode 100644 index 000000000..495f1a98c --- /dev/null +++ b/client/src/standalone/embed-player-api/definitions.ts @@ -0,0 +1,25 @@ +export type EventHandler = (ev: T) => void + +export type PlayerEventType = + 'pause' | 'play' | + 'playbackStatusUpdate' | + 'playbackStatusChange' | + 'resolutionUpdate' | + 'volumeChange' + +export interface PeerTubeResolution { + id: any + label: string + active: boolean + height: number + + src?: string + width?: number +} + +export type PeerTubeTextTrack = { + id: string + label: string + src: string + mode: TextTrackMode +} diff --git a/client/src/standalone/embed-player-api/events.ts b/client/src/standalone/embed-player-api/events.ts new file mode 100644 index 000000000..77d21c78c --- /dev/null +++ b/client/src/standalone/embed-player-api/events.ts @@ -0,0 +1,48 @@ +import { EventHandler } from './definitions' + +interface PlayerEventRegistrar { + registrations: EventHandler[] +} + +interface PlayerEventRegistrationMap { + [ name: string ]: PlayerEventRegistrar +} + +export class EventRegistrar { + + private eventRegistrations: PlayerEventRegistrationMap = {} + + public bindToChannel (channel: Channel.MessagingChannel) { + for (const name of Object.keys(this.eventRegistrations)) { + channel.bind(name, (txn, params) => this.fire(name, params)) + } + } + + public registerTypes (names: string[]) { + for (const name of names) { + this.eventRegistrations[name] = { registrations: [] } + } + } + + public fire (name: string, event: T) { + this.eventRegistrations[name].registrations.forEach(x => x(event)) + } + + public addListener (name: string, handler: EventHandler) { + if (!this.eventRegistrations[name]) { + console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`) + return false + } + + this.eventRegistrations[name].registrations.push(handler) + return true + } + + public removeListener (name: string, handler: EventHandler) { + if (!this.eventRegistrations[name]) return false + + this.eventRegistrations[name].registrations = this.eventRegistrations[name].registrations.filter(x => x !== handler) + + return true + } +} diff --git a/client/src/standalone/embed-player-api/package.json b/client/src/standalone/embed-player-api/package.json new file mode 100644 index 000000000..b549fbf52 --- /dev/null +++ b/client/src/standalone/embed-player-api/package.json @@ -0,0 +1,28 @@ +{ + "name": "@peertube/embed-api", + "private": false, + "version": "0.0.6", + "description": "API to communicate with the PeerTube player embed", + "scripts": { + "build": "../../../node_modules/.bin/tsc && ../../../node_modules/.bin/webpack --mode production --config ./webpack.config.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Chocobozzz/PeerTube.git" + }, + "keywords": [ + "peertube", + "embed" + ], + "main": "./dist/player.js", + "types": "./dist/player.d.ts", + "author": "Chocobozzz", + "license": "AGPL-3.0", + "bugs": { + "url": "https://github.com/Chocobozzz/PeerTube/issues" + }, + "homepage": "https://github.com/Chocobozzz/PeerTube#readme", + "dependencies": { + "jschannel": "^1.0.2" + } +} diff --git a/client/src/standalone/embed-player-api/player.ts b/client/src/standalone/embed-player-api/player.ts new file mode 100644 index 000000000..75487258b --- /dev/null +++ b/client/src/standalone/embed-player-api/player.ts @@ -0,0 +1,236 @@ +import * as Channel from 'jschannel' +import { EventHandler, PeerTubeResolution, PeerTubeTextTrack, PlayerEventType } from './definitions' +import { EventRegistrar } from './events' + +const PASSTHROUGH_EVENTS = [ + 'pause', + 'play', + 'playbackStatusUpdate', + 'playbackStatusChange', + 'resolutionUpdate', + 'volumeChange' +] + +/** + * Allows for programmatic control of a PeerTube embed running in an