Schedule events for your first virtual channel
Tags: javascript, streaming, vodtolive, node
Published: 2020-05-16 12:00:10

In this blog we will walk you through how you by using the Consuo Schedule API can create a virtual channel and add events to the schedule.

## Pre-requisities

You have already an instance of Consuo up and running and you replace `<CHANNELMGR_IP>` in this post with the IP-address to where your Consuo Schedule manager is running. For instructions on how to setup and install Consuo you can read the [Quick Start guide](https://docs.consuo.tv/GET-STARTED.html).

## Create channel

We will use the Consuo Schedule API to create a channel. You find the API documentation at `http://<CHANNELMGR_IP>:8001/api/docs/`. In the examples in this post we will use Node JS but other programming languages can be used as the API is HTTP based. What we will do first is to create a channel.

In this example we use `fetch` as HTTP client and to create a channel we do an HTTP POST with the following body to the endpoint `/channels`.

```
{
  id: "one",
  name: "Channel One"
}
```

Example code:

```
const fetch = require('node-fetch');
const API = "http://<CHANNELMGR_IP>:8001";

async function run() {
  const res = await fetch(API + '/channels', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      id: "one",
      name: "Channel One"
    })
  });
  const json = await res.json();
  console.log(json);
}

run();
```

Now you have a channel created that has the ID `one` and the name `Channel One`.

## Add an event

Next, let us schedule an event. An event has a start, end and a URI to the video file to be played out. The format of the video file must be HLS as that is what Consuo supports today. The start time of the event is provided as a Unix Timestamp in milliseconds and all timestamps are in UTC timezone.

As an example, we want to create an event that starts on May 16 at 14:00 CET (UTC+02:00).

```
const start_time = Date.parse("16 May 2020 14:00:00+02:00");
```

In this example `start_time` is then `1589630400000`. To calculate the `end_time` we need to add the duration of the video file that we want to schedule. The video file we want to schedule is `https://maitv-vod.lab.eyevinn.technology/VINN.mp4/master.m3u8` and it has a duration of 106 seconds. To calculate the `end_time` we need to multiply the duration with 1000 as the timestamps are in milliseconds and then we add this to the `start_time`.

```
const end_time = start_time + (106 * 1000);
```

The event we want to schedule then looks as follows:

```
const event = {
  assetId: "VINN",
  title: "VINN",
  start_time: Date.parse("16 May 2020 14:00:00+02:00"),
  end_time: start_time + (106 * 1000),
  uri: "https://maitv-vod.lab.eyevinn.technology/VINN.mp4/master.m3u8",
  duration: 106
};
```

Then we make an HTTP POST with the above JSON to `/channels/one/schedule`. The schedule for this channel will now include this:

```
[
  {
    "assetId": "VINN",
    "eventId": "4c605714-a3db-48e5-a5af-d0ece30246de",
    "id": "VINN",
    "title": "VINN",
    "start_time": 1589630400000,
    "end_time": 1589630506000,
    "start": "2020-05-16T12:00:00.000Z",
    "end": "2020-05-16T12:01:46.000Z",
    "uri": "https://maitv-vod.lab.eyevinn.technology/VINN.mp4/master.m3u8",
    "duration": 106
  }
]
```

The code example for the above can then look like this:

```
  const uri = "https://maitv-vod.lab.eyevinn.technology/VINN.mp4/master.m3u8";
  const start_time = Date.parse("16 May 2020 14:00:00+02:00");
  const end_time = start_time + (106 * 1000);
  const event = {
    assetId: 'VINN',
    title: 'VINN',
    start_time: start_time,
    end_time: end_time,
    uri: uri,
    duration: 106
  };
  const res = await fetch(API + '/channels/one/schedule', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(event)
  });
```

## Playing the channel

To start playing the channel that you just created and added events to you need an HLS capable video player. If you just want to test you can use the HTML player at http://player.eyevinn.technology/ and enter the URI `http://<ENGINE_IP>:8000/live/master.m3u8?channel=one`. The `<ENGINE_IP>` is the IP to the Consuo Engine service.