Skip to content

Reverse streaming from RTSP to MSE or HLS #1692

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
Mani2820 opened this issue Apr 8, 2025 · 1 comment
Open

Reverse streaming from RTSP to MSE or HLS #1692

Mani2820 opened this issue Apr 8, 2025 · 1 comment
Labels
question Further information is requested

Comments

@Mani2820
Copy link

Mani2820 commented Apr 8, 2025

I am having a recorded RTSP stream which is giving me the stream in reverse mode. like lets say 10:30:30, next is 10:30:29 .. which is fine . But at the same time the frames inside it are in forward direction. so the video is playing like 10:30:29 -> 10:30:30 , then again from 10:30:28 -> 10:30:29 and so on. basically the stream is in reverse order but the frames are in forward. so it playing like a glitch.

We are using HLS and MSE also .
we are having two types of streams one forward and one reverse also from NVR.

For HLS i am fetching the forward stream (not the stream i mentioned above) as 2 mins buffer and reversing it on the frontend. the stream will be now like 10:28:00 to 10:30:00 and reversing it on frontend, and triggering buffer again and fetching 10:26:00 and 10:28:00 and then as soon as the first video completed i am showing the second video and start reversing it. which seems to be fine, but managing the streams becoming so difficult as each will have there own session and we have to wait for 2 mins to get the buffer and sometimes its not fetching the .mp4 files as expected.

In MSE we are having the above issue where i mentioned the frames are in forward but the video is in reverse mode , so i need something like fetch the individual second frames and reverse them. So i tried to pipe RTSP stream using ffmpeg in mpgets. then using split of 188 bytes and reversing the each frame in a second. using web socket i am using JSMPEG cdn to play the stream. I am receiving the bytes but seeing black screen. somewhere the stream is not converting properly to mpgets or maybe the split is doing this black screen.

func startFFmpegStream(conn *websocket.Conn, rtspURL string) {
cmd := exec.Command(ffmpegCmd,
"-rtsp_transport", "tcp",
"-i", rtspURL,
"-an",
"-c:v", "copy",
"-f", "mpegts",
"pipe:1",
)

pipe, err := cmd.StdoutPipe()
if err != nil {
log.Println("Pipe error:", err)
return
}
if err := cmd.Start(); err != nil {
log.Println("FFmpeg start error:", err)
return
}

scanner := bufio.NewScanner(pipe)
scanner.Split(splitMPEGTS)

var (
buffer [][]byte
mu     sync.Mutex
)

go func() {
for scanner.Scan() {
frame := make([]byte, len(scanner.Bytes()))
copy(frame, scanner.Bytes())

mu.Lock()
buffer = append(buffer, frame)
if len(buffer) >= frameLimit {
for i := frameLimit - 1; i >= 0; i-- {
err := conn.WriteMessage(websocket.BinaryMessage, buffer[i])
if err != nil {
log.Println("WebSocket write error:", err)
mu.Unlock()
return
}
time.Sleep(33 * time.Millisecond)
}
buffer = buffer[:0]
}
mu.Unlock()
}
if err := scanner.Err(); err != nil {
log.Println("Scanner error:", err)
}
}()

cmd.Wait()
}

func splitMPEGTS(data []byte, atEOF bool) (advance int, token []byte, err error) {
packetSize := 188
if len(data) < packetSize {
return 0, nil, nil
}
if data[0] != 0x47 {
idx := bytes.IndexByte(data, 0x47)
if idx == -1 {
return len(data), nil, nil
}
return idx, nil, nil
}
return packetSize, data[:packetSize], nil
}

can you provide any solution for the above situation. As we play multiple stream in one go, reverse encoding is not an option so i am copying the RTSP stream.

@AlexxIT AlexxIT added the question Further information is requested label Apr 8, 2025
@AlexxIT
Copy link
Owner

AlexxIT commented Apr 8, 2025

I've never heard of such a situation. In any case, the task is quite clear and should not be a great problem for a programmer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants