private long ConvertOneBuffer(SinkWriter writer, int streamIndex, IWaveProvider inputProvider, long position, byte[] managedBuffer) { long durationConverted = 0; using var buffer = MediaFactory.CreateMemoryBuffer(managedBuffer.Length); using var sample = MediaFactory.CreateSample(); sample.AddBuffer(buffer); var ptr = buffer.Lock(out int maxLength, out int currentLength); int read = inputProvider.Read(managedBuffer, 0, maxLength); if (read > 0) { durationConverted = BytesToNsPosition(read, inputProvider.WaveFormat); Marshal.Copy(managedBuffer, 0, ptr, read); buffer.CurrentLength = read; buffer.Unlock(); sample.SampleTime = position; sample.SampleDuration = durationConverted; writer.WriteSample(streamIndex, sample); //writer.Flush(streamIndex); } else { buffer.Unlock(); } return(durationConverted); }
//This method returns ticks, not bytes! private long WriteBlock(byte[] buffer, int offset, int count, int streamIndex, long positionInTicks, int sourceBytesPerSecond) { using (var mfBuffer = MediaFactory.CreateMemoryBuffer(count)) { using (var sample = MediaFactory.CreateSample()) { sample.AddBuffer(mfBuffer); using (var @lock = mfBuffer.Lock()) { Marshal.Copy(buffer, offset, @lock.Buffer, count); mfBuffer.SetCurrentLength(count); } long ticks = BytesToNanoSeconds(count, sourceBytesPerSecond); sample.SetSampleTime(positionInTicks); sample.SetSampleDuration(ticks); _sinkWriter.WriteSample(streamIndex, sample); return(ticks); } } }
public void Write(byte[] Data, int Offset, int Count) { using (var buffer = MediaFactory.CreateMemoryBuffer(Count)) { var data = buffer.Lock(out _, out _); Marshal.Copy(Data, Offset, data, Count); buffer.CurrentLength = Count; buffer.Unlock(); using (var sample = MediaFactory.CreateVideoSampleFromSurface(null)) { sample.AddBuffer(buffer); sample.SampleTime = _audioWritten * TenPower7 / _audioInBytesPerSecond; sample.SampleDuration = Count * TenPower7 / _audioInBytesPerSecond; _writer.WriteSample(StreamIndex, sample); } } _audioWritten += Count; }
public void WriteSample(Sample sample) { if (closed) { return; } frameNumber++; sample.SampleTime = frameNumber * frameDuration; sample.SampleDuration = frameDuration; if (isFirstFrame) { logger.Verb("MfWriter::isFirstFrame"); //sinkWriter.BeginWriting(); sinkWriter.SendStreamTick(videoStreamIndex, sample.SampleTime); sample.Set(SampleAttributeKeys.Discontinuity, true); isFirstFrame = false; } sinkWriter.WriteSample(videoStreamIndex, sample); //sinkWriter.Flush(VideoStreamIndex); }