-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
SrsEncoder.java
666 lines (578 loc) · 26.6 KB
/
SrsEncoder.java
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package net.ossrs.yasea;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.media.MediaRecorder;
import android.util.Log;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by Leo Ma on 4/1/2016.
*/
public class SrsEncoder {
private static final String TAG = "SrsEncoder";
public static final String VCODEC = "video/avc";
public static final String ACODEC = "audio/mp4a-latm";
public static String x264Preset = "veryfast";
public static int vPrevWidth = 640;
public static int vPrevHeight = 360;
public static int vPortraitWidth = 360;
public static int vPortraitHeight = 640;
public static int vLandscapeWidth = 640;
public static int vLandscapeHeight = 360;
public static int vOutWidth = 360; // Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
public static int vOutHeight = 640; // Since Y component is quadruple size as U and V component, the stride must be set as 32x
public static int vBitrate = 1200 * 1024; // 1200 kbps
public static final int VFPS = 24;
public static final int VGOP = 48;
public static final int ASAMPLERATE = 44100;
public static int aChannelConfig = AudioFormat.CHANNEL_IN_STEREO;
public static final int ABITRATE = 64 * 1024; // 64 kbps
private SrsEncodeHandler mHandler;
private SrsFlvMuxer flvMuxer;
private SrsMp4Muxer mp4Muxer;
private MediaCodecInfo vmci;
private MediaCodec vencoder;
private MediaCodec aencoder;
private boolean networkWeakTriggered = false;
private boolean mCameraFaceFront = true;
private boolean useSoftEncoder = false;
private boolean canSoftEncode = false;
private long mPresentTimeUs;
private long mPausetime;
private int mVideoColorFormat;
private int videoFlvTrack;
private int videoMp4Track;
private int audioFlvTrack;
private int audioMp4Track;
// Y, U (Cb) and V (Cr)
// yuv420 yuv yuv yuv yuv
// yuv420p (planar) yyyy*2 uu vv
// yuv420sp(semi-planner) yyyy*2 uv uv
// I420 -> YUV420P yyyy*2 uu vv
// YV12 -> YUV420P yyyy*2 vv uu
// NV12 -> YUV420SP yyyy*2 uv uv
// NV21 -> YUV420SP yyyy*2 vu vu
// NV16 -> YUV422SP yyyy uv uv
// YUY2 -> YUV422SP yuyv yuyv
public SrsEncoder(SrsEncodeHandler handler) {
mHandler = handler;
mVideoColorFormat = chooseVideoEncoder();
}
public void setFlvMuxer(SrsFlvMuxer flvMuxer) {
this.flvMuxer = flvMuxer;
}
public void setMp4Muxer(SrsMp4Muxer mp4Muxer) {
this.mp4Muxer = mp4Muxer;
}
public boolean start() {
if (flvMuxer == null || mp4Muxer == null) {
return false;
}
// the referent PTS for video and audio encoder.
mPresentTimeUs = System.nanoTime() / 1000;
// Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
// Since Y component is quadruple size as U and V component, the stride must be set as 32x
if (!useSoftEncoder && (vOutWidth % 32 != 0 || vOutHeight % 32 != 0)) {
if (vmci.getName().contains("MTK")) {
//throw new AssertionError("MTK encoding revolution stride must be 32x");
}
}
setEncoderResolution(vOutWidth, vOutHeight);
setEncoderFps(VFPS);
setEncoderGop(VGOP);
// Unfortunately for some android phone, the output fps is less than 10 limited by the
// capacity of poor cheap chips even with x264. So for the sake of quick appearance of
// the first picture on the player, a spare lower GOP value is suggested. But note that
// lower GOP will produce more I frames and therefore more streaming data flow.
// setEncoderGop(15);
setEncoderBitrate(vBitrate);
setEncoderPreset(x264Preset);
if (useSoftEncoder) {
canSoftEncode = openSoftEncoder();
if (!canSoftEncode) {
return false;
}
}
// aencoder pcm to aac raw stream.
// requires sdk level 16+, Android 4.1, 4.1.1, the JELLY_BEAN
try {
aencoder = MediaCodec.createEncoderByType(ACODEC);
} catch (IOException e) {
Log.e(TAG, "create aencoder failed.");
e.printStackTrace();
return false;
}
// setup the aencoder.
// @see https://developer.android.com/reference/android/media/MediaCodec.html
int ach = aChannelConfig == AudioFormat.CHANNEL_IN_STEREO ? 2 : 1;
MediaFormat audioFormat = MediaFormat.createAudioFormat(ACODEC, ASAMPLERATE, ach);
audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, ABITRATE);
audioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);
aencoder.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
// add the audio tracker to muxer.
audioFlvTrack = flvMuxer.addTrack(audioFormat);
audioMp4Track = mp4Muxer.addTrack(audioFormat);
// vencoder yuv to 264 es stream.
// requires sdk level 16+, Android 4.1, 4.1.1, the JELLY_BEAN
try {
vencoder = MediaCodec.createByCodecName(vmci.getName());
} catch (IOException e) {
Log.e(TAG, "create vencoder failed.");
e.printStackTrace();
return false;
}
// setup the vencoder.
// Note: landscape to portrait, 90 degree rotation, so we need to switch width and height in configuration
MediaFormat videoFormat = MediaFormat.createVideoFormat(VCODEC, vOutWidth, vOutHeight);
videoFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, mVideoColorFormat);
videoFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);
videoFormat.setInteger(MediaFormat.KEY_BIT_RATE, vBitrate);
videoFormat.setInteger(MediaFormat.KEY_FRAME_RATE, VFPS);
videoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, VGOP / VFPS);
vencoder.configure(videoFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
// add the video tracker to muxer.
videoFlvTrack = flvMuxer.addTrack(videoFormat);
videoMp4Track = mp4Muxer.addTrack(videoFormat);
// start device and encoder.
vencoder.start();
aencoder.start();
return true;
}
public void pause(){
mPausetime = System.nanoTime() / 1000;
}
public void resume(){
long resumeTime = (System.nanoTime() / 1000) - mPausetime;
mPresentTimeUs = mPresentTimeUs + resumeTime;
mPausetime = 0;
}
public void stop() {
if (useSoftEncoder) {
closeSoftEncoder();
canSoftEncode = false;
}
if (aencoder != null) {
Log.i(TAG, "stop aencoder");
try {
aencoder.stop();
}catch (IllegalStateException e){
e.printStackTrace();
}
aencoder.release();
aencoder = null;
}
if (vencoder != null) {
Log.i(TAG, "stop vencoder");
try {
vencoder.stop();
}catch (IllegalStateException e){
e.printStackTrace();
}
vencoder.release();
vencoder = null;
}
}
public void setCameraFrontFace() {
mCameraFaceFront = true;
}
public void setCameraBackFace() {
mCameraFaceFront = false;
}
public void switchToSoftEncoder() {
useSoftEncoder = true;
}
public void switchToHardEncoder() {
useSoftEncoder = false;
}
public boolean isSoftEncoder() {
return useSoftEncoder;
}
public boolean canHardEncode() {
return vencoder != null;
}
public boolean canSoftEncode() {
return canSoftEncode;
}
public boolean isEnabled() {
return canHardEncode() || canSoftEncode();
}
public void setPreviewResolution(int width, int height) {
vPrevWidth = width;
vPrevHeight = height;
}
public void setPortraitResolution(int width, int height) {
vOutWidth = width;
vOutHeight = height;
vPortraitWidth = width;
vPortraitHeight = height;
vLandscapeWidth = height;
vLandscapeHeight = width;
}
public void setLandscapeResolution(int width, int height) {
vOutWidth = width;
vOutHeight = height;
vLandscapeWidth = width;
vLandscapeHeight = height;
vPortraitWidth = height;
vPortraitHeight = width;
}
public void setVideoHDMode() {
vBitrate = 1200 * 1024; // 1200 kbps
x264Preset = "veryfast";
}
public void setVideoSmoothMode() {
vBitrate = 500 * 1024; // 500 kbps
x264Preset = "superfast";
}
public int getPreviewWidth() {
return vPrevWidth;
}
public int getPreviewHeight() {
return vPrevHeight;
}
public int getOutputWidth() {
return vOutWidth;
}
public int getOutputHeight() {
return vOutHeight;
}
public void setScreenOrientation(int orientation) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
vOutWidth = vPortraitWidth;
vOutHeight = vPortraitHeight;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
vOutWidth = vLandscapeWidth;
vOutHeight = vLandscapeHeight;
}
// Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
// Since Y component is quadruple size as U and V component, the stride must be set as 32x
if (!useSoftEncoder && (vOutWidth % 32 != 0 || vOutHeight % 32 != 0)) {
if (vmci.getName().contains("MTK")) {
//throw new AssertionError("MTK encoding revolution stride must be 32x");
}
}
setEncoderResolution(vOutWidth, vOutHeight);
}
private void onProcessedYuvFrame(byte[] yuvFrame, long pts) {
ByteBuffer[] inBuffers = vencoder.getInputBuffers();
ByteBuffer[] outBuffers = vencoder.getOutputBuffers();
int inBufferIndex = vencoder.dequeueInputBuffer(-1);
if (inBufferIndex >= 0) {
ByteBuffer bb = inBuffers[inBufferIndex];
bb.clear();
bb.put(yuvFrame, 0, yuvFrame.length);
vencoder.queueInputBuffer(inBufferIndex, 0, yuvFrame.length, pts, 0);
}
for (; ; ) {
MediaCodec.BufferInfo vebi = new MediaCodec.BufferInfo();
int outBufferIndex = vencoder.dequeueOutputBuffer(vebi, 0);
if (outBufferIndex >= 0) {
ByteBuffer bb = outBuffers[outBufferIndex];
onEncodedAnnexbFrame(bb, vebi);
vencoder.releaseOutputBuffer(outBufferIndex, false);
} else {
break;
}
}
}
private void onSoftEncodedData(byte[] es, long pts, boolean isKeyFrame) {
ByteBuffer bb = ByteBuffer.wrap(es);
MediaCodec.BufferInfo vebi = new MediaCodec.BufferInfo();
vebi.offset = 0;
vebi.size = es.length;
vebi.presentationTimeUs = pts;
vebi.flags = isKeyFrame ? MediaCodec.BUFFER_FLAG_KEY_FRAME : 0;
onEncodedAnnexbFrame(bb, vebi);
}
// when got encoded h264 es stream.
private void onEncodedAnnexbFrame(ByteBuffer es, MediaCodec.BufferInfo bi) {
mp4Muxer.writeSampleData(videoMp4Track, es.duplicate(), bi);
flvMuxer.writeSampleData(videoFlvTrack, es, bi);
}
// when got encoded aac raw stream.
private void onEncodedAacFrame(ByteBuffer es, MediaCodec.BufferInfo bi) {
mp4Muxer.writeSampleData(audioMp4Track, es.duplicate(), bi);
flvMuxer.writeSampleData(audioFlvTrack, es, bi);
}
public void onGetPcmFrame(byte[] data, int size) {
if(mPausetime > 0){
return;
}
// Check video frame cache number to judge the networking situation.
// Just cache GOP / FPS seconds data according to latency.
AtomicInteger videoFrameCacheNumber = flvMuxer.getVideoFrameCacheNumber();
if (videoFrameCacheNumber != null && videoFrameCacheNumber.get() < VGOP / 2) {
ByteBuffer[] inBuffers = aencoder.getInputBuffers();
ByteBuffer[] outBuffers = aencoder.getOutputBuffers();
int inBufferIndex = aencoder.dequeueInputBuffer(-1);
if (inBufferIndex >= 0) {
ByteBuffer bb = inBuffers[inBufferIndex];
bb.clear();
bb.put(data, 0, size);
long pts = System.nanoTime() / 1000 - mPresentTimeUs;
aencoder.queueInputBuffer(inBufferIndex, 0, size, pts, 0);
}
for (; ; ) {
MediaCodec.BufferInfo aebi = new MediaCodec.BufferInfo();
int outBufferIndex = aencoder.dequeueOutputBuffer(aebi, 0);
if (outBufferIndex >= 0) {
ByteBuffer bb = outBuffers[outBufferIndex];
onEncodedAacFrame(bb, aebi);
aencoder.releaseOutputBuffer(outBufferIndex, false);
} else {
break;
}
}
}
}
public void onGetRgbaFrame(byte[] data, int width, int height) {
if(mPausetime > 0){
return;
}
// Check video frame cache number to judge the networking situation.
// Just cache GOP / FPS seconds data according to latency.
AtomicInteger videoFrameCacheNumber = flvMuxer.getVideoFrameCacheNumber();
if (videoFrameCacheNumber != null && videoFrameCacheNumber.get() < VGOP / 2) {
long pts = System.nanoTime() / 1000 - mPresentTimeUs;
if (useSoftEncoder) {
swRgbaFrame(data, width, height, pts);
} else {
byte[] processedData = hwRgbaFrame(data, width, height);
if (processedData != null) {
onProcessedYuvFrame(processedData, pts);
} else {
mHandler.notifyEncodeIllegalArgumentException(new IllegalArgumentException("libyuv failure"));
}
}
if (networkWeakTriggered) {
networkWeakTriggered = false;
mHandler.notifyNetworkResume();
}
} else {
mHandler.notifyNetworkWeak();
networkWeakTriggered = true;
}
}
public void onGetYuvNV21Frame(byte[] data, int width, int height, Rect boundingBox) {
// Check video frame cache number to judge the networking situation.
// Just cache GOP / FPS seconds data according to latency.
AtomicInteger videoFrameCacheNumber = flvMuxer.getVideoFrameCacheNumber();
if (videoFrameCacheNumber != null && videoFrameCacheNumber.get() < VGOP / 2) {
long pts = System.nanoTime() / 1000 - mPresentTimeUs;
if (useSoftEncoder) {
throw new UnsupportedOperationException("Not implemented");
//swRgbaFrame(data, width, height, pts);
} else {
byte[] processedData = hwYUVNV21FrameScaled(data, width, height, boundingBox);
if (processedData != null) {
onProcessedYuvFrame(processedData, pts);
} else {
mHandler.notifyEncodeIllegalArgumentException(new IllegalArgumentException("libyuv failure"));
}
}
if (networkWeakTriggered) {
networkWeakTriggered = false;
mHandler.notifyNetworkResume();
}
} else {
mHandler.notifyNetworkWeak();
networkWeakTriggered = true;
}
}
public void onGetArgbFrame(int[] data, int width, int height, Rect boundingBox) {
// Check video frame cache number to judge the networking situation.
// Just cache GOP / FPS seconds data according to latency.
AtomicInteger videoFrameCacheNumber = flvMuxer.getVideoFrameCacheNumber();
if (videoFrameCacheNumber != null && videoFrameCacheNumber.get() < VGOP / 2) {
long pts = System.nanoTime() / 1000 - mPresentTimeUs;
if (useSoftEncoder) {
throw new UnsupportedOperationException("Not implemented");
//swArgbFrame(data, width, height, pts);
} else {
byte[] processedData = hwArgbFrameScaled(data, width, height, boundingBox);
if (processedData != null) {
onProcessedYuvFrame(processedData, pts);
} else {
mHandler.notifyEncodeIllegalArgumentException(new IllegalArgumentException("libyuv failure"));
}
}
if (networkWeakTriggered) {
networkWeakTriggered = false;
mHandler.notifyNetworkResume();
}
} else {
mHandler.notifyNetworkWeak();
networkWeakTriggered = true;
}
}
public void onGetArgbFrame(int[] data, int width, int height) {
// Check video frame cache number to judge the networking situation.
// Just cache GOP / FPS seconds data according to latency.
AtomicInteger videoFrameCacheNumber = flvMuxer.getVideoFrameCacheNumber();
if (videoFrameCacheNumber != null && videoFrameCacheNumber.get() < VGOP / 2) {
long pts = System.nanoTime() / 1000 - mPresentTimeUs;
if (useSoftEncoder) {
throw new UnsupportedOperationException("Not implemented");
//swArgbFrame(data, width, height, pts);
} else {
byte[] processedData = hwArgbFrame(data, width, height);
if (processedData != null) {
onProcessedYuvFrame(processedData, pts);
} else {
mHandler.notifyEncodeIllegalArgumentException(new IllegalArgumentException("libyuv failure"));
}
}
if (networkWeakTriggered) {
networkWeakTriggered = false;
mHandler.notifyNetworkResume();
}
} else {
mHandler.notifyNetworkWeak();
networkWeakTriggered = true;
}
}
private byte[] hwRgbaFrame(byte[] data, int width, int height) {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
return RGBAToI420(data, width, height, true, 180);
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
return RGBAToNV12(data, width, height, true, 180);
default:
throw new IllegalStateException("Unsupported color format!");
}
}
private byte[] hwYUVNV21FrameScaled(byte[] data, int width, int height, Rect boundingBox) {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
return NV21ToI420Scaled(data, width, height, true, 180, boundingBox.left, boundingBox.top, boundingBox.width(), boundingBox.height());
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
return NV21ToNV12Scaled(data, width, height, true, 180, boundingBox.left, boundingBox.top, boundingBox.width(), boundingBox.height());
default:
throw new IllegalStateException("Unsupported color format!");
}
}
private byte[] hwArgbFrameScaled(int[] data, int width, int height, Rect boundingBox) {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
return ARGBToI420Scaled(data, width, height, false, 0, boundingBox.left, boundingBox.top, boundingBox.width(), boundingBox.height());
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
return ARGBToNV12Scaled(data, width, height, false, 0, boundingBox.left, boundingBox.top, boundingBox.width(), boundingBox.height());
default:
throw new IllegalStateException("Unsupported color format!");
}
}
private byte[] hwArgbFrame(int[] data, int inputWidth, int inputHeight) {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
return ARGBToI420(data, inputWidth, inputHeight, false, 0);
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
return ARGBToNV12(data, inputWidth, inputHeight, false, 0);
default:
throw new IllegalStateException("Unsupported color format!");
}
}
private void swRgbaFrame(byte[] data, int width, int height, long pts) {
RGBASoftEncode(data, width, height, true, 180, pts);
}
@SuppressLint("MissingPermission")
public AudioRecord chooseAudioRecord() {
AudioRecord mic = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SrsEncoder.ASAMPLERATE,
AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, getPcmBufferSize() * 4);
if (mic.getState() != AudioRecord.STATE_INITIALIZED) {
mic = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SrsEncoder.ASAMPLERATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, getPcmBufferSize() * 4);
if (mic.getState() != AudioRecord.STATE_INITIALIZED) {
mic = null;
} else {
SrsEncoder.aChannelConfig = AudioFormat.CHANNEL_IN_MONO;
}
} else {
SrsEncoder.aChannelConfig = AudioFormat.CHANNEL_IN_STEREO;
}
return mic;
}
private int getPcmBufferSize() {
int pcmBufSize = AudioRecord.getMinBufferSize(ASAMPLERATE, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT) + 8191;
return pcmBufSize - (pcmBufSize % 8192);
}
// choose the video encoder by name.
private MediaCodecInfo chooseVideoEncoder(String name) {
int nbCodecs = MediaCodecList.getCodecCount();
for (int i = 0; i < nbCodecs; i++) {
MediaCodecInfo mci = MediaCodecList.getCodecInfoAt(i);
if (!mci.isEncoder()) {
continue;
}
String[] types = mci.getSupportedTypes();
for (int j = 0; j < types.length; j++) {
if (types[j].equalsIgnoreCase(VCODEC)) {
Log.i(TAG, String.format("vencoder %s types: %s", mci.getName(), types[j]));
if (name == null) {
return mci;
}
if (mci.getName().contains(name)) {
return mci;
}
}
}
}
return null;
}
// choose the right supported color format. @see below:
private int chooseVideoEncoder() {
// choose the encoder "video/avc":
// 1. select default one when type matched.
// 2. google avc is unusable.
// 3. choose qcom avc.
vmci = chooseVideoEncoder(null);
//vmci = chooseVideoEncoder("google");
//vmci = chooseVideoEncoder("qcom");
int matchedColorFormat = 0;
MediaCodecInfo.CodecCapabilities cc = vmci.getCapabilitiesForType(VCODEC);
for (int i = 0; i < cc.colorFormats.length; i++) {
int cf = cc.colorFormats[i];
Log.i(TAG, String.format("vencoder %s supports color fomart 0x%x(%d)", vmci.getName(), cf, cf));
// choose YUV for h.264, prefer the bigger one.
// corresponding to the color space transform in onPreviewFrame
if (cf >= cc.COLOR_FormatYUV420Planar && cf <= cc.COLOR_FormatYUV420SemiPlanar) {
if (cf > matchedColorFormat) {
matchedColorFormat = cf;
}
}
}
for (int i = 0; i < cc.profileLevels.length; i++) {
MediaCodecInfo.CodecProfileLevel pl = cc.profileLevels[i];
Log.i(TAG, String.format("vencoder %s support profile %d, level %d", vmci.getName(), pl.profile, pl.level));
}
Log.i(TAG, String.format("vencoder %s choose color format 0x%x(%d)", vmci.getName(), matchedColorFormat, matchedColorFormat));
return matchedColorFormat;
}
private native void setEncoderResolution(int outWidth, int outHeight);
private native void setEncoderFps(int fps);
private native void setEncoderGop(int gop);
private native void setEncoderBitrate(int bitrate);
private native void setEncoderPreset(String preset);
private native byte[] RGBAToI420(byte[] frame, int width, int height, boolean flip, int rotate);
private native byte[] RGBAToNV12(byte[] frame, int width, int height, boolean flip, int rotate);
private native byte[] ARGBToI420Scaled(int[] frame, int width, int height, boolean flip, int rotate, int crop_x, int crop_y,int crop_width, int crop_height);
private native byte[] ARGBToNV12Scaled(int[] frame, int width, int height, boolean flip, int rotate, int crop_x, int crop_y,int crop_width, int crop_height);
private native byte[] ARGBToI420(int[] frame, int width, int height, boolean flip, int rotate);
private native byte[] ARGBToNV12(int[] frame, int width, int height, boolean flip, int rotate);
private native byte[] NV21ToNV12Scaled(byte[] frame, int width, int height, boolean flip, int rotate, int crop_x, int crop_y,int crop_width, int crop_height);
private native byte[] NV21ToI420Scaled(byte[] frame, int width, int height, boolean flip, int rotate, int crop_x, int crop_y,int crop_width, int crop_height);
private native int RGBASoftEncode(byte[] frame, int width, int height, boolean flip, int rotate, long pts);
private native boolean openSoftEncoder();
private native void closeSoftEncoder();
static {
System.loadLibrary("yuv");
System.loadLibrary("enc");
}
}