Example #1
0
        public void Play(string deviceId, int httpPort, string objectID, ItemManager manager, TimeSpan startTime)
        {
            IUPnPDeviceDocumentAccess deviceAccess;
            UPnPService service  = GetServiceSync(deviceId, "urn:schemas-upnp-org:service:AVTransport:1", out deviceAccess);
            IPAddress   deviceIP = IPAddress.Parse(new Uri(deviceAccess.GetDocumentURL()).Host);

            //Najdenie adresy servera podla adresy zariadenia kde sa ma prehravanie spustit
            IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName()).Where(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToArray();
            int         byteIndex = 0;

            while (addresses.Length > 1)
            {
                addresses = addresses.Where(a => a.GetAddressBytes()[byteIndex] == deviceIP.GetAddressBytes()[byteIndex]).ToArray();
                byteIndex++;
            }
            //Ak ostane jedna adresa - zapise sa
            if (addresses.Length == 1)
            {
                deviceIP = addresses[0];
            }

            string result, firstResource;

            manager.BrowseMetadata("http://" + deviceIP + ":" + httpPort, objectID, startTime, out result, out firstResource);

            object[] inArgs  = new object[] { 0, firstResource, result };
            object   outArgs = null;

            service.InvokeAction("SetAVTransportURI", inArgs, ref outArgs);

            inArgs  = new object[] { 0, 1 };
            outArgs = null;
            service.InvokeAction("Play", inArgs, ref outArgs);
        }
Example #2
0
        private List <DLNAObject> GetContent(string parentID)
        {
            if (ContentDirectory == null)
            {
                return(null);                         //if DLNA device doesn't have Content Directory service, List<DLNAObjects>=null
            }
            //collection for DLNA device objects
            List <DLNAObject> DLNAObjects = new List <DLNAObject>();

            #region Request
            string browseFlag     = "BrowseDirectChildren"; // BrowseDirectChildren or BrowseMetadata as allowed values
            string filter         = "";
            int    startingIndex  = 0;
            int    requestedCount = 1000;
            string sortCriteria   = "";

            object[] inArgs = new object[6];
            inArgs[0] = parentID;
            inArgs[1] = browseFlag;
            inArgs[2] = filter;
            inArgs[3] = startingIndex;
            inArgs[4] = requestedCount;
            inArgs[5] = sortCriteria;

            object outArgs = new object[4];
            ContentDirectory.InvokeAction("Browse", inArgs, ref outArgs);

            #endregion

            #region Responce
            object[] resultobj = (object[])outArgs;

            string result         = (string)resultobj[0];
            int    numberReturned = (int)(UInt32)resultobj[1];
            int    totalMatches   = (int)(UInt32)resultobj[2];
            int    updateID       = (int)(UInt32)resultobj[3];

            if (outArgs == null)
            {
                return(null);
            }
            var myXMLDoc = new XmlDocument();
            #endregion

            //filling in DLNAObjects collection
            myXMLDoc.LoadXml(Convert.ToString(result));

            foreach (XmlNode xmlNode in myXMLDoc.DocumentElement.ChildNodes)
            {
                DLNAObjects.Add(new DLNAObject(xmlNode));
            }

            return(DLNAObjects);
        }
Example #3
0
        public static object[] InvokeAction(UPnPService service, string actionName, params object[] args)
        {
            Array  inArgs  = args;
            object outArgs = null;
            object results = service.InvokeAction(actionName, inArgs, ref outArgs);

            return((object[])outArgs);
        }
Example #4
0
        public void Stop(string deviceId)
        {
            UPnPService service = GetServiceSync(deviceId, "urn:schemas-upnp-org:service:AVTransport:1");

            object[] inArgs  = new object[] { 0 };
            object   outArgs = null;

            service.InvokeAction("Stop", inArgs, ref outArgs);
        }
