DolbyIO / dolbyio-rest-apis-client-node

Dolby.io REST APIs Client for Node.JS

Home Page:https://www.npmjs.com/package/@dolbyio/dolbyio-rest-apis-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Package Publish Package npm License

Dolby.io REST APIs Client for Node.JS

Node.JS wrapper for the dolby.io REST Communications, Streaming and Media APIs.

Install this project

Run the npm command to install the package @dolbyio/dolbyio-rest-apis-client into your Node project:

npm install @dolbyio/dolbyio-rest-apis-client --save

Authentication

To get an access token that will be used by your server to perform backend operations like creating a conference, use the following code.

const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');

const APP_KEY = 'YOUR_APP_KEY';
const APP_SECRET = 'YOUR_APP_SECRET';

const jwt = await dolbyio.authentication.getApiAccessToken(APP_KEY, APP_SECRET);

console.log(`Access Token: ${jwt.access_token}`);

To request a particular scope for this access token:

const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');

const APP_KEY = 'YOUR_APP_KEY';
const APP_SECRET = 'YOUR_APP_SECRET';

const jwt = await dolbyio.authentication.getApiAccessToken(APP_KEY, APP_SECRET, 3600, ['comms:client_access_token:create']);

console.log(`Access Token: ${jwt.access_token}`);
console.log(`Scope: ${jwt.scope}`);

Communications Examples

Authenticate

To get an access token that will be used by the client SDK for an end user to open a session against dolby.io, use the following code:

const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');

const APP_KEY = 'YOUR_APP_KEY';
const APP_SECRET = 'YOUR_APP_SECRET';

// Request an API Token
const api_token = await dolbyio.authentication.getApiAccessToken(APP_KEY, APP_SECRET, 3600, ['comms:client_access_token:create']);

// Request the Client Access Token
const cat = await dolbyio.communications.authentication.getClientAccessTokenV2({
    accessToken: api_token,
    sessionScope: ['*'],
});

console.log(`Client Access Token: ${cat.access_token}`);

Create a conference

To create a Dolby Voice conference, you first must retrieve an API Access Token, then use the following code to create the conference.

const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');

const APP_KEY = 'YOUR_APP_KEY';
const APP_SECRET = 'YOUR_APP_SECRET';

const ownerExternalId = ''; // Identifier of the owner of the conference
const alias = ''; // Conference alias

const options = {
    ownerExternalId: ownerExternalId,
    alias: alias,
    dolbyVoice: true,
    liveRecording: false,
    participants: [
        { externalId: 'hostA', permissions: ['JOIN', 'SEND_AUDIO', 'SEND_VIDEO'], notify: true },
        { externalId: 'listener1', permissions: ['JOIN'], notify: false },
    ],
};

// Request an API Token
const jwt = await dolbyio.authentication.getApiAccessToken(APP_KEY, APP_SECRET, 3600, ['comms:conf:create']);

// Create the conference
const conference = await dolbyio.communications.conference.createConference(jwt, options);

console.log(`Conference created: ${conference.conferenceId}`);

Real-time Streaming Examples

Create a publish token

const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');

const publishToken = await dolbyio.streaming.publishToken.create('api_secret', {
    label: 'My token',
    streams: [
        {
            streamName: 'feedA',
        },
    ],
});
console.log(publishToken);

Create a subscribe token

const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');

const subscribeToken = await dolbyio.streaming.subscribeToken.create('api_secret', {
    label: 'My token',
    streams: [
        {
            streamName: 'feedA',
        },
    ],
});
console.log(subscribeToken);

Media Examples

Here is an example on how to upload a file to the Dolby.io temporary cloud storage, enhance that file and download the result.

Get an API token

Get the App Key and Secret from the Dolby.io dashboard and use the following code in your python script.

const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');

const APP_KEY = 'YOUR_APP_KEY';
const APP_SECRET = 'YOUR_APP_SECRET';

// Request an Access Token
const jwt = await dolbyio.authentication.getApiAccessToken(APP_KEY, APP_SECRET);
console.log('Access token', jwt);

Upload a file for processing

Upload a media file to the Dolby.io temporary cloud storage for processing:

// Temporary storage URL that will be used as reference for the job processing
const inputUrl = 'dlb://in/file.mp4';
// Local path of the file to upload
const originalFilePath = '/path/to/original_file.mp4';

await dolbyio.media.io.uploadFile(jwt, inputUrl, originalFilePath);

Start an enhance job

Generate a job description and send it to Dolby.io.

// Temporary storage URL that will be used as reference for the job processing
const outputUrl = 'dlb://out/file.mp4';

const jobDescription = JSON.stringify({
    content: { type: 'podcast' },
    input: inputUrl,
    output: outputUrl,
});

const jobId = await dolbyio.media.enhance.start(jwt, jobDescription);
console.log(`Job ID: ${jobId}`);

Wait for the job to complete

Get the job status and wait until it is completed.

const sleep = (delay) => new Promise((r) => setTimeout(r, delay));

let result = await dolbyio.media.enhance.getResults(jwt, jobId);
while (result.status === 'Pending' || result.status === 'Running') {
    console.log(`Job status is ${result.status}, taking a 5 second break...`);
    await sleep(5000);

    result = await dolbyio.media.enhance.getResults(jwt, jobId);
}

if (result.status !== 'Success') {
    console.error('There was a problem with processing the file', result);
    return;
}

Download a processed file

At this stage, the file has been processed and written to the temporary storage so we can download it.

// Local path where to download the file to
const enhancedFilePath = '/path/to/enhanced_file.mp4';

await dolbyio.media.io.downloadFile(jwt, outputUrl, enhancedFilePath);

Build this project

To build the dist folder, run the command:

npm run build

About

Dolby.io REST APIs Client for Node.JS

https://www.npmjs.com/package/@dolbyio/dolbyio-rest-apis-client

License:MIT License


Languages

Language:TypeScript 99.8%Language:JavaScript 0.2%