private void WorkerThread(object arg)
        {
            TcpClient client = (TcpClient)arg;

            try
            {
                NetworkStream         stream  = client.GetStream();
                EncodingServerHandler handler = new EncodingServerHandler(stream);

                while (true)
                {
                    NetworkMessageType messageType;
                    try
                    {
                        messageType = (NetworkMessageType)stream.ReadInt32();
                    }
                    catch (EndOfStreamException)
                    {
                        break;
                    }

                    switch (messageType)
                    {
                    case NetworkMessageType.EncodingRequest:
                        EncodingRequestMessage message = new EncodingRequestMessage();
                        message.Read(stream);
                        handler.OnEncodingRequest(message);
                        break;

                    case NetworkMessageType.DataMessage:
                        int length = stream.ReadInt32();
                        handler.OnInputData(length);
                        break;

                    case NetworkMessageType.DataEnd:
                        handler.OnInputDataEnd();
                        break;
                    }
                }
            }
            catch
            {
            }
            finally
            {
                this.threads.Remove(client);
            }
        }
        private void EnsureInitialized()
        {
            if (!this.initialized)
            {
                this.client = new TcpClient();
                this.client.Connect(this.remoteEndpoint);
                this.networkStream = this.client.GetStream();

                EncodingRequestMessage message = new EncodingRequestMessage()
                {
                    ChannelCount     = this.pcm.ChannelCount,
                    BitsPerSample    = this.pcm.BitsPerSample,
                    SampleRate       = this.pcm.SampleRate,
                    FinalSampleCount = this.finalSampleCount,
                    OutputFormat     = this.NetworkEncoderIdentifier,
                    CompressionLevel = this.compressionLevel
                };
                this.networkStream.Write((int)NetworkMessageType.EncodingRequest);
                message.Write(this.networkStream);

                this.initialized = true;
            }
        }
Exemple #3
0
        public void OnEncodingRequest(EncodingRequestMessage message)
        {
            this.outputStream = new MemoryStream();

            AudioPCMConfig pcm = new AudioPCMConfig(message.BitsPerSample, message.ChannelCount, message.SampleRate);

            if (message.OutputFormat == ".mp3")
            {
                this.audioDest = new LameWriter(outputStream, pcm)
                {
                    FinalSampleCount = message.FinalSampleCount,
                    Settings         = LameWriterSettings.CreateVbr(message.CompressionLevel)
                };
            }
            else if (message.OutputFormat == ".flac")
            {
                this.audioDest = new NativeFlacWriter(outputStream, pcm)
                {
                    FinalSampleCount = message.FinalSampleCount,
                    CompressionLevel = message.CompressionLevel
                };
            }
        }
        private void EnsureInitialized()
        {
            if (!this.initialized)
            {
                this.client = new TcpClient();
                this.client.Connect(this.remoteEndpoint);
                this.networkStream = this.client.GetStream();

                EncodingRequestMessage message = new EncodingRequestMessage()
                {
                    ChannelCount = this.pcm.ChannelCount,
                    BitsPerSample = this.pcm.BitsPerSample,
                    SampleRate = this.pcm.SampleRate,
                    FinalSampleCount = this.finalSampleCount,
                    OutputFormat = this.NetworkEncoderIdentifier,
                    CompressionLevel = this.compressionLevel
                };
                this.networkStream.Write((int)NetworkMessageType.EncodingRequest);
                message.Write(this.networkStream);

                this.initialized = true;
            }
        }
        private void WorkerThread(object arg)
        {
            TcpClient client = (TcpClient)arg;
            try
            {
                NetworkStream stream = client.GetStream();
                EncodingServerHandler handler = new EncodingServerHandler(stream);

                while (true)
                {
                    NetworkMessageType messageType;
                    try
                    {
                        messageType = (NetworkMessageType)stream.ReadInt32();
                    }
                    catch (EndOfStreamException)
                    {
                        break;
                    }

                    switch (messageType)
                    {
                        case NetworkMessageType.EncodingRequest:
                            EncodingRequestMessage message = new EncodingRequestMessage();
                            message.Read(stream);
                            handler.OnEncodingRequest(message);
                            break;
                        case NetworkMessageType.DataMessage:
                            int length = stream.ReadInt32();
                            handler.OnInputData(length);
                            break;
                        case NetworkMessageType.DataEnd:
                            handler.OnInputDataEnd();
                            break;
                    }
                }
            }
            catch
            {
            }
            finally
            {
                this.threads.Remove(client);
            }
        }