static void Main(string[] args)
        {
            String       input_path  = "F:\\work\\tmp\\capturedPCM_ffmpeg_stdout_converted_8000Hz_u8bit_1ch_120sec.raw";
            String       output_path = "F:\\work\\tmp\\capturedPCM_ffmpeg_stdout_converted_8000Hz_u8bit_1ch_120sec_encoded_my_DPCM_codec.raw";
            MemoryStream ms          = new MemoryStream();

            byte[] buf = new byte[1024];

            var reader = new FileStream(input_path, FileMode.Open);
            int readBytes;

            try
            {
                while ((readBytes = reader.Read(buf, 0, buf.Length)) > 0)
                {
                    ms.Write(buf, 0, readBytes);
                }
            }
            finally
            {
                ms.Flush();
                reader.Close();
            }

            var encoder = new MyDpcmCodec();
            var decoder = new MyDpcmCodec();

            //Utils.saveByteArrayToFile(decoder.Decode(encoder.Encode(ms.ToArray())), output_path);
            Utils.saveByteArrayToFile(decoder.Decode(encoder.Encode(ms.ToArray())), output_path);
        }
Пример #2
0
        // set ffmpegProc field
        private void kickFFMPEG()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();

            startInfo.UseShellExecute = false; //required to redirect standart input/output

            // redirects on your choice
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError  = true;
            startInfo.RedirectStandardInput  = true;
            startInfo.CreateNoWindow         = true;
            startInfo.FileName = ffmpegPath;

            if (GlobalConfiguration.isEncodeWithDpcmOrUseRawPCM)
            {
                startInfo.Arguments = ffmpegForPCMConvertArgs;
                if (GlobalConfiguration.isUseDPCM)
                {
                    dpcm_encoder = new MyDpcmCodec();
                }
            }
            else
            {
                startInfo.Arguments = ffmpegForAudioEncodeArgs;
            }

            //startInfo.Arguments = ffmpegForDirectStreamingArgs;

            ffmpegProc           = new Process();
            ffmpegProc.StartInfo = startInfo;
            // リダイレクトした標準出力・標準エラーの内容を受信するイベントハンドラを設定する
            //ffmpegProc.OutputDataReceived += useFFMPEGOutputData;
            ffmpegProc.ErrorDataReceived += PrintFFMPEGErrorData;

            ffmpegProc.Start();

            // ffmpegが確実に起動状態になるまで待つ
            Thread.Sleep(3000);

            // 標準出力・標準エラーの非同期読み込みを開始する
            //ffmpegProc.BeginOutputReadLine();
            ffmpegProc.BeginErrorReadLine();

            var task = Task.Run(() =>
            {
                byte[] ffmpegStdout_buf = new byte[2048];
                int readedBytes         = 0;
                while (!ffmpegProc.StandardOutput.EndOfStream)
                {
                    readedBytes = ffmpegProc.StandardOutput.BaseStream.Read(ffmpegStdout_buf, 0, ffmpegStdout_buf.Length);
                    //Console.WriteLine(Utils.getFormatedCurrentTime() + " DEBUG: read tabun one frames " + readedBytes.ToString() + " bytes");
                    //debug_ms.Write(ffmpegStdout_buf, 0, readedBytes);
                    //if (debug_ms.Length > 8000 * 120)
                    //{
                    //    Utils.saveByteArrayToFile(debug_ms.ToArray(), "F:\\work\\tmp\\capturedPCM_ffmpeg_stdout_converted_8000Hz_u8bit_1ch_120sec.raw");
                    //    Environment.Exit(0);
                    //}
                    //continue;
                    if (readedBytes > 0)
                    {
                        byte[] tmp_buf = new byte[readedBytes];

                        if (GlobalConfiguration.isUseDPCM)
                        {
                            Console.WriteLine("run dpcm encoding " + readedBytes.ToString() + " bytes");
                            tmp_buf = dpcm_encoder.Encode(tmp_buf);
                        }

                        if (GlobalConfiguration.isRunCapturedSoundDataHndlingWithoutConn == false)
                        {
                            this.cap_streamer._AudioOutputWriter.handleDataWithTCP(tmp_buf);
                        }
                    }
                }
            });
        }
        private void Socket_EndDataRecievedCallback()
        {
            if (GlobalConfiguration.isUseLossySoundDecoder)
            {
                //var data = mp3data_ms.ToArray();

                encoded_frame_ms.Position = 0;
                if (m_Player.Opened == false)
                {
                    if (GlobalConfiguration.isEncodeWithOpus)
                    {
                        m_Player.Open("hoge", GlobalConfiguration.SamplesPerSecond, config.BitsPerSample, config.Channels, config.BufferCount);
                        m_Player.Play();

                        //concentusOpusDecoder = OpusDecoder.Create(GlobalConfiguration.SamplesPerSecond, config.Channels);
                        concentusOpusDecoder = OpusDecoder.Create(GlobalConfiguration.SampleRateDummyForSoundEnDecoder, config.Channels);
                        //m_DPlayer.setup(RTPConfiguration.SamplesPerSecond, config.Channels, -1, csd_0, "opus");
                    }
                    else
                    {
                        throw new Exception("illigal flag setting on RTPConfiguration.");
                    }
                }

                byte[] data_buf = new byte[encoded_frame_ms.Length - encoded_frame_ms.Position];
                encoded_frame_ms.Read(data_buf, 0, data_buf.Length);

                int     frameSize    = GlobalConfiguration.samplesPerPacket; // must be same as framesize used in input, you can use OpusPacketInfo.GetNumSamples() to determine this dynamically
                short[] outputBuffer = new short[frameSize];

                int thisFrameSize = concentusOpusDecoder.Decode(data_buf, 0, data_buf.Length, outputBuffer, 0, frameSize, false);
                m_Player.WriteData(Utils.convertShortArrToBytes(outputBuffer), false);
                return;
            }
            else
            {
                Byte[] justSound_buf = encoded_frame_ms.ToArray();
                Byte[] linearBytes   = justSound_buf;
                //if (!RTPConfiguration.isUseSoundDecoder && m_Player.Opened == false)
                //{
                //    m_Player.Open("hoge", RTPConfiguration.SamplesPerSecond, config.BitsPerSample, config.Channels, config.BufferCount);
                //}
                if (config.isConvertMulaw)
                {
                    linearBytes = SoundUtils.MuLawToLinear(justSound_buf, config.BitsPerSample, config.Channels);
                }
                if (GlobalConfiguration.isUseDPCM)
                {
                    if (dpcmDecoder == null)
                    {
                        dpcmDecoder = new MyDpcmCodec();
                    }

                    linearBytes = dpcmDecoder.Decode(linearBytes);
                }
                if (!GlobalConfiguration.isUseLossySoundDecoder && m_Player.Opened == false)
                {
                    m_Player.Open("hoge", GlobalConfiguration.SamplesPerSecond, config.BitsPerSample, config.Channels, config.BufferCount);
                    m_Player.Play();
                }
                m_Player.WriteData(linearBytes, false);
                //totalWroteSoundData += linearBytes.Length;
                //if(totalWroteSoundData > config.BufferCount && m_Player.isPlayingStarted == false)
                //{
                //    m_Player.Play();
                //}
            }
        }