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);
 }
Esempio n. 2
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);
        }