wowza and streaming. lesson 12.

id.241224001.672184

Building a Streaming Application with Wowza, Python, and Node.js

This lesson demonstrates how to build and integrate a cloud-based streaming application using Wowza Streaming Engine, Python, Node.js, and cloud platforms like AWS. We'll cover the essential elements, including video protocols, testing, and integration tests.

The application integrates HLS, SRT, and RTMP protocols while leveraging AWS for cloud infrastructure.


Part 1: Setting Up Wowza on AWS

  1. Deploy Wowza Streaming Engine on AWS:

    • Launch an AWS EC2 instance.

    • Install Wowza Streaming Engine on the instance:

        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
      
    • Open Wowza Manager on port 8088 and configure your live application.

  2. Create a Live Application:

    • Go to Applications > Add Live Application.

    • Configure protocols (e.g., HLS, RTMP, and SRT).


Part 2: Python Backend for Stream Management

Install Dependencies:

pip install flask requests boto3

Code: Python API for Stream Management

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

app = Flask(__name__)

# Wowza REST API configuration
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')
    stream_file = f"{stream_name}.stream"

    url = f"{WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/{stream_file}/actions/connect"
    response = requests.put(url, auth=HTTPBasicAuth(WOWZA_USERNAME, WOWZA_PASSWORD))

    if response.status_code == 200:
        return jsonify({"message": "Stream started successfully"}), 200
    else:
        return jsonify({"error": response.json()}), response.status_code

@app.route('/stop_stream', methods=['POST'])
def stop_stream():
    stream_name = request.json.get('stream_name')
    stream_file = f"{stream_name}.stream"

    url = f"{WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/{stream_file}/actions/disconnect"
    response = requests.put(url, auth=HTTPBasicAuth(WOWZA_USERNAME, WOWZA_PASSWORD))

    if response.status_code == 200:
        return jsonify({"message": "Stream stopped successfully"}), 200
    else:
        return jsonify({"error": response.json()}), response.status_code

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

Part 3: Node.js Frontend for Video Streaming

Install Dependencies:

npm install express axios

Code: 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;
    const url = `${WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/${streamName}.stream/actions/connect`;

    try {
        const response = await axios.put(url, {}, {
            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;
    const url = `${WOWZA_API_URL}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/${streamName}.stream/actions/disconnect`;

    try {
        const response = await axios.put(url, {}, {
            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: End-to-End Integration Tests

Testing Frameworks:

  • Python: pytest

  • Node.js: jest

Python Integration 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().get("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().get("message") == "Stream stopped successfully"

Node.js Integration Test:

npm install jest supertest
const request = require('supertest');
const app = require('./app'); // Import your 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: Streaming Protocols Overview

  • HLS: For HTTP-based adaptive streaming to mobile and web.

  • RTMP: For low-latency streaming from encoders.

  • SRT: For secure, low-latency live streaming.


What We Covered:

  • Setting up Wowza for streaming on AWS.

  • Managing streams using Python and Node.js.

  • Writing E2E and integration tests.

  • Using HLS, RTMP, and SRT protocols.

This project demonstrates the role of a senior engineer by combining backend, frontend, testing, and streaming expertise.