API usage examples

Ready-to-use examples of API usage

SRT source sending without re-encoding to two destinations

  1. Ensure your xNodeServer is running and accessible.

  2. Import the xMediaNode client package

    import React, {useMemo, useState} from 'react';
    import {
        EndpointDirection,
        EndpointPropsType,
        EndpointTypes,
        IChannel,
        IDestination, ILicense,
        ISource,
        ModuleTypes,
        XMediaService,
        xServer
    } from "xmedia-node";
    import {v4 as uuid} from "uuid";
  3. Let's create the XMediaService

      const xMediaService = useMemo(() =>
            XMediaService(), []
        );
        
  4. Create the ILicense with username and password

      const [creds,] = useState<ILicense>({
            signaling_url: "vt08.medialooks.com:8080",
            username: username,
            password: password
        });
  5. Now lets log in to signalling server, enumerate the xNodeServer instances and IChannels if we have any

        const getLocations = async (): Promise<xServer[]> => {
            console.log(`enum channel invoked: ${channelId}`);
            //login to server
            const loginResult = await xMediaService.login(creds);
            
    
            if (loginResult.failed) {
                console.error(`Login failed: ${JSON.stringify(loginResult.response)}`);
                setState(`Login failed: ${JSON.stringify(loginResult.response)}`);
                return [];
            }
            //enumerate xMediaNode server instances 
            const getLocationResponse = await xMediaService.getLocations(onLocationsChange);
            if (getLocationResponse.failed) {
                console.error(`Get locations failed: ${JSON.stringify(getLocationResponse.response)}`);
                setState(`Get locations failed: ${JSON.stringify(getLocationResponse.response)}`);
                return [];
            }
    
            console.log(`Get locations response: ${JSON.stringify(getLocationResponse.response, null, 2)}`);
    
    
            return getLocationResponse.response as xServer[];
        }
  6. Now lets Init some channel

    
    //srt source address without, you don't need to specify 'srt://', since it's already 
    //specified in EndpointTypes.SRT,
        const srcUrl = '10.10.10.10:10010?mode=caller';
    //srt destanation address 
        const dstUrl = '10.10.10.10:5051?mode=listener';
    
    const source: ISource = {
            id: `${channelId}-src`,
            direction: EndpointDirection.src,
            name: `${channelId}-src`,
            decodeMedia: false,
            selected: true,
            busy: false,
            channelIds: [channelId],
            type: EndpointTypes.SRT,
            props: {
                sourceUrl: {
                    moduleData: {
                        type: ModuleTypes.srtSource,
                        propName: "open_url"
                    },
                    type: EndpointPropsType.STRING,
                    value: srcUrl
                },
                container: {
                    moduleData: {
                        type: ModuleTypes.demux,
                        propName: "av_format_name"
                    },
                    type: EndpointPropsType.SELECT,
                    value: "mpegts"
                }
            }
        }
    
        const destination1: IDestination = {
            id: `${uuid()}-dst`,
            direction: EndpointDirection.dst,
            name: `${channelId}-dst`,
            selected: true,
            busy: false,
            channelIds: [channelId],
            type: EndpointTypes.SRT,
            props: {
                audioEncoder: {
                    moduleData: {
                        type: ModuleTypes.audioEncoder,
                        propName: "av_codec_name"
                    },
                    type: EndpointPropsType.SELECT,
                    value: "aac",
                },
                videoEncoder: {
                    moduleData: {
                        type: ModuleTypes.videoEncoder,
                        propName: "av_codec_name"
                    },
                    type: EndpointPropsType.SELECT,
                    value: "hevc_nvenc",
                },
                container: {
                    moduleData: {
                        type: ModuleTypes.mux,
                        propName: "av_format_name"
                    },
                    type: EndpointPropsType.SELECT,
                    value: "mpegts",
                },
                audioStreamsCount: {
                    moduleData: {
                        type: ModuleTypes.mux,
                        propName: "av_mux_audio_streams_count"
                    },
                    type: EndpointPropsType.NUMBER,
                    value: 1
                },
                videoStreamsCount: {
                    moduleData: {
                        type: ModuleTypes.mux,
                        propName: "av_mux_video_streams_count"
                    },
                    type: EndpointPropsType.NUMBER,
                    value: 1
                },
                outputUrl: {
                    moduleData: {
                        type: ModuleTypes.srtDst,
                        propName: "open_url"
                    },
                    type: EndpointPropsType.STRING,
                    value: dstUrl
                }
            }
        }
    
    
    const channelInit = async () => {
    
            const locations = await getLocations();
    
            setState("");
    
            console.log(`channel init invoked: ${channelId}`);
    
            if (locations.length === 0) {
                console.error(`No locations in room`);
                return;
            }
    
            if (channelId.length === 0) {
                console.error(`Channel id is empty`);
                return;
            }
    
            const initChannelResult = await xMediaService.initChannel(
                locations[0],
                channelId,
                [source],
                [destination1],
            );
    
            if (initChannelResult.failed) {
                console.error(`Get channel failed: ${JSON.stringify(initChannelResult.response)}`);
                setState(`Get channel failed: ${JSON.stringify(initChannelResult.response)}`);
                return;
            }
    
            console.log(`init channel response: ${JSON.stringify(initChannelResult.response, null, 2)}`);
    
            console.log(`Channel: ${JSON.stringify(initChannelResult.response, null, 2)}`);
            setState(`Channel: ${JSON.stringify(initChannelResult.response)}`);
            setChannels([initChannelResult.response]);
        }