Example #5
0
        public static bool DeleteMap(int externalPort, string protocol)
        {
            if (service == null)
            {
                return(false);
            }
            object[] vInActionArgs   = new object[] { "", externalPort, protocol };
            object   pvOutActionArgs = new object();

            try
            {
                service.InvokeAction("DeletePortMapping", vInActionArgs, ref pvOutActionArgs);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #6
0
        public void VolumeMinus(string deviceId)
        {
            UPnPService service = GetServiceSync(deviceId, "urn:schemas-upnp-org:service:RenderingControl:1");

            object[] inArgs  = new object[] { 0, "Master" };
            object   outArgs = null;

            service.InvokeAction("GetVolume", inArgs, ref outArgs);

            ushort vol = (ushort)((object[])outArgs)[0];

            if ((vol -= 5) > 100)
            {
                vol = 0;
            }

            inArgs  = new object[] { 0, "Master", vol };
            outArgs = null;
            service.InvokeAction("SetVolume", inArgs, ref outArgs);
        }
Example #7
0
 private bool InternalSendCommand(String command)
 {
     if (UPnPService != null)
     {
         UPnPService.InvokeAction("X_SendKey", "X_KeyEvent", command);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #8
0
        /// <summary>
        /// Browses through UPnPService object with id objectId and adds it to a TreeView
        /// </summary>
        /// <param name="service"></param>
        /// <param name="objectId"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static XDocument Browser(UPnPService service, string objectId)
        {
            object output = new object();
            object[] input = new object[6] {objectId, "BrowseDirectChildren", "", 0, 0, "0",};
            object response;
            Array o;

            response = service.InvokeAction("Browse", input, ref output);
            o = (Array) output;
            XDocument content = XDocument.Parse(o.GetValue(0).ToString());

            return content;
        }
Example #9
0
        /// <summary>
        /// Browses through UPnPService object with id objectId and adds it to a TreeView
        /// </summary>
        /// <param name="service"></param>
        /// <param name="objectId"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static XDocument Browser(UPnPService service, string objectId)
        {
            object output = new object();

            object[] input = new object[6] {
                objectId, "BrowseDirectChildren", "", 0, 0, "0",
            };
            object response;
            Array  o;

            response = service.InvokeAction("Browse", input, ref output);
            o        = (Array)output;
            XDocument content = XDocument.Parse(o.GetValue(0).ToString());

            return(content);
        }
Example #10
0
        public void PortForwarding(bool forward, int port)
        {
            bool routerFound = false;

            Refresh();

            UPnPDevice[] devices;
            lock (this)
            {
                devices = this.devices.Values.Where(a => a.Type == "urn:schemas-upnp-org:device:WANConnectionDevice:1").ToArray();
            }

            foreach (UPnPDevice device in devices)
            {
                UPnPService service = GetService(device, "urn:schemas-upnp-org:service:WANIPConnection:1");
                if (service == null)
                {
                    service = GetService(device, "urn:schemas-upnp-org:service:WANPPPConnection:1");
                    if (service == null)
                    {
                        continue;
                    }
                }

                if (forward)
                {
                    //Pouzit presmerovanie
                    try
                    {
                        Uri documentUrl = new Uri(((IUPnPDeviceDocumentAccess)device).GetDocumentURL());

                        //Zistenie IP adresy tohoto PC pridelenu routrom
                        TcpClient client = new TcpClient();
                        client.Connect(documentUrl.Host, documentUrl.Port);
                        string localEndPoint = ((IPEndPoint)client.Client.LocalEndPoint).Address.ToString();
                        client.Close();

                        object[] inArgs  = new object[] { string.Empty, port, "TCP", port, localEndPoint, true, "HomeMediaCenter", 0 };
                        object   outArgs = null;
                        service.InvokeAction("AddPortMapping", inArgs, ref outArgs);

                        //Skontroluje ci bolo presmerovanie zapisane
                        inArgs  = new object[] { string.Empty, port, "TCP" };
                        outArgs = null;
                        service.InvokeAction("GetSpecificPortMappingEntry", inArgs, ref outArgs);

                        if (((string)((object[])outArgs)[1]) != localEndPoint)
                        {
                            throw new Exception();
                        }

                        routerFound = true;
                        this.upnpDevice.OnLogEvent("Port forwarded on UPnP router " + device.FriendlyName);
                    }
                    catch
                    {
                        this.upnpDevice.OnLogEvent("Unable to forward port on UPnP router " + device.FriendlyName);
                    }
                }
                else
                {
                    //Nepouzivat presmerovanie
                    try
                    {
                        //Pri najblizsom spusteni alebo restarte odstrani presmerovany port ak existuje
                        object[] inArgs  = new object[] { string.Empty, port, "TCP" };
                        object   outArgs = null;
                        service.InvokeAction("DeletePortMapping", inArgs, ref outArgs);
                    }
                    catch { }
                }
            }

            if (forward && !routerFound)
            {
                this.upnpDevice.OnLogEvent("UPnP router not found");
            }
        }