public H264StreamHandler(PesStreamParameters parameters) : base(parameters) { if (null == parameters) throw new ArgumentNullException(nameof(parameters)); if (null == parameters.PesPacketPool) throw new ArgumentException("PesPacketPool cannot be null", nameof(parameters)); if (null == parameters.NextHandler) throw new ArgumentException("NextHandler cannot be null", nameof(parameters)); _pesPacketPool = parameters.PesPacketPool; _nextHandler = parameters.NextHandler; _configurator = new H264Configurator(parameters.MediaStreamMetadata, parameters.StreamType.Description); _parser = new NalUnitParser(ResolveHandler); }
public H264StreamHandler(PesStreamParameters parameters) : base(parameters) { if (null == parameters) { throw new ArgumentNullException("parameters"); } if (null == parameters.PesPacketPool) { throw new ArgumentException("PesPacketPool cannot be null", "parameters"); } if (null == parameters.NextHandler) { throw new ArgumentException("NextHandler cannot be null", "parameters"); } this._pesPacketPool = parameters.PesPacketPool; this._nextHandler = parameters.NextHandler; this._configurator = new H264Configurator(parameters.MediaStreamMetadata, parameters.StreamType.Description); this._parser = new NalUnitParser(new Func <byte, NalUnitParser.ParserStateHandler>(this.ResolveHandler)); }
static async Task Parse(string filename) { var buffer = new byte[MAXNALUSIZE * 4]; var offset = 0; var length = 0; var isEof = false; var logFilename = Path.ChangeExtension(filename, ".log"); if (string.Equals(filename, logFilename, StringComparison.InvariantCultureIgnoreCase)) return; using (var outputFile = new FileStream(logFilename, FileMode.Create, FileAccess.Write, FileShare.Read, 1024 * 1024, FileOptions.SequentialScan)) using (var output = new StreamWriter(outputFile, NoBomUtf8, 512 * 1024)) { var localOutput = output; var rbspDecoder = new RbspDecoder(); rbspDecoder.CompletionHandler += b => PrintNalUnit(localOutput, b.ToArray()); var parser = new NalUnitParser(n => (b, o, l, e) => rbspDecoder.Parse(b, o, l, e)); using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 512 * 1024, FileOptions.Asynchronous | FileOptions.SequentialScan)) { for (; ; ) { var bytesRead = await stream.ReadAsync(buffer, length, buffer.Length - length).ConfigureAwait(false); if (bytesRead < 1) { isEof = true; if (length < 1) break; } else length += bytesRead; while ((isEof && length > 0) || length >= MinRemainingSize) { var completedLength = parser.Parse(buffer, offset, length, isEof); if (completedLength < 1) { if (isEof) return; break; } if (completedLength >= length) { offset = 0; length = 0; break; } offset += completedLength; length -= completedLength; } if (length < 1) { offset = 0; continue; } if (offset > 0) { Array.Copy(buffer, offset, buffer, 0, length); offset = 0; } } } } }