Пример #1
0
        public void record(string rtmp2Url, string quality)
        {
            //endcode 0-その他の理由 1-stop 2-最初に終了 3-始まった後に番組終了
            util.debugWriteLine("rtmp recorder" + util.getMainSubStr(isSub, true));
            var _m = (rfu.isPlayOnlyMode) ? "視聴" : "録画";

            if (wr.isTimeShift)
            {
                rm.form.addLogText("タイムシフトの" + _m + "を開始します");
            }
            else
            {
                if (isSub)
                {
                    rm.form.addLogText(_m + "をスタンバイします(サブ)");
                }
                else
                {
                    rm.form.addLogText(_m + "を開始します(" + (quality == null ? "" : ("画質:" + quality + " ")) + "メイン)");
                }
            }
            if (rtmp2Url != null)
            {
                rtmpUrl = rtmp2Url;
            }

            var convertList = new List <string>();
            var isFirst     = true;
            var isFailedRec = false;

            try {
                //var sleepSec = 1;
                while (rm.rfu == rfu && retryMode == 0)
                {
                    util.debugWriteLine("rtmp 録画" + ((isFirst) ? "開始" : "再開"));

                    if (DateTime.Now < lastConnectTime + TimeSpan.FromSeconds(5))
                    {
                        //Thread.Sleep(sleepSec * 10000);
                        Thread.Sleep(5000);
                        //if (sleepSec < 6) sleepSec++;
                        continue;
                    }
                    lastConnectTime = DateTime.Now;

                    var rtmpdumpArg = getProcessArgs(rtmp2Url != null, isFirst);
                    if (rtmpdumpArg == "end")
                    {
                        isEndProgram = true;
                        break;
                    }
                    else if (rtmpdumpArg == "no")
                    {
                        rm.form.addLogText("RTMPデータが見つかりませんでした");
                        return;
                    }
                    if (rtmpdumpArg == null)
                    {
                        continue;
                    }

                    if (rfu.isPlayOnlyMode)
                    {
                        while (rm.rfu == rfu && retryMode == 0)
                        {
                            Thread.Sleep(1000);
                        }
                        return;
                    }

                    if (isTimeshift)
                    {
                        timeshiftRecord(rtmpdumpArg);
                        return;
                    }

                    if (!rfu.isPlayOnlyMode)
                    {
                        getProcess(out rtmpdumpP, out ffmpegP, rtmpdumpArg);
                    }

                    if (!isSub)
                    {
                        if (!isFirst && !rfu.isPlayOnlyMode && !isFailedRec)
                        {
                            wr.resetCommentFile();
                        }
                        isFirst     = false;
                        isFailedRec = false;

                        Task.Run(() => errorReadProcess(rtmpdumpP));

                        testDebugWriteLine("rtmpdump 待機");
                        while (rm.rfu == rfu && retryMode == 0 && !rtmpdumpP.HasExited)
                        {
                            if (!rfu.isPlayOnlyMode && rtmpdumpP.WaitForExit(1000))
                            {
                                break;
                            }
                            if (rfu.isPlayOnlyMode)
                            {
                                Thread.Sleep(1000);
                            }
                        }
                        testDebugWriteLine("rtmpdump 終了準備 " + retryMode);

                        util.debugWriteLine("rtmp Process loop end");

                        if (rm.rfu != rfu || retryMode == 1)
                        {
                            util.debugWriteLine("retrymode " + retryMode);
                            stopRecording();
                        }
                        else
                        {
                            //end program
                            while (rm.rfu == rfu && !rtmpdumpP.HasExited)
                            {
                                Thread.Sleep(1000);
                            }
                        }
                        testDebugWriteLine("rtmpdump 終了");


                        try {
                            var f = new FileInfo(util.getOkSJisOut(recFolderFile) + ".flv");
                            if (f != null && f.Exists && f.Length == 0)
                            {
                                util.debugWriteLine("rtmp delete " + recFolderFile);
                                //File.Delete(f.FullName + ".flv");
                                File.Delete(f.FullName + "");
                                f = new FileInfo(util.getOkSJisOut(recFolderFile) + ".xml");
                                if (f != null && f.Exists)
                                {
                                    File.Delete(f.FullName);
                                }

                                isFailedRec = true;
                            }
                            else
                            {
                                //							if (rm.cfg.get("IsAfterRenketuFFmpeg") == "true" ||
                                //							        (afterConvertMode != "0" && afterConvertMode != "4")) {
                                if (afterConvertMode > 0)
                                {
                                    convertList.Add(recFolderFile + ".flv");
                                }
                                //recFolderFile = wr.getRecFilePath()[1];
                                recFolderFile = util.incrementRecFolderFile(recFolderFile);                                //wr.getRecFilePath()[1];
                            }
                        } catch (Exception e) {
                            util.debugWriteLine(e.Message + e.Source + e.StackTrace + e.TargetSite);
                        }

                        if (rm.rfu != rfu || retryMode != 0)
                        {
                            break;
                        }
                        wr.reConnect();
                    }
                    else
                    {
                        var rtmpdumpTask =
                            Task.Run(() => rtmpdumpReadFFmpegWriteProcess(rtmpdumpP, ffmpegP));
                        //					var ffmpegTask = Task.Run(() => ffmpegReadProcess(rtmpdumpP));

                        var isContinue = false;
                        while (rm.rfu == rfu && retryMode == 0 && !rtmpdumpP.HasExited)
                        {
                            //						if (taskEnd(rtmpdumpTask) && taskEnd(ffmpegTask)) {
                            if (taskEnd(rtmpdumpTask))
                            {
                                isContinue = true;
                                break;
                            }
                            //						if (taskEnd(rtmpdumpTask) || taskEnd(ffmpegTask)) {
                            if (taskEnd(rtmpdumpTask))
                            {
                                //							stopRecording();
                                //							break;
                            }


                            Thread.Sleep(1000);
                        }
                        util.debugWriteLine("rtmp rec end isContinue " + isContinue + "(サブ)");
                        if (isContinue || retryMode == 0)
                        {
                            Thread.Sleep(1000);
                            continue;
                        }

                        util.debugWriteLine("rtmp rec go retryMode " + retryMode + "(サブ)");
                        if (rm.rfu != rfu || retryMode == 1)
                        {
                            stopRecording();
                        }
                        if (retryMode == 2)
                        {
                            //end program
                            util.debugWriteLine("rtmp endprogram retryMode " + retryMode);
                            while (rm.rfu == rfu)
                            {
                                if ((rtmpdumpP == null || rtmpdumpP.HasExited) && (ffmpegP == null || ffmpegP.HasExited))
                                {
                                    break;
                                }
                                Thread.Sleep(1000);
                            }
                        }
                        util.debugWriteLine("rtmp rec end");
                        if (rm.rfu != rfu || retryMode != 0)
                        {
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                util.debugWriteLine("rtmp total error " + e.Message + e.Source + e.StackTrace + e.TargetSite);
                testDebugWriteLine("rtmpエラー " + e.Message + e.Source + e.StackTrace + e.TargetSite);
            }
            testDebugWriteLine("rtmp録画終了");

            //convert
            foreach (var f in convertList)
            {
                var tf = new ThroughFFMpeg(rm);
                tf.start(f, true);
            }
        }
Пример #2
0
        private void timeshiftRecord(string rtmpdumpArg)
        {
//			Process rtmpdumpP, ffmpegP;
            var convertList = new List <string>();
            var argList     = rtmpdumpArg.Split('$');

            tsRecordNum = argList.Length;
            setFileNameList(argList.Length, recFolderFile);
            try {
                for (tsRecordIndex = 0; tsRecordIndex < argList.Length; tsRecordIndex++)
                {
                    if (rfu.tsRecNumArr != null &&
                        Array.IndexOf(rfu.tsRecNumArr, tsRecordIndex + 1) == -1)
                    {
                        continue;
                    }
                    if (tsRecordIndex != 0)
                    {
                        var _args = getProcessArgs(false, false);
                        if (_args != null)
                        {
                            argList = _args.Split('$');
                        }
                        //recFolderFile = wr.getRecFilePath();
                        //recFolderFile = incrementRecFolderFile(recFolderFile);
                    }
                    recFolderFile = getTsRecordIndexRecFolderFile(recFolderFile, tsRecordIndex + 1);


                    //var _arg = argList[tsRecordIndex] + "\"" + recFolderFile + ".flv\"";
                    var _arg = argList[tsRecordIndex] + "\"" + fileNameList[tsRecordIndex] + ".flv\"";

                    if (_arg.StartsWith("-r"))
                    {
                        makeTs(_arg);
                    }
                    getProcess(out rtmpdumpP, out ffmpegP, _arg);

                    Task.Run(() => errorReadProcess(rtmpdumpP));

                    while (rm.rfu == rfu && !rtmpdumpP.HasExited)
                    {
                        if (rtmpdumpP.WaitForExit(1000))
                        {
                            break;
                        }
                    }
                    util.debugWriteLine("rtmp Process loop end");

                    if (rm.rfu != rfu)
                    {
                        stopRecording();
                    }
                    try {
//						if (rm.cfg.get("IsAfterRenketuFFmpeg") == "true" ||
//						        (afterConvertMode != "0" && afterConvertMode != "4")) {
                        if (afterConvertMode > 0)
                        {
                            convertList.Add(recFolderFile + ".flv");
                        }
                    } catch (Exception e) {
                        util.debugWriteLine(e.Message + e.Source + e.StackTrace + e.TargetSite);
                    }
                    if (rm.rfu != rfu)
                    {
                        break;
                    }
                }
            } catch (Exception ee) {
                util.debugWriteLine(ee.Message + ee.Source + ee.StackTrace + ee.TargetSite);
            }
            //convert
            foreach (var f in convertList)
            {
                var tf = new ThroughFFMpeg(rm);
                tf.start(f, true);
            }
            if (rm.rfu == rfu)
            {
                isEndProgram = true;
            }
            util.debugWriteLine("timeshift rtmp record end");
        }
Пример #3
0
        private string concatFiles(List <string> files)
        {
            if (files.Count() == 0)
            {
                return(null);
            }
            rm.form.addLogText("結合を開始します");

            string outPath = null;
            var    count   = 0;

            var outName = getOutFileName(files[0], files.Count == 1);

            outPath = outName;
            var isFFmpegConcat = true;

            if (isFFmpegConcat)
            {
                new FFMpegConcat(rm, null).concat(outName, files);
            }
            else
            {
                using (var outFileStream = new FileStream(outName, FileMode.Append, FileAccess.Write)) {
                    //using (var outFileStream = getOutFileStream(files[0])) {
                    if (outFileStream == null)
                    {
                        //				rm.form.addLogText("出力先パスが取得できませんでした");
                        return(null);
                    }
                    util.debugWriteLine("outfname " + outFileStream + outFileStream.Name);
                    //outPath = outFileStream.Name;

                    foreach (var f in files)
                    {
                        util.debugWriteLine(f);
                        try {
                            using (var r = new FileStream(f, FileMode.Open, FileAccess.Read)) {
                                var pos   = 0;
                                var readI = 0;
                                var bytes = new byte[1000000];
                                while ((readI = r.Read(bytes, 0, bytes.Length)) != 0)
                                {
                                    outFileStream.Write(bytes, 0, readI);
                                    pos += readI;
                                }
                            }
                            count++;
                        } catch (Exception e) {
                            util.debugWriteLine("arg concat write exception " + f + " " + e.Message + " " + e.StackTrace + " " + e.Source + " " + e.TargetSite);
                        }
                    }
                }
                if (int.Parse(rm.cfg.get("afterConvertMode")) > 0)
                {
                    var tf = new ThroughFFMpeg(rm);
                    tf.start(outPath, true);
                }
            }
            util.debugWriteLine("concated count " + count);
            return(outPath);
        }