Example Flow With Node-Red
Kurento Client Setup and ICE Handling
Purpose:
This flow initializes the Kurento Media Server (KMS) client, handles WebRTC session management, and gathers ICE candidates for WebRTC connections.
Nodes and Logic:
-
Inject Node (set kurento client to null): Resets the Kurento client context by setting the kurento-client variable to null. This is used to clear any previous client session.
-
Function Node (clear ICE candidates queue): Initializes and stores a function in the global context that clears the ICE candidates queue for a particular session. This function is invoked whenever a WebRTC session ends to ensure no stale ICE candidates remain.
-
Function Node (create Kurento MS element): Defines and stores a function that creates Kurento Media Server (KMS) elements (e.g., WebRtcEndpoint, PlayerEndpoint) using the Kurento client. It returns a promise that resolves with the created element or rejects with an error.
-
Function Node (process SDP offer): Defines a function that processes the SDP offer from a WebRTC client, generating an SDP answer. The function is stored in the global context and is used by other nodes when establishing WebRTC sessions.
-
Function Node (gather and send ICE candidates): Defines a function to gather and handle ICE candidates for a WebRTC connection. It adds queued ICE candidates to the WebRtcEndpoint and listens for new ICE candidates to send back to the client. These candidates are sent over WebSockets.
-
WebSocket Nodes: Handles WebSocket communication between the Node-RED flow and WebRTC clients for ICE candidate exchange and SDP negotiation.
WebRTC Pipeline Creation and Management
Purpose:
To create and manage media pipelines in Kurento, allowing WebRTC clients to stream video from media sources (e.g., a video file or live camera feed).
Nodes and Logic:
-
WebSocket In (WebRTC signaling): Receives WebRTC signaling messages from clients, including SDP offers and ICE candidates.
-
Function Node (create pipeline and play video source): Handles the creation of media pipelines in Kurento. This involves:
-
Creating a MediaPipeline in Kurento.
-
Creating a PlayerEndpoint to play a media source (e.g., video file) from the provided URL.
-
Creating a WebRtcEndpoint to send the media stream to the client.
-
Processing the client’s SDP offer to establish the WebRTC connection.
-
Handling ICE candidates for establishing a reliable connection.
-
Starting the media playback on the PlayerEndpoint and connecting it to the WebRtcEndpoint.
-
-
WebSocket Out (send SDP answer and ICE candidates): Sends the SDP answer and ICE candidates to the WebRTC client over WebSockets to complete the WebRTC handshake.
How it works:
When a client sends a request to create a new WebRTC pipeline, the flow sets up the necessary Kurento media elements (PlayerEndpoint, WebRtcEndpoint) and begins streaming media to the client. SDP offers are processed, and ICE candidates are exchanged to establish the WebRTC connection.
Kurento and Orion Integration (Metadata and Stats)
Purpose:
To fetch and send metadata (such as version, entities, and subscriptions) from Orion Context Broker and relay WebRTC session statistics to Orion.
Nodes and Logic:
-
HTTP Request Nodes (metadata queries): Fetch metadata (e.g., version info, entities, and subscriptions) from the Orion Context Broker using REST API requests.
-
Change Nodes: Format the metadata and prepare it for WebSocket communication.
-
WebSocket Out: Sends metadata to a WebSocket client, likely a web application that displays the Kurento and Orion integration status.
-
Function Nodes (request stats): Periodically requests statistics from WebRTC peers (e.g., frame rate, video resolution).
-
HTTP Request (send stats to Orion): Sends the average WebRTC session stats (frame rate, video resolution) to Orion as an entity attribute update. This allows Orion to monitor the WebRTC sessions' quality in real time.
Session Management and Pipeline Cleanup
Purpose:
To manage the lifecycle of WebRTC sessions and release Kurento pipelines when a session ends.
Nodes and Logic:
-
Function Node (release pipeline): Releases the Kurento pipeline and WebRtcEndpoint when a WebRTC session ends. It also clears the ICE candidates queue to ensure no stale data remains.
-
Link Nodes (stop session): Handles session termination requests and triggers the release of resources associated with the WebRTC session.