From cc134a035d377d860290f396205984fceeb7c1cd Mon Sep 17 00:00:00 2001 From: pedro Date: Tue, 26 Sep 2023 10:21:02 +0200 Subject: [PATCH] avoid access to internal interfaces --- .../com/pedro/library/base/Camera1Base.java | 101 +++++++++--------- .../com/pedro/library/base/Camera2Base.java | 81 +++++++------- .../com/pedro/library/base/DisplayBase.java | 79 +++++++------- .../com/pedro/library/base/FromFileBase.java | 77 ++++++------- .../com/pedro/library/base/OnlyAudioBase.java | 49 ++++----- .../java/com/pedro/library/base/StreamBase.kt | 49 +++++---- .../base/recording/RecordController.java | 2 +- 7 files changed, 228 insertions(+), 210 deletions(-) diff --git a/library/src/main/java/com/pedro/library/base/Camera1Base.java b/library/src/main/java/com/pedro/library/base/Camera1Base.java index 9dd76267d..e9c5f409b 100644 --- a/library/src/main/java/com/pedro/library/base/Camera1Base.java +++ b/library/src/main/java/com/pedro/library/base/Camera1Base.java @@ -78,8 +78,7 @@ * Created by pedro on 7/07/17. */ -public abstract class Camera1Base - implements GetAacData, GetCameraData, GetVideoData, GetMicrophoneData { +public abstract class Camera1Base { private static final String TAG = "Camera1Base"; @@ -98,13 +97,13 @@ public abstract class Camera1Base public Camera1Base(SurfaceView surfaceView) { context = surfaceView.getContext(); - cameraManager = new Camera1ApiManager(surfaceView, this); + cameraManager = new Camera1ApiManager(surfaceView, getCameraData); init(); } public Camera1Base(TextureView textureView) { context = textureView.getContext(); - cameraManager = new Camera1ApiManager(textureView, this); + cameraManager = new Camera1ApiManager(textureView, getCameraData); init(); } @@ -136,7 +135,7 @@ public Camera1Base(Context context) { } private void init() { - videoEncoder = new VideoEncoder(this); + videoEncoder = new VideoEncoder(getVideoData); setMicrophoneMode(MicrophoneMode.ASYNC); recordController = new AndroidMuxerRecordController(); } @@ -152,18 +151,18 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) { switch (microphoneMode) { case SYNC: microphoneManager = new MicrophoneManagerManual(); - audioEncoder = new AudioEncoder(this); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setGetFrame(((MicrophoneManagerManual) microphoneManager).getGetFrame()); audioEncoder.setTsModeBuffer(false); break; case ASYNC: - microphoneManager = new MicrophoneManager(this); - audioEncoder = new AudioEncoder(this); + microphoneManager = new MicrophoneManager(getMicrophoneData); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setTsModeBuffer(false); break; case BUFFER: - microphoneManager = new MicrophoneManager(this); - audioEncoder = new AudioEncoder(this); + microphoneManager = new MicrophoneManager(getMicrophoneData); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setTsModeBuffer(true); break; } @@ -1010,57 +1009,59 @@ public RecordController.Status getRecordStatus() { protected abstract void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info); - @Override - public void getAacData(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - recordController.recordAudio(aacBuffer, info); - } - if (streaming) getAacDataRtp(aacBuffer, info); - } - protected abstract void onSpsPpsVpsRtp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps); - @Override - public void onSpsPpsVps(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps) { - onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); - } - protected abstract void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info); - @Override - public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { - fpsListener.calculateFps(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - recordController.recordVideo(h264Buffer, info); - } - if (streaming) getH264DataRtp(h264Buffer, info); + public void setRecordController(BaseRecordController recordController) { + if (!isRecording()) this.recordController = recordController; } - @Override - public void inputPCMData(Frame frame) { - audioEncoder.inputPCMData(frame); - } + public abstract void setLogs(boolean enable); + + public abstract void setCheckServerAlive(boolean enable); - @Override - public void inputYUVData(Frame frame) { + private final GetCameraData getCameraData = frame -> { videoEncoder.inputYUVData(frame); - } + }; - @Override - public void onVideoFormat(MediaFormat mediaFormat) { - recordController.setVideoFormat(mediaFormat, !audioInitialized); - } + private final GetMicrophoneData getMicrophoneData = frame -> { + audioEncoder.inputPCMData(frame); + }; - @Override - public void onAudioFormat(MediaFormat mediaFormat) { - recordController.setAudioFormat(mediaFormat); - } + private final GetAacData getAacData = new GetAacData() { + @Override + public void getAacData(@NonNull ByteBuffer aacBuffer, @NonNull MediaCodec.BufferInfo info) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + recordController.recordAudio(aacBuffer, info); + } + if (streaming) getAacDataRtp(aacBuffer, info); + } - public void setRecordController(BaseRecordController recordController) { - if (!isRecording()) this.recordController = recordController; - } + @Override + public void onAudioFormat(@NonNull MediaFormat mediaFormat) { + recordController.setAudioFormat(mediaFormat); + } + }; - public abstract void setLogs(boolean enable); + private final GetVideoData getVideoData = new GetVideoData() { + @Override + public void onSpsPpsVps(@NonNull ByteBuffer sps, @NonNull ByteBuffer pps, @Nullable ByteBuffer vps) { + onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); + } - public abstract void setCheckServerAlive(boolean enable); + @Override + public void getVideoData(@NonNull ByteBuffer h264Buffer, @NonNull MediaCodec.BufferInfo info) { + fpsListener.calculateFps(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + recordController.recordVideo(h264Buffer, info); + } + if (streaming) getH264DataRtp(h264Buffer, info); + } + + @Override + public void onVideoFormat(@NonNull MediaFormat mediaFormat) { + recordController.setVideoFormat(mediaFormat, !audioInitialized); + } + }; } \ No newline at end of file diff --git a/library/src/main/java/com/pedro/library/base/Camera2Base.java b/library/src/main/java/com/pedro/library/base/Camera2Base.java index 11b01097f..2824da728 100644 --- a/library/src/main/java/com/pedro/library/base/Camera2Base.java +++ b/library/src/main/java/com/pedro/library/base/Camera2Base.java @@ -78,7 +78,7 @@ * Created by pedro on 7/07/17. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public abstract class Camera2Base implements GetAacData, GetVideoData, GetMicrophoneData { +public abstract class Camera2Base { private static final String TAG = "Camera2Base"; @@ -148,7 +148,7 @@ public Camera2Base(Context context, boolean useOpengl) { private void init(Context context) { cameraManager = new Camera2ApiManager(context); - videoEncoder = new VideoEncoder(this); + videoEncoder = new VideoEncoder(getVideoData); setMicrophoneMode(MicrophoneMode.ASYNC); recordController = new AndroidMuxerRecordController(); } @@ -164,18 +164,18 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) { switch (microphoneMode) { case SYNC: microphoneManager = new MicrophoneManagerManual(); - audioEncoder = new AudioEncoder(this); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setGetFrame(((MicrophoneManagerManual) microphoneManager).getGetFrame()); audioEncoder.setTsModeBuffer(false); break; case ASYNC: - microphoneManager = new MicrophoneManager(this); - audioEncoder = new AudioEncoder(this); + microphoneManager = new MicrophoneManager(getMicrophoneData); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setTsModeBuffer(false); break; case BUFFER: - microphoneManager = new MicrophoneManager(this); - audioEncoder = new AudioEncoder(this); + microphoneManager = new MicrophoneManager(getMicrophoneData); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setTsModeBuffer(true); break; } @@ -1060,48 +1060,51 @@ public boolean isOnPreview() { protected abstract void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info); - @Override - public void getAacData(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { - recordController.recordAudio(aacBuffer, info); - if (streaming) getAacDataRtp(aacBuffer, info); - } - protected abstract void onSpsPpsVpsRtp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps); - @Override - public void onSpsPpsVps(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps) { - onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); - } - protected abstract void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info); - @Override - public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { - fpsListener.calculateFps(); - recordController.recordVideo(h264Buffer, info); - if (streaming) getH264DataRtp(h264Buffer, info); + public void setRecordController(BaseRecordController recordController) { + if (!isRecording()) this.recordController = recordController; } - @Override - public void inputPCMData(Frame frame) { + public abstract void setLogs(boolean enable); + + public abstract void setCheckServerAlive(boolean enable); + + private final GetMicrophoneData getMicrophoneData = frame -> { audioEncoder.inputPCMData(frame); - } + }; - @Override - public void onVideoFormat(MediaFormat mediaFormat) { - recordController.setVideoFormat(mediaFormat, !audioInitialized); - } + private final GetAacData getAacData = new GetAacData() { + @Override + public void getAacData(@NonNull ByteBuffer aacBuffer, @NonNull MediaCodec.BufferInfo info) { + recordController.recordAudio(aacBuffer, info); + if (streaming) getAacDataRtp(aacBuffer, info); + } - @Override - public void onAudioFormat(MediaFormat mediaFormat) { - recordController.setAudioFormat(mediaFormat); - } + @Override + public void onAudioFormat(@NonNull MediaFormat mediaFormat) { + recordController.setAudioFormat(mediaFormat); + } + }; - public void setRecordController(BaseRecordController recordController) { - if (!isRecording()) this.recordController = recordController; - } + private final GetVideoData getVideoData = new GetVideoData() { + @Override + public void onSpsPpsVps(@NonNull ByteBuffer sps, @NonNull ByteBuffer pps, @Nullable ByteBuffer vps) { + onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); + } - public abstract void setLogs(boolean enable); + @Override + public void getVideoData(@NonNull ByteBuffer h264Buffer, @NonNull MediaCodec.BufferInfo info) { + fpsListener.calculateFps(); + recordController.recordVideo(h264Buffer, info); + if (streaming) getH264DataRtp(h264Buffer, info); + } - public abstract void setCheckServerAlive(boolean enable); + @Override + public void onVideoFormat(@NonNull MediaFormat mediaFormat) { + recordController.setVideoFormat(mediaFormat, !audioInitialized); + } + }; } diff --git a/library/src/main/java/com/pedro/library/base/DisplayBase.java b/library/src/main/java/com/pedro/library/base/DisplayBase.java index f2b9d8778..bcf3ef381 100644 --- a/library/src/main/java/com/pedro/library/base/DisplayBase.java +++ b/library/src/main/java/com/pedro/library/base/DisplayBase.java @@ -70,7 +70,7 @@ * Created by pedro on 9/08/17. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -public abstract class DisplayBase implements GetAacData, GetVideoData, GetMicrophoneData { +public abstract class DisplayBase { private OffScreenGlThread glInterface; private MediaProjection mediaProjection; @@ -96,8 +96,8 @@ public DisplayBase(Context context, boolean useOpengl) { mediaProjectionManager = ((MediaProjectionManager) context.getSystemService(MEDIA_PROJECTION_SERVICE)); this.surfaceView = null; - videoEncoder = new VideoEncoder(this); - audioEncoder = new AudioEncoder(this); + videoEncoder = new VideoEncoder(getVideoData); + audioEncoder = new AudioEncoder(getAacData); //Necessary use same thread to read input buffer and encode it with internal audio or audio is choppy. setMicrophoneMode(MicrophoneMode.SYNC); recordController = new AndroidMuxerRecordController(); @@ -114,12 +114,12 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) { switch (microphoneMode) { case SYNC: microphoneManager = new MicrophoneManagerManual(); - audioEncoder = new AudioEncoder(this); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setGetFrame(((MicrophoneManagerManual) microphoneManager).getGetFrame()); break; case ASYNC: - microphoneManager = new MicrophoneManager(this); - audioEncoder = new AudioEncoder(this); + microphoneManager = new MicrophoneManager(getMicrophoneData); + audioEncoder = new AudioEncoder(getAacData); break; } } @@ -614,49 +614,52 @@ public RecordController.Status getRecordStatus() { protected abstract void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info); - @Override - public void getAacData(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { - recordController.recordAudio(aacBuffer, info); - if (streaming) getAacDataRtp(aacBuffer, info); - } - protected abstract void onSpsPpsVpsRtp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps); - @Override - public void onSpsPpsVps(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps) { - onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); - } - protected abstract void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info); - @Override - public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { - fpsListener.calculateFps(); - recordController.recordVideo(h264Buffer, info); - if (streaming) getH264DataRtp(h264Buffer, info); + public void setRecordController(BaseRecordController recordController) { + if (!isRecording()) this.recordController = recordController; } - @Override - public void inputPCMData(Frame frame) { + public abstract void setLogs(boolean enable); + + public abstract void setCheckServerAlive(boolean enable); + + private final GetMicrophoneData getMicrophoneData = frame -> { audioEncoder.inputPCMData(frame); - } + }; - @Override - public void onVideoFormat(MediaFormat mediaFormat) { - recordController.setVideoFormat(mediaFormat, !audioInitialized); - } + private final GetAacData getAacData = new GetAacData() { + @Override + public void getAacData(@NonNull ByteBuffer aacBuffer, @NonNull MediaCodec.BufferInfo info) { + recordController.recordAudio(aacBuffer, info); + if (streaming) getAacDataRtp(aacBuffer, info); + } - @Override - public void onAudioFormat(MediaFormat mediaFormat) { - recordController.setAudioFormat(mediaFormat); - } + @Override + public void onAudioFormat(@NonNull MediaFormat mediaFormat) { + recordController.setAudioFormat(mediaFormat); + } + }; - public void setRecordController(BaseRecordController recordController) { - if (!isRecording()) this.recordController = recordController; - } + private final GetVideoData getVideoData = new GetVideoData() { + @Override + public void onSpsPpsVps(@NonNull ByteBuffer sps, @NonNull ByteBuffer pps, @Nullable ByteBuffer vps) { + onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); + } - public abstract void setLogs(boolean enable); + @Override + public void getVideoData(@NonNull ByteBuffer h264Buffer, @NonNull MediaCodec.BufferInfo info) { + fpsListener.calculateFps(); + recordController.recordVideo(h264Buffer, info); + if (streaming) getH264DataRtp(h264Buffer, info); + } - public abstract void setCheckServerAlive(boolean enable); + @Override + public void onVideoFormat(@NonNull MediaFormat mediaFormat) { + recordController.setVideoFormat(mediaFormat, !audioInitialized); + } + }; } diff --git a/library/src/main/java/com/pedro/library/base/FromFileBase.java b/library/src/main/java/com/pedro/library/base/FromFileBase.java index eb9cd77a9..49aa6a6bf 100644 --- a/library/src/main/java/com/pedro/library/base/FromFileBase.java +++ b/library/src/main/java/com/pedro/library/base/FromFileBase.java @@ -65,7 +65,7 @@ */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) -public abstract class FromFileBase implements GetVideoData, GetAacData, GetMicrophoneData { +public abstract class FromFileBase { private static final String TAG = "FromFileBase"; @@ -114,10 +114,10 @@ public FromFileBase(LightOpenGlView lightOpenGlView, VideoDecoderInterface video private void init(VideoDecoderInterface videoDecoderInterface, AudioDecoderInterface audioDecoderInterface) { - videoEncoder = new VideoEncoder(this); - audioEncoder = new AudioEncoder(this); + videoEncoder = new VideoEncoder(getVideoData); + audioEncoder = new AudioEncoder(getAacData); videoDecoder = new VideoDecoder(videoDecoderInterface); - audioDecoder = new AudioDecoder(this, audioDecoderInterface); + audioDecoder = new AudioDecoder(getMicrophoneData, audioDecoderInterface); recordController = new AndroidMuxerRecordController(); } @@ -624,51 +624,54 @@ public void moveTo(double time) { protected abstract void onSpsPpsVpsRtp(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps); - @Override - public void onSpsPpsVps(ByteBuffer sps, ByteBuffer pps, ByteBuffer vps) { - onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); - } - protected abstract void getH264DataRtp(ByteBuffer h264Buffer, MediaCodec.BufferInfo info); - @Override - public void getVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) { - fpsListener.calculateFps(); - recordController.recordVideo(h264Buffer, info); - if (streaming) getH264DataRtp(h264Buffer, info); - } - - @Override - public void onVideoFormat(MediaFormat mediaFormat) { - recordController.setVideoFormat(mediaFormat, !audioEnabled); - } - protected abstract void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info); - @Override - public void getAacData(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { - recordController.recordAudio(aacBuffer, info); - if (streaming) getAacDataRtp(aacBuffer, info); + public void setRecordController(BaseRecordController recordController) { + if (!isRecording()) this.recordController = recordController; } - @Override - public void onAudioFormat(MediaFormat mediaFormat) { - recordController.setAudioFormat(mediaFormat, !videoEnabled); - } + public abstract void setLogs(boolean enable); - @Override - public void inputPCMData(Frame frame) { + public abstract void setCheckServerAlive(boolean enable); + + private final GetMicrophoneData getMicrophoneData = frame -> { if (audioTrackPlayer != null) { audioTrackPlayer.write(frame.getBuffer(), frame.getOffset(), frame.getSize()); } audioEncoder.inputPCMData(frame); - } + }; - public void setRecordController(BaseRecordController recordController) { - if (!isRecording()) this.recordController = recordController; - } + private final GetAacData getAacData = new GetAacData() { + @Override + public void getAacData(@NonNull ByteBuffer aacBuffer, @NonNull MediaCodec.BufferInfo info) { + recordController.recordAudio(aacBuffer, info); + if (streaming) getAacDataRtp(aacBuffer, info); + } - public abstract void setLogs(boolean enable); + @Override + public void onAudioFormat(@NonNull MediaFormat mediaFormat) { + recordController.setAudioFormat(mediaFormat, !videoEnabled); + } + }; - public abstract void setCheckServerAlive(boolean enable); + private final GetVideoData getVideoData = new GetVideoData() { + @Override + public void onSpsPpsVps(@NonNull ByteBuffer sps, @NonNull ByteBuffer pps, @Nullable ByteBuffer vps) { + onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps != null ? vps.duplicate() : null); + } + + @Override + public void getVideoData(@NonNull ByteBuffer h264Buffer, @NonNull MediaCodec.BufferInfo info) { + fpsListener.calculateFps(); + recordController.recordVideo(h264Buffer, info); + if (streaming) getH264DataRtp(h264Buffer, info); + } + + @Override + public void onVideoFormat(@NonNull MediaFormat mediaFormat) { + recordController.setVideoFormat(mediaFormat, !audioEnabled); + } + }; } diff --git a/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java b/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java index ba2370c3e..ab6ad259e 100644 --- a/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java +++ b/library/src/main/java/com/pedro/library/base/OnlyAudioBase.java @@ -47,7 +47,7 @@ * * Created by pedro on 10/07/18. */ -public abstract class OnlyAudioBase implements GetAacData, GetMicrophoneData { +public abstract class OnlyAudioBase { protected BaseRecordController recordController; private MicrophoneManager microphoneManager; @@ -70,18 +70,18 @@ public void setMicrophoneMode(MicrophoneMode microphoneMode) { switch (microphoneMode) { case SYNC: microphoneManager = new MicrophoneManagerManual(); - audioEncoder = new AudioEncoder(this); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setGetFrame(((MicrophoneManagerManual) microphoneManager).getGetFrame()); audioEncoder.setTsModeBuffer(false); break; case ASYNC: - microphoneManager = new MicrophoneManager(this); - audioEncoder = new AudioEncoder(this); + microphoneManager = new MicrophoneManager(getMicrophoneData); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setTsModeBuffer(false); break; case BUFFER: - microphoneManager = new MicrophoneManager(this); - audioEncoder = new AudioEncoder(this); + microphoneManager = new MicrophoneManager(getMicrophoneData); + audioEncoder = new AudioEncoder(getAacData); audioEncoder.setTsModeBuffer(true); break; } @@ -358,24 +358,6 @@ public boolean isStreaming() { protected abstract void getAacDataRtp(ByteBuffer aacBuffer, MediaCodec.BufferInfo info); - @Override - public void getAacData(ByteBuffer aacBuffer, MediaCodec.BufferInfo info) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - recordController.recordAudio(aacBuffer, info); - } - if (streaming) getAacDataRtp(aacBuffer, info); - } - - @Override - public void inputPCMData(Frame frame) { - audioEncoder.inputPCMData(frame); - } - - @Override - public void onAudioFormat(MediaFormat mediaFormat) { - recordController.setAudioFormat(mediaFormat, true); - } - public void setRecordController(BaseRecordController recordController) { if (!isRecording()) this.recordController = recordController; } @@ -383,4 +365,23 @@ public void setRecordController(BaseRecordController recordController) { public abstract void setLogs(boolean enable); public abstract void setCheckServerAlive(boolean enable); + + private final GetMicrophoneData getMicrophoneData = frame -> { + audioEncoder.inputPCMData(frame); + }; + + private final GetAacData getAacData = new GetAacData() { + @Override + public void getAacData(@NonNull ByteBuffer aacBuffer, @NonNull MediaCodec.BufferInfo info) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + recordController.recordAudio(aacBuffer, info); + } + if (streaming) getAacDataRtp(aacBuffer, info); + } + + @Override + public void onAudioFormat(@NonNull MediaFormat mediaFormat) { + recordController.setAudioFormat(mediaFormat, true); + } + }; } diff --git a/library/src/main/java/com/pedro/library/base/StreamBase.kt b/library/src/main/java/com/pedro/library/base/StreamBase.kt index 7cdc0aedf..e238ca64e 100644 --- a/library/src/main/java/com/pedro/library/base/StreamBase.kt +++ b/library/src/main/java/com/pedro/library/base/StreamBase.kt @@ -19,6 +19,7 @@ import com.pedro.encoder.audio.AudioEncoder import com.pedro.encoder.audio.GetAacData import com.pedro.encoder.input.audio.GetMicrophoneData import com.pedro.encoder.input.video.CameraHelper +import com.pedro.encoder.input.video.GetCameraData import com.pedro.encoder.video.FormatVideoEncoder import com.pedro.encoder.video.GetVideoData import com.pedro.encoder.video.VideoEncoder @@ -43,16 +44,16 @@ abstract class StreamBase( context: Context, videoSource: VideoManager.Source, audioSource: AudioManager.Source -): GetVideoData, GetAacData, GetMicrophoneData { +) { //video and audio encoders - private val videoEncoder by lazy { VideoEncoder(this) } - private val audioEncoder by lazy { AudioEncoder(this) } + private val videoEncoder by lazy { VideoEncoder(getVideoData) } + private val audioEncoder by lazy { AudioEncoder(getAacData) } //video render private val glInterface = GlStreamInterface(context) //video and audio sources private val videoManager = VideoManager(context, videoSource) - private val audioManager by lazy { AudioManager(this, audioSource) } + private val audioManager by lazy { AudioManager(getMicrophoneData, audioSource) } //video/audio record private var recordController: BaseRecordController = AndroidMuxerRecordController() var isStreaming = false @@ -513,30 +514,36 @@ abstract class StreamBase( return videoEncoder.prepareVideoEncoder() && audioEncoder.prepareAudioEncoder() } - override fun inputPCMData(frame: Frame) { - audioEncoder.inputPCMData(frame) + private val getMicrophoneData = object: GetMicrophoneData { + override fun inputPCMData(frame: Frame) { + audioEncoder.inputPCMData(frame) + } } - override fun onVideoFormat(mediaFormat: MediaFormat) { - recordController.setVideoFormat(mediaFormat) - } + private val getAacData: GetAacData = object : GetAacData { + override fun getAacData(aacBuffer: ByteBuffer, info: MediaCodec.BufferInfo) { + getAacDataRtp(aacBuffer, info) + recordController.recordAudio(aacBuffer, info) + } - override fun onAudioFormat(mediaFormat: MediaFormat) { - recordController.setAudioFormat(mediaFormat) + override fun onAudioFormat(mediaFormat: MediaFormat) { + recordController.setAudioFormat(mediaFormat) + } } - override fun onSpsPpsVps(sps: ByteBuffer, pps: ByteBuffer, vps: ByteBuffer?) { - onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps?.duplicate()) - } + private val getVideoData: GetVideoData = object : GetVideoData { + override fun onSpsPpsVps(sps: ByteBuffer, pps: ByteBuffer, vps: ByteBuffer?) { + onSpsPpsVpsRtp(sps.duplicate(), pps.duplicate(), vps?.duplicate()) + } - override fun getVideoData(h264Buffer: ByteBuffer, info: MediaCodec.BufferInfo) { - getH264DataRtp(h264Buffer, info) - recordController.recordVideo(h264Buffer, info) - } + override fun getVideoData(h264Buffer: ByteBuffer, info: MediaCodec.BufferInfo) { + getH264DataRtp(h264Buffer, info) + recordController.recordVideo(h264Buffer, info) + } - override fun getAacData(aacBuffer: ByteBuffer, info: MediaCodec.BufferInfo) { - getAacDataRtp(aacBuffer, info) - recordController.recordAudio(aacBuffer, info) + override fun onVideoFormat(mediaFormat: MediaFormat) { + recordController.setVideoFormat(mediaFormat) + } } protected abstract fun audioInfo(sampleRate: Int, isStereo: Boolean) diff --git a/library/src/main/java/com/pedro/library/base/recording/RecordController.java b/library/src/main/java/com/pedro/library/base/recording/RecordController.java index ab7b2eade..f581052fd 100644 --- a/library/src/main/java/com/pedro/library/base/recording/RecordController.java +++ b/library/src/main/java/com/pedro/library/base/recording/RecordController.java @@ -18,7 +18,7 @@ public interface RecordController { void recordVideo(ByteBuffer videoBuffer, MediaCodec.BufferInfo videoInfo); void recordAudio(ByteBuffer audioBuffer, MediaCodec.BufferInfo audioInfo); void setVideoFormat(MediaFormat videoFormat, boolean isOnlyVideo); - void setAudioFormat(MediaFormat audioFormat, boolean isOnlyVideo); + void setAudioFormat(MediaFormat audioFormat, boolean isOnlyAudio); void resetFormats(); interface Listener {