Корректировка видеокамеры
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
// cmd/server/main.go
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -12,6 +11,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gpio-monitor/internal/adapter"
|
"gpio-monitor/internal/adapter"
|
||||||
@@ -79,8 +79,8 @@ func (a *API) HandleHistory(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) HandleStream(w http.ResponseWriter, r *http.Request) {
|
func (a *API) HandleStream(w http.ResponseWriter, r *http.Request) {
|
||||||
// Проверяем доступность go2rtc
|
// Проверяем доступность MJPEG потока в go2rtc
|
||||||
resp, err := http.Get("http://localhost:1984/api/streams?src=cam")
|
resp, err := http.Get("http://localhost:1984/api/streams?src=cam_mjpeg")
|
||||||
camAvailable := err == nil && resp.StatusCode == 200
|
camAvailable := err == nil && resp.StatusCode == 200
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
@@ -89,17 +89,15 @@ func (a *API) HandleStream(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeJSON(w, map[string]interface{}{
|
writeJSON(w, map[string]interface{}{
|
||||||
"cam": "/api/cam",
|
"cam": "/api/cam",
|
||||||
"available": camAvailable,
|
"available": camAvailable,
|
||||||
"source": fmt.Sprintf("http://%s:1984/api/stream.mjpeg?src=cam", r.Host),
|
"source": fmt.Sprintf("http://%s:1984/api/stream.mjpeg?src=cam_mjpeg", strings.Split(r.Host, ":")[0]),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Прокси для MJPEG потока с go2rtc
|
// Прокси для MJPEG потока — просто ретранслирует готовый поток из go2rtc
|
||||||
func handleCamProxy(w http.ResponseWriter, r *http.Request) {
|
func handleCamProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("[cam] proxy request from %s", r.RemoteAddr)
|
log.Printf("[cam] proxy request from %s", r.RemoteAddr)
|
||||||
|
|
||||||
resp, err := http.Get(
|
resp, err := http.Get("http://127.0.0.1:1984/api/stream.mjpeg?src=cam_mjpeg")
|
||||||
"http://127.0.0.1:1984/api/stream.mjpeg?src=cam",
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[cam] go2rtc unavailable: %v", err)
|
log.Printf("[cam] go2rtc unavailable: %v", err)
|
||||||
http.Error(w, "camera unavailable", 503)
|
http.Error(w, "camera unavailable", 503)
|
||||||
@@ -107,6 +105,7 @@ func handleCamProxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Прокидываем заголовки
|
||||||
for k, vv := range resp.Header {
|
for k, vv := range resp.Header {
|
||||||
for _, v := range vv {
|
for _, v := range vv {
|
||||||
w.Header().Add(k, v)
|
w.Header().Add(k, v)
|
||||||
@@ -114,7 +113,6 @@ func handleCamProxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
|
||||||
w.WriteHeader(resp.StatusCode)
|
w.WriteHeader(resp.StatusCode)
|
||||||
|
|
||||||
flusher, ok := w.(http.Flusher)
|
flusher, ok := w.(http.Flusher)
|
||||||
@@ -134,7 +132,6 @@ func handleCamProxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Printf("[cam] client disconnected")
|
log.Printf("[cam] client disconnected")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
streams:
|
streams:
|
||||||
|
# Основной поток для WebRTC (быстрый, минимальная задержка)
|
||||||
cam:
|
cam:
|
||||||
- exec:ffmpeg -f v4l2 -input_format mjpeg -video_size 640x480 -framerate 15 -i /dev/video0 -c:v copy -f mjpeg pipe:1
|
- exec:ffmpeg -f v4l2 -input_format yuyv422 -video_size 640x480 -framerate 30 -i /dev/video0 -c:v libx264 -preset ultrafast -tune zerolatency -f rawvideo pipe:1
|
||||||
|
# MJPEG поток для браузера (оптимизирован под теги img)
|
||||||
|
cam_mjpeg:
|
||||||
|
- exec:ffmpeg -f v4l2 -input_format yuyv422 -video_size 640x480 -framerate 15 -i /dev/video0 -c:v mjpeg -q:v 8 -f mpjpeg pipe:1
|
||||||
|
|
||||||
api:
|
api:
|
||||||
listen: ":1984"
|
listen: ":1984"
|
||||||
|
|||||||
Reference in New Issue
Block a user