Skip to content

Ocyss/srs-auth / .agents/skills/srs-develop

srs-develop

Verified

Develop, modify, debug, review, maintain, and explain the SRS and Oryx codebases and the SRS Docker image toolchain. Use for planned changes to the next-generation SRS Go proxy, SRS browser player, ossrs/dev-docker images, or Oryx Go backend, React dashboard, integrated runtime, packaging, installers, releases, and tests; bug maintenance; issue and pull-request triage; pull-request review; and Learn Code questions. The C++ SRS server is in maintenance mode, and planned Go origin and edge development is not yet supported. NOT for end-user support, usage questions, or configuration help — use srs-support for those.

FileKindSize
SKILL.mdskill md8.2 KB
references/develop-code.mdmarkdown15.1 KB
references/fix-a-bug.mdmarkdown6.6 KB
references/integration-tests.mdmarkdown2.9 KB
references/learn-code.mdmarkdown3.3 KB
references/oryx-issues.mdmarkdown218 B
references/oryx-local-development.mdmarkdown11.9 KB
references/review-a-pr.mdmarkdown3.5 KB
references/scan-issues.mdmarkdown993 B
references/scan-prs.mdmarkdown1.4 KB
references/srs-issues.mdmarkdown58.5 KB
references/version-and-changelog.mdmarkdown2.3 KB
scripts/browser-page-url-test.jsscript2.7 KB
scripts/gb28181-api-timeout-test.shscript5.3 KB
scripts/gb28181-create-session.shscript2.1 KB
scripts/gb28181-publish-stream.shscript3.9 KB
scripts/gb28181-tcp-disconnect-test.shscript4.3 KB
scripts/oryx-api-smoke-test.shscript4.2 KB
scripts/oryx-camera-test.shscript12.2 KB
scripts/oryx-forward-test.shscript10.9 KB
scripts/oryx-live-room-test.shscript10.2 KB
scripts/oryx-live-streaming-test.shscript12.6 KB
scripts/oryx-record-test.shscript10.4 KB
scripts/oryx-run-tests.shscript2.3 KB
scripts/oryx-stack-start.shscript5.2 KB
scripts/oryx-stack-stop.shscript1.8 KB
scripts/oryx-transcode-test.shscript9.8 KB
scripts/oryx-vlive-test.shscript12.0 KB
scripts/proxy-e2e-bearer-auth-test.shscript12.0 KB
scripts/proxy-e2e-cluster-test.shscript10.4 KB
scripts/proxy-e2e-edge-test.shscript10.6 KB
scripts/proxy-e2e-redis-test.shscript11.7 KB
scripts/proxy-e2e-srt-test.shscript9.5 KB
scripts/proxy-e2e-test.shscript5.7 KB
scripts/proxy-e2e-transmux-test.shscript7.3 KB
scripts/proxy-e2e-whip-test.shscript11.5 KB
scripts/proxy-utest.shscript1.5 KB
scripts/setup-ffmpeg-with-whip.shscript7.5 KB

scripts/proxy-e2e-redis-test.sh

View on GitHub
#!/bin/bash
# E2E test for RTMP proxy Redis load balancer: starts two proxy instances with
# Redis-backed shared state + one SRS origin. The origin registers through proxy A,
# publishing goes through proxy A, and playback goes through proxy B. Playback must
# succeed because proxy B resolves the stream-to-origin mapping from Redis.
set -e

SCRIPT_DIR="$(cd -P "$(dirname "$0")" && pwd)"
# Walk up from SCRIPT_DIR looking for go.mod. This avoids brittle "../../../.."
# counting when the skills directory is reached via a symlink (which changes
# the symbolic vs. physical depth).
WORKSPACE="$SCRIPT_DIR"
while [[ "$WORKSPACE" != "/" && ! -f "$WORKSPACE/go.mod" ]]; do
  WORKSPACE="$(dirname "$WORKSPACE")"
done

if [[ ! -f "$WORKSPACE/go.mod" ]]; then
  echo "Error: go.mod not found walking up from: $SCRIPT_DIR" >&2
  exit 1
fi

