I'm trying to use the MediaRecorder to stream Video via UDP, but this code leaves me with this error:
E/MediaRecorder: prepare failed: -38
I'm using RXJava2 to make sure that the DatagramSocket
is created on another thread, my code is as follows:
public class MainActivity extends AppCompatActivity {
...
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Observable.just(0) // Yep, that's one ugly way to start...
.observeOn(Schedulers.io())
.map(i -> createFileDescriptor())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::createMediaRecorder);
...
}
private ParcelFileDescriptor createFileDescriptor() {
try {
String localHostName = "192.168.1.1";
String remoteHostName = "192.168.1.2";
int port = 5004;
DatagramSocket ds = new DatagramSocket(port, InetAddress.getByName(localHostName));
ds.connect(InetAddress.getByName(remoteHostName), port);
return ParcelFileDescriptor.fromDatagramSocket(ds);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void createMediaRecorder(ParcelFileDescriptor parcelFileDescriptor) {
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(1280, 720);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setOutputFile(parcelFileDescriptor.getFileDescriptor());
try {
mMediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mMediaRecorder.start();
}
}
The code works if I save to file instead, so it must have something todo with saving to a FileDescriptor
from a DatagramSocket
.
Any suggestions?