public static void ShowConsole() { var handle = GetConsoleWindow(); var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle); // If we are outputting somewhere unexpected, add an additional console window if (outputRedirected) { var originalStream = Console.OpenStandardOutput(); // Free before trying to allocate FreeConsole(); AllocConsole(); var outputStream = Console.OpenStandardOutput(); if (originalStream != null) { outputStream = new DualStream(originalStream, outputStream); } TextWriter writer = new StreamWriter(outputStream) { AutoFlush = true }; Console.SetOut(writer); } else if (handle != IntPtr.Zero) { const int SW_SHOW = 5; ShowWindow(handle, SW_SHOW); } }
public static void ShowConsole() { var handle = GetConsoleWindow(); var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle); if (handle == IntPtr.Zero || outputRedirected) { Stream originalStream = null; if (outputRedirected) { originalStream = Console.OpenStandardOutput(); } AllocConsole(); Stream outputStream = Console.OpenStandardOutput(); if (originalStream != null) { outputStream = new DualStream(originalStream, outputStream); } TextWriter writer = new StreamWriter(outputStream) { AutoFlush = true }; Console.SetOut(writer); } else { const int SW_SHOW = 5; ShowWindow(handle, SW_SHOW); } }
public void TestDoubleStream() { byte[] inputBytes = new byte[] { 6, 8, 4, 22, 11 }; byte[] outputBytes = new byte[] { 66, 88, 101 }; MemoryStream inputStream = new MemoryStream(inputBytes); MemoryStream outputStream = new MemoryStream(); DualStream testStream = new DualStream(); testStream.InputStream = inputStream; testStream.OutputStream = outputStream; foreach (byte value in inputBytes) { Assert.IsTrue(testStream.ReadByte() == value); } foreach (byte value in outputBytes) { testStream.WriteByte(value); } Assert.IsTrue(outputStream.ToArray().SequenceEqual(outputBytes)); Assert.IsTrue(testStream.Read(inputBytes, 0, 5) == 0); }
public static void ShowConsole() { var handle = GetConsoleWindow(); var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle); // If we are outputting somewhere unexpected, add an additional console window if (outputRedirected) { var originalStream = Console.OpenStandardOutput(); // Free before trying to allocate FreeConsole(); AllocConsole(); var outputStream = Console.OpenStandardOutput(); if (originalStream != null) { outputStream = new DualStream(originalStream, outputStream); } TextWriter writer = new StreamWriter(outputStream) { AutoFlush = true }; Console.SetOut(writer); } else if (handle != IntPtr.Zero) { const int SW_SHOW = 5; ShowWindow(handle, SW_SHOW); } }
private async Task BufferSong() { //start feeding the buffer var p = Process.Start(new ProcessStartInfo { FileName = "ffmpeg", Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = false, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, }); int attempt = 0; while (true) { int magickBuffer = 1; //wait for the read pos to catch up with write pos while (buffer.writePos - buffer.readPos > 1.MB() && State != StreamState.Completed) { prebufferingComplete = true; await Task.Delay(150); } if (State == StreamState.Completed) { try { p.CancelOutputRead(); p.Close(); } catch (Exception) { } Console.WriteLine("Buffering canceled, stream is completed."); return; } if (buffer.readPos > 1.MiB() && buffer.writePos > 1.MiB()) { // if buffer is over 5 MiB, create new one var skip = 1.MB(); //remove only 5 MB, just in case var newBuffer = new DualStream(); lock (_bufferLock) { byte[] data = buffer.ToArray().Skip(skip).ToArray(); var newReadPos = buffer.readPos - skip; var newPos = buffer.Position - skip; buffer = newBuffer; buffer.Write(data, 0, data.Length); buffer.readPos = newReadPos; buffer.Position = newPos; } } var buf = new byte[1024]; int read = 0; read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, 1024); //Console.WriteLine($"Read: {read}"); if (read == 0) { if (attempt == 5) { try { p.CancelOutputRead(); p.Close(); } catch (Exception) { } Console.WriteLine($"Didn't read anything from the stream for {attempt} attempts. {buffer.Length/1.MB()}MB length"); return; } else { ++attempt; await Task.Delay(20); } } else { attempt = 0; await buffer.WriteAsync(buf, 0, read); } } }
public MusicStreamer(StreamRequest parent, string directUrl) { this.parent = parent; this.buffer = new DualStream(); this.Url = directUrl; State = StreamState.Queued; }
public static void ShowConsole() { var handle = GetConsoleWindow(); var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle); if (handle == IntPtr.Zero || outputRedirected) { Stream originalStream = null; if (outputRedirected) { originalStream = Console.OpenStandardOutput(); } AllocConsole(); Stream outputStream = Console.OpenStandardOutput(); if (originalStream != null) { outputStream = new DualStream(originalStream, outputStream); } TextWriter writer = new StreamWriter(outputStream) { AutoFlush = true }; Console.SetOut(writer); } else { const int SW_SHOW = 5; ShowWindow(handle, SW_SHOW); } }