Ejemplo n.º 1
0
        private List <ISocket> CreateSocketAndListenForResponsesAsync()
        {
            var sockets = new List <ISocket>();

            sockets.Add(_SocketFactory.CreateSsdpUdpSocket(IpAddressInfo.Any, _LocalPort));

            if (_enableMultiSocketBinding)
            {
                foreach (var address in _networkManager.GetLocalIpAddresses().ToList())
                {
                    try
                    {
                        sockets.Add(_SocketFactory.CreateSsdpUdpSocket(address, _LocalPort));
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorException("Error in CreateSsdpUdpSocket. IPAddress: {0}", ex, address);
                    }
                }
            }

            foreach (var socket in sockets)
            {
                ListenToSocket(socket);
            }

            return(sockets);
        }
Ejemplo n.º 2
0
        private List <ISocket> CreateSocketAndListenForResponsesAsync()
        {
            var sockets = new List <ISocket>();

            sockets.Add(_SocketFactory.CreateSsdpUdpSocket(IPAddress.Any, _LocalPort));

            if (_enableMultiSocketBinding)
            {
                foreach (var address in _networkManager.GetLocalIpAddresses())
                {
                    if (address.AddressFamily == AddressFamily.InterNetworkV6)
                    {
                        // Not support IPv6 right now
                        continue;
                    }

                    try
                    {
                        sockets.Add(_SocketFactory.CreateSsdpUdpSocket(address, _LocalPort));
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error in CreateSsdpUdpSocket. IPAddress: {0}", address);
                    }
                }
            }

            foreach (var socket in sockets)
            {
                _ = ListenToSocketInternal(socket);
            }

            return(sockets);
        }
Ejemplo n.º 3
0
        private void RegisterServerEndpoints()
        {
            foreach (var address in _network.GetLocalIpAddresses())
            {
                var guid = address.GetMD5();

                var descriptorURI = "/dlna/" + guid.ToString("N") + "/description.xml";

                var uri = new Uri(_appHost.GetLocalApiUrl(address) + descriptorURI);

                var services = new List <string>
                {
                    "upnp:rootdevice",
                    "urn:schemas-upnp-org:device:MediaServer:1",
                    "urn:schemas-upnp-org:service:ContentDirectory:1",
                    "urn:schemas-upnp-org:service:ConnectionManager:1",
                    "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
                    "uuid:" + guid.ToString("N")
                };

                _ssdpHandler.RegisterNotification(guid, uri, IPAddress.Parse(address), services);

                _registeredServerIds.Add(guid.ToString("N"));
            }
        }
Ejemplo n.º 4
0
        void _ssdpHandler_MessageReceived(object sender, SsdpMessageEventArgs e)
        {
            string nts;

            e.Headers.TryGetValue("NTS", out nts);

            if (String.Equals(e.Method, "NOTIFY", StringComparison.OrdinalIgnoreCase) &&
                String.Equals(nts, "ssdp:byebye", StringComparison.OrdinalIgnoreCase) &&
                !_disposed)
            {
                EventHelper.FireEventIfNotNull(DeviceLeft, this, e, _logger);
                return;
            }

            try
            {
                var ip = _networkManager.GetLocalIpAddresses().FirstOrDefault();

                if (ip != null)
                {
                    e.LocalIp = IPAddress.Parse(ip);
                    TryCreateDevice(e);
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error creating play to controller", ex);
            }
        }
Ejemplo n.º 5
0
        private string GetServerAddress()
        {
            return(string.Format("{0}://{1}:{2}/mediabrowser",

                                 "http",
                                 _networkManager.GetLocalIpAddresses().FirstOrDefault() ?? "localhost",
                                 "8096"
                                 ));
        }
Ejemplo n.º 6
0
        private void RegisterEndpoints()
        {
            foreach (var address in _network.GetLocalIpAddresses())
            {
                var guid = address.GetMD5();

                var descriptorURI = "/mediabrowser/dlna/" + guid.ToString("N") + "/description.xml";

                var uri = new Uri(string.Format("http://{0}:{1}{2}", address, _config.Configuration.HttpServerPortNumber, descriptorURI));

                _ssdpHandler.RegisterNotification(guid, uri, IPAddress.Parse(address));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the local ip address.
        /// </summary>
        /// <returns>System.String.</returns>
        private string GetLocalIpAddress()
        {
            var localAddresses = _networkManager.GetLocalIpAddresses().ToList();

            // Cross-check the local ip addresses with addresses that have been received on with the http server
            var matchedAddress = _httpServer.LocalEndPoints
                                 .ToList()
                                 .Select(i => i.Split(':').FirstOrDefault())
                                 .Where(i => !string.IsNullOrEmpty(i))
                                 .FirstOrDefault(i => localAddresses.Contains(i, StringComparer.OrdinalIgnoreCase));

            // Return the first matched address, if found, or the first known local address
            return(matchedAddress ?? localAddresses.FirstOrDefault());
        }
Ejemplo n.º 8
0
        private bool IsInLocalNetwork(string remoteEndpoint)
        {
            if (string.IsNullOrWhiteSpace(remoteEndpoint))
            {
                throw new ArgumentNullException("remoteEndpoint");
            }

            IPAddress address;

            if (!IPAddress.TryParse(remoteEndpoint, out address))
            {
                return(true);
            }

            const int lengthMatch = 4;

            if (remoteEndpoint.Length >= lengthMatch)
            {
                var prefix = remoteEndpoint.Substring(0, lengthMatch);

                if (_networkManager.GetLocalIpAddresses()
                    .Any(i => i.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
                {
                    return(true);
                }
            }

            // Private address space:
            // http://en.wikipedia.org/wiki/Private_network

            return

                // If url was requested with computer name, we may see this
                (remoteEndpoint.IndexOf("::", StringComparison.OrdinalIgnoreCase) != -1 ||

                 remoteEndpoint.StartsWith("10.", StringComparison.OrdinalIgnoreCase) ||
                 remoteEndpoint.StartsWith("192.", StringComparison.OrdinalIgnoreCase) ||
                 remoteEndpoint.StartsWith("172.", StringComparison.OrdinalIgnoreCase) ||
                 remoteEndpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase));
        }
Ejemplo n.º 9
0
        private void RegisterServerEndpoints()
        {
            foreach (var address in _network.GetLocalIpAddresses())
            {
                var guid = address.GetMD5();

                var descriptorURI = "/mediabrowser/dlna/" + guid.ToString("N") + "/description.xml";

                var uri = new Uri(string.Format("http://{0}:{1}{2}", address, _config.Configuration.HttpServerPortNumber, descriptorURI));

                var services = new List <string>
                {
                    "upnp:rootdevice",
                    "urn:schemas-upnp-org:device:MediaServer:1",
                    "urn:schemas-upnp-org:service:ContentDirectory:1",
                    "uuid:" + guid.ToString("N")
                };

                _ssdpHandler.RegisterNotification(guid, uri, IPAddress.Parse(address), services);

                _registeredServerIds.Add(guid);
            }
        }
Ejemplo n.º 10
0
 private List <IPAddress> GetLocalIpAddresses()
 {
     return(_networkManager.GetLocalIpAddresses().ToList());
 }
        private StreamingDetails GetStream(CancellationToken cancellationToken, WebMediaType webMediaType, string itemId, TimeSpan startPosition)
        {
            var profile     = GetTranscoderProfile(cancellationToken, Configuration.StreamingProfileName);
            var profilename = String.Format("{0}", Configuration.StreamingProfileName);

            //var mediatype = String.Format("{0}", webMediaType);
            //if (mediatype == "Recording")
            //{
            //    profilename = "Android VLC direct";
            //}

            var identifier          = HttpUtility.UrlEncode(String.Format("{0}-{1}-{2:yyyyMMddHHmmss}", webMediaType, itemId, DateTime.UtcNow));
            var isStreamInitialised = GetFromService <WebBoolResult>(cancellationToken,
                                                                     "InitStream?type={0}&provider={1}&itemId={2}&identifier={3}&idleTimeout={4}&clientDescription={5}",
                                                                     webMediaType,
                                                                     STREAM_TV_RECORDING_PROVIDER, // Provider - use 0 for recordings and tv
                                                                     itemId,                       // itemId
                                                                     identifier,                   // identifier
                                                                     STREAM_TIMEOUT_DIRECT,
                                                                     identifier).Result;           //Idletimoue

            if (!isStreamInitialised)
            {
                throw new Exception(String.Format("Could not initialise the stream. Identifier={0}", identifier));
            }

            // Returns the url for streaming
            var url = GetFromService <WebStringResult>(cancellationToken, "StartStream?identifier={0}&profileName={1}&startPosition={2}",
                                                       identifier,
                                                       profilename, // Provider
                                                       (Int32)startPosition.TotalSeconds).Result;

            var isAuthorised = true;

            foreach (var ipAddress in _networkManager.GetLocalIpAddresses())
            {
                isAuthorised = isAuthorised && GetFromService <WebBoolResult>(
                    cancellationToken, "AuthorizeRemoteHostForStreaming?host={0}", ipAddress).Result;
            }

            if (!isAuthorised)
            {
                throw new Exception(String.Format("Could not authorise the stream. Identifier={0}", identifier));
            }

            var streamingDetails = new StreamingDetails()
            {
                StreamIdentifier = identifier,
                SourceInfo       = new MediaSourceInfo()
                {
                    Path     = url,
                    Protocol = MediaProtocol.Http,
                    Id       = itemId,
                    ReadAtNativeFramerate = true,
                }
            };

            var mediaInfoId = webMediaType == WebMediaType.Recording ? itemId : identifier;
            var mediaInfo   = GetMediaInfoFromStream(cancellationToken, webMediaType, mediaInfoId);

            if (mediaInfo != null)
            {
                streamingDetails.SourceInfo.Container    = mediaInfo.Container;
                streamingDetails.SourceInfo.RunTimeTicks = TimeSpan.FromMilliseconds(mediaInfo.Duration).Ticks;

                //streamingDetails.SourceInfo.AudioChannels = mediaInfo.AudioStreams.Count;
                //var defaultAudioStream = mediaInfo.AudioStreams.FirstOrDefault();
                //if (defaultAudioStream != null)
                //{
                //    streamingDetails.SourceInfo.AudioCodec = defaultAudioStream.Codec;
                //}

                //var defaultVideoStream = mediaInfo.VideoStreams.FirstOrDefault();
                //if (defaultVideoStream != null)
                //{
                //    streamingDetails.SourceInfo.VideoCodec = defaultVideoStream.Codec;
                //    streamingDetails.SourceInfo.Height = defaultVideoStream.Height;
                //    streamingDetails.SourceInfo.Width = defaultVideoStream.Width;
                //}
            }
            return(streamingDetails);
        }