// return a byte array containing the full content of the atom (including header) public byte[] getBytes() { byte[] atom_bytes = new byte[mSize]; int offset = 0; atom_bytes[offset++] = (byte) ((mSize >> 24) & 0xFF); atom_bytes[offset++] = (byte) ((mSize >> 16) & 0xFF); atom_bytes[offset++] = (byte) ((mSize >> 8) & 0xFF); atom_bytes[offset++] = (byte) (mSize & 0xFF); atom_bytes[offset++] = (byte) ((mType >> 24) & 0xFF); atom_bytes[offset++] = (byte) ((mType >> 16) & 0xFF); atom_bytes[offset++] = (byte) ((mType >> 8) & 0xFF); atom_bytes[offset++] = (byte) (mType & 0xFF); if (mVersion >= 0) { atom_bytes[offset++] = mVersion; atom_bytes[offset++] = (byte) ((mFlags >> 16) & 0xFF); atom_bytes[offset++] = (byte) ((mFlags >> 8) & 0xFF); atom_bytes[offset++] = (byte) (mFlags & 0xFF); } if (mData != null) { System.arraycopy(mData, 0, atom_bytes, offset, mData.length); } else if (mChildren != null) { byte[] child_bytes; for (Atom child : mChildren) { child_bytes = child.getBytes(); System.arraycopy(child_bytes, 0, atom_bytes, offset, child_bytes.length); offset += child_bytes.length; } } return atom_bytes; }
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; }