コード例 #1
0
ファイル: Form_tcp.cs プロジェクト: wawawa3/ScreenShare
        private void SendCaptureStartMessage(UserContext ctx)
        {
            var setting = new SettingData
            {
                audioSettingData = new SettingData.AudioSettingData
                {
                    sampleRate = m_Recorder.WaveFormat.SampleRate,
                    isStereo = m_Recorder.WaveFormat.Channels == 2,
                },
                captureSettingData = new SettingData.CaptureSettingData
                {
                    divisionNum = m_Capture.CaptureDivisionNum,
                },
            };
            var data = new MessageData { type = MessageData.Type.Settings, data = setting };
            var json = JsonConvert.SerializeObject(data);

            ctx.Send(json);

            data = new MessageData { type = MessageData.Type.StartCapture };
            json = JsonConvert.SerializeObject(data);

            ctx.Send(json);
        }
コード例 #2
0
ファイル: Form_tcp.cs プロジェクト: wawawa3/ScreenShare
        /// <summary>
        /// HTTPサーバとWebSocketサーバの停止/切断
        /// </summary>
        private void Disconnect()
        {
            if (m_Capture.Capturing)
                StopCapture();

            if (m_ServerRunning)
            {
                m_ServerRunning = false;

                var data = new MessageData { type = MessageData.Type.Disconnect };
                var json = JsonConvert.SerializeObject(data);
                foreach (var pair in m_WebSocketClients) 
                    pair.Value.Send(json);

                m_WebSocketClients.Clear();
                m_WebSocketServer.Stop();
                this.label_ConnectionNum.Text = "0";

                m_HttpServer.Close();
            }

            Invoke(new FormDelegate(() => m_FormOverRay.Hide()));
        }
コード例 #3
0
ファイル: Form_tcp.cs プロジェクト: wawawa3/ScreenShare
        /// <summary>
        /// 画面キャプチャ(音声録音)の停止
        /// </summary>
        private void StopCapture()
        {
            foreach (var pair in m_WebSocketClients)
            {
                var data = new MessageData { type = MessageData.Type.StopCapture };
                var json = JsonConvert.SerializeObject(data);

                pair.Value.Send(json);
            }

            m_Capture.StopAsync();

            if (checkBox_sendAudio.Checked || checkBox_recordAudio.Checked)
            {
                m_Recorder.StopRecording();
            }

            if (m_AviWriter != null)
            {
                Debug.Log("" + m_AviVideoStream.FramesWritten);
                m_AviWriter.Close();
                m_AviWriter = null;
                m_AviVideoStream = null;
                m_AviAudioStream = null;
            }
        }
コード例 #4
0
ファイル: Form_tcp.cs プロジェクト: wawawa3/ScreenShare
        public Form_tcp()
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(Settings.Default.SpecificCulture);

            InitializeComponent();

            try
            {
                ProcessStartInfo info = new ProcessStartInfo(Batch_FirewallAdd);
                info.UseShellExecute = true;
                info.Verb = "runas";

                var p = Process.Start(info);
                p.WaitForExit();
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show(Resources.BatchNotFound + "\n\"" + Batch_FirewallAdd + "\"", Resources.CaptionError);
                Debug.Log(ex.Message + Batch_FirewallAdd);
            }
            catch
            {
                MessageBox.Show(Resources.BatchFailed + "\n\"" + Batch_FirewallAdd + "\"", Resources.CaptionError);
            }

            Application.ApplicationExit += (s, e) =>
            {
                try
                {
                    ProcessStartInfo info = new ProcessStartInfo(Batch_FirewallDelete);
                    info.UseShellExecute = true;
                    info.Verb = "runas";

                    var p = Process.Start(info);
                    p.WaitForExit();
                }
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show(Resources.BatchNotFound + "\n\"" + Batch_FirewallDelete + "\"", Resources.CaptionError);
                    Debug.Log(ex.Message);
                }
                catch
                {
                    MessageBox.Show(Resources.BatchFailed + "\n\"" + Batch_FirewallDelete + "\"", Resources.CaptionError);
                }

                Debug.Log("Application Exit");
            };


            ReloadRunningProcesses();
            ReloadWaveInDevices();

            IPHostEntry ipentry = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in ipentry.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    textBox_ip.Text = ip.ToString();
                    break;
                }
            }

            var codecs = Mpeg4VideoEncoderVcm.GetAvailableCodecs();
            foreach (var codec in codecs)
            {
                comboBox_videoCodec.Items.Add(codec.Name);
            }

            comboBox_audioQuality.SelectedIndex = 1;
            comboBox_captureScale.SelectedIndex = 0;
            comboBox_divisionNumber.SelectedIndex = 2;
            comboBox_videoCodec.SelectedIndex = 0;

            m_FormCapture.Selected += (rect) => 
            { 
                m_CaptureBounds = rect;

                m_FormCapture.Hide();

                m_FormOverRay.CaptureBounds = rect;

                if (checkBox_showOverRayForm.Checked)
                    m_FormOverRay.Show();
            };

            m_Capture.SegmentCaptured += (s, data) =>
            {
                var frameHeader = new FrameHeader
                {
                    type = BufferType.FrameBuffer,
                    segmentIndex = (byte)data.segmentIdx,
                };
                var headerBuffer = ByteUtils.GetBytesFromStructure(frameHeader);
                var buffer = ByteUtils.Concatenation(headerBuffer, data.encodedFrameBuffer);

                if (m_WebSocketClients.ContainsKey(0))
                {
                    //Debug.Log("send");
                    m_WebSocketClients[0].Send(buffer);
                }

                m_LatestIntraFrameBuffer[data.segmentIdx] = (byte[])buffer.Clone();
            };

            m_Capture.Captured += (s, data) =>
            {
                if (m_AviVideoStream == null) return;

                var buf = ByteUtils.GetBytesFromPtr(data.captureData, data.captureSize.Width * data.captureSize.Height * 4);

                try
                {
                    m_AviVideoStream.WriteFrame(true, buf, 0, buf.Length);
                }
                catch (Exception e)
                {
                    Debug.Log("Captured Exception: " + e.Message);
                }
            };

            m_Capture.Error += (s, ex) =>
            {
                label_message.Text = Resources.CaptureError + " : " + ex.Message;
            };

            //m_Recorder.BufferMilliseconds = 100;
            m_Recorder.DataAvailable += (s, we) =>
            {
                var normalizedSampleBuffer = new byte[we.BytesRecorded / 2 * 4];

                for (int i = 0, j = 0; i < we.BytesRecorded; i += 2)
                {
                    short sample = (short)((we.Buffer[i + 1] << 8) | we.Buffer[i + 0]);
                    float s32 = sample / 32768f;
                    var sampleByte = BitConverter.GetBytes(s32);

                    normalizedSampleBuffer[j++] = sampleByte[0];
                    normalizedSampleBuffer[j++] = sampleByte[1];
                    normalizedSampleBuffer[j++] = sampleByte[2];
                    normalizedSampleBuffer[j++] = sampleByte[3];
                }

                var frameHeader = new AudioHeader
                {
                    type = BufferType.AudioBuffer,
                };
                var headerBuffer = ByteUtils.GetBytesFromStructure(frameHeader);
                var buffer = ByteUtils.Concatenation(headerBuffer, normalizedSampleBuffer);

                if (checkBox_sendAudio.Checked)
                {
                    if (m_WebSocketClients.ContainsKey(0))
                    {
                        m_WebSocketClients[0].Send(buffer);
                    }
                }

                if (m_AviAudioStream == null || (m_AviVideoStream != null && m_AviVideoStream.FramesWritten == 0)) return;

                try
                {
                    m_AviAudioStream.WriteBlock(we.Buffer, 0, we.BytesRecorded);
                }
                catch (Exception e)
                {
                    Debug.Log("Audio Recorded Exception: " + e.Message);
                }
            };

            m_WebSocketServer = new Alchemy.WebSocketServer(Settings.Default.Port_WebSocket)
            {
                OnConnected = (ctx) =>
                {
                    if (!m_ServerRunning) return;

                    lock (m_WebSocketServer)
                    {
                        int id = m_WebSocketClients.Count, parentId;
                        UserContext parent;

                        MessageData data;
                        string json;

                        Debug.Log("User Connected: id = " + id);

                        try
                        {
                            try
                            {
                                parentId = m_WebSocketClients.GetParentKey(id);
                                parent = m_WebSocketClients[parentId];
                            }
                            catch
                            {
                                parentId = -1;
                                parent = null;
                            }

                            data = new MessageData { type = MessageData.Type.Connected, id = id, };
                            json = JsonConvert.SerializeObject(data);

                            m_WebSocketClients[id] = ctx;
                            m_WebSocketClients[id].Send(json);

                            if (m_Capture.Capturing)
                            {
                                SendCaptureStartMessage(m_WebSocketClients[id]);

                                /*
                                if (parent == null)
                                {
                                    foreach (var buffer in m_LatestIntraFrameBuffer)
                                        m_WebSocketClients[id].Send(buffer);
                                }
                                    * */
                            }

                            if (parent != null)
                            {
                                data = new MessageData { type = MessageData.Type.PeerConnection, targetId = id, };
                                json = JsonConvert.SerializeObject(data);

                                parent.Send(json);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Log("Connected Error : " + e.Message);
                        }

                        ctx.MaxFrameSize = 0x80000;
                    }

                    Invoke(new FormDelegate(() => { this.label_ConnectionNum.Text = "" + m_WebSocketClients.Count; }));
                },
                OnDisconnect = (ctx) =>
                {
                    if (!m_ServerRunning) return;

                    lock (m_WebSocketServer)
                    {
                        int rmId, updatedId, rmParentId, lastId, lastParentId;
                        int[] rmChildrenId;

                        UserContext rm, updated, rmParent, last, lastParent;
                        UserContext[] rmChildren;

                        MessageData data;
                        string json;

                        rm = ctx;

                        try
                        {
                            rmId = m_WebSocketClients.TryGetKey(rm);
                        }
                        catch
                        {
                            Debug.Log("Disconnect: Can't find User");
                            return;
                        }

                        try
                        {
                            rmParentId = m_WebSocketClients.GetParentKey(rmId);
                            rmParent = m_WebSocketClients[rmParentId];
                        }
                        catch
                        {
                            rmParentId = -1;
                            rmParent = null;
                        }

                        lastId = m_WebSocketClients.GetLastKey();
                        last = m_WebSocketClients[lastId];

                        try
                        {
                            lastParentId = m_WebSocketClients.GetParentKey(lastId);
                            lastParent = m_WebSocketClients[lastParentId];
                        }
                        catch
                        {
                            lastParentId = -1;
                            lastParent = null;
                        }

                        rmChildrenId = m_WebSocketClients.GetChildrenKey(rmId);
                        rmChildren = m_WebSocketClients.GetValues(rmChildrenId);

                        try
                        {
                            if (rmParent != null)
                            {
                                data = new MessageData { type = MessageData.Type.RemoveOffer, id = rmId, };
                                json = JsonConvert.SerializeObject(data);
                                rmParent.Send(json);
                            }

                            if (lastId == rmId)
                            {
                                m_WebSocketClients.Remove(lastId);
                                Debug.Log("RemoveID = LastID");
                                return;
                            }
                           
                            data = new MessageData { type = MessageData.Type.RemoveAnswer, id = rmId, };
                            json = JsonConvert.SerializeObject(data);
                            foreach (var child in rmChildren)
                            {
                                child.Send(json);
                            }

                            if (lastParentId != rmId)
                            {
                                data = new MessageData { type = MessageData.Type.RemoveOffer, id = lastId };
                                json = JsonConvert.SerializeObject(data);
                                lastParent.Send(json);
                            }

                            data = new MessageData { type = MessageData.Type.RemoveAnswer, id = lastParentId, };
                            json = JsonConvert.SerializeObject(data);
                            last.Send(json);

                            m_WebSocketClients[rmId] = m_WebSocketClients[lastId];
                            m_WebSocketClients.Remove(lastId);

                            updatedId = rmId;
                            updated = m_WebSocketClients[rmId];

                            data = new MessageData { type = MessageData.Type.UpdateID, id = rmId, };
                            json = JsonConvert.SerializeObject(data);
                            updated.Send(json);

                            if (rmParent != null)
                            {
                                data = new MessageData { type = MessageData.Type.PeerConnection, targetId = updatedId, };
                                json = JsonConvert.SerializeObject(data);
                                rmParent.Send(json);
                            }

                            foreach (var cId in rmChildrenId)
                            {
                                data = new MessageData { type = MessageData.Type.PeerConnection, targetId = cId, };
                                json = JsonConvert.SerializeObject(data);
                                updated.Send(json);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Log("Disconnected Error : " + e.Message);
                        }
                        finally
                        {
                            Debug.Log("User Disconnected: id = " + rmId);
                            Invoke(new FormDelegate(() => { this.label_ConnectionNum.Text = "" + m_WebSocketClients.Count; }));
                        }
                    }
                },
                OnReceive = (ctx) =>
                {
                    if (!m_ServerRunning) return;

                    lock (m_WebSocketServer)
                    {
                        try
                        {
                            var json = ctx.DataFrame.ToString();
                            var recv = JsonConvert.DeserializeObject<MessageData>(json);
                            try
                            {
                                var dest = m_WebSocketClients[recv.targetId];

                                if (dest != null)
                                {
                                    dest.Send(json);
                                    Debug.Log("Relay '" + recv.type + "' from id:" + recv.id + " to id: " + recv.targetId);
                                }
                                else
                                {
                                    Debug.Log("Failed to Relay Message: id = " + recv.targetId);
                                }
                            }
                            catch (Exception e)
                            {
                                Debug.Log("Receive Error : " + e.Message);
                                return;
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Log("Receive Error : " + e.Message);
                        }
                    }
                },
                TimeOut = new TimeSpan(0, 5, 0),
            };
        }