LLibVLC Chromecast Discovering
Posted: 16 Feb 2022 21:28
Hi All,
I wrote a simple class that using LibVLCSharp (ver. 3.6.5.0) for discover any chromecast in my local network.
The caller, inizialize the class, then call the "DiscoverChromecasts" method to search chromecast
If I run the attached code in my home, I found 2 items. If I try to run the code in my brother's home, i cannot discover any chromecast (but it is visible from Chrome or Youtube application).
I don't understand why it works perfectly for me, while it doesn't go to my brother
Can anyone help me?
Thanks
Semola
I wrote a simple class that using LibVLCSharp (ver. 3.6.5.0) for discover any chromecast in my local network.
The caller, inizialize the class, then call the "DiscoverChromecasts" method to search chromecast
If I run the attached code in my home, I found 2 items. If I try to run the code in my brother's home, i cannot discover any chromecast (but it is visible from Chrome or Youtube application).
I don't understand why it works perfectly for me, while it doesn't go to my brother
Can anyone help me?
Thanks
Semola
Code: Select all
public class ChrVLC
{
LibVLC _libVLC;
RendererDiscoverer _rendererDiscoverer;
readonly HashSet<RendererItem> _rendererItems = new HashSet<RendererItem>();
private List<String> messages;
public ChrVLC()
{
Core.Initialize();
_libVLC = new LibVLC();
messages = new List<string>();
}
public HashSet<String> DiscoverChromecasts()
{
messages.Clear();
HashSet<String> returnValue = new HashSet<String>();
// create a renderer discoverer
_rendererDiscoverer = new RendererDiscoverer(_libVLC);
// register callback when a new renderer is found
_rendererDiscoverer.ItemAdded += RendererDiscoverer_ItemAdded;
// start discovery on the local network
_rendererDiscoverer.Start();
Thread.Sleep(500);
foreach (RendererItem item in _rendererItems)
{
messages.Add($"Added {item.Name} - {item.IconUri}");
returnValue.Add(item.Name);
}
return returnValue;
}
private void RendererDiscoverer_ItemAdded(object sender, RendererDiscovererItemAddedEventArgs e)
{
messages.Add($"New item discovered: { e.RendererItem.Name} of type { e.RendererItem.Type} ");
messages.Add($" iconUri: {e.RendererItem.IconUri}");
if (e.RendererItem.CanRenderVideo) {
messages.Add($"{e.RendererItem.Name} - Can render video");
}
if (e.RendererItem.CanRenderAudio)
{
messages.Add($"{e.RendererItem.Name} - Can render audio");
}
_rendererItems.Add(e.RendererItem);
}
public List<String> getNessages()
{
return messages;
}
public string RenderIpAddress(String rendererItem)
{
String ipAddress = String.Empty;
messages.Clear();
foreach (RendererItem _item in _rendererItems)
{
messages.Add($"Scan {_item.Name} - IconUri: {_item.IconUri}");
if (_item.Name.Equals(rendererItem))
{
int lastPos = 0;
ipAddress= _item.IconUri.Replace("http://", "").Replace("https://", "");
lastPos = ipAddress.IndexOf(":");
if (lastPos < 0)
{
lastPos = ipAddress.IndexOf("/");
}
if (lastPos > 0)
{
ipAddress = ipAddress.Substring(0, lastPos);
}
messages.Add($"Match {_item.Name} - iconUri: {_item.IconUri} - ipAddress: {ipAddress}");
}
}
return ipAddress;
}
public string RendererType(String rendererItem)
{
foreach (RendererItem _item in _rendererItems)
{
if (_item.Name.Equals(rendererItem))
{
return _item.Type;
}
}
return string.Empty;
}
}