Skip to content

Commit

Permalink
Load test resources from classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisribble committed Dec 30, 2021
1 parent d3c6843 commit 4243eb5
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,109 +16,93 @@

package com.coremedia.drm.packager.isoparser;

import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import org.mp4parser.IsoFile;
import org.mp4parser.support.BoxComparator;
import org.mp4parser.tools.ByteBufferByteChannel;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.nio.channels.Channels;
import junit.framework.TestCase;

/**
* Tests ISO Roundtrip.
*/
public class RoundTripTest extends TestCase {
String defaultTestFileDir;

@Override
protected void setUp() throws Exception {
super.setUp();
defaultTestFileDir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
/* Logger.getLogger("").setLevel(Level.ALL);
Handler[] handlers = Logger.getLogger("").getHandlers();
for (Handler handler : handlers) {
handler.setLevel(Level.ALL);
}*/
}

/* public void testRoundDeleteMe() throws Exception {
testRoundTrip_1("/suckerpunch-distantplanet_h1080p.mov");
}*/
public void testRoundTrip_TinyExamples_Old() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/Tiny Sample - OLD.mp4");
testRoundTrip_1("Tiny Sample - OLD.mp4");
}

public void testRoundTrip_TinyExamples_Metaxed() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/Tiny Sample - NEW - Metaxed.mp4");
testRoundTrip_1("Tiny Sample - NEW - Metaxed.mp4");
}

public void testRoundTrip_TinyExamples_Untouched() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/Tiny Sample - NEW - Untouched.mp4");
testRoundTrip_1("Tiny Sample - NEW - Untouched.mp4");
}


public void testRoundTrip_1a() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/multiTrack.3gp");
testRoundTrip_1("multiTrack.3gp");
}

public void testRoundTrip_1b() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/MOV00006.3gp");
testRoundTrip_1("MOV00006.3gp");
}

public void testRoundTrip_1c() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/Beethoven - Bagatelle op.119 no.11 i.m4a");
testRoundTrip_1("Beethoven - Bagatelle op.119 no.11 i.m4a");
}

public void testRoundTrip_1d() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/test.m4p");
testRoundTrip_1("test.m4p");
}

public void testRoundTrip_1e() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/test-pod.m4a");
testRoundTrip_1("test-pod.m4a");
}

public void testRoundTrip_QuickTimeFormat() throws Exception {
testRoundTrip_1(defaultTestFileDir + "/QuickTimeFormat.mp4");
testRoundTrip_1("QuickTimeFormat.mp4");
}


