wowza and streaming. Lesson 13:

id.241224001.582761

Advanced Streaming Application Development with Wowza, Python, and Node.js

This lesson focuses on building advanced features into a cloud-based streaming application using Wowza Streaming Engine, Python, Node.js, and modern protocols like HLS, RTMP, and SRT. It also incorporates AWS Cloud, end-to-end (E2E) testing, and integration tests.


Part 1: Wowza Setup on AWS

Deploy Wowza on AWS EC2

  1. Launch an AWS EC2 Instance.

  2. Install Wowza Streaming Engine:

     wget https://www.wowza.com/downloads/WowzaStreamingEngine-x.x.x/wowza-streaming-engine-x.x.x-linux-x64-installer.run
     chmod +x wowza-streaming-engine-x.x.x-linux-x64-installer.run
     sudo ./wowza-streaming-engine-x.x.x-linux-x64-installer.run
    
  3. Open the Wowza Manager on http://<EC2-IP>:8088 and configure live streaming.

Create a Wowza Live Application

  1. Go to Applications > Add Live Application.

  2. Configure protocols (HLS, RTMP, SRT) for stream delivery.


Part 2: Backend Development with Python

Install Required Libraries

pip install flask requests boto3

Python API for Stream Management

from flask import Flask, request, jsonify
import requests
from requests.auth import HTTPBasicAuth

app = Flask(__name__)

# Wowza API credentials
WOWZA_API_URL = "http://<wowza-server-ip>:8087/v2"
WOWZA_USERNAME = "admin"
WOWZA_PASSWORD = "password"

@app.route('/start_stream', methods=['POST'])
def start_stream():
    stream_name = request.json.get('stream_name')
    url = f"{WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/{stream_name}.stream/actions/connect"
    response = requests.put(url, auth=HTTPBasicAuth(WOWZA_USERNAME, WOWZA_PASSWORD))
    if response.status_code == 200:
        return jsonify({"message": "Stream started successfully"}), 200
    return jsonify({"error": response.json()}), response.status_code

@app.route('/stop_stream', methods=['POST'])
def stop_stream():
    stream_name = request.json.get('stream_name')
    url = f"{WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/{stream_name}.stream/actions/disconnect"
    response = requests.put(url, auth=HTTPBasicAuth(WOWZA_USERNAME, WOWZA_PASSWORD))
    if response.status_code == 200:
        return jsonify({"message": "Stream stopped successfully"}), 200
    return jsonify({"error": response.json()}), response.status_code

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Part 3: Frontend API with Node.js

Install Required Packages

npm install express axios

Node.js Client for Streaming

const express = require('express');
const axios = require('axios');

const app = express();
app.use(express.json());

const WOWZA_API_URL = "http://<wowza-server-ip>:8087/v2";
const WOWZA_USERNAME = "admin";
const WOWZA_PASSWORD = "password";

app.post('/start', async (req, res) => {
    const { streamName } = req.body;
    try {
        const response = await axios.put(
            `${WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/${streamName}.stream/actions/connect`,
            {},
            { auth: { username: WOWZA_USERNAME, password: WOWZA_PASSWORD } }
        );
        res.json({ message: 'Stream started successfully' });
    } catch (error) {
        res.status(error.response.status).json({ error: error.response.data });
    }
});

app.post('/stop', async (req, res) => {
    const { streamName } = req.body;
    try {
        const response = await axios.put(
            `${WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/${streamName}.stream/actions/disconnect`,
            {},
            { auth: { username: WOWZA_USERNAME, password: WOWZA_PASSWORD } }
        );
        res.json({ message: 'Stream stopped successfully' });
    } catch (error) {
        res.status(error.response.status).json({ error: error.response.data });
    }
});

app.listen(3000, () => {
    console.log('Node.js server running on port 3000');
});

Part 4: Testing with Frameworks

Python End-to-End Test

pip install pytest
import requests

BASE_URL = "http://<backend-server-ip>:5000"

def test_start_stream():
    response = requests.post(f"{BASE_URL}/start_stream", json={"stream_name": "testStream"})
    assert response.status_code == 200
    assert response.json()["message"] == "Stream started successfully"

def test_stop_stream():
    response = requests.post(f"{BASE_URL}/stop_stream", json={"stream_name": "testStream"})
    assert response.status_code == 200
    assert response.json()["message"] == "Stream stopped successfully"

Node.js Integration Test

npm install jest supertest
const request = require('supertest');
const app = require('./app'); // Import the Node.js app

describe('Stream API', () => {
    test('Start Stream', async () => {
        const response = await request(app)
            .post('/start')
            .send({ streamName: 'testStream' });
        expect(response.statusCode).toBe(200);
        expect(response.body.message).toBe('Stream started successfully');
    });

    test('Stop Stream', async () => {
        const response = await request(app)
            .post('/stop')
            .send({ streamName: 'testStream' });
        expect(response.statusCode).toBe(200);
        expect(response.body.message).toBe('Stream stopped successfully');
    });
});

Part 5: Protocols and Features

  • HLS: HTTP-based streaming for adaptive bitrate delivery.

  • RTMP: Real-Time Messaging Protocol for low-latency live streaming.

  • SRT: Secure Reliable Transport for error-free, low-latency video delivery.


What We Achieved:

  • Developed APIs for managing streams with Wowza.

  • Integrated Python and Node.js for backend/frontend functionality.

  • Implemented end-to-end (E2E) and integration tests.

  • Utilized AWS and advanced protocols like HLS, RTMP, and SRT.

This lesson encapsulates the skills required for a senior engineer working on cloud-based streaming systems.