wowza and streaming. lesson 5.

id.241224001.987654

Lesson 5: Wowza REST API for Streaming

This lesson demonstrates how to use Wowza REST APIs with Node.js and Python to manage applications, streams, and transcoder settings.


Step 1: Set Up Wowza REST API

  1. Enable REST API:

    • Log in to Wowza Streaming Engine Manager.

    • Go to Server > REST API.

    • Enable the REST API and note the Server REST URI (e.g., http://<server-ip>:8087/v2).

    • Create a user for API access with a username and password.

  2. Test API with cURL:

     curl -u <username>:<password> http://<server-ip>:8087/v2/server
    

Step 2: Node.js Implementation

Example: Create a New Application

  1. Install Dependencies:

     npm install axios
    
  2. Node.js Code:

     const axios = require('axios');
    
     const wowzaApiUrl = 'http://<server-ip>:8087/v2';
     const username = 'your-username';
     const password = 'your-password';
    
     // Create a new live application
     async function createApplication() {
         const appConfig = {
             appType: "Live",
             name: "NodeLiveApp",
             dvrEnabled: false,
             drmEnabled: false
         };
    
         try {
             const response = await axios.post(
                 `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications`,
                 appConfig,
                 {
                     auth: { username, password },
                     headers: { 'Content-Type': 'application/json' }
                 }
             );
             console.log('Application created:', response.data);
         } catch (error) {
             console.error('Error creating application:', error.response.data);
         }
     }
    
     createApplication();
    

Step 3: Python Implementation

Example: Get Active Streams

  1. Install Dependencies:

     pip install requests
    
  2. Python Code:

     import requests
     from requests.auth import HTTPBasicAuth
    
     wowza_api_url = 'http://<server-ip>:8087/v2'
     username = 'your-username'
     password = 'your-password'
    
     # Fetch active streams
     def get_active_streams():
         url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/instances/_definst_/incomingstreams"
         try:
             response = requests.get(url, auth=HTTPBasicAuth(username, password))
             if response.status_code == 200:
                 streams = response.json()
                 print("Active Streams:", streams)
             else:
                 print("Error:", response.status_code, response.json())
         except Exception as e:
             print("Error fetching streams:", e)
    
     get_active_streams()
    

Step 4: Example API Use Cases

  1. Start a Transcoder (Node.js):

     async function startTranscoder(streamName) {
         const url = `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/instances/_definst_/transcoder/actions/start`;
         try {
             const response = await axios.put(
                 url,
                 { streamName },
                 { auth: { username, password } }
             );
             console.log('Transcoder started:', response.data);
         } catch (error) {
             console.error('Error starting transcoder:', error.response.data);
         }
     }
    
     startTranscoder('mystream');
    
  2. Stop a Stream (Python):

     def stop_stream(stream_name):
         url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/instances/_definst_/incomingstreams/{stream_name}/actions/disable"
         try:
             response = requests.put(url, auth=HTTPBasicAuth(username, password))
             if response.status_code == 200:
                 print("Stream stopped:", response.json())
             else:
                 print("Error:", response.status_code, response.json())
         except Exception as e:
             print("Error stopping stream:", e)
    
     stop_stream('mystream')
    

Step 5: Test with a Running Wowza Server

Create, Stream, and Manage:

  1. Create Application:

    • Use the Node.js script to create a new live application.
  2. Stream Content:

    • Stream using OBS to the application (e.g., rtmp://<server-ip>:1935/NodeLiveApp).
  3. Fetch Streams:

    • Use the Python script to monitor active streams.
  4. Control Streams:

    • Start/stop transcoding or disable streams using API calls.

Next Steps: