Skip to content

Commit

Permalink
feat: push mediamtx streams + update devices page
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Mar 9, 2024
1 parent 1b3c9e9 commit 56e037c
Show file tree
Hide file tree
Showing 12 changed files with 3,819 additions and 306 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ jobs:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up tooling
run: make workflow-tooling
run: make build-tooling

- name: Build nightly
run: make workflow-nightly
run: make build-nightly

- name: Publish nightly
env:
Expand Down
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ TOOL_GOOSE=github.com/pressly/goose/v3/cmd/goose@v3.18.0
TOOL_SQLC=github.com/sqlc-dev/sqlc/cmd/sqlc@v1.25.0
TOOL_TWIRP=github.com/twitchtv/twirp/protoc-gen-twirp@v8.1.3
TOOL_PROTOC_GEN_GO=google.golang.org/protobuf/cmd/protoc-gen-go@v1.32.0
TOOL_OAPI_CODEGEN=github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@v2.1.0


PROTOC_VERSION=25.1
PROTOC_ZIP=protoc-$(PROTOC_VERSION)-linux-x86_64.zip
Expand Down Expand Up @@ -57,7 +59,7 @@ dev-proxy:
# ---------- Gen

# Generate code
gen: gen-sqlc gen-pubsub gen-hub gen-proto gen-typescriptify
gen: gen-sqlc gen-pubsub gen-hub gen-proto gen-typescriptify gen-mediamtx

gen-sqlc:
sqlc generate
Expand All @@ -75,10 +77,13 @@ gen-proto:
gen-typescriptify:
go run ./scripts/typescriptify $(WEB_PATH)/src/lib/models.gen.ts

gen-mediamtx:
oapi-codegen -package mediamtx ./internal/mediamtx/swagger.json > ./internal/mediamtx/mediamtx.gen.go

# ---------- Tooling

# Install tooling
tooling: tooling-air tooling-task tooling-goose tooling-sqlc tooling-twirp tooling-protoc-gen-go tooling-protoc-gen-ts
tooling: tooling-air tooling-task tooling-goose tooling-sqlc tooling-twirp tooling-protoc-gen-go tooling-protoc-gen-ts tooling-oapi-codegen

tooling-air:
go install $(TOOL_AIR)
Expand All @@ -101,6 +106,9 @@ tooling-protoc-gen-go:
tooling-protoc-gen-ts:
cd $(WEB_PATH) && pnpm install

tooling-oapi-codegen:
go install $(TOOL_OAPI_CODEGEN)

# ---------- Install

install-protoc:
Expand All @@ -117,7 +125,7 @@ install-atlas:

# ---------- Workflow

workflow-tooling: tooling-task tooling-sqlc tooling-twirp tooling-protoc-gen-go tooling-protoc-gen-ts
build-tooling: tooling-task tooling-sqlc tooling-twirp tooling-protoc-gen-go tooling-protoc-gen-ts tooling-oapi-codegen

workflow-nightly:
build-nightly:
task nightly
75 changes: 72 additions & 3 deletions cmd/ipcmanview/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"errors"
"net/http"
"strconv"

"github.com/ItsNotGoodName/ipcmanview/internal/api"
Expand All @@ -20,7 +22,6 @@ import (
"github.com/ItsNotGoodName/ipcmanview/pkg/pubsub"
"github.com/ItsNotGoodName/ipcmanview/pkg/sutureext"
"github.com/ItsNotGoodName/ipcmanview/rpc"

"github.com/thejerf/suture/v4"
)

