Пример #1
0
        public static ASFHeader Read(ASFChunk chunk)
        {
            var streams = new List <StreamType>();
            int bitrate = 0;
            var s       = new MemoryStream(chunk.Data, false);

            ASFObject.ReadHeader(s); //root_obj
            var sub_headers = BinaryReader.ReadUInt32LE(s);

            s.Seek(2, SeekOrigin.Current); //2 bytes padding?
            for (var i = 0; i < sub_headers; i++)
            {
                var obj = ASFObject.Read(s);
                switch (obj.Type)
                {
                case ASFObject.KnownType.FileProperty: {
                    var objdata = new MemoryStream(obj.Data, false);
                    objdata.Seek(32, SeekOrigin.Current);
                    BinaryReader.ReadInt64LE(objdata); //packets
                    objdata.Seek(28, SeekOrigin.Current);
                    BinaryReader.ReadInt32LE(objdata); //packetsize_min
                    BinaryReader.ReadInt32LE(objdata); //packetsize_max
                    bitrate = (BinaryReader.ReadInt32LE(objdata) + 999) / 1000;
                }
                break;

                case ASFObject.KnownType.StreamProperty: {
                    var objdata     = new MemoryStream(obj.Data, false);
                    var stream_type = new Guid(BinaryReader.ReadBytes(objdata, 16));
                    if (stream_type == ASFObject.StreamIDAudio)
                    {
                        streams.Add(StreamType.Audio);
                    }
                    else if (stream_type == ASFObject.StreamIDVideo)
                    {
                        streams.Add(StreamType.Video);
                    }
                    else
                    {
                        streams.Add(StreamType.Unknown);
                    }
                }
                break;

                default:
                    break;
                }
            }
            return(new ASFHeader(bitrate, streams.ToArray()));
        }
Пример #2
0
 public bool TryParseContentType(byte[] header_bytes, out string content_type, out string mime_type)
 {
     using (var stream = new MemoryStream(header_bytes)) {
         try {
             for (var chunks = 0; chunks < 8; chunks++)
             {
                 var chunk = ASFChunk.Read(stream);
                 if (chunk.KnownType != ASFChunk.ChunkType.Header)
                 {
                     continue;
                 }
                 var header = ASFHeader.Read(chunk);
                 if (header.Streams.Any(type => type == ASFHeader.StreamType.Video))
                 {
                     content_type = "WMV";
                     mime_type    = "video/x-ms-wmv";
                 }
                 else if (header.Streams.Any(type => type == ASFHeader.StreamType.Audio))
                 {
                     content_type = "WMA";
                     mime_type    = "audio/x-ms-wma";
                 }
                 else
                 {
                     content_type = "ASF";
                     mime_type    = "video/x-ms-asf";
                 }
                 return(true);
             }
         }
         catch (EndOfStreamException) {
         }
     }
     content_type = null;
     mime_type    = null;
     return(false);
 }
Пример #3
0
        public async Task ReadAsync(IContentSink sink, Stream stream, CancellationToken cancel_token)
        {
            int      streamIndex     = -1;
            DateTime streamOrigin    = DateTime.Now;
            long     contentPosition = 0;
            bool     eof             = false;

            do
            {
                ASFChunk chunk = null;
                try {
                    chunk = await ASFChunk.ReadAsync(stream, cancel_token).ConfigureAwait(false);
                }
                catch (EndOfStreamException) {
                    eof = true;
                    continue;
                }
                switch (chunk.KnownType)
                {
                case ASFChunk.ChunkType.Header:
                {
                    var header = ASFHeader.Read(chunk);
                    var info   = new AtomCollection(Channel.ChannelInfo.Extra);
                    info.SetChanInfoBitrate(header.Bitrate);
                    if (header.Streams.Any(type => type == ASFHeader.StreamType.Video))
                    {
                        info.SetChanInfoType("WMV");
                        info.SetChanInfoStreamType("video/x-ms-wmv");
                        info.SetChanInfoStreamExt(".wmv");
                    }
                    else if (header.Streams.Any(type => type == ASFHeader.StreamType.Audio))
                    {
                        info.SetChanInfoType("WMA");
                        info.SetChanInfoStreamType("audio/x-ms-wma");
                        info.SetChanInfoStreamExt(".wma");
                    }
                    else
                    {
                        info.SetChanInfoType("ASF");
                        info.SetChanInfoStreamType("video/x-ms-asf");
                        info.SetChanInfoStreamExt(".asf");
                    }
                    sink.OnChannelInfo(new ChannelInfo(info));
                    streamIndex     = Channel.GenerateStreamID();
                    streamOrigin    = DateTime.Now;
                    contentPosition = 0;
                    var data = chunk.ToByteArray();
                    sink.OnContentHeader(new Content(streamIndex, TimeSpan.Zero, contentPosition, data, PCPChanPacketContinuation.None));
                    contentPosition += data.Length;
                    break;
                }

                case ASFChunk.ChunkType.Data:
                {
                    var data = chunk.ToByteArray();
                    sink.OnContent(
                        new Content(streamIndex, DateTime.Now - streamOrigin, contentPosition, chunk.ToByteArray(), PCPChanPacketContinuation.None)
                        );
                    contentPosition += data.Length;
                }
                break;

                case ASFChunk.ChunkType.Unknown:
                    break;
                }
            } while (!eof);
        }
