DJI Mobile SDK real-time RTSP video streaming
See original GitHub issueI have been tackling this issue for a week now. Read numerous issues, couple from this very library even. Most of the answers or code snippets are no longer available anymore.
I have taken this repo as the base for this project: https://github.com/DJI-Mobile-SDK-Tutorials/Android-VideoStreamDecodingSample and tried to implement this library into it.
Managed to stream a very short video feed to my local machine and then it breaks for reasons beyond me.
Here is what I have at this very point:
public class StreamVideo extends MyClassNotNecessaryToPointOut {
private long presentTimeUs = 0L;
private RtspClient rtspClient = new RtspClient(new ConnectCheckerRtsp() {
@Override
public void onConnectionSuccessRtsp() {
}
@Override
public void onConnectionFailedRtsp(String reason) {
}
@Override
public void onNewBitrateRtsp(long bitrate) {
}
@Override
public void onDisconnectRtsp() {
}
@Override
public void onAuthErrorRtsp() {
}
@Override
public void onAuthSuccessRtsp() {
}
});
private MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo();
private boolean started;
protected VideoFeeder.VideoDataListener mReceivedVideoDataListener = (videoBuffer, size) -> {
videoInfo.size = size;
videoInfo.offset = 0;
videoInfo.flags = MediaCodec.BUFFER_FLAG_PARTIAL_FRAME;
videoInfo.presentationTimeUs = System.nanoTime() / 1000 - presentTimeUs;
int naluType = videoBuffer[0] & 0x1f;
//First keyframe received and you start stream.
// Change conditional as you want but stream must start with a keyframe
if (naluType == 5 && !rtspClient.isStreaming() && started) {
videoInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
Pair<ByteBuffer, ByteBuffer> videoData = decodeSpsPpsFromBuffer(videoBuffer, size);
if (videoData != null) {
rtspClient.setIsStereo(true);
rtspClient.setSampleRate(44100);
presentTimeUs = System.nanoTime() / 1000;
ByteBuffer newSps = videoData.first;
ByteBuffer newPps = videoData.second;
rtspClient.setSPSandPPS(newSps, newPps, null);
rtspClient.setProtocol(Protocol.TCP);
rtspClient.connect(this.endpoint);
}
}
ByteBuffer h264Buffer = ByteBuffer.wrap(videoBuffer);
rtspClient.sendVideo(h264Buffer, videoInfo);
};
private VideoFeeder.VideoFeed standardVideoFeeder;
private String endpoint = "rtsp://192.168.1.100:8554/dji/demo";
public StreamVideo(String endpoint) {
this.endpoint = endpoint;
standardVideoFeeder = VideoFeeder.getInstance().provideTranscodedVideoFeed();
standardVideoFeeder.addVideoDataListener(mReceivedVideoDataListener);
}
@Override
public void start() {
started = true;
}
@Override
public void end() {
started = false;
rtspClient.disconnect();
}
private Pair<ByteBuffer, ByteBuffer> decodeSpsPpsFromBuffer(byte[] csd, int length) {
byte[] mSPS = null, mPPS = null;
int i = 0;
int spsIndex = -1;
int ppsIndex = -1;
while (i < length - 4) {
if (csd[i] == 0 && csd[i + 1] == 0 && csd[i + 2] == 0 && csd[i + 3] == 1) {
if (spsIndex == -1) {
spsIndex = i;
} else {
ppsIndex = i;
break;
}
}
i++;
}
if (spsIndex != -1 && ppsIndex != -1) {
mSPS = new byte[ppsIndex];
System.arraycopy(csd, spsIndex, mSPS, 0, ppsIndex);
mPPS = new byte[length - ppsIndex];
System.arraycopy(csd, ppsIndex, mPPS, 0, length - ppsIndex);
}
if (mSPS != null && mPPS != null) {
return new Pair<>(ByteBuffer.wrap(mSPS), ByteBuffer.wrap(mPPS));
}
return null;
}
}
I am hosting https://github.com/pedroSG94/vlc-example-streamplayer as the server on my local machine and trying to access that using the VLC player.
DJI VideoFeeder is spitting out encoded h264 buffer. My question is what could be the reason for this bad video feed?
Reference: https://developer.dji.com/api-reference/android-api/BaseClasses/DJIVideoFeeder.html?search=videofeed&i=2& https://github.com/DJI-Mobile-SDK-Tutorials/Android-VideoStreamDecodingSample/issues/33
Issue Analytics
- State:
- Created 2 years ago
- Comments:17 (8 by maintainers)
Top Related StackOverflow Question
Hello all, I am also trying to stream the raw drone data to an RTSP server.
However, I am having difficulty regarding the keyframes of the video data. As per above, the condition
if (naluType == 5 && !rtspClient.isStreaming())is never true for me.videoBuffer[0]is always0for me.Does anyone know how to set the
naluTypeor any way of getting the keyframe? Appreciate any advice! My code is the same as above and am using a Mavic 2 Pro.Hi @pedroSG94 I have a problem: On SDK, I use getVideoStartCodeSize() process the h264 data, get nalutype, but only has naluType 1/7/9, never get 8 or 5 ,
how can I push this data use your library by rtmp? Thank you very much!