-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add native support for the raspberry pi camera
- Loading branch information
Showing
15 changed files
with
948 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
/release | ||
/coverage*.txt | ||
/apidocs/*.html | ||
/rtsp-simple-server-rpi | ||
/rtsp-simple-server-rpi64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/aler9/gortsplib" | ||
"github.com/aler9/gortsplib/pkg/h264" | ||
"github.com/aler9/gortsplib/pkg/rtph264" | ||
|
||
"github.com/aler9/rtsp-simple-server/internal/logger" | ||
"github.com/aler9/rtsp-simple-server/internal/rpicamera" | ||
) | ||
|
||
type rpiCameraSourceParent interface { | ||
log(logger.Level, string, ...interface{}) | ||
sourceStaticImplSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes | ||
sourceStaticImplSetNotReady(req pathSourceStaticSetNotReadyReq) | ||
} | ||
|
||
type rpiCameraSource struct { | ||
parent rpiCameraSourceParent | ||
} | ||
|
||
func newRPICameraSource( | ||
parent rpiCameraSourceParent, | ||
) *rpiCameraSource { | ||
return &rpiCameraSource{ | ||
parent: parent, | ||
} | ||
} | ||
|
||
func (s *rpiCameraSource) Log(level logger.Level, format string, args ...interface{}) { | ||
s.parent.log(level, "[rpicamera source] "+format, args...) | ||
} | ||
|
||
// run implements sourceStaticImpl. | ||
func (s *rpiCameraSource) run(ctx context.Context) error { | ||
track := &gortsplib.TrackH264{PayloadType: 96} | ||
enc := &rtph264.Encoder{PayloadType: 96} | ||
enc.Init() | ||
var stream *stream | ||
var start time.Time | ||
|
||
onData := func(nalus [][]byte) { | ||
if stream == nil { | ||
res := s.parent.sourceStaticImplSetReady(pathSourceStaticSetReadyReq{ | ||
tracks: gortsplib.Tracks{track}, | ||
}) | ||
if res.err != nil { | ||
return | ||
} | ||
|
||
s.Log(logger.Info, "ready") | ||
stream = res.stream | ||
start = time.Now() | ||
} | ||
|
||
pts := time.Since(start) | ||
|
||
pkts, err := enc.Encode(nalus, pts) | ||
if err != nil { | ||
return | ||
} | ||
|
||
lastPkt := len(pkts) - 1 | ||
for i, pkt := range pkts { | ||
if i != lastPkt { | ||
stream.writeData(&data{ | ||
trackID: 0, | ||
rtp: pkt, | ||
ptsEqualsDTS: false, | ||
}) | ||
} else { | ||
stream.writeData(&data{ | ||
trackID: 0, | ||
rtp: pkt, | ||
ptsEqualsDTS: h264.IDRPresent(nalus), | ||
h264NALUs: nalus, | ||
h264PTS: pts, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
cam, err := rpicamera.New(onData) | ||
if err != nil { | ||
return err | ||
} | ||
defer cam.Close() | ||
|
||
defer func() { | ||
if stream != nil { | ||
s.parent.sourceStaticImplSetNotReady(pathSourceStaticSetNotReadyReq{}) | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
// apiSourceDescribe implements sourceStaticImpl. | ||
func (*rpiCameraSource) apiSourceDescribe() interface{} { | ||
return struct { | ||
Type string `json:"type"` | ||
}{"rpiCameraSource"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//go:build rpilibcamera || rpilegacy | ||
// +build rpilibcamera rpilegacy | ||
|
||
package rpicamera | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
const ( | ||
embeddedExecutableTempPath = "/dev/shm/rtspss-embeddedbin" | ||
) | ||
|
||
func getKernelArch() (string, error) { | ||
cmd := exec.Command("uname", "-m") | ||
|
||
byts, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(byts[:len(byts)-1]), nil | ||
} | ||
|
||
// 32-bit embedded binaries can't run on 64-bit. | ||
func checkArch() error { | ||
if runtime.GOARCH != "arm" { | ||
return nil | ||
} | ||
|
||
arch, err := getKernelArch() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if arch == "aarch64" { | ||
return fmt.Errorf("you need the arm64 version") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type embeddedExecutable struct { | ||
cmd *exec.Cmd | ||
} | ||
|
||
func newEmbeddedExecutable(content []byte, arg string) (*embeddedExecutable, error) { | ||
err := checkArch() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = os.WriteFile(embeddedExecutableTempPath, content, 0o755) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cmd := exec.Command(embeddedExecutableTempPath, arg) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
err = cmd.Start() | ||
if err != nil { | ||
os.Remove(embeddedExecutableTempPath) | ||
return nil, err | ||
} | ||
|
||
return &embeddedExecutable{ | ||
cmd: cmd, | ||
}, nil | ||
} | ||
|
||
func (e *embeddedExecutable) close() { | ||
e.cmd.Process.Kill() | ||
e.cmd.Wait() | ||
os.Remove(embeddedExecutableTempPath) | ||
} |
Oops, something went wrong.