private void resetAllInstanseState() { this._WaveIn.RecordingStopped -= this.WaveInOnRecordingStopped; this._WaveIn.StopRecording(); this._WaveIn.Dispose(); this._WaveIn = null; if (GlobalConfiguration.isEncodeWithOpus && m_opusEncoder != null) { m_opusEncoder.Dispose(); m_opusEncoder = null; } //------ this._WaveIn = new WasapiLoopbackCapture(m_device); this._WaveIn.DataAvailable += this.WaveInOnDataAvailable; this._WaveIn.RecordingStopped += this.WaveInOnRecordingStopped; sdsock.ReListen(); Start(); // start capture and send stream }
private void WaveInOnDataAvailable(object sender, WaveInEventArgs e) { //Console.WriteLine($"{DateTime.Now:yyyy/MM/dd hh:mm:ss.fff} : {e.BytesRecorded} bytes"); if (GlobalConfiguration.isRunCapturedSoundDataHndlingWithoutConn == false && sdsock.IsConnected() == false) { return; } if (e.BytesRecorded == 0) { return; } if (GlobalConfiguration.isUseFFMPEG) { if (GlobalConfiguration.caputuedPcmBufferSamples == 0) { if (e.BytesRecorded > 0) { MainApplicationContext.ffmpegProc.StandardInput.BaseStream.Write(e.Buffer, 0, e.BytesRecorded); MainApplicationContext.ffmpegProc.StandardInput.BaseStream.Flush(); } } else { captured_buf.Write(e.Buffer, 0, e.BytesRecorded); int needed_samples = GlobalConfiguration.caputuedPcmBufferSamples; //1024 * 100; //1024; // 指定されたサンプル数が溜まったら書き込む (adtsでは 1フレーム = 1024サンプル) if (captured_buf.Length / (4 * 2) >= needed_samples) { Console.WriteLine(Utils.getFormatedCurrentTime() + " DEBUG: pass " + needed_samples.ToString() + " samples to ffmpeg"); captured_buf.Position = 0; byte[] tmp_buf = new byte[4 * 2 * needed_samples]; captured_buf.Read(tmp_buf, 0, tmp_buf.Length); MainApplicationContext.ffmpegProc.StandardInput.BaseStream.Write(tmp_buf, 0, tmp_buf.Length); MemoryStream new_ms = new MemoryStream(); // 残ったデータの処理 captured_buf.Position = 4 * 2 * needed_samples; byte[] left_data_buf = new byte[captured_buf.Length - 4 * 2 * needed_samples]; captured_buf.Read(left_data_buf, 0, left_data_buf.Length); captured_buf.Position = 0; captured_buf.SetLength(0); captured_buf.Write(left_data_buf, 0, left_data_buf.Length); MainApplicationContext.ffmpegProc.StandardInput.BaseStream.Flush(); } } } else if (GlobalConfiguration.isEncodeWithOpus) { if (m_opusEncoder == null) { // m_opusEncoder = new OpusEncoderManager(this, GlobalConfiguration.SamplesPerSecond); m_opusEncoder = new OpusEncoderManager(this, GlobalConfiguration.SampleRateDummyForSoundEnDecoder); } byte[] conved_pcm = convert32bitFloat48000HzStereoPCMTo16bitMonoPCM(e, GlobalConfiguration.SamplesPerSecond); //byte[] conved_pcm = convert32bitFloat48000HzStereoPCMTo16bitMonoPCM_Alpha(e, GlobalConfiguration.SamplesPerSecond); Console.WriteLine(Utils.getFormatedCurrentTime() + " DEBUG: pass " + conved_pcm.Length.ToString() + " bytes to opus encoder"); // エンコーダクラスが流量制御をして送信まで行う m_opusEncoder.addPCMSamples(conved_pcm, conved_pcm.Length); } else { throw new Exception("condition is invalid"); } }