private void setHeader() { // create the atoms needed to build the header. Atom a_ftyp = getFTYPAtom(); Atom a_moov = getMOOVAtom(); Atom a_mdat = new Atom("mdat"); // create an empty atom. The AAC stream data should follow // immediately after. The correct size will be set later. // set the correct chunk offset in the stco atom. Atom a_stco = a_moov.getChild("trak.mdia.minf.stbl.stco"); if (a_stco == null) { mHeader = null; return; } byte[] data = a_stco.getData(); int chunk_offset = a_ftyp.getSize() + a_moov.getSize() + a_mdat.getSize(); int offset = data.length - 4; // here stco should contain only one chunk offset. data[offset++] = (byte) ((chunk_offset >> 24) & 0xFF); data[offset++] = (byte) ((chunk_offset >> 16) & 0xFF); data[offset++] = (byte) ((chunk_offset >> 8) & 0xFF); data[offset++] = (byte) (chunk_offset & 0xFF); // create the header byte array based on the previous atoms. byte[] header = new byte[chunk_offset]; // here chunk_offset is also the size of the header offset = 0; for (Atom atom : new Atom[] {a_ftyp, a_moov, a_mdat}) { byte[] atom_bytes = atom.getBytes(); System.arraycopy(atom_bytes, 0, header, offset, atom_bytes.length); offset += atom_bytes.length; } // set the correct size of the mdat atom int size = 8 + mTotSize; offset -= 8; header[offset++] = (byte) ((size >> 24) & 0xFF); header[offset++] = (byte) ((size >> 16) & 0xFF); header[offset++] = (byte) ((size >> 8) & 0xFF); header[offset++] = (byte) (size & 0xFF); mHeader = header; }
// set the size field of the atom based on its content. private void setSize() { int size = 8; // type + size if (mVersion >= 0) { size += 4; // version + flags } if (mData != null) { size += mData.length; } else if (mChildren != null) { for (Atom child : mChildren) { size += child.getSize(); } } mSize = size; }