// return the child atom of the corresponding type. // type can contain grand children: e.g. type = "trak.mdia.minf" // return null if the atom does not contain such a child. public Atom getChild(String type) { if (mChildren == null) { return null; } String[] types = type.split("\\.", 2); for (Atom child : mChildren) { if (child.getTypeStr().equals(types[0])) { if (types.length == 1) { return child; } else { return child.getChild(types[1]); } } } return null; }
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; }