Oryx Documentation

repository·main·Indexed 21 days ago

https://github.com/ossrs/oryx

Oryx (formerly SRS Stack) is an all-in-one, open-source video solution for creating online video services. It supports live streaming and WebRTC, providing features such as transcoding, recording, and AI integration. The platform includes tools for deploying origin servers and NGINX edge servers for HLS caching and CDN architectures.

Tokens
35K
Snippets
83
Records
154
Agent score
74%

What's inside Oryx

  1. Follow Go JSON naming conventions

    main

    When defining Go structs for JSON fields containing more than one word, ORYX generally follows one of two patterns (though legacy code may vary). The preferred approach is to use snake_case for JSON tags, but camelCase is also used in some areas.

    Example of camelCase:

    type CameraConfigure struct {
    	ExtraAudio string `json:"extraAudio"`
    }

    Example of snake_case:

    type CameraConfigure struct {
        ExtraAudio string `json:"extra_audio"`
    }
  2. Develop the DigitalOcean Droplet Image

    main

    This guide describes how to build and test an SRS droplet image for the DigitalOcean Marketplace using Packer.

    1. Install Packer and DigitalOcean Plugin

    On macOS, use Homebrew to install Packer and the required DigitalOcean plugin:

    brew tap hashicorp/tap &&
    brew install hashicorp/tap/packer &&
    PACKER_LOG=1 packer plugins install github.com/digitalocean/digitalocean

    2. Build the Image

    You can either set the DIGITALOCEAN_TOKEN environment variable manually or extract it from your doctl config. Run the build from the scripts/setup-droplet directory:

    (export DIGITALOCEAN_TOKEN=$(grep market "${HOME}/Library/Application Support/doctl/config.yaml" |grep -v context |awk '{print $2}') &&
    cd scripts/setup-droplet && packer build srs.json)

    3. Test the Droplet

    1. Create a Test Droplet: Use doctl to create a droplet from the newly created snapshot.
    2. Prepare Environment: SSH into the droplet to set up directories, upload test media (source.200kbps.768x320.flv), and install necessary tools like ffmpeg.
    3. Run Tests: Execute ./test/oryx.test on the droplet via SSH to verify RTMP, HTTP, and SRT endpoints.

    4. Cleanup

    After testing, delete the droplet and the snapshot (if submitting to the marketplace, the snapshot should be removed).

    doctl compute droplet delete oryx-test --context market --force
  3. Develop the TencentCloud Lighthouse Image

    main

    To build an SRS image for TencentCloud Lighthouse, follow these steps:

    1. Configure Credentials: Create a TencentCloud Secret and save it to ~/.lighthouse/.env with the following keys:

      • LH_ACCOUNT
      • LH_PROD
      • SECRET_ID
      • SECRET_KEY
    2. Build the Image: Use the provided helper scripts to create a CVM instance, build the image, and share it. The process involves creating a token, running scripts/tools/tencent-cloud/helper.sh create-cvm.py, building via scripts/setup-lighthouse/build.sh, and finally sharing the image.

    3. Verify the Image: Create a test CVM instance using scripts/tools/tencent-cloud/helper.sh create-verify.py and run the oryx.test suite against the new instance to ensure functionality.

    4. Cleanup: Remove the test CVM, images, and snapshots using the remove-cvm.py and remove-image.py helpers.

    # Example: Create a CVM instance and build the image
    rm -f .tmp/lh-*.txt &&
    echo "$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | head -c 16)A0" >.tmp/lh-token.txt &&
    VM_TOKEN=$(cat .tmp/lh-token.txt) bash scripts/tools/tencent-cloud/helper.sh create-cvm.py --id $(pwd)/.tmp/lh-instance.txt &&
    bash scripts/tools/tencent-cloud/helper.sh query-cvm-ip.py --instance $(cat .tmp/lh-instance.txt) --id $(pwd)/.tmp/lh-ip.txt &&
    echo "Instance: $(cat .tmp/lh-instance.txt), IP: ubuntu@$(cat .tmp/lh-ip.txt), Password: $(cat .tmp/lh-token.txt)" && sleep 5 &&
    bash scripts/setup-lighthouse/build.sh --ip $(cat .tmp/lh-ip.txt) --os ubuntu --user ubuntu --password $(cat .tmp/lh-token.txt) &&
    bash scripts/tools/tencent-cloud/helper.sh create-image.py --instance $(cat .tmp/lh-instance.txt) --id $(pwd)/.tmp/lh-image.txt &&
    bash scripts/tools/tencent-cloud/helper.sh share-image.py --image $(cat .tmp/lh-image.txt) &&
    echo "Image: $(cat .tmp/lh-image.txt) created and shared." &&
    bash scripts/tools/tencent-cloud/helper.py remove-cvm.py --instance $(cat .tmp/lh-instance.txt)
  4. Configure HTTPS for NGINX Edge Servers

    main

    To serve HLS streams over HTTPS, use the ossrs/oryx:nginx-hls-cdn-https image.

    1. Prepare Certificates: Generate or provide an SSL certificate (.crt) and a private key (.key).
    2. Run with SSL: Mount your certificate files into the container at /data/config/ and map port 443.
    3. Environment Variables:
      • ORYX_SERVER: The IP and port of your origin server.
      • SRS_M3U8_EXPIRE: (Optional) Set to your SRS hls_fragment duration or desired refresh time. Default is 10s.
      • SRS_TS_EXPIRE: (Optional) Set the expiration time for .ts files. Default is 3600s.
    # 1. Generate self-signed certificates
    openssl genrsa -out nginx.key 2048
    openssl req -new -x509 -key nginx.key -out nginx.crt -days 3650 -subj "/CN=nginx01.your-domain.com"
    
    # 2. Run NGINX Edge with HTTPS
    docker run --rm -it --name oryx-nginx01 -d \
      -p 80:80 -p 443:443 \
      -e ORYX_SERVER=<YOUR_ORYX_IP>:2022 \
      -v $(pwd)/nginx.key:/data/config/nginx.key \
      -v $(pwd)/nginx.crt:/data/config/nginx.crt \
      ossrs/oryx:nginx-hls-cdn-https
  5. Develop and Use NGINX HLS CDN

    main

    You can use NGINX as a proxy/cache for HLS streams generated by Oryx.

    Development

    Build the NGINX HLS CDN image using: docker build -t ossrs/oryx:nginx-hls-cdn scripts/nginx-hls-cdn

    Running as a Proxy

    To run NGINX as a proxy in Docker, pass the ORYX_SERVER environment variable pointing to your Oryx instance:

    ORYX_SERVER=$(ifconfig en0 | grep 'inet ' | awk '{print $2}')
    docker run --rm -it -p 80:80 --name nginx -e ORYX_SERVER=${ORYX_SERVER}:2022 ossrs/oryx:nginx-hls-cdn

    Production Setup

    To set up a production HLS Edge server:

    1. Install Oryx (via BT, aaPanel, or Docker).
    2. Create a new WebSite in your web server (e.g., Nginx) to proxy requests to the NGINX HLS Edge server.
    3. Use the following Nginx configuration for the proxy site:
    location /tools/ {
      proxy_pass http://localhost:2022;
    }
    location / {
      proxy_no_cache 1;
      proxy_cache_bypass 1;
      add_header X-Cache-Status-Proxy $upstream_cache_status;
      proxy_pass http://localhost:23080;
    }
    1. Start the NGINX HLS Edge server container, mapping port 80 to 23080:

    docker run --rm -it -e ORYX_SERVER=$PIP:2022 -p 23080:80 --name oryx-nginx01 -d ossrs/oryx:nginx-hls-cdn

    Important Note on CORS: The NGINX cache stores CORS headers. If a request is cached without CORS headers, subsequent requests with an Origin header that requires CORS will still receive the cached version without CORS.

  6. Set up Oryx as the Origin Server

    main

    To build an HLS CDN, first deploy Oryx on a VPS to act as your origin.

    1. Deploy Oryx: Use Docker to run the Oryx container. Ensure you map the necessary ports for RTMP, HTTP, and UDP.
    2. Enable High Performance HLS: Access the Oryx web console at http://<YOUR_IP>:2022. Navigate to System > HLS > Delivery HLS in High Performance mode and click Submit. This allows NGINX to cache .m3u8 and .ts files effectively.
    3. Publish a Stream: Use ffmpeg to push an RTMP stream to your Oryx server using a secret for authentication.
    # 1. Run Oryx via Docker
    docker run --rm --name oryx -d -v $HOME/data:/data \
      -p 2022:2022 -p 2443:2443 -p 1935:1935/tcp -p 8000:8000/udp -p 10080:10080/udp \
      ossrs/oryx:5
    
    # 2. Push a stream via ffmpeg
    ffmpeg -re -i ~/git/srs/trunk/doc/source.flv -c copy -f flv \
        rtmp://<YOUR_ORYX_IP>/live/livestream?secret=<YOUR_SECRET>
  7. Develop the SSL Cert for HTTPS

    main

    This guide covers building and deploying the SSL certificate infrastructure for HTTPS support.

    1. Domain Setup: Create a domain record using doctl.
    2. Build Platform Image: Build the platform:latest Docker image and save it as a tarball.
    3. Deploy to Server: Transfer the tarball to the target server, load it, and tag it as ossrs/oryx:$version.
    4. BT Plugin Development:
      • Build the BT plugin using scripts/setup-bt/auto/zip.sh.
      • Unzip the plugin into /www/server/panel/plugin on the target server.
      • Run the install.sh script to install the plugin.
    5. Site Configuration: Use the provided BT API scripts (bt_api_remove_site.py, bt_api_create_site.py, bt_api_setup_site.py) and setup.sh to configure the site and Nginx.

    Environment Variables for BT: Set these in your .bashrc on the server:

    • BT_KEY
    • PYTHONIOENCODING=UTF-8
    # Build and save the platform image
    docker build -t platform:latest -f Dockerfile .
    docker save -o platform.tar platform:latest
    
    # Build the BT plugin
    bash scripts/setup-bt/auto/zip.sh --output $(pwd)/build --extract
    
    # Install the BT plugin on the server
    ssh root@$LNAME.$LDOMAIN unzip -q bt-oryx.zip -d /www/server/panel/plugin
    ssh root@$LNAME.$LDOMAIN bash /www/server/panel/plugin/oryx/install.sh install
  8. Develop the BT (Baota) Plugin

    main

    To develop the ORYX plugin for the BT (Baota) panel, you must run a development container that mounts your local build as a plugin and integrates with your BT account credentials.

    Prerequisites

    1. BT Account Info: Bind the Docker container to your BT account to obtain userInfo.json. Save it to $HOME/.bt/userInfo.json.
    2. BT HTTP API: Enable the HTTP API and save the resulting api.json to $HOME/.bt/api.json.

    Development Workflow

    1. Start the Dev Container: Run the provided docker run command (see code) to mount your local build/oryx directory into the BT plugin path.
    2. Build Platform Image: Build the platform:latest image and save it as a tarball.
    3. Load Image into BT Container: Use docker exec to load the platform image into the running bt container and tag it with the appropriate version.
    4. Build and Install Plugin: Use zip.sh to package the plugin and run the install.sh script inside the container.
    5. Setup Site: Use the provided Python scripts (bt_api_create_site.py, etc.) and setup.sh to configure the site within the BT environment.
    6. DNS Setup: Add srs.stack.local to the /etc/hosts file of both the bt container and the oryx container to allow internal communication.
    7. Testing: Run the oryx.test suite inside the bt container to verify the installation and API functionality.

    Deployment

    After successful testing, the plugin is located at build/bt-oryx.zip. This file can be uploaded directly to the production BT panel's application store.

    # Start the BT development container
    docker rm -f bt aapanel 2>/dev/null &&
    BT_KEY=$(cat $HOME/.bt/api.json |awk -F token_crypt '{print $2}' |cut -d'"' -f3) &&
    docker run -p 80:80 -p 443:443 -p 7800:7800 -p 1935:1935 -p 8000:8000/udp -p 10080:10080/udp \
        --env CANDIDATE=$(ifconfig en0 |grep 'inet ' |awk '{print $2}') \
        -v $(pwd)/build/oryx:/www/server/panel/plugin/oryx \
        -v $HOME/.bt/userInfo.json:/www/server/panel/data/userInfo.json \
        -v $HOME/.bt/api.json:/www/server/panel/config/api.json -e BT_KEY=$BT_KEY \
        --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:rw --cgroupns=host \
        -d --rm -it -v $(pwd):/g -w /g --name=bt ossrs/bt-plugin-dev:1
  9. Analyze Oryx performance with Go pprof

    main

    To perform performance analysis on Oryx, enable the Go pprof tool by setting the GO_PPROF environment variable. You can then run a CPU profile to identify hot functions.

    # Enable pprof
    GO_PPROF=localhost:6060 go run .
    
    # Run a 30-second CPU profile
    go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
  10. Build and run the Lightsail Installer

    main

    To develop the Lightsail installer, build the Docker image using the specific setup-ubuntu Dockerfile and then run it in daemon mode with necessary privileges and volume mounts.

    # Build the image
    docker rm -f script 2>/dev/null && \
    docker rmi srs-script-dev 2>/dev/null || echo OK && \
    docker build -t srs-script-dev -f scripts/setup-ubuntu/Dockerfile.script .
    
    # Run the container in daemon mode
    docker rm -f script 2>/dev/null && \
    docker run -p 2022:2022 -p 2443:2443 -p 1935:1935 -p 8000:8000/udp -p 10080:10080/udp \
        --env CANDIDATE=$(ifconfig en0 |grep 'inet ' |awk '{print $2}') \
        --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:rw --cgroupns=host \
        -d --rm -it -v $(pwd):/g -w /g --name=script srs-script-dev
    
    # Install Oryx using the script inside the container
    docker exec -it -w /tmp script bash /g/scripts/lightsail.sh 
  11. Software dependencies for ORYX

    main

    The ORYX platform depends on the following software components:

    • Docker: Required for containerization. Install via apt-get install -y docker.io.
    • Nginx: Used for reverse proxy and serving static files. Configuration files are located in platform/containers/conf/nginx.conf and include files in platform/containers/data/config/.
    • LEGO: Used for Let's Encrypt certificate management. Webroot verification is at platform/containers/data/.well-known/acme-challenge/ and certificates are in platform/containers/data/lego/.lego/certificates/.
    • SRS (Simple Realtime Server): The core media server. Configuration is at platform/containers/conf/srs.release.conf (mounted as /usr/local/srs/conf/srs.conf).
    • FFmpeg: Used for media processing. Tools like ffmpeg and ffprobe are provided in the ossrs/srs:ubuntu20 image.
  12. Deploy NGINX Edge Servers for HLS Caching

    main

    NGINX Edge servers act as the CDN layer, caching HLS streams from the Oryx origin to handle high client loads.

    To deploy an edge server, run the ossrs/oryx:nginx-hls-cdn-http image and provide the ORYX_SERVER environment variable pointing to your origin's IP and port.

    You can scale horizontally by deploying multiple edge servers. For even larger scales, you can configure NGINX servers to retrieve streams from other NGINX servers, creating a multi-layer CDN architecture.

    # Deploy an NGINX Edge server
    docker run --rm -it --name oryx-nginx01 -d \
      -p 80:80 -e ORYX_SERVER=<YOUR_ORYX_IP>:2022 \
      ossrs/oryx:nginx-hls-cdn-http