public override void Parse(TimeSpan timeOffset, int mediaId, ArraySegment <byte> byteSegment, bool markerBit) { DateTime timestamp = GetFrameTimestamp(timeOffset); var frame = new RawPCMFrame(timestamp, mediaId, byteSegment, _pcmCodecInfo.SampleRate, _pcmCodecInfo.BitsPerSample, _pcmCodecInfo.Channels); OnFrameGenerated(frame); }
/// <summary> /// Creates media frame data segments. /// </summary> /// <param name="mediaFrame">Media frame.</param> /// <param name="metadataRequired">Indicates whether to include metadata.</param> /// <returns>An array of data segments.</returns> private static ArraySegment <byte>[] CreateDataSegments(RawFrame mediaFrame, bool metadataRequired) { if (!metadataRequired) { return new[] { new byte[] { 0 }, mediaFrame.FrameSegment } } ; var codecName = mediaFrame switch { RawAACFrame _ => "AAC", RawG711AFrame _ => "G711A", RawG711UFrame _ => "G711U", RawG726Frame _ => "G726", RawPCMFrame _ => "PCM", RawH264IFrame _ => "H264", RawH264PFrame _ => "H264", RawJpegFrame _ => "MJPEG", _ => string.Empty }; var bitsPerCodedUnit = mediaFrame switch { RawG726Frame rawG726Frame => rawG726Frame.BitsPerCodedSample, _ => 0 }; var configSegment = mediaFrame switch { RawAACFrame rawAacFrame => rawAacFrame.ConfigSegment, RawH264IFrame rawH264IFrame => rawH264IFrame.SpsPpsSegment, _ => default }; var codecBytes = Encoding.UTF8.GetBytes(codecName); Array.Resize(ref codecBytes, 10); var metaSegment = new byte[19]; using var stream = new MemoryStream(metaSegment); using var writer = new EndianBinaryWriter(stream); writer.Write((byte)1); writer.Write(codecBytes); writer.Write(bitsPerCodedUnit); writer.Write(configSegment.Count); return(configSegment.Count > 0 ? new[] { metaSegment, configSegment, mediaFrame.FrameSegment } : new[] { metaSegment, mediaFrame.FrameSegment }); }
public void Parse_TestData_ReturnsValidFrame() { var testCodecInfo = new PCMCodecInfo(44100, 16, 2); byte[] testBytes = { 1, 2, 3, 4, 5, 6, 7, 8 }; RawPCMFrame frame = null; var parser = new PCMAudioPayloadParser(testCodecInfo); parser.FrameGenerated = rawFrame => frame = (RawPCMFrame)rawFrame; parser.Parse(TimeSpan.Zero, new ArraySegment <byte>(testBytes), true); Assert.IsNotNull(frame); Assert.AreEqual(FrameType.Audio, frame.Type); Assert.IsTrue(frame.FrameSegment.SequenceEqual(testBytes)); Assert.AreEqual(44100, frame.SampleRate); Assert.AreEqual(16, frame.BitsPerSample); Assert.AreEqual(2, frame.Channels); }