Пример #4
0
        public ParsedContent Read(Stream stream)
        {
            var chunks = 0;
            var res    = new ParsedContent();
            var pos    = Channel.ContentPosition;

            try {
                while (chunks < 8)
                {
                    var chunk = ASFChunk.Read(stream);
                    chunks++;
                    switch (chunk.KnownType)
                    {
                    case ASFChunk.ChunkType.Header: {
                        var header = ASFHeader.Read(chunk);
                        var info   = new AtomCollection(Channel.ChannelInfo.Extra);
                        info.SetChanInfoBitrate(header.Bitrate);
                        if (header.Streams.Any(type => type == ASFHeader.StreamType.Video))
                        {
                            info.SetChanInfoType("WMV");
                            info.SetChanInfoStreamType("video/x-ms-wmv");
                            info.SetChanInfoStreamExt(".wmv");
                        }
                        else if (header.Streams.Any(type => type == ASFHeader.StreamType.Audio))
                        {
                            info.SetChanInfoType("WMA");
                            info.SetChanInfoStreamType("audio/x-ms-wma");
                            info.SetChanInfoStreamExt(".wma");
                        }
                        else
                        {
                            info.SetChanInfoType("ASF");
                            info.SetChanInfoStreamType("video/x-ms-asf");
                            info.SetChanInfoStreamExt(".asf");
                        }
                        res.ChannelInfo   = new ChannelInfo(info);
                        res.ContentHeader = new Content(pos, chunk.ToByteArray());
                        pos += chunk.TotalLength;
                    }
                    break;

                    case ASFChunk.ChunkType.Data:
                        if (res.Contents == null)
                        {
                            res.Contents = new System.Collections.Generic.List <Content>();
                        }
                        res.Contents.Add(new Content(pos, chunk.ToByteArray()));
                        pos += chunk.TotalLength;
                        break;

                    case ASFChunk.ChunkType.Unknown:
                        break;
                    }
                }
            }
            catch (EndOfStreamException) {
                if (chunks == 0)
                {
                    throw;
                }
            }
            return(res);
        }
Пример #5
0
 public static ASFHeader Read(ASFChunk chunk)
 {
     var streams = new List<StreamType>();
       int bitrate = 0;
       var s = new MemoryStream(chunk.Data, false);
       ASFObject.ReadHeader(s); //root_obj
       var sub_headers = BinaryReader.ReadUInt32LE(s);
       s.Seek(2, SeekOrigin.Current); //2 bytes padding?
       for (var i=0; i<sub_headers; i++) {
     var obj = ASFObject.Read(s);
     switch (obj.Type) {
     case ASFObject.KnownType.FileProperty: {
       var objdata = new MemoryStream(obj.Data, false);
       objdata.Seek(32, SeekOrigin.Current);
       BinaryReader.ReadInt64LE(objdata); //packets
       objdata.Seek(28, SeekOrigin.Current);
       BinaryReader.ReadInt32LE(objdata); //packetsize_min
       BinaryReader.ReadInt32LE(objdata); //packetsize_max
       bitrate = (BinaryReader.ReadInt32LE(objdata)+999) / 1000;
       }
       break;
     case ASFObject.KnownType.StreamProperty: {
       var objdata = new MemoryStream(obj.Data, false);
       var stream_type = new Guid(BinaryReader.ReadBytes(objdata, 16));
       if (stream_type==ASFObject.StreamIDAudio) {
     streams.Add(StreamType.Audio);
       }
       else if (stream_type==ASFObject.StreamIDVideo) {
     streams.Add(StreamType.Video);
       }
       else {
     streams.Add(StreamType.Unknown);
       }
       }
       break;
     default:
       break;
     }
       }
       return new ASFHeader(bitrate, streams.ToArray());
 }