skyscraper8/skyscraper8/ThreemediaOtt/ThreemediaContentFragment.cs
feyris-tan 124bc001aa
All checks were successful
🚀 Pack skyscraper8 / make-zip (push) Successful in 1m14s
Began parsing Threemedia OTT fragments.
2026-06-28 12:37:47 +02:00

94 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Skyscraper.IO;
using skyscraper8.Ietf.FLUTE;
namespace skyscraper8.ThreemediaOtt
{
internal class ThreemediaContentFragment : IDisposable
{
public void IngestPacket(LctFrame lctFrame)
{
int totalBlocks = lctFrame.FecHeader.SourceBlockLength.Value;
if (blocks == null)
blocks = new byte[totalBlocks][];
int targetBlock = lctFrame.FecHeader.EncodingSymbolId;
if (targetBlock >= blocks.Length)
return;
if (blocks[targetBlock] != null)
return;
blocks[targetBlock] = lctFrame.Payload;
}
private byte[][] blocks;
public void Dispose()
{
if (blocks != null)
{
for (int i = 0; i < blocks.Length; i++)
{
blocks[i] = null;
}
}
blocks = null;
}
public bool IsComplete()
{
if (blocks == null)
return false;
for (int i = 0; i < blocks.Length; i++)
{
if (blocks[i] == null)
return false;
}
return true;
}
public ThreemediaContentFragmentStream CreateStream()
{
byte[] lastBlock = blocks[blocks.Length - 1];
MemoryStream lastBlockStream = new MemoryStream(lastBlock, false);
lastBlockStream.Position = lastBlockStream.Length - 8;
int footerLength = (int)lastBlockStream.ReadUInt32BE();
int footerOffset = (int)(lastBlockStream.Length - 8 - footerLength);
Dictionary<string, string> footer = ExtractFooter(lastBlock, footerLength, footerOffset);
int[] blockSizes = Array.ConvertAll(blocks, (x) => x.Length);
blockSizes[blocks.Length - 1] = footerOffset;
ThreemediaContentFragmentStream result = new ThreemediaContentFragmentStream(blocks, blockSizes, footer);
throw new NotImplementedException();
}
private Dictionary<string, string> ExtractFooter(byte[] lastBlock, int footerLength, int footerOffset)
{
MemoryStream ms = new MemoryStream(lastBlock, footerOffset, footerLength, false);
StreamReader sr = new StreamReader(ms, Encoding.ASCII);
Dictionary<string, string> result = new Dictionary<string, string>();
while (!sr.EndOfStream)
{
string? line = sr.ReadLine();
int seperator = line.IndexOf(':');
string key = line.Substring(0, seperator);
string value = line.Substring(seperator + 1);
result.Add(key, value);
}
return result;
}
}
}