Expand All @@ -29,6 +30,7 @@ type CmdServe struct {
HTTPHost string `env:"HTTP_HOST" help:"HTTP host to listen on (e.g. \"127.0.0.1\")."`
HTTPPort uint16 `env:"HTTP_PORT" default:"8080" help:"HTTP port to listen on."`
HTTPSPort uint16 `env:"HTTPS_PORT" default:"8443" help:"HTTPS port to listen on."`
HTTPRedirect bool `env:"HTTP_REDIRECT" default:"true" help:"Redirect HTTP to HTTPS."`
SMTPHost string `env:"SMTP_HOST" help:"SMTP host to listen on (e.g. \"127.0.0.1\")."`
SMTPPort uint16 `env:"SMTP_PORT" default:"1025" help:"SMTP port to listen on."`
MQTTAddress string `env:"MQTT_ADDRESS" help:"MQTT server address (e.g. \"mqtt://192.168.1.20:1883\")."`
Expand All @@ -40,7 +42,7 @@ type CmdServe struct {
MediamtxHost string `env:"MEDIAMTX_HOST" help:"MediaMTX host address (e.g. \"192.168.1.20\")."`
MediamtxWebrtcPort uint16 `env:"MEDIAMTX_WEBRTC_PORT" default:"8889" help:"MediaMTX WebRTC port."`
MediamtxHLSPort uint16 `env:"MEDIAMTX_HLS_PORT" default:"8888" help:"MediaMTX HLS port."`
MediamtxPathTemplate string `env:"MEDIAMTX_PATH_TEMPLATE" help:"Template for generating MediaMTX paths (e.g. \"ipcmanview_dahua_{{.DeviceID}}_{{.Channel}}_{{.Subtype}}\")."`
MediamtxPathTemplate string `env:"MEDIAMTX_PATH_TEMPLATE" default:"ipcmanview_dahua_{{.DeviceID}}_{{.Channel}}_{{.Subtype}}" help:"Template for generating MediaMTX paths."`
MediamtxStreamProtocol string `env:"MEDIAMTX_STREAM_PROTOCOL" default:"webrtc" enum:"webrtc,hls" help:"MediaMTX stream protocol."`
}

Expand Down Expand Up @@ -112,6 +114,69 @@ func (c *CmdServe) Run(ctx *Context) error {
return err
}

// TODO: move this
hub.OnDahuaDeviceUpdated("DEBUG", func(ctx context.Context, event bus.DahuaDeviceUpdated) error {
client, err := mediamtx.NewClient("http://" + c.MediamtxHost + ":9997")
if err != nil {
return err
}

device, err := dahua.GetDevice(ctx, dahua.GetDeviceFilter{ID: event.DeviceID})
if err != nil {
return err
}

streams, err := db.C().DahuaListStreamsByDevice(ctx, event.DeviceID)
if err != nil {
return err
}

for _, stream := range streams {
name := mediamtxConfig.DahuaEmbedPath(stream)
rtspURL := dahua.GetLiveRTSPURL(dahua.GetLiveRTSPURLParams{
Username: device.Username,
Password: device.Password,
Host: device.Ip,
Port: 554,
Channel: int(stream.Channel),
Subtype: int(stream.Subtype),
})
rtspTransport := "tcp"
pathConf := mediamtx.PathConf{
Source: &rtspURL,
RtspTransport: &rtspTransport,
}

rsp, err := client.ConfigPathsGet(ctx, name)
if err != nil {
return err
}
res, err := mediamtx.ParseConfigPathsGetResponse(rsp)
if err != nil {
return err
}

switch res.StatusCode() {
case http.StatusOK:
rsp, err := client.ConfigPathsPatch(ctx, name, pathConf)
if err != nil {
return err
}
rsp.Body.Close()
case http.StatusNotFound, http.StatusInternalServerError:
rsp, err := client.ConfigPathsAdd(ctx, name, pathConf)
if err != nil {
return err
}
rsp.Body.Close()
default:
return errors.New(string(res.Body))
}
}

return nil
})

// Dahua
if err := dahua.Normalize(ctx); err != nil {
return err
Expand Down Expand Up @@ -173,8 +238,12 @@ func (c *CmdServe) Run(ctx *Context) error {
Register(rpc.NewAdminServer(rpcserver.NewAdmin(configProvider, db), rpcLogger, rpcserver.RequireAdminAuthSession()))

// HTTP server
httpServer := httpRouter
if c.HTTPRedirect {
httpServer = server.NewHTTPRedirect(strconv.Itoa(int(c.HTTPSPort)))
}
super.Add(server.NewHTTPServer(
server.NewHTTPRedirect(strconv.Itoa(int(c.HTTPSPort))),
httpServer,
core.Address(c.HTTPHost, int(c.HTTPPort)),
nil,
))
Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43
github.com/emersion/go-smtp v0.20.2
github.com/georgysavva/scany/v2 v2.1.0
github.com/getkin/kin-openapi v0.123.0
github.com/go-playground/locales v0.14.1
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.18.0
Expand All @@ -23,6 +24,7 @@ require (
github.com/labstack/echo/v4 v4.11.4
github.com/mattn/go-sqlite3 v1.14.22
github.com/nathan-osman/go-sunrise v1.1.0
github.com/oapi-codegen/runtime v1.1.1
github.com/pkg/sftp v1.13.6
github.com/pressly/goose/v3 v3.18.0
github.com/rs/zerolog v1.32.0
Expand All @@ -40,31 +42,38 @@ require (
)

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lib/pq v1.10.8 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
Expand All @@ -85,7 +94,6 @@ require (
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.41.0 // indirect
Expand Down
Loading

0 comments on commit 56e037c

Please sign in to comment.