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

Added ability to not record audio through session config. Only tested wi... #7

Open
wants to merge 1 commit into
base: preview
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions sdk/src/main/java/io/kickflip/sdk/av/AVRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ public AVRecorder(SessionConfig config){

private void init(SessionConfig config){
mCamEncoder = new CameraEncoder(config);
mMicEncoder = new MicrophoneEncoder(config);

if(config.getRecordAudio())
mMicEncoder = new MicrophoneEncoder(config);
else
mMicEncoder = null;

mConfig = config;
setupMuxer();
mIsRecording = false;
}

Expand Down Expand Up @@ -71,7 +77,8 @@ public void signalVerticalVideo(FullFrameRect.SCREEN_ROTATION orientation) {

public void startRecording(){
mIsRecording = true;
mMicEncoder.startRecording();
if(mMicEncoder != null)
mMicEncoder.startRecording();
mCamEncoder.startRecording();
}

Expand All @@ -81,7 +88,8 @@ public boolean isRecording(){

public void stopRecording(){
mIsRecording = false;
mMicEncoder.stopRecording();
if(mMicEncoder != null)
mMicEncoder.stopRecording();
mCamEncoder.stopRecording();
}

Expand All @@ -92,11 +100,31 @@ public void stopRecording(){
*/
public void reset(SessionConfig config){
mCamEncoder.reset(config);
mMicEncoder.reset(config);

if(config.getRecordAudio()) {
if(mMicEncoder == null)
mMicEncoder = new MicrophoneEncoder(config);
else
mMicEncoder.reset(config);
}
else {
mMicEncoder = null;
}

mConfig = config;
setupMuxer();
mIsRecording = false;
}

private void setupMuxer() {
if(mConfig.getRecordAudio()) {
mConfig.getMuxer().setExpectedNumTracks(2);
}
else {
mConfig.getMuxer().setExpectedNumTracks(1);
}
}

/**
* Release resources. Must be called after {@link #stopRecording()} After this call
* this instance may no longer be used.
Expand Down
43 changes: 24 additions & 19 deletions sdk/src/main/java/io/kickflip/sdk/av/AndroidMuxer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,36 @@ public boolean isStarted() {
@Override
public void writeSampleData(MediaCodec encoder, int trackIndex, int bufferIndex, ByteBuffer encodedData, MediaCodec.BufferInfo bufferInfo) {
super.writeSampleData(encoder, trackIndex, bufferIndex, encodedData, bufferInfo);
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
// MediaMuxer gets the codec config info via the addTrack command
if (VERBOSE) Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");
encoder.releaseOutputBuffer(bufferIndex, false);
return;
}

if(bufferInfo.size == 0){
if(VERBOSE) Log.d(TAG, "ignoring zero size buffer");
encoder.releaseOutputBuffer(bufferIndex, false);
return;
}
//Use 'do' so we can have break functionality and check if all tracks are finished after everything
do {
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
// MediaMuxer gets the codec config info via the addTrack command
if (VERBOSE) Log.d(TAG, "ignoring BUFFER_FLAG_CODEC_CONFIG");
encoder.releaseOutputBuffer(bufferIndex, false);
break;
}

if (!mStarted) {
Log.e(TAG, "writeSampleData called before muxer started. Ignoring packet. Track index: " + trackIndex + " tracks added: " + mNumTracks);
encoder.releaseOutputBuffer(bufferIndex, false);
return;
}
if (bufferInfo.size == 0) {
if (VERBOSE) Log.d(TAG, "ignoring zero size buffer");
encoder.releaseOutputBuffer(bufferIndex, false);
break;
}

bufferInfo.presentationTimeUs = getSafePts(bufferInfo.presentationTimeUs);
mMuxer.writeSampleData(trackIndex, encodedData, bufferInfo);
if (!mStarted) {
Log.e(TAG, "writeSampleData called before muxer started. Ignoring packet. Track index: " + trackIndex + " tracks added: " + mNumTracks);
encoder.releaseOutputBuffer(bufferIndex, false);
break;
}

encoder.releaseOutputBuffer(bufferIndex, false);
bufferInfo.presentationTimeUs = getSafePts(bufferInfo.presentationTimeUs);
mMuxer.writeSampleData(trackIndex, encodedData, bufferInfo);

encoder.releaseOutputBuffer(bufferIndex, false);
} while(false);

if(allTracksFinished()){
if (VERBOSE) Log.d(TAG, "All tracks finished, stopping");
stop();
}
}
Expand Down
11 changes: 10 additions & 1 deletion sdk/src/main/java/io/kickflip/sdk/av/AudioEncoderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ public class AudioEncoderConfig {
protected final int mNumChannels;
protected final int mSampleRate;
protected final int mBitrate;
protected final boolean mRecordAudio;

public AudioEncoderConfig(int channels, int sampleRate, int bitRate) {
this(channels, sampleRate, bitRate, true);
}

public AudioEncoderConfig(int channels, int sampleRate, int bitRate, boolean recordAudio) {
mNumChannels = channels;
mBitrate = bitRate;
mSampleRate = sampleRate;
mRecordAudio = recordAudio;
}

public int getNumChannels() {
Expand All @@ -28,6 +34,9 @@ public int getBitrate() {

@Override
public String toString() {
return "AudioEncoderConfig: " + mNumChannels + " channels totaling " + mBitrate + " bps @" + mSampleRate + " Hz";
if(mRecordAudio)
return "AudioEncoderConfig: " + mNumChannels + " channels totaling " + mBitrate + " bps @" + mSampleRate + " Hz";
else
return "AudioEncoderConfig: Don't record audio";
}
}
10 changes: 9 additions & 1 deletion sdk/src/main/java/io/kickflip/sdk/av/Muxer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class Muxer {

public static enum FORMAT { MPEG4, HLS, RTMP }

private final int mExpectedNumTracks = 2; // TODO: Make this configurable?
private int mExpectedNumTracks;

protected FORMAT mFormat;
protected String mOutputPath;
Expand All @@ -39,6 +39,7 @@ protected Muxer(String outputPath, FORMAT format){
mNumTracks = 0;
mNumTracksFinished = 0;
mLastPts = 0;
mExpectedNumTracks = 2;
}

public void setEventBus(EventBus eventBus){
Expand Down Expand Up @@ -117,6 +118,13 @@ public void signalEndOfTrack(){
mNumTracksFinished++;
}

/**
* Set the number of tracks. For example 2 for audio + video. Or just 1 for video only.
* Default is 2 on construction.
* @param numTracks
*/
public void setExpectedNumTracks(int numTracks) { mExpectedNumTracks = numTracks; }

/**
* Does this Muxer's format require AAC ADTS headers?
* see http://wiki.multimedia.cx/index.php?title=ADTS
Expand Down
12 changes: 11 additions & 1 deletion sdk/src/main/java/io/kickflip/sdk/av/SessionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public int getHlsSegmentDuration() {
return mHlsSegmentDuration;
}

public boolean getRecordAudio() { return mAudioConfig.mRecordAudio; }

public void setUseAdaptiveBitrate(boolean useAdaptiveBit) {
mIsAdaptiveBitrate = useAdaptiveBit;
}
Expand Down Expand Up @@ -189,6 +191,7 @@ public static class Builder {
private boolean mConvertVerticalVideo;
private boolean mAdaptiveStreaming;
private Map mExtraInfo;
private boolean mRecordAudio;

private int mHlsSegmentDuration;

Expand Down Expand Up @@ -273,6 +276,8 @@ private void setAVDefaults() {
mAudioSamplerate = 44100;
mAudioBitrate = 96 * 1000;
mNumAudioChannels = 1;

mRecordAudio = true;
}

private void setMetaDefaults() {
Expand Down Expand Up @@ -356,10 +361,15 @@ public Builder withHlsSegmentDuration(int segmentDuration) {
return this;
}

public Builder withRecordAudio(boolean recordAudio) {
mRecordAudio = recordAudio;
return this;
}

public SessionConfig build() {
SessionConfig session = new SessionConfig(mUUID, mMuxer,
new VideoEncoderConfig(mWidth, mHeight, mVideoBitrate),
new AudioEncoderConfig(mNumAudioChannels, mAudioSamplerate, mAudioBitrate));
new AudioEncoderConfig(mNumAudioChannels, mAudioSamplerate, mAudioBitrate, mRecordAudio));

session.setTitle(mTitle);
session.setDescription(mDescription);
Expand Down