Skip to content

Commit

Permalink
[common][fmp4] Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Mar 22, 2016
1 parent 9da6f3d commit 7ac3d7b
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 56 deletions.
7 changes: 7 additions & 0 deletions common/src/main/java/com/frostwire/fmp4/Bits.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ public static int make4cc(String c) {

return int32(code[0], code[1], code[2], code[3]);
}

public static int l2i(long n) {
if (n < Integer.MIN_VALUE || Integer.MAX_VALUE < n) {
throw new IllegalArgumentException("Can't convert long to int: " + n);
}
return (int) n;
}
}
42 changes: 14 additions & 28 deletions common/src/main/java/com/frostwire/fmp4/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
* @author gubatron
* @author aldenml
*/
public abstract class Box {
public class Box {

public static final int uuid = Bits.make4cc("uuid");
public static final int mdat = Bits.make4cc("mdat");
Expand All @@ -48,43 +47,30 @@ public abstract class Box {
protected int type;
protected Long largesize;
protected byte[] usertype;
protected LinkedList<Box> boxes;

private long length;

Box() {
}

void header(int size, int type, Long largesize, byte[] usertype) {
this.size = size;
this.type = type;
this.largesize = largesize;
this.usertype = usertype;
void read(InputChannel ch, ByteBuffer buf) throws IOException {
throw new UnsupportedOperationException();
}

void write(OutputChannel ch, ByteBuffer buf) throws IOException {
throw new UnsupportedOperationException();
}

// set length
length = size - 8;
long length() {
long n = size - 8;
if (size == 1) {
length = largesize - 16;
n = largesize - 16;
} else if (size == 0) {
length = -1;
n = -1;
}
if (type == uuid) {
length -= 16;
n -= 16;
}
}

long length() {
return length;
}

void read(InputChannel in, ByteBuffer buf) throws IOException {
long len = length();

if (len > 0) {
IO.skip(in, len, buf);
} else {
IO.skip(in, buf);
}
return n;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/com/frostwire/fmp4/FileTypeBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class FileTypeBox extends Box {

@Override
void read(InputChannel in, ByteBuffer buf) throws IOException {
IO.read1(in, length(), buf);
IO.read(in, Bits.l2i(length()), buf);

major_brand = buf.getInt();
minor_version = buf.getInt();
Expand Down
7 changes: 0 additions & 7 deletions common/src/main/java/com/frostwire/fmp4/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ public static void read(InputChannel in, int len, ByteBuffer buf) throws IOExcep
buf.flip();
}

public static void read1(InputChannel in, long len, ByteBuffer buf) throws IOException {
if (len <= 0 || Integer.MAX_VALUE < len) {
throw new IllegalArgumentException("len must be in (0, Integer.MAX_VALUE]: len=");
}
IO.read(in, (int) len, buf);
}

public static void skip(InputChannel in, long len, ByteBuffer buf) throws IOException {
if (len <= 0) {
throw new IllegalArgumentException("len argument must be > 0");
Expand Down
31 changes: 14 additions & 17 deletions common/src/main/java/com/frostwire/fmp4/IsoMedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,48 +40,45 @@ static void read(InputChannel in, long len) throws IOException {
read(in, len, null, buf);
}

public static void read(InputChannel in, long len, Box parent, ByteBuffer buf) throws IOException {
long n = in.count();
public static void read(InputChannel ch, long len, Box parent, ByteBuffer buf) throws IOException {
long n = ch.count();
do {
IO.read(in, 8, buf);
IO.read(ch, 8, buf);

int size = buf.getInt();
int type = buf.getInt();

Long largesize = null;
if (size == 1) {
IO.read(in, 8, buf);
IO.read(ch, 8, buf);
largesize = buf.getLong();
}

byte[] usertype = null;
if (type == Box.uuid) {
usertype = new byte[16];
IO.read(in, 16, buf);
IO.read(ch, 16, buf);
buf.get(usertype);
}

System.out.println(Bits.make4cc(type));
Box b = Box.empty(type);
b.header(size, type, largesize, usertype);
long r = in.count();
b.read(in, buf);
r = in.count() - r;
if (parent != null) {
if (parent.boxes == null) {
parent.boxes = new LinkedList<>();
}
parent.boxes.add(b);
}
b.size = size;
b.type = type;
b.largesize = largesize;
b.usertype = usertype;
long r = ch.count();
b.read(ch, buf);
r = ch.count() - r;

if (type == Box.mdat) {
System.out.println("need to handle mdat");
//return;
}

if (r < b.length()) {
read(in, b.length() - r, b, buf);
read(ch, b.length() - r, b, buf);
}
} while (len == -1 || in.count() - n < len);
} while (len == -1 || ch.count() - n < len);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class ObjectDescriptorBox extends FullBox {
void read(InputChannel in, ByteBuffer buf) throws IOException {
super.read(in, buf);
long len = length() - 4;
IO.read1(in, len, buf);
IO.read(in, Bits.l2i(len), buf);
data = new byte[(int) len];
buf.get(data);
}
Expand Down
10 changes: 8 additions & 2 deletions common/src/main/java/com/frostwire/fmp4/UnknownBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ public final class UnknownBox extends Box {
}

@Override
void read(InputChannel in, ByteBuffer buf) throws IOException {
void read(InputChannel ch, ByteBuffer buf) throws IOException {
System.out.println("Reading unknown box: " + Bits.make4cc(type));
super.read(in, buf);

long len = length();
if (len > 0) {
IO.skip(ch, len, buf);
} else {
IO.skip(ch, buf);
}
}
}
39 changes: 39 additions & 0 deletions common/src/test/java/com/frostwire/fmp4/SimpleReadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Created by Angel Leon (@gubatron), Alden Torres (aldenml)
* Copyright (c) 2011-2016, FrostWire(R). All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.frostwire.fmp4;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
* @author gubatron
* @author aldenml
*/
public class SimpleReadTest {

@Test
public void testRead() throws IOException {
File f = new File("/Users/aldenml/Downloads/test2.mp4");
RandomAccessFile in = new RandomAccessFile("/Users/aldenml/Downloads/test2.mp4", "r");
InputChannel ch = new InputChannel(in.getChannel());
IsoMedia.read(ch, f.length());
}
}

0 comments on commit 7ac3d7b

Please sign in to comment.