# Ports — use high ports to avoid conflicts with running services.
# Each proxy starts ALL servers, so each proxy needs a unique full port set.
PROXY_A_RTMP_PORT=11935
PROXY_A_HTTP_API_PORT=11985
PROXY_A_HTTP_SERVER_PORT=18080
PROXY_A_WEBRTC_PORT=18000
PROXY_A_SRT_PORT=20080
PROXY_A_SYSTEM_API_PORT=12025

PROXY_B_RTMP_PORT=11936
PROXY_B_HTTP_API_PORT=11986
PROXY_B_HTTP_SERVER_PORT=18081
PROXY_B_WEBRTC_PORT=18001
PROXY_B_SRT_PORT=20081
PROXY_B_SYSTEM_API_PORT=12026

REDIS_HOST="${PROXY_REDIS_HOST:-127.0.0.1}"
REDIS_PORT="${PROXY_REDIS_PORT:-6379}"
REDIS_PASSWORD="${PROXY_REDIS_PASSWORD:-}"
REDIS_DB="${PROXY_REDIS_DB:-0}"
PYTHON_BIN="${PYTHON_BIN:-python3}"
if [[ -n "${PROXY_REDIS_KEY_PREFIX:-}" ]]; then
  REDIS_KEY_PREFIX="$PROXY_REDIS_KEY_PREFIX"
else
  REDIS_KEY_PREFIX="e2e-$(LC_ALL=C tr -dc 'a-f0-9' </dev/urandom | head -c 8)"
fi

SOURCE_FLV="$WORKSPACE/trunk/doc/source.flv"
SRS_BINARY="$WORKSPACE/trunk/objs/srs"
# Keep the stream name recognizable in logs. Redis state isolation comes from
# REDIS_KEY_PREFIX, which is unique per test invocation unless explicitly set.
STREAM_NAME="redis$(date +%s)"
STREAM_PATH="live/$STREAM_NAME"
TEST_STREAM_URL="__defaultVhost__/$STREAM_PATH"

# PIDs to clean up on exit.
PROXY_A_PID=""
PROXY_B_PID=""
ORIGIN_PID=""
FFMPEG_PID=""

redis_cli() {
  if [[ -n "$REDIS_PASSWORD" ]]; then
    redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" -a "$REDIS_PASSWORD" -n "$REDIS_DB" "$@"
  else
    redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" -n "$REDIS_DB" "$@"
  fi
}

redis_key() {
  local key="$1"
  if [[ -n "$REDIS_KEY_PREFIX" ]]; then
    printf '%s:%s' "$REDIS_KEY_PREFIX" "$key"
  else
    printf '%s' "$key"
  fi
}

