8000 Add rotation and change track for Android MediaRecorder by rostopira · Pull Request #1191 · flutter-webrtc/flutter-webrtc · GitHub
[go: up one dir, main page]

Skip to content

Add rotation and change track for Android MediaRecorder #1191

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

Open
wants to merge 1 commit into
base: main
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
12 changes: 10 additions & 2 deletions android/src/main/java/com/cloudwebrtc/webrtc/GetUserMediaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,11 @@ public void onCameraSwitchError(String s) {
* @param path to the file for record
* @param videoTrack to record or null if only audio needed
* @param audioChannel channel for recording or null
* @param rotation additional rotation of the resulting video
* @throws Exception lot of different exceptions, pass back to dart layer to print them at least
*/
void startRecordingToFile(
String path, Integer id, @Nullable VideoTrack videoTrack, @Nullable AudioChannel audioChannel)
String path, Integer id, @Nullable VideoTrack videoTrack, @Nullable AudioChannel audioChannel, @Nullable Integer rotation)
throws Exception {
AudioSamplesInterceptor interceptor = null;
if (audioChannel == AudioChannel.INPUT) {
Expand All @@ -842,11 +843,18 @@ void startRecordingToFile(
}
interceptor = outputSamplesInterceptor;
}
MediaRecorderImpl mediaRecorder = new MediaRecorderImpl(id, videoTrack, interceptor);
MediaRecorderImpl mediaRecorder = new MediaRecorderImpl(id, videoTrack, interceptor, rotation);
mediaRecorder.startRecording(new File(path));
mediaRecorders.append(id, mediaRecorder);
}

public void changeRecorderTrack(VideoTrack track, Integer recorderId) {
final MediaRecorderImpl mr = mediaRecorders.get(recorderId);
if (mr != null) {
mr.switchVideoTrack(track);
}
}

void stopRecording(Integer id) {
MediaRecorderImpl mediaRecorder = mediaRecorders.get(id);
if (mediaRecorder != null) {
Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,9 @@ public void onMethodCall(MethodCall call, @NonNull Result notSafeResult) {
audioChannel = AudioChannel.values()[(Integer) call.argument("audioChannel")];
}
Integer recorderId = call.argument("recorderId");
Integer rotation = call.argument("rotation");
if (videoTrack != null || audioChannel != null) {
getUserMediaImpl.startRecordingToFile(path, recorderId, videoTrack, audioChannel);
getUserMediaImpl.startRecordingToFile(path, recorderId, videoTrack, audioChannel, rotation);
result.success(null);
} else {
resultError("startRecordToFile", "No tracks", result);
Expand All @@ -537,6 +538,15 @@ public void onMethodCall(MethodCall call, @NonNull Result notSafeResult) {
resultError("startRecordToFile", e.getMessage(), result);
}
break;
case "changeRecorderTrack":
String videoTrackId = call.argument("videoTrackId");
Integer recorderId = call.argument("recorderId");
MediaStreamTrack track = getTrackForId(videoTrackId);
if (track instanceof VideoTrack) {
getUserMediaImpl.changeRecorderTrack((VideoTrack) track, recorderId);
}
result.success(null);
break;
case "stopRecordToFile":
Integer recorderId = call.argument("recorderId");
getUserMediaImpl.stopRecording(recorderId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@
public class MediaRecorderImpl {

private final Integer id;
private final VideoTrack videoTrack;
private VideoTrack videoTrack;
private final AudioSamplesInterceptor audioInterceptor;
private VideoFileRenderer videoFileRenderer;
private boolean isRunning = false;
private File recordFile;
private final int rotation;

public MediaRecorderImpl(Integer id, @Nullable VideoTrack videoTrack, @Nullable AudioSamplesInterceptor audioInterceptor) {
public MediaRecorderImpl(Integer id, @Nullable VideoTrack videoTrack, @Nullable AudioSamplesInterceptor audioInterceptor, @Nullable Integer rotation) {
this.id = id;
this.videoTrack = videoTrack;
this.audioInterceptor = audioInterceptor;
if (rotation != null)
this.rotation = rotation;
else
this.rotation = 0;
}

public void startRecording(File file) throws Exception {
Expand All @@ -35,7 +40,8 @@ public void startRecording(File file) throws Exception {
videoFileRenderer = new VideoFileRenderer(
file.getAbsolutePath(),
EglUtils.getRootEglBaseContext(),
audioInterceptor != null
audioInterceptor != null,
rotation
);
videoTrack.addSink(videoFileRenderer);
if (audioInterceptor != null)
Expand All @@ -49,6 +55,14 @@ public void startRecording(File file) throws Exception {
}
}

public void switchVideoTrack(VideoTrack newVideoTrack) {
if (videoTrack != null) {
videoTrack.removeSink(videoFileRenderer);
}
newVideoTrack.addSink(videoFileRenderer);
videoTrack = newVideoTrack;
}

public File getRecordFile() { return recordFile; }

public void stopRecording() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class VideoFileRenderer implements VideoSink, SamplesReadyCallback {
private Surface surface;
private MediaCodec audioEncoder;

VideoFileRenderer(String outputFile, final EglBase.Context sharedContext, boolean withAudio) throws IOException {
VideoFileRenderer(String outputFile, final EglBase.Context sharedContext, boolean withAudio, int rotation) throws IOException {
renderThread = new HandlerThread(TAG + "RenderThread");
renderThread.start();
renderThreadHandler = new Handler(renderThread.getLooper());
Expand All @@ -70,7 +70,7 @@ class VideoFileRenderer implements VideoSink, SamplesReadyCallback {
// obtained from the encoder after it has started processing data.
mediaMuxer = new MediaMuxer(outputFile,
MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

mediaMuxer.setOrientationHint(rotation % 360);
audioTrackIndex = withAudio ? -1 : 0;
}

Expand Down
0