private static TrackState SelectMuxTrack(TrackState vtrack, TrackState atrack) { if (vtrack.Index != TrackState.Disabled && atrack.Index != TrackState.Disabled) { if (vtrack.Frame == null) { return(vtrack); } if (atrack.Frame == null) { return(atrack); } return(vtrack.Frame.StartTime <= atrack.Frame.StartTime ? vtrack : atrack); } if (vtrack.Index != TrackState.Disabled) { return(vtrack); } if (atrack.Index != TrackState.Disabled) { return(atrack); } return(null); }
/* * uncompressed audio and video input */ public static bool Run(MediaSocket vinput, string vfile, MediaSocket ainput, string afile, MediaSocket output) { bool res; TrackState vtrack = new TrackState(); TrackState atrack = new TrackState(); using (UncompressedAVSplitter vsplit = new UncompressedAVSplitter()) using (UncompressedAVSplitter asplit = new UncompressedAVSplitter()) using (var transcoder = new Transcoder() { AllowDemoMode = true }) { try { if (vinput != null) { Console.WriteLine("video input file: \"{0}\"", vfile); vsplit.Init(vinput, vfile); Console.WriteLine("OK"); } if (ainput != null) { Console.WriteLine("audio input file: \"{0}\"", afile); asplit.Init(ainput, afile); Console.WriteLine("OK"); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } // setup transcoder int trackIndex = 0; // start index if (vinput != null) { transcoder.Inputs.Add(vinput); vtrack.Index = trackIndex++; } if (ainput != null) { transcoder.Inputs.Add(ainput); atrack.Index = trackIndex++; } transcoder.Outputs.Add(output); res = transcoder.Open(); PrintError("transcoder open", transcoder.Error); if (!res) { return(false); } // transcoding loop for (;;) { if (vtrack.Index != TrackState.Disabled && vtrack.Frame == null) { vtrack.Frame = vsplit.GetFrame(); } if (atrack.Index != TrackState.Disabled && atrack.Frame == null) { atrack.Frame = asplit.GetFrame(); } TrackState track = SelectMuxTrack(vtrack, atrack); if (track == null) { break; } // log if (track.Frame != null) { if (track.Frame.StartTime - track.Progress >= 1.0) { track.Progress = track.Frame.StartTime; Console.WriteLine("track {0} frame #{1} pts:{2}", track.Index, track.FrameCount, track.Frame.StartTime); } } else { Console.WriteLine("track {0} eos", track.Index); } if (track.Frame != null) { res = transcoder.Push(track.Index, track.Frame); if (!res) { PrintError("transcoder push frame", transcoder.Error); return(false); } track.Frame = null; // clear the muxed frame in order to read to the next one track.FrameCount++; } else { res = transcoder.Push(track.Index, null); if (!res) { PrintError("transcoder push eos", transcoder.Error); return(false); } track.Index = TrackState.Disabled; // disable track } } res = transcoder.Flush(); if (!res) { PrintError("transcoder flush", transcoder.Error); return(false); } transcoder.Close(); } return(true); }