Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SrsEncoder video reconfigure on fly #836

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 72 additions & 13 deletions library/src/main/java/net/ossrs/yasea/SrsEncoder.java
Expand Up @@ -62,6 +62,9 @@ public class SrsEncoder {
private int videoMp4Track;
private int audioFlvTrack;
private int audioMp4Track;

private boolean reconfiguringVideo = false;
private int requestReconfigure = 0;

// Y, U (Cb) and V (Cr)
// yuv420 yuv yuv yuv yuv
Expand Down Expand Up @@ -142,34 +145,80 @@ public boolean start() {
audioFlvTrack = flvMuxer.addTrack(audioFormat);
audioMp4Track = mp4Muxer.addTrack(audioFormat);

MediaFormat videoFormat = videoCodecConfigure();

if(videoFormat == null){
return false;
}


// add the video tracker to muxer.
videoFlvTrack = flvMuxer.addTrack(videoFormat);
videoMp4Track = mp4Muxer.addTrack(videoFormat);

// start device and encoder.
vencoder.start();
aencoder.start();
return true;
}

private MediaFormat videoCodecConfigure(){
// 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;
Log.d(TAG, "VideoCodec configure");

setEncoderResolution(vOutWidth, vOutHeight);


if(!reconfiguringVideo || vencoder == null) {
try {
vencoder = MediaCodec.createByCodecName(vmci.getName());
} catch (IOException e) {
Log.e(TAG, "create vencoder failed.");
e.printStackTrace();
return null;
}
}

// 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.
flvMuxer.addTrack(videoFormat);

return videoFormat;
}


public void videoCodecReConfigure(){


if(requestReconfigure == 0){
Log.e(TAG, "Request reconfigure");
requestReconfigure = 1;
return;
}

if(requestReconfigure == 1){
throw new IllegalStateException("reconfiguring");
}

Log.d(TAG, "VideoCodec reconfigure: " + vOutWidth +"x"+ vOutHeight);
reconfiguringVideo = true;

vencoder.reset();
videoCodecConfigure();
vencoder.start();
aencoder.start();
return true;
}
requestReconfigure = 0;

}

public void pause(){
mPausetime = System.nanoTime() / 1000;
Expand Down Expand Up @@ -328,6 +377,16 @@ private void onProcessedYuvFrame(byte[] yuvFrame, long pts) {
ByteBuffer bb = outBuffers[outBufferIndex];
onEncodedAnnexbFrame(bb, vebi);
vencoder.releaseOutputBuffer(outBufferIndex, false);

if(vebi.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME){
if(requestReconfigure == 1){
//reconfigure when all P (predicred) frames have been sent
requestReconfigure = 2;
videoCodecReConfigure();
}

}

} else {
break;
}
Expand Down