# videojs/videojs-contrib-eme architecture

> Architecture diagram of the GitHub repository videojs/videojs-contrib-eme, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/videojs/videojs-contrib-eme
- Repository: https://github.com/videojs/videojs-contrib-eme

## Overview

##### Main Components
- Video.js Player Integration – The entry point where the plugin is initialized by the user on a Video.js player.
- Plugin Initialization & API Interface – Represented by “plugin.js”, this component exposes methods such as player.eme(), initializeMediaKeys(), detectSupportedCDMs(), and initLegacyFairplay().
- DRM Modules – Group together individual modules handling various DRM systems:
- EME & CDM Utilities – “cdm.js” and “eme.js” provide general purpose functionality for managing MediaKeys and EME events.
- FairPlay Support – “fairplay.js” handles certificate fetching and license requests specifically for Apple’s FairPlay.
- PlayReady Support – “playready.js” (and “ms-prefixed.js” for Microsoft-specific extensions) manage PlayReady license retrieval and key handling.
- HTTP Request Handling – “http-handler.js” centralizes network calls to fetch licenses and certificates.
- Constants & Error Handling – “consts/errors.js” supplies standardized error messages and configuration constants.
- Utility Functions – “utils.js” includes helper methods for various common tasks.
- Testing & Build Infrastructure – Although secondary in the production architecture, show the test suite (“test” directory) for unit testing each module and the configuration/build scripts (e.g., “rollup.config.js”, “karma.conf.js” and other project-level configs). This reinforces that the plugin is built, bundled, and maintained with CI/CD integration.

##### Relationships & Interactions
- Each DRM module (e.g., fairplay.js, playready.js) should be shown interacting with the HTTP Handler (http-handler.js) for retrieving licenses/certificates, as well as relying on the general utilities (utils.js, constants/error definitions).
- A network component (an external “License Server”/ “Certificate Server”) to indicate that HTTP calls are made externally.

##### Architectural Patterns & Technologies
- Plugin Pattern – The design is plug-in based for Video.js, allowing easy extension or overriding of default behaviors.
- Use of Configuration Files – Mention that build scripts (rollup.config.js, karma.conf.js) indicate a bundling process which is a key element in maintaining cross-platform compatibility.

##### Elements
- Clear Labels: Name each component based on its file/module name.

##### Final Assembly
- The DRM Handling branch, which covers license and certificate fetching, key system detection, and MediaKey setup.
- The Plugin API branch which serves as the interface layer to expose methods such as initializeMediaKeys(), detectSupportedCDMs(), and initLegacyFairplay().

This approach follows the project’s modular design as evidenced by the file tree and the comprehensive configuration and usage details in the README, ensuring a clear, maintainable, and extensible architectural overview.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Video.js Player Integration
    subgraph "Video.js Player Integration"
        VP1("Player Initialization (index.html)"):::player
        VP2("Player Options (index-player-options.html)"):::player
    end

    %% Plugin API
    subgraph "Plugin API"
        Plugin("Plugin Initialization & API Interface (plugin.js)"):::core
    end

    %% Core Utilities
    subgraph "Core Utilities"
        EME("EME Utilities (eme.js)"):::core
        CDM("CDM Utilities (cdm.js)"):::core
        CONST("Constants & Error Handling (consts/errors.js)"):::core
        UTILS("Utility Functions (utils.js)"):::core
    end

    %% DRM Modules
    subgraph "DRM Modules"
        Fair("FairPlay Support (fairplay.js)"):::drm
        Play("PlayReady Support (playready.js)"):::drm
        MS("MS Specific Support (ms-prefixed.js)"):::drm
    end

    %% HTTP and External Systems
    subgraph "HTTP & External"
        HTTP("HTTP Request Handler (http-handler.js)"):::http
        Ext("License/Certificate Server"):::external
    end

    %% Testing & Build Infrastructure
    subgraph "Testing & Build Infrastructure"
        Test("Unit Tests (test)"):::testing
        Build("Build Scripts (scripts)"):::testing
    end

    %% Connections from Video.js Player to Plugin API
    VP1 -->|"init"| Plugin
    VP2 -->|"init"| Plugin

    %% Plugin calling Core Utilities and DRM Modules
    Plugin -->|"calls"| EME
    Plugin -->|"calls"| CDM
    Plugin -->|"calls"| Fair
    Plugin -->|"calls"| Play
    Plugin -->|"calls"| MS

    %% DRM Modules using HTTP Handler for license/certificate retrieval
    Fair -->|"async-fetch"| HTTP
    Play -->|"async-fetch"| HTTP
    MS -->|"async-fetch"| HTTP

    %% HTTP Handler contacting External License/Certificate Server
    HTTP -->|"license/cert request"| Ext

    %% Shared Utility Usage (dashed to indicate dependency)
    Plugin ---|"uses"| CONST
    Plugin ---|"uses"| UTILS
    Fair ---|"uses"| CONST
    Play ---|"uses"| CONST
    MS ---|"uses"| CONST
    Fair ---|"uses"| UTILS
    Play ---|"uses"| UTILS
    MS ---|"uses"| UTILS

    %% Class Definitions for Color Coding
    classDef player fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef drm fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef http fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef external fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef testing fill:#D3D3D3,stroke:#333,stroke-width:2px;

    %% Click Events
    click VP1 "https://github.com/videojs/videojs-contrib-eme/blob/main/index.html"
    click VP2 "https://github.com/videojs/videojs-contrib-eme/blob/main/index-player-options.html"
    click Plugin "https://github.com/videojs/videojs-contrib-eme/blob/main/src/plugin.js"
    click EME "https://github.com/videojs/videojs-contrib-eme/blob/main/src/eme.js"
    click CDM "https://github.com/videojs/videojs-contrib-eme/blob/main/src/cdm.js"
    click Fair "https://github.com/videojs/videojs-contrib-eme/blob/main/src/fairplay.js"
    click Play "https://github.com/videojs/videojs-contrib-eme/blob/main/src/playready.js"
    click MS "https://github.com/videojs/videojs-contrib-eme/blob/main/src/ms-prefixed.js"
    click HTTP "https://github.com/videojs/videojs-contrib-eme/blob/main/src/http-handler.js"
    click CONST "https://github.com/videojs/videojs-contrib-eme/blob/main/src/consts/errors.js"
    click UTILS "https://github.com/videojs/videojs-contrib-eme/blob/main/src/utils.js"
    click Test "https://github.com/videojs/videojs-contrib-eme/tree/main/test"
    click Build "https://github.com/videojs/videojs-contrib-eme/tree/main/scripts"
```
