Example #1
0
        public BluetoothServer()
        {
            DevEnum.RegisterEndpointNotificationCallback(NOCL);
            AudioSessionTimer.Interval = 1000;
            AudioSessionTimer.Tick    += AudioSessionTimer_Tick;
            AudioSessionTimer.Start();
            GCColTimer.Interval = 60000;
            GCColTimer.Tick    += GCColTimer_Tick;
            GCColTimer.Start();

            lastChangedVol  = new ProcessVolumeInt(-99999, -99999, false);
            lastSentVol     = new ProcessVolumeInt(-99999, -99999, false);
            lastChangedVolD = new DeviceVolumeInt("", -99999, false);
            lastSentVolD    = new DeviceVolumeInt("", -99999, false);
            instance        = this;
            if (BluetoothRadio.IsSupported)
            {
                Console.WriteLine("Bluetooth Supported");
                Console.WriteLine("-------------------");
                Console.WriteLine("Primary Bluetooth Radio Name: " + BluetoothRadio.PrimaryRadio.Name);
                Console.WriteLine("Primary Bluetooth Radio Address: " + BluetoothRadio.PrimaryRadio.LocalAddress);
                Console.WriteLine("Primary Bluetooth Radio Manufacturer: " + BluetoothRadio.PrimaryRadio.Manufacturer);
                Console.WriteLine("Primary Bluetooth Radio Mode: " + BluetoothRadio.PrimaryRadio.Mode);
                Console.WriteLine("Primary Bluetooth Radio Software Manufacturer: " + BluetoothRadio.PrimaryRadio.SoftwareManufacturer);
                Console.WriteLine("Primary Bluetooth Radio Version: " + BluetoothRadio.PrimaryRadio.HciVersion);
                Console.WriteLine("-------------------");
            }
            else
            {
                BackgroundWindow.instance.notificationManager.SendNotification(Constants.Notifications.BluetoothNotSupported);
            }
        }
Example #2
0
        public void AudioSessionVolumeChanged(int ProcessID, float newVol, bool isMuted)
        {
            //Console.WriteLine(newVol*100 + ", " + isMuted.ToString().ToLower());

            if (isMuted)
            {
                newVol = 0;
            }

            if (Client.CurrentPage == Constants.Device_Pages.LEVELS)
            {
                if ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds - lastChangeSessionLevelTime > 200)
                {
                    if (!(lastChangedVol.pid == ProcessID && (int)(newVol * 100) == lastChangedVol.vol) || lastSentVol.isMuted)
                    {
                        if (lastSentVol.pid == ProcessID && (int)(newVol * 100) == lastSentVol.vol)
                        {
                            //Console.WriteLine("Not sending 2");
                        }
                        else
                        {
                            string id = ProcessID.ToString();
                            if (ProcessID == -69001)
                            {
                                id = "master";
                            }

                            SendMessage("{\"" + Constants.Commands.CHANGE_SESSION_LEVEL + "\": [\"" + id + "\", " + newVol * 100 + "]}", false);
                            lastSentVol = new ProcessVolumeInt(ProcessID, (int)(newVol * 100), isMuted);
                        }
                    }
                    else
                    {
                        //Console.WriteLine("Not sending 1");
                    }
                }
            }
            else
            {
                //Console.WriteLine("Not sending: " + Client.CurrentPage);
            }
        }
