Skip to content

Instantly share code, notes, and snippets.

@joshooaj
Created February 19, 2020 14:57
Show Gist options
  • Save joshooaj/c12fb89e7332d035999ce0fc96c5ada6 to your computer and use it in GitHub Desktop.
Save joshooaj/c12fb89e7332d035999ce0fc96c5ada6 to your computer and use it in GitHub Desktop.
An example of a live stream retriever for Milestone MIP SDK
using System;
using System.Threading;
using VideoOS.Platform;
using VideoOS.Platform.Live;
namespace SampleSnapshotService
{
public class StreamReceiver : IDisposable
{
private readonly Item _item;
private readonly ManualResetEventSlim _requestSignal = new ManualResetEventSlim();
private readonly ManualResetEventSlim _arrivedSignal = new ManualResetEventSlim();
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private JPEGLiveSource _source;
private LiveSourceContent _image;
public Item Item => _item;
public StreamReceiver(Item item)
{
_item = item;
}
public void Start()
{
_source = new JPEGLiveSource(_item)
{
Width = 0,
Height = 0,
Compression = 100,
KeyFramesOnly = false
};
_source.SetKeepAspectRatio(true, false);
_source.Init();
_source.LiveContentEvent += SourceOnLiveContentEvent;
_source.LiveModeStart = true;
}
public void Stop()
{
_cts.Cancel();
if (_source == null) return;
_source.LiveContentEvent -= SourceOnLiveContentEvent;
_source.LiveModeStart = false;
_source.Close();
_source = null;
}
private void SourceOnLiveContentEvent(object sender, EventArgs e)
{
if (!_requestSignal.IsSet) return;
_requestSignal.Reset();
var content = (e as LiveContentEventArgs)?.LiveContent;
if (content == null) return;
_image = content;
_arrivedSignal.Set();
}
public void Dispose()
{
Stop();
_cts.Dispose();
}
public LiveSourceContent GetImage(int timeoutMilliseconds = 5000)
{
try
{
_requestSignal.Set();
_arrivedSignal.Wait(TimeSpan.FromMilliseconds(timeoutMilliseconds), _cts.Token);
_arrivedSignal.Reset();
var image = _image;
_image = null;
return image;
}
catch (OperationCanceledException)
{
return null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment