What is the REST API?
REST API is a way of accessing services through web interface in a simple and flexible way. Here, a “service” can mean any application created using the SDK, and we are often asked about this:
Does the Video SDK have native REST API support?
Basically, the answer is no, this is not a part of the SDK, but you can always add such a support on the application level. For example, using Grapevine NuGet package.
Adding the REST API to the Playlist Sample (MPlatform SDK)
Here is the simple REST implementation showing basic playlist control from a web page (the modified Playlist Sample which includes REST server is attached - https://drive.medialooks.com/index.php/s/4B7CzWWSecPHoHa).
By default, REST server run in localhost:
private void Form1_Load(object sender, EventArgs e)
{
Task.Run(() =>
{
using (var server = RestServerBuilder.UseDefaults().Build())
{
server.Start();
while (server.IsListening)
{
Thread.Sleep(1);
}
}
});
...
But you can add any IP (LAN or WAN) using to be able to get access to the server from other PCs, for example:
server.Prefixes.Add(@"http://192.168.10.111:1234/");
In this sample REST server can handle only four commands (add, play, pause, stop):
[RestRoute("Get", "/api/play")]
public async Task Start(IHttpContext context)
{
await Task.Run(()=>
{
Form1.m_objPlaylist.FilePlayStart();
Form1.m_objWriter.ObjectStateGet(out var state);
if (!(state == eMState.eMS_Running))
{
Form1.m_objWriter.WriterNameSet(@"D:\Live-stream-server-portable-Windows-Nginx-RTMP-HLS-Dash-main\temp\tmp_hls\stream\index.m3u8", "format='hls' hls_time='2' hls_delete_threshold='1' hls_flags='delete_segments' hls_list_size='2' video::b='5M' video::codec='libopenh264' audio::codec='aac' audio::b='128K'");
Form1.m_objWriter.ObjectStart(Form1.m_objPlaylist);
}
context.Response.SendResponseAsync(HttpStatusCode.Ok);
});
}
[RestRoute("Get", "/api/stop")]
public async Task Stop(IHttpContext context)
{
await Task.Run(() =>
{
context.Response.SendResponseAsync(HttpStatusCode.Ok);
Form1.m_objPlaylist.FilePlayStop(0);
});
}
[RestRoute("Get", "/api/pause")]
public async Task Pause(IHttpContext context)
{
await Task.Run(() =>
{
context.Response.SendResponseAsync(HttpStatusCode.Ok);
Form1.m_objPlaylist.FilePlayPause(0);
});
}
[RestRoute("Post", "/api/add")]
public async Task Add(IHttpContext context)
{
using (var reader = new StreamReader(context.Request.InputStream))
{
var path = reader.ReadLine();
Form1.m_objPlaylist.PlaylistAdd(null, path, "", -1, out _);
}
await context.Response.SendResponseAsync(HttpStatusCode.Ok);
}
As a web client, you can use an attached HTML page (index.html) which can send GET requests in localhost. Of course, you can change localhost to some real IP, add other commands, add CSS design and so on.
Also, you can publish this playlist as HLS stream to see the preview on the web page (with a couple of seconds delay).
To do it, download any HLS server, for example - https://github.com/ustoopia/Live-stream-server-portable-Windows-Nginx-RTMP-HLS-Dash
and then embed web client functionality to the HLS player webpage, as a result, you will see web preview: