public ParsedContent Read(Stream stream) { var res = new ParsedContent(); var info = new AtomCollection(Channel.ChannelInfo.Extra); var processed = false; var eos = false; while (!eos) { var start_pos = stream.Position; try { switch (state) { case ReaderState.Header: { var bin = ReadBytes(stream, 13); var header = new FileHeader(bin); if (header.IsValid) { Logger.Info("FLV Header found"); fileHeader = header; bin = header.Binary; res.ContentHeader = new Content(position, bin); res.Contents = null; info.SetChanInfoType("FLV"); info.SetChanInfoStreamType("video/x-flv"); info.SetChanInfoStreamExt(".flv"); res.ChannelInfo = new ChannelInfo(info); position = bin.Length; tags.Clear(); state = ReaderState.Body; } else { throw new BadDataException(); } } break; case ReaderState.Body: { var bin = ReadBytes(stream, 11); var read_valid = false; var body = new FLVTag(bin); if (body.IsValidHeader) { body.ReadBody(stream); body.ReadFooter(stream); if (body.IsValidFooter) { read_valid = true; bin = body.Binary; if (res.Contents == null) { res.Contents = new List <Content>(); } res.Contents.Add(new Content(position, bin)); tags.AddLast(new TagDesc { Timestamp = body.Timestamp / 1000.0, DataSize = body.DataSize }); var timespan = tags.Last.Value.Timestamp - tags.First.Value.Timestamp; if (timespan >= 30.0) { var sz = tags.Take(tags.Count - 1).Sum(t => t.DataSize); info.SetChanInfoBitrate((int)(sz * 8 / timespan + 900) / 1000); res.ChannelInfo = new ChannelInfo(info); while (tags.Count > 1) { tags.RemoveFirst(); } } position += bin.Length; } } if (!read_valid) { stream.Position = start_pos; var header = new FileHeader(ReadBytes(stream, 13)); if (header.IsValid) { Logger.Info("New FLV Header found"); read_valid = true; fileHeader = header; bin = header.Binary; res.ContentHeader = new Content(0, bin); res.Contents = null; info.SetChanInfoType("FLV"); info.SetChanInfoStreamType("video/x-flv"); info.SetChanInfoStreamExt(".flv"); res.ChannelInfo = new ChannelInfo(info); tags.Clear(); position = bin.Length; } } if (!read_valid) { throw new BadDataException(); } } break; } processed = true; } catch (EndOfStreamException) { if (!processed) { throw; } stream.Position = start_pos; eos = true; } catch (BadDataException) { stream.Position = start_pos + 1; } } return(res); }
public bool Read(Stream stream, IRTMPContentSink sink) { var processed = false; var eos = false; while (!eos) { retry: var start_pos = stream.Position; try { switch (state) { case ReaderState.Header: { var bin = ReadBytes(stream, 13, out eos); if (eos) { goto error; } var header = new FLVFileHeader(bin); if (header.IsValid) { sink.OnFLVHeader(header); state = ReaderState.Body; } else { throw new BadDataException(); } } break; case ReaderState.Body: { var bin = ReadBytes(stream, 11, out eos); if (eos) { goto error; } var read_valid = false; var body = new FLVTag(this, bin); if (body.IsValidHeader) { if (!body.ReadBody(stream)) { eos = true; goto error; } if (!body.ReadFooter(stream)) { eos = true; goto error; } if (body.IsValidFooter) { read_valid = true; switch (body.Type) { case FLVTag.TagType.Audio: sink.OnAudio(body.ToRTMPMessage()); break; case FLVTag.TagType.Video: sink.OnVideo(body.ToRTMPMessage()); break; case FLVTag.TagType.Script: sink.OnData(new DataAMF0Message(body.ToRTMPMessage())); break; } } } else { stream.Position = start_pos; var headerbin = ReadBytes(stream, 13, out eos); if (eos) { goto error; } var header = new FLVFileHeader(headerbin); if (header.IsValid) { read_valid = true; sink.OnFLVHeader(header); } } if (!read_valid) { stream.Position = start_pos + 1; var b = stream.ReadByte(); while (true) { if (b < 0) { eos = true; goto error; } if ((b & 0xC0) == 0 && ((b & 0x1F) == 8 || (b & 0x1F) == 9 || (b & 0x1F) == 18)) { break; } b = stream.ReadByte(); } stream.Position = stream.Position - 1; goto retry; } } break; } processed = true; } catch (EndOfStreamException) { stream.Position = start_pos; eos = true; } catch (BadDataException) { stream.Position = start_pos + 1; } error: if (eos) { stream.Position = start_pos; eos = true; } } return(processed); }
public ParsedContent Read(Stream stream) { var processed = false; var eos = false; while (!eos) { var start_pos = stream.Position; try { switch (state) { case ReaderState.Header: { var bin = ReadBytes(stream, 13); var header = new FileHeader(bin); if (header.IsValid) { Logger.Info("FLV Header found"); contentBuffer.OnStart(); state = ReaderState.Body; } else { throw new BadDataException(); } } break; case ReaderState.Body: { var bin = ReadBytes(stream, 11); var read_valid = false; var body = new FLVTag(bin); if (body.IsValidHeader) { body.ReadBody(stream); body.ReadFooter(stream); if (body.IsValidFooter) { read_valid = true; switch (body.Type) { case FLVTag.TagType.Audio: contentBuffer.OnAudio(body.ToRTMPMessage()); break; case FLVTag.TagType.Video: contentBuffer.OnVideo(body.ToRTMPMessage()); break; case FLVTag.TagType.Script: contentBuffer.OnData(new RTMP.DataAMF0Message(body.ToRTMPMessage())); break; } } } if (!read_valid) { stream.Position = start_pos; var header = new FileHeader(ReadBytes(stream, 13)); if (header.IsValid) { Logger.Info("New FLV Header found"); read_valid = true; contentBuffer.OnStart(); } } if (!read_valid) throw new BadDataException(); } break; } processed = true; } catch (EndOfStreamException) { if (!processed) throw; stream.Position = start_pos; eos = true; } catch (BadDataException) { stream.Position = start_pos+1; } } return contentBuffer.GetContents(); }
public bool Read(Stream stream, IRTMPContentSink sink) { var processed = false; var eos = false; while (!eos) { retry: var start_pos = stream.Position; try { switch (state) { case ReaderState.Header: { var bin = ReadBytes(stream, 13, out eos); if (eos) goto error; var header = new FileHeader(bin); if (header.IsValid) { sink.OnFLVHeader(); state = ReaderState.Body; } else { throw new BadDataException(); } } break; case ReaderState.Body: { var bin = ReadBytes(stream, 11, out eos); if (eos) goto error; var read_valid = false; var body = new FLVTag(this, bin); if (body.IsValidHeader) { if (!body.ReadBody(stream)) { eos = true; goto error; } if (!body.ReadFooter(stream)) { eos = true; goto error; } if (body.IsValidFooter) { read_valid = true; switch (body.Type) { case FLVTag.TagType.Audio: sink.OnAudio(body.ToRTMPMessage()); break; case FLVTag.TagType.Video: sink.OnVideo(body.ToRTMPMessage()); break; case FLVTag.TagType.Script: sink.OnData(new DataAMF0Message(body.ToRTMPMessage())); break; } } } else { stream.Position = start_pos; var headerbin = ReadBytes(stream, 13, out eos); if (eos) goto error; var header = new FileHeader(headerbin); if (header.IsValid) { read_valid = true; sink.OnFLVHeader(); } } if (!read_valid) { stream.Position = start_pos+1; var b = stream.ReadByte(); while (true) { if (b<0) { eos = true; goto error; } if ((b & 0xC0)==0 && ((b & 0x1F)==8 || (b & 0x1F)==9 || (b & 0x1F)==18)) { break; } b = stream.ReadByte(); } stream.Position = stream.Position-1; goto retry; } } break; } processed = true; } catch (EndOfStreamException) { stream.Position = start_pos; eos = true; } catch (BadDataException) { stream.Position = start_pos+1; } error: if (eos) { stream.Position = start_pos; eos = true; } } return processed; }
public ParsedContent Read(Stream stream) { var processed = false; var eos = false; while (!eos) { var start_pos = stream.Position; try { switch (state) { case ReaderState.Header: { var bin = ReadBytes(stream, 13); var header = new FileHeader(bin); if (header.IsValid) { Logger.Info("FLV Header found"); contentBuffer.OnStart(); state = ReaderState.Body; } else { throw new BadDataException(); } } break; case ReaderState.Body: { var bin = ReadBytes(stream, 11); var read_valid = false; var body = new FLVTag(bin); if (body.IsValidHeader) { body.ReadBody(stream); body.ReadFooter(stream); if (body.IsValidFooter) { read_valid = true; switch (body.Type) { case FLVTag.TagType.Audio: contentBuffer.OnAudio(body.ToRTMPMessage()); break; case FLVTag.TagType.Video: contentBuffer.OnVideo(body.ToRTMPMessage()); break; case FLVTag.TagType.Script: contentBuffer.OnData(new RTMP.DataAMF0Message(body.ToRTMPMessage())); break; } } } if (!read_valid) { stream.Position = start_pos; var header = new FileHeader(ReadBytes(stream, 13)); if (header.IsValid) { Logger.Info("New FLV Header found"); read_valid = true; contentBuffer.OnStart(); } } if (!read_valid) { throw new BadDataException(); } } break; } processed = true; } catch (EndOfStreamException) { if (!processed) { throw; } stream.Position = start_pos; eos = true; } catch (BadDataException) { stream.Position = start_pos + 1; } } return(contentBuffer.GetContents()); }
public ParsedContent Read(Stream stream) { var res = new ParsedContent(); var info = new AtomCollection(Channel.ChannelInfo.Extra); var processed = false; var eos = false; while (!eos) { var start_pos = stream.Position; try { switch (state) { case ReaderState.Header: { var bin = ReadBytes(stream, 13); var header = new FileHeader(bin); if (header.IsValid) { Logger.Info("FLV Header found"); fileHeader = header; bin = header.Binary; res.ContentHeader = new Content(position, bin); res.Contents = null; info.SetChanInfoType("FLV"); info.SetChanInfoStreamType("video/x-flv"); info.SetChanInfoStreamExt(".flv"); res.ChannelInfo = new ChannelInfo(info); position = bin.Length; tags.Clear(); state = ReaderState.Body; } else { throw new BadDataException(); } } break; case ReaderState.Body: { var bin = ReadBytes(stream, 11); var read_valid = false; var body = new FLVTag(bin); if (body.IsValidHeader) { body.ReadBody(stream); body.ReadFooter(stream); if (body.IsValidFooter) { read_valid = true; bin = body.Binary; if (res.Contents==null) res.Contents = new List<Content>(); res.Contents.Add(new Content(position, bin)); tags.AddLast(new TagDesc { Timestamp=body.Timestamp/1000.0, DataSize=body.DataSize }); var timespan = tags.Last.Value.Timestamp-tags.First.Value.Timestamp; if (timespan>=30.0) { var sz = tags.Take(tags.Count-1).Sum(t => t.DataSize); info.SetChanInfoBitrate((int)(sz*8/timespan+900)/1000); res.ChannelInfo = new ChannelInfo(info); while (tags.Count>1) tags.RemoveFirst(); } position += bin.Length; } } if (!read_valid) { stream.Position = start_pos; var header = new FileHeader(ReadBytes(stream, 13)); if (header.IsValid) { Logger.Info("New FLV Header found"); read_valid = true; fileHeader = header; bin = header.Binary; res.ContentHeader = new Content(0, bin); res.Contents = null; info.SetChanInfoType("FLV"); info.SetChanInfoStreamType("video/x-flv"); info.SetChanInfoStreamExt(".flv"); res.ChannelInfo = new ChannelInfo(info); tags.Clear(); position = bin.Length; } } if (!read_valid) throw new BadDataException(); } break; } processed = true; } catch (EndOfStreamException) { if (!processed) throw; stream.Position = start_pos; eos = true; } catch (BadDataException) { stream.Position = start_pos+1; } } return res; }