Exemplo n.º 1
0
        /// <summary>
        ///    Renders the current instance, including its children, to
        ///    a new <see cref="ByteVector" /> object, preceeding the
        ///    contents with a specified block of data.
        /// </summary>
        /// <param name="topData">
        ///    A <see cref="ByteVector" /> object containing box
        ///    specific header data to preceed the content.
        /// </param>
        /// <returns>
        ///    A <see cref="ByteVector" /> object containing the
        ///    rendered version of the current instance.
        /// </returns>
        protected virtual ByteVector Render(ByteVector topData)
        {
            var free_found = false;
            var output     = new ByteVector();

            if (Children != null)
            {
                foreach (var box in Children)
                {
                    if (box.GetType() == typeof(
                            IsoFreeSpaceBox))
                    {
                        free_found = true;
                    }
                    else
                    {
                        output.Add(box.Render());
                    }
                }
            }
            else if (Data != null)
            {
                output.Add(Data);
            }

            // If there was a free, don't take it away, and let meta
            // be a special case.
            if (free_found || BoxType == Mpeg4.BoxType.Meta)
            {
                long size_difference = DataSize - output.Count;

                // If we have room for free space, add it so we
                // don't have to resize the file.
                if (header.DataSize != 0 && size_difference >= 8)
                {
                    output.Add(new IsoFreeSpaceBox(
                                   size_difference).Render());
                }

                // If we're getting bigger, get a lot bigger so
                // we might not have to again.
                else
                {
                    output.Add(new IsoFreeSpaceBox(2048
                                                   ).Render());
                }
            }

            // Adjust the header's data size to match the content.
            header.DataSize = topData.Count + output.Count;

            // Render the full box.
            output.Insert(0, topData);
            output.Insert(0, header.Render());

            return(output);
        }