From that point, we already initialize one channel SRT -> SRT,

7. Now let's add one more destination to the current channel via


const destination2: IDestination = {
        id: `${uuid()}-dst`,
        direction: EndpointDirection.dst,
        name: `${channelId}-dst`,
        selected: true,
        busy: false,
        channelIds: [channelId],
        type: EndpointTypes.SRT,
        props: {
            audioEncoder: {
                moduleData: {
                    type: ModuleTypes.audioEncoder,
                    propName: "av_codec_name"
                },
                type: EndpointPropsType.SELECT,
                value: "aac",
            },
            videoEncoder: {
                moduleData: {
                    type: ModuleTypes.videoEncoder,
                    propName: "av_codec_name"
                },
                type: EndpointPropsType.SELECT,
                value: "hevc_nvenc",
            },
            container: {
                moduleData: {
                    type: ModuleTypes.mux,
                    propName: "av_format_name"
                },
                type: EndpointPropsType.SELECT,
                value: "mpegts",
            },
            audioStreamsCount: {
                moduleData: {
                    type: ModuleTypes.mux,
                    propName: "av_mux_audio_streams_count"
                },
                type: EndpointPropsType.NUMBER,
                value: 1
            },
            videoStreamsCount: {
                moduleData: {
                    type: ModuleTypes.mux,
                    propName: "av_mux_video_streams_count"
                },
                type: EndpointPropsType.NUMBER,
                value: 1
            },
            outputUrl: {
                moduleData: {
                    type: ModuleTypes.srtDst,
                    propName: "open_url"
                },
                type: EndpointPropsType.STRING,
                value: dstUrl2
            }
        }
    

const addDestinationToChannel = async () => {

        const locations = await getLocations();

        if (locations.length === 0) {
            return console.error(`No locations found`);
        }
        console.log(`adding dest`);
        xMediaService.addDestinationToChannel(channels[0], destination2)
            .then(response => {
                if (response.failed) {
                    return console.error(`Failed to add destination: ${JSON.stringify(response.response)}`);
                }
                console.log(`add dest success: ${JSON.stringify(response.response, null, 2)}`);
                setChannels([response.response]);
            });
    }

Full Visual Studio Code could be downloaded here - xMediaNode SRT to SRT sample

Local file to NDI

Create NDI output from local file example

To start broadcasting to NDI from a local file, you need to:

  • Run xnodeserver at the location where you have access to the media data you plan to work with under your license.

  • Import XMediaService into your project:

    import { XMediaService } from "xmedia-node";
  • Create an xMediaService object:

    const xMediaService = XMediaService();
  • Authenticate using your license details:

    xMediaService.login({
        username: user.username,
        password: user.password,
        signaling_url: user.signaling_url
    } as ILicense);
  • Get the list of locations

    const locations = xMediaService.getLocations(onLocationsChanged); // you can pass a callback to be invoked when locations connect or disconnect
  • Create a source of type "file":

    const source = {
        id: string,           // unique ID
        direction: "src" | "dst", // source or destination flag
        name: string, // name
        busy: boolean, // orchestration parameter (initialize as false)
        chosen: boolean, // orchestration parameter (initialize as true)
        channelId: string, // unique stream ID
        type: endpointType, // source type
        props: currentSrcProps // source settings
    };

    Example:

    {
      "id": "4ec9b2b8-62d5-47f3-b8b8-1a3510b07a27",
      "direction": "src",
      "name": "Source 0",
      "busy": false,
      "chosen": true,
      "channelId": "00",
      "type": "File",
      "props": {
        "path": {
          "moduleData": {
            "type": "demux",
            "propName": "open_url"
          },
          "type": "STRING",
          "value": "/home/rtc/xsdk/ori.mp4"
        },
        "in": {
          "moduleData": {
            "type": "demux",
            "propName": "in"
          },
          "type": "NUMBER",
          "value": 0
        },
        "loop": {
          "moduleData": {
            "type": "demux",
            "propName": "loop"
          },
          "type": "BOOL",
          "value": true
        },
        "out": {
          "moduleData": {
            "type": "demux",
            "propName": "loop"
          },
          "type": "NUMBER",
          "value": 0
        }
      }
    }
  • Create a destination of type NDI:

    const destination = {
        id: "a7c3380a-41b6-4f17-a292-c03f37b10666",
        direction: "dst",
        name: "Destination 0",
        busy: false,
        chosen: true,
        channelId: "00",
        type: "NDI",
        props: {
            "streamName": {
                "moduleData": {
                    "type": "deviceRenderer",
                    "propName": "open_url"
                },
                "type": "STRING",
                "value": "ndi"
            }
        }
    };
  • Initialize the channel:

    const initChannelResult = await xMediaService.initChannel(
        locations[0], // target location with running xNodeServer
        channelId,
        [source],
        [destination]
    );

Last updated