wowza and streaming . lesson 6 .

id.241224001.472186

Lesson 6: Managing Stream Targets with Wowza REST API

In this lesson, we will use the Wowza REST API to manage stream targets. Stream targets allow you to push your live streams to external destinations like YouTube, Facebook, or other RTMP servers.


Step 1: Prerequisites

  1. Enable REST API in Wowza:

    • Log in to the Wowza Streaming Engine Manager and ensure REST API is enabled.
  2. Credentials:

    • You will need the API username and password.

Step 2: Create a Stream Target

Node.js Implementation

  1. Install Dependencies:

     npm install axios
    
  2. Code:

     const axios = require('axios');
    
     const wowzaApiUrl = 'http://<server-ip>:8087/v2';
     const username = 'your-username';
     const password = 'your-password';
    
     async function createStreamTarget() {
         const targetConfig = {
             id: "YouTubeTarget",
             provider: "rtmp",
             streamName: "your-stream-key", // Replace with your YouTube stream key
             host: "a.rtmp.youtube.com", // YouTube RTMP server
             application: "live2"
         };
    
         try {
             const response = await axios.post(
                 `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamtargets`,
                 targetConfig,
                 {
                     auth: { username, password },
                     headers: { 'Content-Type': 'application/json' }
                 }
             );
             console.log('Stream target created:', response.data);
         } catch (error) {
             console.error('Error creating stream target:', error.response.data);
         }
     }
    
     createStreamTarget();
    

Python Implementation

  1. Install Dependencies:

     pip install requests
    
  2. Code:

     import requests
     from requests.auth import HTTPBasicAuth
    
     wowza_api_url = 'http://<server-ip>:8087/v2'
     username = 'your-username'
     password = 'your-password'
    
     def create_stream_target():
         target_config = {
             "id": "YouTubeTarget",
             "provider": "rtmp",
             "streamName": "your-stream-key",  # Replace with your YouTube stream key
             "host": "a.rtmp.youtube.com",  # YouTube RTMP server
             "application": "live2"
         }
    
         url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamtargets"
         try:
             response = requests.post(
                 url,
                 json=target_config,
                 auth=HTTPBasicAuth(username, password)
             )
             if response.status_code == 201:
                 print("Stream target created:", response.json())
             else:
                 print("Error:", response.status_code, response.json())
         except Exception as e:
             print("Error creating stream target:", e)
    
     create_stream_target()
    

Step 3: Enable the Stream Target

Node.js Implementation

async function enableStreamTarget(targetId) {
    try {
        const response = await axios.put(
            `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamtargets/${targetId}/actions/enable`,
            {},
            { auth: { username, password } }
        );
        console.log('Stream target enabled:', response.data);
    } catch (error) {
        console.error('Error enabling stream target:', error.response.data);
    }
}

enableStreamTarget('YouTubeTarget');

Python Implementation

def enable_stream_target(target_id):
    url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamtargets/{target_id}/actions/enable"
    try:
        response = requests.put(url, auth=HTTPBasicAuth(username, password))
        if response.status_code == 200:
            print("Stream target enabled:", response.json())
        else:
            print("Error:", response.status_code, response.json())
    except Exception as e:
        print("Error enabling stream target:", e)

enable_stream_target("YouTubeTarget")

Step 4: Monitor Stream Target

Node.js Implementation

async function getStreamTarget(targetId) {
    try {
        const response = await axios.get(
            `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamtargets/${targetId}`,
            { auth: { username, password } }
        );
        console.log('Stream target details:', response.data);
    } catch (error) {
        console.error('Error fetching stream target details:', error.response.data);
    }
}

getStreamTarget('YouTubeTarget');

Python Implementation

def get_stream_target(target_id):
    url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamtargets/{target_id}"
    try:
        response = requests.get(url, auth=HTTPBasicAuth(username, password))
        if response.status_code == 200:
            print("Stream target details:", response.json())
        else:
            print("Error:", response.status_code, response.json())
    except Exception as e:
        print("Error fetching stream target details:", e)

get_stream_target("YouTubeTarget")

Example Use Case

  1. Scenario:

    • Push a live stream from Wowza to YouTube Live.

    • YouTube RTMP URL: rtmp://a.rtmp.youtube.com/live2

    • Stream Key: your-stream-key

  2. Steps:

    • Use the Node.js or Python script to create the stream target.

    • Enable the stream target.

    • Monitor the stream target for status.


Next Steps:

  • Automate stream target management with Node.js or Python.

  • Explore advanced Wowza REST API features like stream transcoding and logging.