Exemplo n.º 1
0
 public static Mp4Box CreateMp4Box(byte[] buffer, int offset)
 {
     if ((buffer != null) &&
         (offset + 8 < buffer.Length))
     {
         Mp4Box box = CreateMp4BoxFromType(ReadMp4BoxType(buffer, offset));
         if (box != null)
         {
             box.Length = ReadMp4BoxLength(buffer, offset);
             if ((offset + box.Length <= buffer.Length) && (box.Length > 8))
             {
                 box.Type = ReadMp4BoxType(buffer, offset);
                 box.SetPath("/" + box.GetBoxType());
                 box.Data = ReadMp4BoxData(buffer, offset, box.Length);
                 List <Mp4Box> list = box.GetChildren();
                 if ((list != null) && (list.Count > 0))
                 {
                     foreach (var b in list)
                     {
                         box.AddMp4Box(b);
                     }
                 }
                 return(box);
             }
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        public static Mp4Box ReadMp4Box(FileStream fs)
        {
            Mp4Box box = null;

            if (fs != null)
            {
                byte[] buffer = new byte[4];
                if (buffer != null)
                {
                    if (fs.Read(buffer, 0, 4) == 4)
                    {
                        int mp4BoxLen = 0;
                        mp4BoxLen |= (int)(buffer[0] << 24);
                        mp4BoxLen |= (int)(buffer[1] << 16);
                        mp4BoxLen |= (int)(buffer[2] << 8);
                        mp4BoxLen |= (int)(buffer[3] << 0);
                        if (mp4BoxLen >= 8)
                        {
                            buffer = new byte[mp4BoxLen];
                            if (buffer != null)
                            {
                                WriteMp4BoxInt32(buffer, 0, mp4BoxLen);
                                if (fs.Read(buffer, 4, mp4BoxLen - 4) == (mp4BoxLen - 4))
                                {
                                    return(CreateMp4Box(buffer, 0));
                                }
                            }
                        }
                    }
                }
            }
            return(box);
        }
Exemplo n.º 3
0
        public void UpdateParentLength(Mp4Box box, int Len)
        {
            Mp4Box pbox = box.GetParent();

            while (pbox != null)
            {
                pbox.SetBoxLength(pbox.GetBoxLength() + Len);
                pbox = pbox.GetParent();
            }
        }
Exemplo n.º 4
0
        public static Mp4Box CreateEmptyMp4Box(string Type)
        {
            Mp4Box box = new Mp4Box();

            if (box != null)
            {
                box.Length = 8;
                if (!string.IsNullOrEmpty(Type) &&
                    (Type.Length <= 4))
                {
                    box.Type = Type;
                    return(box);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
 public bool AddMp4Box(Mp4Box box, bool bAddInData = false)
 {
     if (Children == null)
     {
         Children = new List <Mp4Box>();
     }
     if (Children != null)
     {
         box.SetParent(this);
         box.SetPath(this.GetPath() + "/" + box.GetBoxType());
         Children.Add(box);
         if (bAddInData == true)
         {
             Append(box.Data);
         }
         return(true);
     }
     return(false);
 }
Exemplo n.º 6
0
        public List <Mp4Box> GetChildren()
        {
            List <Mp4Box> list = new List <Mp4Box>();

            if ((list != null) && (Data != null))
            {
                ChildrenOffset = 0;
                if (this.GetBoxType() == "stsd")
                {
                    ChildrenOffset = 8;
                }
                else if (this.GetBoxType() == "dref")
                {
                    ChildrenOffset = 8;
                }
                else if (this.GetBoxType() == "encv")
                {
                    ChildrenOffset = 78;
                }
                else if (this.GetBoxType() == "enca")
                {
                    ChildrenOffset = 28;
                }
                int Offset = ChildrenOffset;
                while (Offset < Data.Length)
                {
                    Mp4Box box = CreateMp4Box(Data, Offset);
                    if (box != null)
                    {
                        list.Add(box);
                        Offset += box.Length;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(list);
        }
Exemplo n.º 7
0
        public static bool WriteMp4Box(Mp4Box box, FileStream fs)
        {
            bool result = false;

            if ((box != null) && (fs != null))
            {
                try
                {
                    byte[] header = new byte[8];
                    Mp4Box.WriteMp4BoxInt32(header, 0, box.Length);
                    Mp4Box.WriteMp4BoxString(header, 4, box.GetBoxType());
                    fs.Write(header, 0, 8);
                    fs.Write(box.Data, 0, box.Data.Length);
                    result = true;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Write("Exception while writing box in file: " + ex.Message);
                }
            }
            return(result);
        }
Exemplo n.º 8
0
 public Mp4Box GetChild(string type)
 {
     if (Children != null)
     {
         foreach (var box in Children)
         {
             if (box.GetBoxType() == type)
             {
                 return(box);
             }
             else
             {
                 Mp4Box childbox = box.GetChild(type);
                 if (childbox != null)
                 {
                     return(childbox);
                 }
             }
         }
     }
     return(null);
 }
Exemplo n.º 9
0
 public Mp4Box FindChildBox(string BoxType)
 {
     if (Children != null)
     {
         foreach (var box in Children)
         {
             if (box.GetBoxType() == BoxType)
             {
                 return(box);
             }
             else
             {
                 Mp4Box ChildBox = box.FindChildBox(BoxType);
                 if (ChildBox != null)
                 {
                     return(ChildBox);
                 }
             }
         }
     }
     return(null);
 }
Exemplo n.º 10
0
        public static string GetBoxChildrenString(int level, Mp4Box box)
        {
            string result     = string.Empty;
            int    locallevel = level + 1;

            if (box != null)
            {
                List <Mp4Box> list = box.GetChildren();
                if ((list != null) && (list.Count > 0))
                {
                    foreach (var m in list)
                    {
                        string prefix = string.Empty;
                        for (int i = 0; i < locallevel; i++)
                        {
                            prefix += "\t\t";
                        }
                        result += prefix + m.ToString() + "\r\n";
                        result += GetBoxChildrenString(locallevel, m);
                    }
                }
            }
            return(result);
        }
Exemplo n.º 11
0
 public void SetParent(Mp4Box box)
 {
     this.Path = box.GetPath() + "/" + this.GetBoxType();
     Parent    = box;
 }