Ejemplo n.º 1
0
        internal void CopyAtom(AtomInfo atom, Stream output)
        {
            Contract.Requires(atom != null);
            Contract.Requires(output != null);
            Contract.Requires(output.CanWrite);

            _stream.Position = atom.Start;
            _stream.CopyRangeTo(output, atom.Size);
        }
Ejemplo n.º 2
0
        internal byte[] ReadAtom(AtomInfo atom)
        {
            Contract.Requires(atom != null);
            Contract.Ensures(Contract.Result <byte[]>() != null);

            _stream.Position = atom.Start;

            using (var reader = new BinaryReader(_stream, Encoding.Default, true))
                return(reader.ReadBytes((int)atom.Size));
        }
Ejemplo n.º 3
0
        internal void DescendToAtom(params string[] hierarchy)
        {
            Contract.Requires(hierarchy != null);
            Contract.Requires(Contract.ForAll(hierarchy, fourCC => !string.IsNullOrEmpty(fourCC)));
            Contract.Requires(Contract.ForAll(hierarchy, fourCC => fourCC.Length == 4));

            _stream.Position = 0;
            _atomInfoStack.Clear();

            using (var reader = new BinaryReader(_stream, Encoding.GetEncoding(1252), true))
            {
                foreach (string fourCC in hierarchy)
                {
                    do
                    {
                        var subAtom = new AtomInfo((uint)_stream.Position, reader.ReadUInt32BigEndian(),
                                                   reader.ReadFourCC());
                        if (subAtom.Size == 0)
                        {
                            throw new IOException(Resources.Mp4AtomNotFoundError);
                        }

                        if (subAtom.FourCC == fourCC)
                        {
                            _atomInfoStack.Push(subAtom);

                            // Some containers also contain data, which needs to be skipped:
                            switch (fourCC)
                            {
                            case "meta":
                                _stream.Seek(4, SeekOrigin.Current);
                                break;

                            case "stsd":
                                _stream.Seek(8, SeekOrigin.Current);
                                break;

                            case "mp4a":
                                _stream.Seek(28, SeekOrigin.Current);
                                break;
                            }

                            break;
                        }

                        _stream.Position = subAtom.End;
                    } while (_stream.Position < (_atomInfoStack.Count == 0 ? _stream.Length : _atomInfoStack.Peek().End));
                }
            }
        }
Ejemplo n.º 4
0
        internal AtomInfo[] GetChildAtomInfo()
        {
            Contract.Ensures(Contract.Result <AtomInfo[]>() != null);

            var result = new List <AtomInfo>();

            using (var reader = new BinaryReader(_stream, Encoding.GetEncoding(1252), true))
            {
                _stream.Position = _atomInfoStack.Count == 0 ? 0 : _atomInfoStack.Peek().Start + 8;

                while (_stream.Position < (_atomInfoStack.Count == 0 ? _stream.Length : _atomInfoStack.Peek().End))
                {
                    var childAtom = new AtomInfo((uint)_stream.Position, reader.ReadUInt32BigEndian(), reader.ReadFourCC());
                    result.Add(childAtom);
                    _stream.Position = childAtom.End;
                }
            }

            return(result.ToArray());
        }