Example #3
0
        void ParseJSONPacket(string json)
        {
            Console.WriteLine("Parsing: " + json);

            dynamic data = DynamicJson.Parse(json);

            string command = "";

            try
            {
                command = data.c;
            }
            catch
            {
                Console.WriteLine("Command identifier not found...");
            }

            switch (command)
            {
            case Constants.Commands.DISCONNECT:
                DisconnectClient();
                break;

            case Constants.Commands.UPDATE_OBJECT:

                break;

            case Constants.Commands.UPDATE_PAGE:
                Client.CurrentPage = data.d;
                Notif("Changed client's current page to: " + Client.CurrentPage);
                break;

            case Constants.Commands.PING:
                SendMessage("{\"" + Constants.Commands.PONG + "\": \"\"}", true);
                break;

            case Constants.Commands.PONG:
                if (stopwatch.IsRunning)
                {
                    stopwatch.Stop();
                    Console.WriteLine("Ping to client: " + stopwatch.ElapsedMilliseconds + "ms");
                }
                else
                {
                    Error(Constants.ErrorType.InvalidlyTimedCommand, "Unable to output client response time due to the timer not running.");
                }
                break;

            case Constants.Commands.REQUEST_OUTPUT_DEVICES:
                MMDeviceCollection coll    = DevEnum.EnumerateAudioEndPoints(EDataFlow.eRender, EDeviceState.DEVICE_STATE_ACTIVE);
                MMDevice           defoutd = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

                string output = "{\"" + Constants.Commands.REQUEST_OUTPUT_DEVICES + "\":[";

                for (int i = 0; i < coll.Count; i++)
                {
                    output += "[\"" + coll[i].ID + "\",\"" + coll[i].FriendlyName + "\", " + (coll[i].ID == defoutd.ID).ToString().ToLower() + "],";
                }

                output = output.Remove(output.Length - 1, 1);

                output += "]}";
                SendMessage(output, true);
                break;

            case Constants.Commands.REQUEST_INPUT_DEVICES:
                foreach (KeyValuePair <int, string> pair in InputDevices)
                {
                    DevEnum.GetDevice(pair.Value).AudioEndpointVolume.OnVolumeNotification -= InputDeviceVolChange;
                }
                InputDevices.Clear();
                MMDeviceCollection coll2 = DevEnum.EnumerateAudioEndPoints(EDataFlow.eCapture, EDeviceState.DEVICE_STATE_ACTIVE);

                string output2 = "{\"" + Constants.Commands.REQUEST_INPUT_DEVICES + "\":[";

                for (int i = 0; i < coll2.Count; i++)
                {
                    InputDevices.Add(i, coll2[i].ID);
                    coll2[i].AudioEndpointVolume.OnVolumeNotification += InputDeviceVolChange;
                    output2 += "[\"" + i.ToString() + "\",\"" + coll2[i].FriendlyName + "\", " + coll2[i].AudioEndpointVolume.MasterVolumeLevelScalar * 100 + "],";
                }

                output2 = output2.Remove(output2.Length - 1, 1);

                output2 += "]}";

                SendMessage(output2, true);
                break;

            case Constants.Commands.REQUEST_AUDIO_SESSIONS:

                foreach (AudioSessionListener asl in listeners)
                {
                    asl.ASC.UnregisterAudioSessionNotification(asl);
                    asl.Dispose();
                }

                listeners.Clear();
                SessionCollection scol    = null;
                string            output3 = "";

                try
                {
                    lastDevice   = DevEnum.GetDevice(data.d);
                    lastDeviceID = lastDevice.ID;
                    lastDevice.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification;
                    scol = lastDevice.AudioSessionManager.Sessions;


                    output3 = "{\"" + Constants.Commands.REQUEST_AUDIO_SESSIONS + "\":{\"" + lastDevice.ID + "\": {";

                    output3 += "\"master\": " + lastDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100 + ",";
                }
                catch { }

                for (int i = 0; i < scol.Count; i++)
                {
                    if (!deadSessionProcessIDs.Contains((int)scol[i].ProcessID))
                    {
                        string name = "";
                        try
                        {
                            name = FileVersionInfo.GetVersionInfo(Process.GetProcessById((int)scol[i].ProcessID).MainModule.FileName).FileDescription.Trim();

                            if (name.Length > 20)
                            {
                                name = Process.GetProcessById((int)scol[i].ProcessID).ProcessName;
                                name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1));
                            }
                        }
                        catch
                        {
                            try
                            {
                                name = Process.GetProcessById((int)scol[i].ProcessID).ProcessName;
                                name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1));
                            }
                            catch
                            {
                                name = scol[i].DisplayName;
                            }
                        }

                        if (name == "Idle")
                        {
                            name = "System Sounds";
                        }

                        AudioSessionListener t = new AudioSessionListener((int)scol[i].ProcessID, scol[i], name);

                        scol[i].RegisterAudioSessionNotification(t);

                        listeners.Add(t);

                        output3 += "\"" + scol[i].ProcessID + "\": [\"" + name + "\", " + scol[i].SimpleAudioVolume.MasterVolume * 100 + "],";
                    }
                }

                output3 = output3.Remove(output3.Length - 1, 1);

                output3 += "} } }";

                SendMessage(output3, true);

                break;

            case Constants.Commands.CHANGE_SESSION_LEVEL:
                if (data.d.pid == "master")
                {
                    try
                    {
                        lastDevice = DevEnum.GetDevice(lastDeviceID);
                        lastDevice.AudioEndpointVolume.MasterVolumeLevelScalar = (float)(data.d.vol / 100);

                        if (lastDevice.AudioEndpointVolume.MasterVolumeLevelScalar == 0)
                        {
                            lastDevice.AudioEndpointVolume.Mute = true;
                        }
                        else
                        {
                            lastDevice.AudioEndpointVolume.Mute = false;
                        }

                        lastChangeSessionLevelTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
                        lastChangedVol             = new ProcessVolumeInt(-69001, (int)data.d.vol, ((int)data.d.vol == 0));
                    }
                    catch { }
                }
                else
                {
                    List <AudioSessionListener> asls = listeners.Where(value => value.ProcessID.ToString() == data.d.pid).ToList();
                    foreach (AudioSessionListener asl in asls)
                    {
                        asl.ASC.SimpleAudioVolume.MasterVolume = (float)(data.d.vol / 100);

                        if ((int)data.d.vol == 0)
                        {
                            asl.ASC.SimpleAudioVolume.Mute = true;
                        }
                        else
                        {
                            asl.ASC.SimpleAudioVolume.Mute = false;
                        }

                        lastChangedVol = new ProcessVolumeInt(asl.ProcessID, (int)data.d.vol, ((int)data.d.vol == 0));
                    }

                    lastChangeSessionLevelTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;

                    /*foreach (AudioSessionListener asl2 in listeners)
                     * {
                     *  if (asl2.ProcessID.ToString() == data.d.pid)
                     *  {
                     *      asl2.ASC.SimpleAudioVolume.MasterVolume = (float)(data.d.vol / 100);
                     *      break;
                     *  }
                     * }*/
                }
                break;

            case Constants.Commands.CHANGE_INPUT_LEVEL:
                MMDevice dev = DevEnum.GetDevice(InputDevices[Convert.ToInt32(data.d[0])]);
                dev.AudioEndpointVolume.MasterVolumeLevelScalar = (float)(data.d[1] / 100);
                lastChangedVolD            = new DeviceVolumeInt(InputDevices[Convert.ToInt32(data.d[0])], Convert.ToInt32(data.d[1]), (bool)(Convert.ToInt32(data.d[1]) == 0));
                lastChangeSessionLevelTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
                break;

            case Constants.Commands.REQUEST_TIME_ZONE:
                SendMessage("{\"" + Constants.Commands.REQUEST_TIME_ZONE + "\": \"" + olsonmap.Find(System.TimeZoneInfo.Local.Id) + "\"}", true);
                break;
            }
        }