public void testRoundTrip_1(String originalFile) throws Exception {

long start1 = System.currentTimeMillis();
long start2 = System.currentTimeMillis();

try (InputStream is = getClass().getClassLoader().getResourceAsStream(originalFile);
ReadableByteChannel channel = Channels.newChannel(is)) {
IsoFile isoFile = new IsoFile(channel);

IsoFile isoFile = new IsoFile(new FileInputStream(originalFile).getChannel());

long start3 = System.currentTimeMillis();


long start4 = System.currentTimeMillis();
Walk.through(isoFile);
long start5 = System.currentTimeMillis();
long start3 = System.currentTimeMillis();


ByteArrayOutputStream baos = new ByteArrayOutputStream();
isoFile.getBox(Channels.newChannel(baos));
long start4 = System.currentTimeMillis();
Walk.through(isoFile);
long start5 = System.currentTimeMillis();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
isoFile.getBox(Channels.newChannel(baos));

long start6 = System.currentTimeMillis();
long start6 = System.currentTimeMillis();

/* System.err.println("Preparing tmp copy took: " + (start2 - start1) + "ms");
System.err.println("Parsing took : " + (start3 - start2) + "ms");
System.err.println("Writing took : " + (start6 - start3) + "ms");
System.err.println("Walking took : " + (start5 - start4) + "ms");*/


IsoFile copyViaIsoFileReparsed = new IsoFile(new ByteBufferByteChannel(baos.toByteArray()));
BoxComparator.check(isoFile, copyViaIsoFileReparsed, "moov[0]/mvhd[0]", "moov[0]/trak[0]/tkhd[0]", "moov[0]/trak[0]/mdia[0]/mdhd[0]");
isoFile.close();
copyViaIsoFileReparsed.close();
// as windows cannot delete file when something is memory mapped and the garbage collector
// doesn't necessarily free the Buffers quickly enough we cannot delete the file here (we could but only for linux)


IsoFile copyViaIsoFileReparsed = new IsoFile(new ByteBufferByteChannel(baos.toByteArray()));
BoxComparator.check(isoFile, copyViaIsoFileReparsed, "moov[0]/mvhd[0]", "moov[0]/trak[0]/tkhd[0]", "moov[0]/trak[0]/mdia[0]/mdhd[0]");
isoFile.close();
copyViaIsoFileReparsed.close();
// as windows cannot delete file when something is memory mapped and the garbage collector
// doesn't necessarily free the Buffers quickly enough we cannot delete the file here (we could but only for linux)
}
}


}
9 changes: 6 additions & 3 deletions isoparser/src/test/java/org/mp4parser/SkippingBoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -20,9 +23,9 @@ public class SkippingBoxTest {

@Before
public void setup() throws IOException {
FileInputStream fis = new FileInputStream(PathTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/test.m4p");
isoFile = new IsoFile(fis.getChannel(), new PropertyBoxParserImpl().skippingBoxes("mdat", "mvhd"));
fis.close();
try (FileInputStream fis = new FileInputStream(getClass().getClassLoader().getResource("test.m4p").getPath())) {
isoFile = new IsoFile(fis.getChannel(), new PropertyBoxParserImpl().skippingBoxes("mdat", "mvhd"));
}
}


Expand Down
4 changes: 3 additions & 1 deletion isoparser/src/test/java/org/mp4parser/tools/PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class PathTest {

@Before
public void setup() throws IOException {
isoFile = new IsoFile(new FileInputStream(PathTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/multiTrack.3gp").getChannel());
try (FileInputStream fis = new FileInputStream(getClass().getClassLoader().getResource("multiTrack.3gp").getPath())) {
isoFile = new IsoFile(fis.getChannel());
}
}


Expand Down
56 changes: 32 additions & 24 deletions muxer/src/test/java/org/mp4parser/muxer/CencFileRoundtripTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package org.mp4parser.muxer;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -14,16 +28,7 @@
import org.mp4parser.tools.ByteBufferByteChannel;
import org.mp4parser.tools.RangeStartMap;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.Channels;
import java.util.*;

public class CencFileRoundtripTest {
private String baseDir = CencFileRoundtripTest.class.getProtectionDomain().getCodeSource().getLocation().getFile();
private Map<UUID, SecretKey> keys;
private RangeStartMap<Integer, UUID> keyRotation1;
private RangeStartMap<Integer, UUID> keyRotation2;
Expand Down Expand Up @@ -59,67 +64,67 @@ public void setUp() throws Exception {

@Test
public void testSingleKeysStdMp4_cbc1() throws IOException {
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
}


@Test
public void testSingleKeysFragMp4_cbc1() throws IOException {
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
}

@Test
public void testSingleKeysStdMp4_cenc() throws IOException {
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
}

@Test
public void testSingleKeysFragMp4_cenc() throws IOException {
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
}


@Test
public void testClearLeadStdMp4_2_cbc1() throws IOException {
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
}

@Test
public void testClearLeadFragMp4_2_cbc1() throws IOException {
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
}


@Test
public void testClearLeadStdMp4_2_cenc() throws IOException {
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
}

@Test
public void testClearLeadFragMp4_2_cenc() throws IOException {
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
}


@Test
public void testMultipleKeysStdMp4_2_cbc1() throws IOException {
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
}

@Test
public void testMultipleKeysFragMp4_2_cbc1() throws IOException {
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
}


@Test
public void testMultipleKeysStdMp4_2_cenc() throws IOException {
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
}

@Test
public void testMultipleKeysFragMp4_2_cenc() throws IOException {
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
}


Expand All @@ -128,13 +133,14 @@ public void testMultipleKeysFragMp4_2_cenc() throws IOException {

@Test
public void testMultipleKeysFragMp4_2_cenc_pseudo_encrypted() throws IOException {
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", true);
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", true);
}

private void testMultipleKeys(Mp4Builder builder, String testFile, Map<UUID, SecretKey> keys,
RangeStartMap<Integer, UUID> keyRotation,
String encAlgo, boolean encryptButClear) throws IOException {
Movie m1 = MovieCreator.build(testFile);
String filePath = getClass().getClassLoader().getResource(testFile).getPath();
Movie m1 = MovieCreator.build(filePath);
Movie m2 = new Movie();
for (Track track : m1.getTracks()) {
CencEncryptingTrackImpl cencEncryptingTrack =
Expand All @@ -145,7 +151,9 @@ private void testMultipleKeys(Mp4Builder builder, String testFile, Map<UUID, Sec

ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeContainer(Channels.newChannel(baos));
new FileOutputStream("m2.mp4").write(baos.toByteArray());
try (OutputStream os = new FileOutputStream("m2.mp4")) {
os.write(baos.toByteArray());
}

Movie m3 = MovieCreator.build(
new ByteBufferByteChannel(baos.toByteArray()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class FragmentedMp4BuilderTest {
@Test
public void testSimpleMuxing() throws Exception {
Movie m = new Movie();
Movie v = MovieCreator.build(FragmentedMp4BuilderTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4");
Movie a = MovieCreator.build(FragmentedMp4BuilderTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/BBB_qpfile_10sec/output_audio-2ch-20s.mp4");
Movie v = MovieCreator.build(FragmentedMp4BuilderTest.class.getClassLoader().getResource("BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4").toURI().getPath());
Movie a = MovieCreator.build(FragmentedMp4BuilderTest.class.getClassLoader().getResource("BBB_qpfile_10sec/output_audio-2ch-20s.mp4").toURI().getPath());

m.addTrack(v.getTracks().get(0));
m.addTrack(a.getTracks().get(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.mp4parser.muxer;

import org.mp4parser.muxer.container.mp4.MovieCreator;

import java.io.IOException;
import java.net.URISyntaxException;

import org.mp4parser.muxer.container.mp4.MovieCreator;


public class InTestMovieCreator {
public static Movie createMovieOnlyVideo(String... names) throws IOException {
public static Movie createMovieOnlyVideo(String... names) throws IOException, URISyntaxException {
Movie m = new Movie();
for (String name : names) {
Movie m1 = MovieCreator.build((InTestMovieCreator.class.getProtectionDomain().getCodeSource().getLocation().getFile() + name));
Movie m1 = MovieCreator.build((InTestMovieCreator.class.getClassLoader().getResource(name).toURI().getPath()));
for (Track track : m1.getTracks()) {
if ("vide".equals(track.getHandler())) {
m.addTrack(track);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
* Just check it works.
*/
public class DefaultFragmenterTest {
long[] samples = new long[]{1, 87, 174, 261, 348, 435, 522, 609, 696, 783, 870, 957, 1044, 1131, 1218, 1305, 1392, 1479, 1566, 1653, 1740, 1827, 1914, 2001, 2088, 2175, 2262, 2349, 2436, 2523, 2610, 2697, 2784, 2871, 2958, 3045, 3132, 3219, 3306, 3393, 3480, 3567, 3654, 3741, 3828, 3915, 4002, 4089, 4176, 4263, 4350, 4437, 4524, 4611, 4698, 4785};
long[] samples = new long[] { 1, 87, 174, 261, 348, 435, 522, 609, 696, 783, 870, 957, 1044, 1131, 1218, 1305, 1392, 1479, 1566, 1653, 1740, 1827, 1914,
2001, 2088, 2175, 2262, 2349, 2436, 2523, 2610, 2697, 2784, 2871, 2958, 3045, 3132, 3219, 3306, 3393, 3480, 3567, 3654, 3741, 3828, 3915, 4002,
4089, 4176, 4263, 4350, 4437, 4524, 4611, 4698, 4785 };

@Test
public void testSampleNumbers() throws Exception {
String f = DefaultFragmenterTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/Beethoven - Bagatelle op.119 no.11 i.m4a";
String f = DefaultFragmenterTest.class.getClassLoader().getResource("Beethoven - Bagatelle op.119 no.11 i.m4a").toURI().getPath();
Movie m = MovieCreator.build(f);
DefaultFragmenterImpl intersectionFinder = new DefaultFragmenterImpl(2);
long[] s = intersectionFinder.sampleNumbers(m.getTracks().get(0));
Expand Down
Loading

0 comments on commit 4243eb5

Please sign in to comment.