public void RegisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException("server"); } var guid = server.Uuid; if (servers.ContainsKey(guid)) { throw new ArgumentException("Attempting to register more than once"); } var end = listener.LocalEndpoint as IPEndPoint; var mount = new MediaMount(server); servers[guid] = mount; RegisterHandler(mount); foreach (var address in IP.ExternalIPAddresses) { var uri = new Uri(string.Format("http://{0}:{1}{2}", address, end.Port, mount.DescriptorURI)); ssdpServer.RegisterNotification(guid, uri); InfoFormat("New mount at: {0}", uri); } }
public void RegisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException(nameof(server)); } var guid = server.UUID; if (servers.ContainsKey(guid)) { throw new ArgumentException("Attempting to register more than once"); } var end = (IPEndPoint)listener.LocalEndpoint; var mount = new MediaMount(server); servers[guid] = mount; RegisterHandler(mount); foreach (var address in IP.ExternalIPAddresses) { DebugFormat("Registering device for {0}", address); var deviceGuid = Guid.NewGuid(); var list = devicesForServers.GetOrAdd(guid, new List <Guid>()); lock (list) { list.Add(deviceGuid); } mount.AddDeviceGuid(deviceGuid, address); var uri = new Uri($"http://{address}:{end.Port}{mount.DescriptorURI}"); lock (list) { ssdpServer.RegisterNotification(deviceGuid, uri, address); } NoticeFormat("New mount at: {0}", uri); } }
public void UnregisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException(nameof(server)); } MediaMount mount; if (!servers.TryGetValue(server.UUID, out mount)) { return; } List <Guid> list; if (devicesForServers.TryGetValue(server.UUID, out list)) { lock (list) { foreach (var deviceGuid in list) { ssdpServer.UnregisterNotification(deviceGuid); } } devicesForServers.TryRemove(server.UUID, out list); } UnregisterHandler(mount); MediaMount ignored; if (servers.TryRemove(server.UUID, out ignored)) { InfoFormat("Unregistered Media Server {0}", server.UUID); } }
public MediaMount(IMediaServer aServer) { server = aServer; prefix = String.Format("/mm-{0}/", ++mount); if (server is IVolatileMediaServer) { (server as IVolatileMediaServer).Changed += ChangedServer; } }
public MediaMount(IMediaServer aServer) { server = aServer; prefix = String.Format("/mm-{0}/", ++mount); var vms = server as IVolatileMediaServer; if (vms != null) { vms.Changed += ChangedServer; } }
public MediaMount(IMediaServer aServer) { server = aServer; prefix = String.Format("/mm-{0}/", ++mount); if (server is IVolatileMediaServer) { (server as IVolatileMediaServer).Changed += ChangedServer; } }
public bool AddServer(IMediaServer server) { if (Dispatcher.IsWorking()) { return(false); // coz we will not lock OutputList } OutputList.Add(new ChannelServerItem(server, server.GetClients(ChannelName))); return(true); }
public MediaMount(IMediaServer aServer) { server = aServer; prefix = String.Format("/mm-{0}/", ++mount); var vms = server as IVolatileMediaServer; if (vms != null) { vms.Changed += ChangedServer; } }
public MediaMount(IMediaServer aServer) { server = aServer; Prefix = $"/mm-{++mount}/"; var vms = server as IVolatileMediaServer; if (vms != null) { vms.Changed += ChangedServer; } }
public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo) { try { if (mediaInfo != null && mediaInfo.Length > 0) { foreach (var channel in channels) { channel.SetWelcomeText(mediaInfo); channel.Process(new BufferData(channel.GetWelcomeText())); } } // PcmHandler will ignore MediaServer.InputBufferSize int inputBufferSize = mediaServer.OutputBufferSize > 0 ? mediaServer.OutputBufferSize : 1; try { while (mediaServer.IsWorking()) { int realSize = 0; byte[] data = new byte[inputBufferSize]; try { realSize = inputStream.Read(data, 0, data.Length); } catch (Exception ex) { realSize = 0; mediaServer.Logger.Error("PCM input error: " + ex.Message); } if (realSize <= 0) { break; } foreach (var channel in channels) { channel.Process(new BufferData(data, realSize)); } } } catch (Exception ex) { mediaServer.Logger.Error("PCM input error: " + ex.Message); } } catch (Exception ex) { mediaServer.Logger.Error("PCM input process error in thread: " + ex.Message); } }
public void AddServer(IMediaServer mediaServer) { if (mediaServer == null) { return; } if (m_Servers.ContainsKey(mediaServer.ServerName)) { m_Servers.Remove(mediaServer.ServerName); } m_Servers.Add(mediaServer.ServerName, mediaServer); }
public MediaMount(IRoadieSettings configuration, ILogger logger, IMediaServer aServer) { Configuration = configuration; Logger = logger; server = aServer; Prefix = $"/mm-{++mount}/"; var vms = server as IVolatileMediaServer; if (vms != null) { vms.Changed += ChangedServer; } }
public void initMediaServer() { switch (Config.MediaServer) { case 0: mServer = new Plex(); break; case 1: mServer = new Emby(); break; } mSType = Config.MediaServer; }
public void RegisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException(nameof(server)); } var guid = server.UUID; if (servers.ContainsKey(guid)) { throw new ArgumentException("Attempting to register more than once"); } var end = (IPEndPoint)listener.LocalEndpoint; var mount = new MediaMount(server); servers[guid] = mount; RegisterHandler(mount); foreach (var address in IP.ExternalIPAddresses) { DebugFormat("Registering device for {0}", address); // Setup initial byes based on server and machine var macBytes = HexadecimalStringToByteArray(IP.GetMAC(address).Replace(":", "")); var addressByte = address.GetAddressBytes()[0]; var serverBytes = MD5Hash(server.FriendlyName); var guidBytes = macBytes.Concat(new byte[] { addressByte }).Concat(serverBytes.Take(16 - 1 - macBytes.Length)).ToArray(); var lguid = new Guid(guidBytes); var deviceGuid = lguid; var list = devicesForServers.GetOrAdd(guid, new List <Guid>()); lock (list) { list.Add(deviceGuid); } mount.AddDeviceGuid(deviceGuid, address); var uri = new Uri($"http://{address}:{end.Port}{mount.DescriptorURI}"); lock (list) { ssdpServer.RegisterNotification(deviceGuid, uri, address); } NoticeFormat("New mount at: {0}", uri); } }
public void RegisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException("server"); } var guid = server.Uuid; if (servers.ContainsKey(guid)) { throw new ArgumentException("Attempting to register more than once"); } var end = listener.LocalEndpoint as IPEndPoint; var mount = new MediaMount(server); servers[guid] = mount; RegisterHandler(mount); foreach (var address in IP.ExternalIPAddresses) { DebugFormat("Registering device for {0}", address); var lguid = new Guid(System.Text.Encoding.UTF8.GetBytes(IP.GetMAC(address).Replace(":", "") + "." + (address.Address % 256).ToString())); var deviceGuid = lguid; var list = devicesForServers.GetOrAdd(guid, new List <Guid>()); lock (list) { list.Add(deviceGuid); } mount.AddDeviceGuid(deviceGuid, address); var uri = new Uri(string.Format( "http://{0}:{1}{2}", address, end.Port, mount.DescriptorURI )); ssdpServer.RegisterNotification(deviceGuid, uri, address); NoticeFormat("New mount at: {0}", uri); } }
public bool AddServer(IMediaServer server) { if (Dispatcher.IsWorking()) { return(false); // coz we will not lock OutputList } if (server != null) { //m_Logger = server.Logger; int inputQueueLength = server.GetChannelInputQueueLength(ChannelName); if (inputQueueLength > 0) { m_MaxInputQueueSize = inputQueueLength; //server.Logger.Info("Max InputQueueLength of [" + ChannelName + "] is set to " + m_MaxInputQueueSize); } else { inputQueueLength = server.InputQueueSize; if (inputQueueLength > 0) { if (m_MaxInputQueueSize <= 0) { m_MaxInputQueueSize = server.InputQueueSize; //server.Logger.Info("Max InputQueueLength of [" + ChannelName + "] is set to " + m_MaxInputQueueSize); } else { if (m_MaxInputQueueSize > server.InputQueueSize) { m_MaxInputQueueSize = server.InputQueueSize; // just get the small one //server.Logger.Info("Max InputQueueLength of [" + ChannelName + "] is set to " + m_MaxInputQueueSize); } } } } } OutputList.Add(new ChannelServerItem(server, server.GetClients(ChannelName))); return(true); }
public void UnregisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException("server"); } MediaMount mount; if (!servers.TryGetValue(server.Uuid, out mount)) { return; } ssdpServer.UnregisterNotification(server.Uuid); UnregisterHandler(mount); MediaMount ignored; if (servers.TryRemove(server.Uuid, out ignored)) { InfoFormat("Unregistered Media Server {0}", server.Uuid); } }
public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo) { try { var channel = channels.First(); string sourceName = channel.ChannelName; if (sourceName.Length > 0) { Int16 width = VIDEO_DEFAULT_WIDTH; Int16 height = VIDEO_DEFAULT_HEIGHT; string vinfo = ""; // new jsmepg would not need "header" message anymore /* * lock (m_Headers) * { * if (m_Headers.ContainsKey(sourceName)) m_Headers.Remove(sourceName); // refresh it * * if (mediaInfo.Length > 0) * { * try * { * var mpegInfo = mediaInfo.Replace("/", ""); * if (mpegInfo.Contains('(') && mpegInfo.Contains(')')) * mpegInfo = mpegInfo.Substring(mpegInfo.IndexOf('(') + 1, * mpegInfo.IndexOf(')') - mpegInfo.IndexOf('(') - 1); * * vinfo = mpegInfo; * var parts = (vinfo.Split('@')[0]).Split('x'); * * if (parts.Length > 0) width = Convert.ToInt16(parts[0]); * if (parts.Length > 1) height = Convert.ToInt16(parts[1]); * } * catch * { * width = VIDEO_DEFAULT_WIDTH; * height = VIDEO_DEFAULT_HEIGHT; * } * } * * byte[] header = GenHeader(width, height); * m_Headers.Add(sourceName, header); * } */ if (mediaInfo.Length > 0) { try { var mpegInfo = mediaInfo.Replace("/", ""); if (mpegInfo.Contains('(') && mpegInfo.Contains(')')) { mpegInfo = mpegInfo.Substring(mpegInfo.IndexOf('(') + 1, mpegInfo.IndexOf(')') - mpegInfo.IndexOf('(') - 1); } vinfo = mpegInfo; var parts = (vinfo.Split('@')[0]).Split('x'); if (parts.Length > 0) { width = Convert.ToInt16(parts[0]); } if (parts.Length > 1) { height = Convert.ToInt16(parts[1]); } } catch { width = VIDEO_DEFAULT_WIDTH; height = VIDEO_DEFAULT_HEIGHT; } } if (vinfo.Length <= 0) { vinfo = width + "x" + height; } } // new jsmepg would not need "header" message anymore //channel.SetWelcomeData(GetWelcomeData(sourceName)); int inputBufferSize = mediaServer.InputBufferSize > 0 ? mediaServer.InputBufferSize : 1; while (mediaServer.IsWorking()) { int realSize = 0; byte[] data = new byte[inputBufferSize]; // MpegHandler will ignore MediaServer.OutputBufferSize try { realSize = inputStream.Read(data, 0, data.Length); } catch (Exception ex) { realSize = 0; mediaServer.Logger.Error("MPEG1 input error: " + ex.Message); } if (realSize <= 0) { break; } channel.Process(new BufferData(data, realSize)); } if (sourceName.Length > 0) { lock (m_Headers) { if (m_Headers.ContainsKey(sourceName)) { m_Headers.Remove(sourceName); } } } } catch (Exception ex) { mediaServer.Logger.Error("MPEG1 input process error in thread: " + ex.Message); } }
public ServerNetworkEventHandler(IMediaServer mediaServer) : base() { m_MediaServer = mediaServer; }
public WebSocketServer(IMediaServer server, string certFile = "", string certKey = "") { ActualServer = new Server(); ActualServer.SetIoFilter(new MessageCodec()); ActualServer.SetIoHandler(new ServerNetworkEventHandler(server)); try { string certFilepath = certFile == null ? "" : String.Copy(certFile).Trim(); if (certFilepath.Length > 0) { certFilepath = certFilepath.Replace('\\', '/'); if (certFilepath[0] != '/' && certFilepath.IndexOf(":/") != 1) // if it is not abs path { string folder = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory); if (folder == null || folder.Trim().Length <= 0) { var entry = Assembly.GetEntryAssembly(); var location = ""; try { if (entry != null) { location = entry.Location; } } catch { } if (location != null && location.Length > 0) { folder = Path.GetDirectoryName(location); } } if (folder != null && folder.Length > 0) { certFilepath = folder.Replace('\\', '/') + "/" + certFilepath; } } //Console.WriteLine("Try to load cert: " + certFilepath); //server.Logger.Info("Try to load cert: " + certFilepath); if (File.Exists(certFilepath)) { if (certKey == null) { ActualServer.SetCert(new X509Certificate2(certFilepath)); } else { ActualServer.SetCert(new X509Certificate2(certFilepath, certKey)); } Console.WriteLine("Loaded cert: " + certFilepath); server.Logger.Info("Loaded cert: " + certFilepath); } else { Console.WriteLine("Cert file not found: " + certFilepath); server.Logger.Error("Cert file not found: " + certFilepath); } } } catch (Exception ex) { Console.WriteLine("Failed to setup SSL: " + ex.Message); server.Logger.Error("Failed to setup SSL: " + ex.Message); } }
public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo) { try { if (mediaInfo != null && mediaInfo.Length > 0) { foreach (var channel in channels) { channel.SetWelcomeText(mediaInfo); channel.Process(new BufferData(channel.GetWelcomeText())); } } bool foundFirstMagicTag = false; List <byte> headerData = new List <byte>(); List <List <byte> > currentChunks = new List <List <byte> >(); int totalBytes = 0; int currentByte = 0; List <byte> cache = new List <byte>(); int inputBufferSize = mediaServer.InputBufferSize > 0 ? mediaServer.InputBufferSize : 1; byte[] inputBuffer = new byte[inputBufferSize]; int currentSize = 0; bool foundError = false; try { while (mediaServer.IsWorking()) { currentSize = inputStream.Read(inputBuffer, 0, inputBuffer.Length); if (currentSize <= 0) { break; } foundError = false; for (var currentIdx = 0; currentIdx < currentSize; currentIdx++) { currentByte = inputBuffer[currentIdx]; bool foundTag = false; if (currentByte == MAGIC_TAG.Last() && cache.Count >= MAGIC_TAG_LEN - 1) { foundTag = true; for (var i = 1; i <= MAGIC_TAG_LEN - 1; i++) { if (cache[cache.Count - i] != MAGIC_TAG[MAGIC_TAG_LEN - 1 - i]) { foundTag = false; break; } } if (foundTag) { cache.RemoveRange(cache.Count - (MAGIC_TAG_LEN - 1), MAGIC_TAG_LEN - 1); } } if (foundTag) { if (!foundFirstMagicTag) { foundFirstMagicTag = true; } else { currentChunks.Add(new List <byte>(cache)); totalBytes += cache.Count; if (totalBytes >= mediaServer.OutputBufferSize) { if (headerData.Count <= 0 && currentChunks.Count > 1) { headerData.AddRange(currentChunks[0]); headerData.AddRange(currentChunks[1]); currentChunks.RemoveRange(0, 2); } byte[] data = GenAudioData(headerData, currentChunks); foreach (var channel in channels) { channel.Process(new BufferData(data, data.Length)); } currentChunks.Clear(); totalBytes = 0; } } cache.Clear(); cache.AddRange(MAGIC_TAG); } else { cache.Add((byte)currentByte); } if (cache.Count > MAX_CACHE_SIZE) { mediaServer.Logger.Error("Exceeded max cache size when read OGG input."); foundError = true; break; } } if (foundError) { break; } } } catch (Exception ex) { mediaServer.Logger.Error("OGG input error: " + ex.Message); } } catch (Exception ex) { mediaServer.Logger.Error("OGG input process error in thread: " + ex.Message); } }
public ChannelClientItem(IMediaServer server, object client) { Server = server; Client = client; }
public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo) { try { if (mediaInfo != null && mediaInfo.Length > 0) { foreach (var channel in channels) { channel.SetWelcomeText(mediaInfo); channel.Process(new BufferData(channel.GetWelcomeText())); } } bool foundFirstSyncWord = false; List <byte> tagPart = new List <byte>(); List <byte> headerPart = new List <byte>(); List <List <byte> > currentChunks = new List <List <byte> >(); int totalBytes = 0; int currentByte = 0; int lastByte = 0; List <byte> cache = new List <byte>(); int inputBufferSize = mediaServer.InputBufferSize > 0 ? mediaServer.InputBufferSize : 1; byte[] inputBuffer = new byte[inputBufferSize]; int currentSize = 0; bool foundError = false; try { while (mediaServer.IsWorking()) { currentSize = inputStream.Read(inputBuffer, 0, inputBuffer.Length); if (currentSize <= 0) { break; } foundError = false; for (var currentIdx = 0; currentIdx < currentSize; currentIdx++) { currentByte = inputBuffer[currentIdx]; if (currentByte == SECOND_SYNC_BYTE && lastByte == FIRST_SYNC_BYTE) { if (!foundFirstSyncWord) { foundFirstSyncWord = true; tagPart.AddRange(cache); cache.Clear(); cache.Add(FIRST_SYNC_BYTE); cache.Add(SECOND_SYNC_BYTE); } else { currentChunks.Add(new List <byte>(cache)); totalBytes += cache.Count; if (totalBytes >= mediaServer.OutputBufferSize) { byte[] data = GenAudioData(tagPart, headerPart, currentChunks); foreach (var channel in channels) { channel.Process(new BufferData(data, data.Length)); } currentChunks.Clear(); totalBytes = 0; } cache.Clear(); cache.Add(FIRST_SYNC_BYTE); cache.Add(SECOND_SYNC_BYTE); } lastByte = currentByte; continue; } if (lastByte == FIRST_SYNC_BYTE) { cache.Add(FIRST_SYNC_BYTE); } if (currentByte != FIRST_SYNC_BYTE) { cache.Add((byte)currentByte); } lastByte = currentByte; if (cache.Count > MAX_CACHE_SIZE) { mediaServer.Logger.Error("Exceeded max cache size when read MP3 input."); foundError = true; break; } } if (foundError) { break; } } } catch (Exception ex) { mediaServer.Logger.Error("MP3 input error: " + ex.Message); } } catch (Exception ex) { mediaServer.Logger.Error("MP3 input process error in thread: " + ex.Message); } }
public void RegisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException("server"); } var guid = server.Uuid; if (servers.ContainsKey(guid)) { throw new ArgumentException("Attempting to register more than once"); } var end = listener.LocalEndpoint as IPEndPoint; var mount = new MediaMount(server); servers[guid] = mount; RegisterHandler(mount); foreach (var address in IP.ExternalIPAddresses) { DebugFormat("Registering device for {0}", address); var deviceGuid = Guid.NewGuid(); var list = devicesForServers.GetOrAdd(guid, new List<Guid>()); lock (list) { list.Add(deviceGuid); } mount.AddDeviceGuid(deviceGuid, address); var uri = new Uri(string.Format( "http://{0}:{1}{2}", address, end.Port, mount.DescriptorURI )); ssdpServer.RegisterNotification(deviceGuid, uri, address); NoticeFormat("New mount at: {0}", uri); } }
public void UnregisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException("server"); } MediaMount mount; if (!servers.TryGetValue(server.Uuid, out mount)) { return; } List<Guid> list; if (devicesForServers.TryGetValue(server.Uuid, out list)) { lock (list) { foreach (var deviceGuid in list) { ssdpServer.UnregisterNotification(deviceGuid); } } devicesForServers.TryRemove(server.Uuid, out list); } UnregisterHandler(mount); MediaMount ignored; if (servers.TryRemove(server.Uuid, out ignored)) { InfoFormat("Unregistered Media Server {0}", server.Uuid); } }
public void UnregisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException("server"); } MediaMount mount; if (!servers.TryGetValue(server.Uuid, out mount)) { return; } ssdpServer.UnregisterNotification(server.Uuid); UnregisterHandler(mount); MediaMount ignored; if (servers.TryRemove(server.Uuid, out ignored)) { InfoFormat("Unregistered Media Server {0}", server.Uuid); } }
public void RegisterMediaServer(IMediaServer server) { if (server == null) { throw new ArgumentNullException("server"); } var guid = server.Uuid; if (servers.ContainsKey(guid)) { throw new ArgumentException("Attempting to register more than once"); } var end = listener.LocalEndpoint as IPEndPoint; var mount = new MediaMount(server); servers[guid] = mount; RegisterHandler(mount); foreach (var address in IP.ExternalIPAddresses) { var uri = new Uri(string.Format("http://{0}:{1}{2}", address, end.Port, mount.DescriptorURI)); ssdpServer.RegisterNotification(guid, uri); NoticeFormat("New mount at: {0}", uri); } }
public ChannelServerItem(IMediaServer server, List <object> clients) { Server = server; Clients = clients; }