private void PerformStop(CapturingProcess capturing) { try { capturing.Process.Stop(); } catch (Exception ex) { try { capturing.CloseOwnedOutputStream(); } finally { //Stop should not be called before the owned out stream is closed. FireStopped(); } throw; } try { capturing.CloseOwnedOutputStream(); } finally { //Stop should not be called before the owned out stream is closed. FireStopped(); } }
/// <summary> /// Stops capturing process. If WaitOnStop property is false then /// exits immediately - the output stream can be then closed only after Stopped event /// is fired. /// </summary> /// <remarks> /// Stopping process may last some time especially when NormalizeVolume is true. /// The sound is normalized after the call to Stop() method, then - if you use MP3 output /// type - all data must be yet compressed. All that can take time. /// </remarks> public void Stop() { if (capturing != null) { lock (CaptureLock) { CapturingProcess capturingObject = capturing; capturing = null; FireEvent(Stopping); if (WaitOnStop) { PerformStop(capturingObject); } else { StopMethod stopping = () => { PerformStop(capturingObject); }; stopping.BeginInvoke(null, null); } } } }
/// <summary> /// Asynchronously starts capturing. /// </summary> /// <param name="outputStream">A writeable stream where the captured data will be /// written (in formate specified by OutputType property).</param> private void Start(Stream outputStream, bool ownsOutputStream) { if (capturing == null) { lock (CaptureLock) { if (capturing == null) { try { capturing = new CapturingProcess(); capturing.OwnsOutputStream = ownsOutputStream; FireEvent(Starting); SoundCapture capture = new SoundCapture(WaveFormat, outputStream, UseSynchronizationContext); capturing.Process = capture; capture.CaptureDevice = CaptureDevice; if (NormalizeVolume) { capture.Filters.Add(new Filters.PcmNormalizerFilter()); } if (OutputType == Outputs.Mp3) { Filters.Mp3CompressingFilter compressor = new Filters.Mp3CompressingFilter(); compressor.Mp3BitRate = Mp3BitRate; capture.Filters.Add(compressor); } else if (OutputType == Outputs.Wav) { capture.Filters.Add(new Filters.RiffFilter()); } //Else a raw PCM will go off. capture.Started += (o, e) => { if (Started != null) { Started(this, e); } }; //Stopped is fired by this component itself. capture.Start(); } catch { Stop(); throw; } } } } }
static void Main(string[] args) { Console.Write("Enter PID number: "); int pid = Convert.ToInt32(Console.ReadLine()); CapturingProcess capturing = new CapturingProcess(pid, "output.txt"); //capturing.ShowRunningProcesses(); capturing.CaptureAfterWaiting(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
private void FireStopped(CapturingProcess capturing, Exception exception) { if (Stopped != null) { if (!UseSynchronizationContext || syncContext == null) { Stopped(this, new StoppedEventArgs(capturing, exception)); } else { syncContext.Send((obj) => Stopped(this, new StoppedEventArgs(capturing, exception)), null); } } }
internal StoppedEventArgs(CapturingProcess capturing, Exception exception) { FileStream fileStream = capturing.Process == null ? null : capturing.Process.OutputStream as FileStream; this.OutputFileName = fileStream == null ? null : fileStream.Name; this.OutputStream = capturing.Process == null ? null : capturing.Process.OutputStream; this.Exception = exception; }
/// <summary> /// Stops capturing process. If WaitOnStop property is false then /// exits immediately - the output stream can be then closed only after Stopped event /// is fired. /// </summary> /// <remarks> /// Stopping process may last some time especially when NormalizeVolume is true. /// The sound is normalized after the call to Stop() method, then - if you use MP3 output /// type - all data must be yet compressed. All that can take time. /// </remarks> public void Stop() { if (capturing != null) { lock (CaptureLock) { CapturingProcess capturingObject = capturing; capturing = null; if (WaitOnStop) { PerformStop(capturingObject); } else { AsyncStopping.BeginInvoke(capturingObject, new AsyncCallback(StoppingCallback), null); } } } }
/// <summary> /// Asynchronously starts capturing. /// </summary> /// <param name="outputStream">A writeable stream where the captured data will be /// written (in formate specified by OutputType property).</param> private void Start(Stream outputStream, bool ownsOutputStream) { if (capturing == null) { lock (CaptureLock) { if (capturing == null) { try { capturing = new CapturingProcess(); capturing.OwnsOutputStream = ownsOutputStream; SoundCapture capture = new SoundCapture(WaveFormat, outputStream, UseSynchronizationContext); capturing.Process = capture; capture.CaptureDevice = CaptureDevice; if (UseVOX) { int OneSecBufferSize = WaveFormat.SampleRate * ((WaveFormat.BitsPerSample * WaveFormat.Channels) / 8); this.vox = new Filters.VOXFilter(OneSecBufferSize * BufferSeconds); this.vox.NAttack = MinSoundSeconds * 8; // One buffer is 1/8 sec (125usec) this.vox.NDecay = MinSoundSeconds * 8; this.vox.VolumeThreshold = VOXThreshold; this.vox.VOXStopping += new EventHandler(vox_VOXStopping); capture.Filters.Add(vox); } if (NormalizeVolume) { capture.Filters.Add(new Filters.PcmNormalizerFilter()); } if (OutputType == Outputs.Mp3) { Filters.Mp3CompressingFilter compressor = new Filters.Mp3CompressingFilter(); compressor.Mp3BitRate = Mp3BitRate; capture.Filters.Add(compressor); } else if (OutputType == Outputs.Wav) { capture.Filters.Add(new Filters.RiffFilter()); } //Else a raw PCM will go off. capture.Started += (o, e) => { if (Started != null) { Started(this, e); } }; //Stopped is fired by this component itself. capture.Start(); } catch { Stop(); throw; } } } } }