cleanup_redis_state() {
  # Remove only the Redis records created by this E2E test. Never flush the DB,
  # and never delete every srs-proxy-* key because the same Redis DB may be used
  # by another proxy/origin-cluster test or by a developer's local proxy.
  if ! command -v redis-cli &>/dev/null; then
    return
  fi
  if ! command -v "$PYTHON_BIN" &>/dev/null; then
    echo "Skip Redis cleanup: $PYTHON_BIN is not available"
    return
  fi
  if ! redis_cli ping 2>/dev/null | grep -q "PONG"; then
    echo "Skip Redis cleanup: Redis is not available at $REDIS_HOST:$REDIS_PORT db=$REDIS_DB"
    return
  fi

  local count=0
  local stream_key
  stream_key="$(redis_key "srs-proxy-url:$TEST_STREAM_URL")"
  if [[ "$(redis_cli exists "$stream_key" 2>/dev/null || echo 0)" != "0" ]]; then
    redis_cli del "$stream_key" >/dev/null 2>&1 || true
    count=$((count + 1))
  fi

  # The origin server generates its server key from runtime IDs, so discover only
  # server records that match this test origin's identity and configured ports.
  local server_keys=()
  local server_pattern
  server_pattern="$(redis_key "srs-proxy-server:*")"
  local key value
  while IFS= read -r key; do
    [[ -z "$key" ]] && continue
    value="$(redis_cli get "$key" 2>/dev/null || true)"
    if [[ "$value" == *'"device_id":"origin1"'* && \
          "$value" == *'"rtmp":["19351"]'* && \
          "$value" == *'"http":["8081"]'* && \
          "$value" == *'"api":["19851"]'* ]]; then
      server_keys+=("$key")
      redis_cli del "$key" >/dev/null 2>&1 || true
      count=$((count + 1))
    fi
  done < <(redis_cli --scan --pattern "$server_pattern" 2>/dev/null || true)

  # Keep the shared server index, but remove only the test origin server keys.
  if [[ ${#server_keys[@]} -gt 0 ]]; then
    local servers_key servers_json updated_json
    servers_key="$(redis_key "srs-proxy-all-servers")"
    servers_json="$(redis_cli get "$servers_key" 2>/dev/null || true)"
    if [[ -n "$servers_json" ]]; then
      updated_json="$($PYTHON_BIN - "$servers_json" "${server_keys[@]}" <<'PY'
import json, sys
servers = json.loads(sys.argv[1]) if sys.argv[1] else []
remove = set(sys.argv[2:])
servers = [server for server in servers if server not in remove]
print(json.dumps(servers, separators=(",", ":")))
PY
)"
      if [[ "$updated_json" == "[]" ]]; then
        redis_cli del "$servers_key" >/dev/null 2>&1 || true
      else
        redis_cli set "$servers_key" "$updated_json" >/dev/null 2>&1 || true
      fi
    fi
  fi

  echo "Cleaned $count Redis proxy test key(s)."
}
cleanup() {
  echo ""
  echo "=== Cleaning up ==="
  for pid in $PROXY_A_PID $PROXY_B_PID $ORIGIN_PID $FFMPEG_PID; do
    if kill -0 "$pid" 2>/dev/null; then
      kill "$pid" 2>/dev/null || true
    fi
  done
  sleep 1
  for pid in $PROXY_A_PID $PROXY_B_PID $ORIGIN_PID $FFMPEG_PID; do
    if kill -0 "$pid" 2>/dev/null; then
      kill -9 "$pid" 2>/dev/null || true
    fi
  done
  cleanup_redis_state
  echo "Cleanup done."
}
trap cleanup EXIT

echo "=== E2E RTMP Proxy Redis Load Balancer Test ==="
echo "Workspace: $WORKSPACE"
echo "Redis: $REDIS_HOST:$REDIS_PORT db=$REDIS_DB"
echo "Redis key prefix: $REDIS_KEY_PREFIX"
echo ""

# --- Pre-checks ---
if [[ ! -f "$SOURCE_FLV" ]]; then
  echo "Error: test source not found: $SOURCE_FLV" >&2
  exit 1
fi
if ! command -v ffmpeg &>/dev/null; then
  echo "Error: ffmpeg not found in PATH" >&2
  exit 1
fi
if ! command -v ffprobe &>/dev/null; then
  echo "Error: ffprobe not found in PATH" >&2
  exit 1
fi
if ! command -v redis-cli &>/dev/null; then
  echo "Error: redis-cli not found in PATH" >&2
  echo "Install Redis on macOS with: brew install redis" >&2
  exit 1
fi
if ! redis_cli ping 2>/dev/null | grep -q "PONG"; then
  echo "Error: Redis is not available at $REDIS_HOST:$REDIS_PORT db=$REDIS_DB" >&2
  echo "Start Redis on macOS with: brew services start redis" >&2
  echo "Or run a foreground Redis with: redis-server" >&2
  exit 1
fi

# Origin ports (from origin1-for-proxy.conf).
ORIGIN_RTMP_PORT=19351
ORIGIN_HTTP_PORT=8081
ORIGIN_API_PORT=19851
ORIGIN_RTC_PORT=8001
ORIGIN_SRT_PORT=10081

# --- Step 0: Clean up stale state ---
# Remove stale SRS PID file that prevents restart.
rm -f "$WORKSPACE/trunk/objs/origin1.pid"
cleanup_redis_state
# Kill any leftover processes on our ports (proxy A + proxy B + origin).
ALL_PORTS="$PROXY_A_RTMP_PORT $PROXY_A_HTTP_API_PORT $PROXY_A_HTTP_SERVER_PORT $PROXY_A_WEBRTC_PORT $PROXY_A_SRT_PORT $PROXY_A_SYSTEM_API_PORT $PROXY_B_RTMP_PORT $PROXY_B_HTTP_API_PORT $PROXY_B_HTTP_SERVER_PORT $PROXY_B_WEBRTC_PORT $PROXY_B_SRT_PORT $PROXY_B_SYSTEM_API_PORT $ORIGIN_RTMP_PORT $ORIGIN_HTTP_PORT $ORIGIN_API_PORT $ORIGIN_RTC_PORT $ORIGIN_SRT_PORT"
for port in $ALL_PORTS; do
  lsof -ti :"$port" 2>/dev/null | xargs kill 2>/dev/null || true
done
sleep 1

# --- Step 1: Build proxy ---
echo "=== Step 1: Building proxy ==="
cd "$WORKSPACE"
make -s 2>&1
echo "Proxy built: $WORKSPACE/bin/srs-proxy"

# --- Step 2: Build SRS origin (if not already built) ---
if [[ ! -f "$SRS_BINARY" ]]; then
  echo "=== Step 2: Building SRS origin ==="
  cd "$WORKSPACE/trunk"
  ./configure && make 2>&1 | tail -3
  echo "SRS origin built: $SRS_BINARY"
else
  echo "=== Step 2: SRS origin already built ==="
fi

# --- Step 3: Start proxy A ---
echo "=== Step 3: Starting proxy A (RTMP :$PROXY_A_RTMP_PORT, System API :$PROXY_A_SYSTEM_API_PORT) ==="
cd "$WORKSPACE"
env PROXY_RTMP_SERVER=$PROXY_A_RTMP_PORT \
    PROXY_HTTP_API=$PROXY_A_HTTP_API_PORT \
    PROXY_HTTP_SERVER=$PROXY_A_HTTP_SERVER_PORT \
    PROXY_WEBRTC_SERVER=$PROXY_A_WEBRTC_PORT \
    PROXY_SRT_SERVER=$PROXY_A_SRT_PORT \
    PROXY_SYSTEM_API=$PROXY_A_SYSTEM_API_PORT \
    PROXY_LOAD_BALANCER_TYPE=redis \
    PROXY_REDIS_HOST="$REDIS_HOST" \
    PROXY_REDIS_PORT="$REDIS_PORT" \
    PROXY_REDIS_PASSWORD="$REDIS_PASSWORD" \
    PROXY_REDIS_DB="$REDIS_DB" \
    PROXY_REDIS_KEY_PREFIX="$REDIS_KEY_PREFIX" \
    ./bin/srs-proxy >/tmp/srs-proxy-redis-a-e2e.log 2>&1 &
PROXY_A_PID=$!
echo "Proxy A PID: $PROXY_A_PID"
sleep 1

if ! kill -0 "$PROXY_A_PID" 2>/dev/null; then
  echo "Error: proxy A failed to start. Logs:" >&2
  cat /tmp/srs-proxy-redis-a-e2e.log >&2
  exit 1
fi
echo "Proxy A started."

# --- Step 4: Start proxy B ---
echo "=== Step 4: Starting proxy B (RTMP :$PROXY_B_RTMP_PORT, System API :$PROXY_B_SYSTEM_API_PORT) ==="
cd "$WORKSPACE"
env PROXY_RTMP_SERVER=$PROXY_B_RTMP_PORT \
    PROXY_HTTP_API=$PROXY_B_HTTP_API_PORT \
    PROXY_HTTP_SERVER=$PROXY_B_HTTP_SERVER_PORT \
    PROXY_WEBRTC_SERVER=$PROXY_B_WEBRTC_PORT \
    PROXY_SRT_SERVER=$PROXY_B_SRT_PORT \
    PROXY_SYSTEM_API=$PROXY_B_SYSTEM_API_PORT \
    PROXY_LOAD_BALANCER_TYPE=redis \
    PROXY_REDIS_HOST="$REDIS_HOST" \
    PROXY_REDIS_PORT="$REDIS_PORT" \
    PROXY_REDIS_PASSWORD="$REDIS_PASSWORD" \
    PROXY_REDIS_DB="$REDIS_DB" \
    PROXY_REDIS_KEY_PREFIX="$REDIS_KEY_PREFIX" \
    ./bin/srs-proxy >/tmp/srs-proxy-redis-b-e2e.log 2>&1 &
PROXY_B_PID=$!
echo "Proxy B PID: $PROXY_B_PID"
sleep 1

if ! kill -0 "$PROXY_B_PID" 2>/dev/null; then
  echo "Error: proxy B failed to start. Logs:" >&2
  cat /tmp/srs-proxy-redis-b-e2e.log >&2
  exit 1
fi
echo "Proxy B started."

# --- Step 5: Start SRS origin ---
echo "=== Step 5: Starting SRS origin ==="
ulimit -n 10000 2>/dev/null || true
cd "$WORKSPACE/trunk"
./objs/srs -c conf/origin1-for-proxy.conf >/tmp/srs-origin-redis-e2e.log 2>&1 &
ORIGIN_PID=$!
echo "SRS origin PID: $ORIGIN_PID"

# Wait for SRS to start and register with proxy A (heartbeat interval is 9s).
echo "Waiting for SRS origin to register with proxy A and Redis (up to 15s)..."
sleep 12

if ! kill -0 "$ORIGIN_PID" 2>/dev/null; then
  echo "Error: SRS origin failed to start. Logs:" >&2
  cat /tmp/srs-origin-redis-e2e.log >&2
  exit 1
fi
if ! redis_cli --scan --pattern "$(redis_key "srs-proxy-server:*")" | grep -q 'srs-proxy-server:'; then
  echo "Error: SRS origin did not register in Redis. Proxy A logs:" >&2
  cat /tmp/srs-proxy-redis-a-e2e.log >&2
  exit 1
fi
echo "SRS origin started and registered in Redis."

# --- Step 6: Publish RTMP stream to proxy A ---
echo "=== Step 6: Publishing RTMP stream to proxy A ==="
ffmpeg -stream_loop -1 -re -i "$SOURCE_FLV" -c copy -f flv \
  "rtmp://localhost:$PROXY_A_RTMP_PORT/$STREAM_PATH" >/tmp/srs-ffmpeg-redis-e2e.log 2>&1 &
FFMPEG_PID=$!
echo "FFmpeg publisher PID: $FFMPEG_PID"

# Wait for stream to stabilize.
sleep 5

if ! kill -0 "$FFMPEG_PID" 2>/dev/null; then
  echo "Error: FFmpeg publisher failed. Logs:" >&2
  cat /tmp/srs-ffmpeg-redis-e2e.log >&2
  exit 1
fi
echo "Stream publishing through proxy A."

# --- Step 7: Verify RTMP playback through proxy B ---
echo "=== Step 7: Verifying RTMP playback through proxy B ==="
PROBE_OUTPUT=$(ffprobe -v error -show_streams \
  "rtmp://localhost:$PROXY_B_RTMP_PORT/$STREAM_PATH" 2>&1 || true)

if echo "$PROBE_OUTPUT" | grep -q "codec_type=video"; then
  echo "PASS: Video stream detected through proxy B."
else
  echo "FAIL: No video stream detected through proxy B." >&2
  echo "ffprobe output:" >&2
  echo "$PROBE_OUTPUT" >&2
  echo "Proxy B logs:" >&2
  cat /tmp/srs-proxy-redis-b-e2e.log >&2
  exit 1
fi

if echo "$PROBE_OUTPUT" | grep -q "codec_type=audio"; then
  echo "PASS: Audio stream detected through proxy B."
else
  echo "FAIL: No audio stream detected through proxy B." >&2
  echo "ffprobe output:" >&2
  echo "$PROBE_OUTPUT" >&2
  echo "Proxy B logs:" >&2
  cat /tmp/srs-proxy-redis-b-e2e.log >&2
  exit 1
fi

echo ""
echo "=== E2E RTMP Proxy Redis Load Balancer Test PASSED ==="
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 321kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 321kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 321kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 197kUpdated Claude CodeCodexVerifiedInstall