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);
 }
        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);
        }
示例#3
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);
        }