-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTestMedia.java
More file actions
261 lines (241 loc) · 6.57 KB
/
Copy pathTestMedia.java
File metadata and controls
261 lines (241 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package javaforce.tests;
import java.io.*;
import java.util.*;
import javaforce.*;
import javaforce.media.*;
import javaforce.voip.*;
import javaforce.net.*;
/** TestMedia.
*
* To run :
* bin/jfexec -cp javaforce.jar javaforce.utils.TestMedia
*
* @author pquiring
*/
public class TestMedia implements MediaIO {
public static void usage() {
System.out.println("TestMedia output | input");
System.exit(1);
}
public static void main(String[] args) {
if (args.length == 0) {
usage();
}
for(int a=0;a<args.length;a++) {
int idx = args[a].indexOf('=');
if (idx != -1) {
String key = args[a].substring(0, idx);
String value = args[a].substring(idx+1);
switch (key) {
case "seconds": encoder_seconds = Integer.valueOf(value); break;
case "audiosrc": encoder_audio_src = value; break;
}
continue;
}
try {
switch (args[a]) {
case "input": input(); break;
case "output": output(true); break;
default: usage();
}
} catch (Exception e) {
JFLog.log(e);
}
}
}
public static void input() {
if (!new File("test-0.mp4").exists()) {
output(false);
}
while (true) {
TestMedia media = new TestMedia();
MediaInput decoder = new MediaInput();
if (use_media_io) {
media.open("test-0.mp4");
decoder.open(media);
} else {
decoder.open("test-0.mp4", "mp4");
}
int pkts = 0;
MediaVideoDecoder videoDecoder = decoder.createVideoDecoder();
MediaAudioDecoder audioDecoder = decoder.createAudioDecoder();
Packet pkt;
do {
pkt = decoder.readPacket();
if (pkt == null || pkt.length == 0) break;
pkts++;
if (pkt.stream == videoDecoder.getStream()) {
int[] px = videoDecoder.decode(pkt);
if (px != null) {
JFLog.log("video=" + px.length);
} else {
JFLog.log("video decode failed");
}
}
else if (pkt.stream == audioDecoder.getStream()) {
short[] sams = audioDecoder.decode(pkt);
if (sams != null) {
JFLog.log("audio=" + sams.length);
} else {
JFLog.log("audio decode failed");
}
}
} while (true);
System.out.println("packets=" + pkts);
decoder.close();
media.close();
}
}
public static void random(int[] px) {
Random r = new Random();
int len = px.length;
for(int a=0;a<len;a++) {
px[a] = r.nextInt() | 0xff000000;
}
}
private static AudioGenerate audio_gen = new AudioGenerate();
public static void random(short[] sams) {
Random r = new Random();
int len = sams.length;
for(int a=0;a<len;a++) {
sams[a] = (short)(r.nextInt(65536) - 32768);
}
}
public static void sine(short[] sams) {
audio_gen.generate(sams, AudioGenerate.SINE, 44100, 5000, 1.0f);
}
public static int encoder_seconds = 4;
public static String encoder_audio_src = "random";
public static boolean use_media_io = true;
public static void output(boolean loop) {
TestMedia media = new TestMedia();
int[] px = new int[640*480];
short[] sams = new short[7350];
int i = 0;
CodecInfo info = new CodecInfo();
Packet packet;
do {
MediaOutput encoder = new MediaOutput();
MediaVideoEncoder videoEncoder;
MediaAudioEncoder audioEncoder;
AudioInput input = new AudioInput();
if (use_media_io) {
media.size = 0;
media.create(i++);
encoder.create(media, "mp4");
} else {
String file = "test-" + (i++) + ".mp4";
encoder.create(file, "mp4");
System.out.println("file=" + file);
}
info.width = 640;
info.height = 480;
info.fps = 10f;
info.video_bit_rate = (int)(1 * JF.MB); //1Mb/s
info.video_codec = MediaCoder.AV_CODEC_ID_H265;
videoEncoder = encoder.createVideoEncoder(info);
info.chs = 2; //stereo
info.freq = 44100;
info.audio_bit_rate = (int)(128 * JF.KB); //128kb/s
info.audio_codec = MediaCoder.AV_CODEC_ID_AAC;
audioEncoder = encoder.createAudioEncoder(info);
if (i == 10) i = 0;
int frame_size = audioEncoder.getAudioFramesize() * 2; //*2=stereo
JFLog.log("frame_size=" + frame_size);
if (encoder_audio_src.equals("mic")) {
input.start(2, 44100, 16, frame_size, "<default>");
}
System.out.println("Audio Frame Size=" + audioEncoder.getAudioFramesize());
System.gc();
for(int a=0;a<24 * encoder_seconds;a++) {
JFLog.log("loop=" + a);
random(px);
packet = videoEncoder.encode(px, 0, px.length);
if (packet != null) {
encoder.writePacket(packet);
}
if (encoder_audio_src.equals("mic")) {
while (!input.read(sams)) {
JF.sleep(50);
}
} else {
sine(sams);
}
packet = audioEncoder.encode(sams, 0, sams.length);
if (packet != null) {
encoder.writePacket(packet);
}
}
encoder.close();
if (use_media_io) {
media.close();
System.out.println("size=" + media.size);
}
if (encoder_audio_src.equals("mic")) {
input.stop();
}
System.gc();
} while (loop);
}
public RandomAccessFile raf;
public void create(int i) {
try {
raf = new RandomAccessFile("test-" + i + ".mp4", "rw");
} catch (Exception e) {
e.printStackTrace();
}
}
public void open(String filename) {
try {
raf = new RandomAccessFile(filename, "rw");
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
raf = null;
}
public int read(byte[] data) {
try {
return raf.read(data);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public long size;
public int write(byte[] data) {
try {
size += data.length;
raf.write(data);
return data.length;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public long seek(long pos, int type) {
try {
switch (type) {
case MediaCoder.SEEK_SET:
break;
case MediaCoder.SEEK_CUR:
pos += raf.getFilePointer();
break;
case MediaCoder.SEEK_END:
pos += raf.length();
break;
}
raf.seek(pos);
return pos;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}