Exemplo n.º 1
0
        public NetIQServerStream(IPEndPoint endpoint, int bufferSize, NetIQSampleFormat format)
        {
            //Set
            this.endpoint   = endpoint;
            this.bufferSize = bufferSize;
            this.format     = format;

            //Validate
            if (endpoint.Port < 9000)
            {
                throw new Exception("Port must be >= 9000!");
            }
            if (bufferSize < 64 || bufferSize > NetIQUtil.GetMaxBufferSize(format))
            {
                throw new Exception("Buffer size is invalid.");
            }
            if (format != NetIQSampleFormat.Byte && format != NetIQSampleFormat.Short && format != NetIQSampleFormat.Float)
            {
                throw new Exception("Invalid format.");
            }

            //Make socket
            sock = new Socket(SocketType.Dgram, ProtocolType.Udp);

            //Create buffer and get pointers to it
            buffer         = new byte[((int)format / 8) * 2 * bufferSize];
            bufferHandle   = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            bufferPtrByte  = (byte *)bufferHandle.AddrOfPinnedObject();
            bufferPtrShort = (short *)bufferHandle.AddrOfPinnedObject();
            bufferPtrFloat = (float *)bufferHandle.AddrOfPinnedObject();
        }
Exemplo n.º 2
0
        public void OpenStream(ushort bufferSize, NetIQSampleFormat format)
        {
            //Validate
            int maxBufferSize = NetIQUtil.GetMaxBufferSize(format);

            if (bufferSize > maxBufferSize)
            {
                throw new Exception($"Max buffer size is {maxBufferSize}. Please lower the buffer size or make the sample format smaller.");
            }

            //Set
            streamSampleFormat = format;

            //Open the incoming buffer
            streamBuffer         = new byte[((int)format / 8) * 2 * bufferSize];
            streamBufferHandle   = GCHandle.Alloc(streamBuffer, GCHandleType.Pinned);
            streamBufferPtrByte  = (byte *)streamBufferHandle.AddrOfPinnedObject();
            streamBufferPtrShort = (short *)streamBufferHandle.AddrOfPinnedObject();
            streamBufferPtrFloat = (float *)streamBufferHandle.AddrOfPinnedObject();

            //Open the outgoing buffer
            sampleBuffer = UnsafeBuffer.Create(bufferSize, out sampleBufferPtr);

            //Get port
            int port = endpoint.Port + 1;

            //Open socket
            stream = new Socket(SocketType.Dgram, ProtocolType.Udp);
            stream.Bind(new IPEndPoint(IPAddress.Any, port));
            stream.BeginReceive(streamBuffer, 0, streamBuffer.Length, SocketFlags.None, OnStreamPacket, null);

            //Send command to start streaming
            NetIQCommandOpenStream cmd = new NetIQCommandOpenStream();

            cmd.StreamPort   = (ushort)port;
            cmd.BufferSize   = bufferSize;
            cmd.SampleFormat = format;
            cmd.SendOnSocket(sock);
        }