Skip to content

Commit

Permalink
Update v4l2 discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jan 8, 2025
1 parent 7e0a163 commit 879ef60
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions internal/v4l2/v4l2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,46 @@ func apiV4L2(w http.ResponseWriter, r *http.Request) {

formats, _ := dev.ListFormats()
for _, fourCC := range formats {
source := &api.Source{}
name, ffmpeg := findFormat(fourCC)
source := &api.Source{Name: name}

for _, format := range device.Formats {
if format.FourCC == fourCC {
source.Name = format.Name
source.URL = "v4l2:device?video=" + path + "&input_format=" + format.FFmpeg + "&video_size="
break
sizes, _ := dev.ListSizes(fourCC)
for _, wh := range sizes {
if source.Info != "" {
source.Info += " "
}
}

if source.Name != "" {
sizes, _ := dev.ListSizes(fourCC)
for _, wh := range sizes {
size := fmt.Sprintf("%dx%d", wh[0], wh[1])
if source.Info != "" {
source.Info += " " + size
} else {
source.Info = size
source.URL += size
source.Info += fmt.Sprintf("%dx%d", wh[0], wh[1])

frameRates, _ := dev.ListFrameRates(fourCC, wh[0], wh[1])
for _, fr := range frameRates {
source.Info += fmt.Sprintf("@%d", fr)

if source.URL == "" && ffmpeg != "" {
source.URL = fmt.Sprintf(
"v4l2:device?video=%s&input_format=%s&video_size=%dx%d&framerate=%d",
path, ffmpeg, wh[0], wh[1], fr,
)
}
}
} else {
source.Name = string(binary.LittleEndian.AppendUint32(nil, fourCC))
}

sources = append(sources, source)
if source.Info != "" {
sources = append(sources, source)
}
}

_ = dev.Close()
}

api.ResponseSources(w, sources)
}

func findFormat(fourCC uint32) (name, ffmpeg string) {
for _, format := range device.Formats {
if format.FourCC == fourCC {
return format.Name, format.FFmpeg
}
}
return string(binary.LittleEndian.AppendUint32(nil, fourCC)), ""
}

0 comments on commit 879ef60

Please sign in to comment.