Skip to content

Commit

Permalink
Add shell command media source drivers
Browse files Browse the repository at this point in the history
New drivers to allow the use of shell command output
as a video or audio source.
Added tests and example code for this driver.
This can be used in cases where command line programs
support media sources that pion/mediadevices doesn't yet
(EG: libcamera based cameras), or to add filters to a video or audio
stream before pion/mediadevices using ffmpeg or gstreamer.
  • Loading branch information
KW-M committed Jan 20, 2023
1 parent f8f8511 commit 1d228e0
Show file tree
Hide file tree
Showing 13 changed files with 863 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/cmdsource/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cmdsource
recorded.h264
45 changes: 45 additions & 0 deletions examples/cmdsource/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## Instructions

This example is nearly the same as the archive example, but uses the output of a shell command (in this case ffmpeg) as a video input instead of a camera. See the other examples for how to take this track and use or stream it.

### Install required codecs

In this example, we'll be using x264 as our video codec. We also use FFMPEG to generate the test video stream. (Note that cmdsource does not requre ffmpeg to run, it is just what this example uses) Therefore, we need to make sure that these are installed within our system.

Installation steps:

* [ffmpeg](https://ffmpeg.org/)
* [x264](https://github.com/pion/mediadevices#x264)

### Download cmdsource example

```
git clone https://github.com/pion/mediadevices.git
```

### Run cmdsource example

Run `cd mediadevices/examples/cmdsource && go build && ./cmdsource recorded.h264`

To stop recording, press `Ctrl+c` or send a SIGINT signal.

### Playback recorded video

Use ffplay (part of the ffmpeg project):
```
ffplay -f h264 recorded.h264
```

Or install GStreamer and run:
```
gst-launch-1.0 playbin uri=file://${PWD}/recorded.h264
```

Or run VLC media plyer:
```
vlc recorded.h264
```

A video should start playing in a window.

Congrats, you have used pion-MediaDevices! Now start building something cool
139 changes: 139 additions & 0 deletions examples/cmdsource/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

// !!!! This example requires ffmpeg to be installed !!!!

import (
"errors"
"fmt"
"image"
"io"
"os"
"os/signal"
"syscall"
"time"

"github.com/pion/mediadevices"
"github.com/pion/mediadevices/pkg/codec/x264" // This is required to use H264 video encoder
"github.com/pion/mediadevices/pkg/driver"
"github.com/pion/mediadevices/pkg/driver/cmdsource"
"github.com/pion/mediadevices/pkg/frame"
"github.com/pion/mediadevices/pkg/io/video"
"github.com/pion/mediadevices/pkg/prop"
)

// handy correlation between the names of frame formats in pion media devices and the same -pix_fmt as passed to ffmpeg
var ffmpegFrameFormatMap = map[frame.Format]string{
frame.FormatI420: "yuv420p",
frame.FormatNV21: "nv21",
frame.FormatNV12: "nv12",
frame.FormatYUY2: "yuyv422",
frame.FormatUYVY: "uyvy422",
frame.FormatZ16: "gray",
}

func ffmpegTestPatternCmd(width int, height int, frameRate float32, frameFormat frame.Format) string {
// returns the (command-line) command to tell the ffmpeg program to output a test video stream with the given pixel format, size and framerate to stdout:
return fmt.Sprintf("ffmpeg -hide_banner -f lavfi -i testsrc=size=%dx%d:rate=%f -vf realtime -f rawvideo -pix_fmt %s -", width, height, frameRate, ffmpegFrameFormatMap[frameFormat])
}

func getMediaDevicesDriverId(label string) (string, error) {
drivers := driver.GetManager().Query(func(d driver.Driver) bool {
return d.Info().Label == label
})
if len(drivers) == 0 {
return "", errors.New("Failed to find the media devices driver for device label: " + label)
}
return drivers[0].ID(), nil
}

func must(err error) {
if err != nil {
panic(err)
}
}

func main() {
if len(os.Args) != 2 {
fmt.Printf("usage: %s <path/to/output_file.h264>\n", os.Args[0])
return
}
dest := os.Args[1]

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)

x264Params, err := x264.NewParams()
must(err)
x264Params.Preset = x264.PresetMedium
x264Params.BitRate = 1_000_000 // 1mbps

codecSelector := mediadevices.NewCodecSelector(
mediadevices.WithVideoEncoders(&x264Params),
)

// configure source video properties (raw video stream format that we should expect the command to output)
label := "My Cool Video"
videoProps := prop.Media{
Video: prop.Video{
Width: 640,
Height: 480,
FrameFormat: frame.FormatI420,
FrameRate: 30,
},
// OR Audio: prop.Audio{}
}

// Add the command source:
cmdString := ffmpegTestPatternCmd(videoProps.Video.Width, videoProps.Video.Height, videoProps.Video.FrameRate, videoProps.FrameFormat)
err = cmdsource.AddVideoCmdSource(label, cmdString, []prop.Media{videoProps}, 10, true)
must(err)

// Now your video command source will be a driver in mediaDevices:
driverId, err := getMediaDevicesDriverId(label)
must(err)

mediaStream, err := mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{
Video: func(c *mediadevices.MediaTrackConstraints) {
c.DeviceID = prop.String(driverId)
},
Codec: codecSelector,
})
must(err)

videoTrack := mediaStream.GetVideoTracks()[0].(*mediadevices.VideoTrack)
defer videoTrack.Close()
//// --- OR (if the track was setup as audio) --
// audioTrack := mediaStream.GetAudioTracks()[0].(*mediadevices.AudioTrack)
// defer audioTrack.Close()

// Do whatever you want with the track, the rest of this example is the same as the archive example:
// =================================================================================================

videoTrack.Transform(video.TransformFunc(func(r video.Reader) video.Reader {
return video.ReaderFunc(func() (img image.Image, release func(), err error) {
// we send io.EOF signal to the encoder reader to stop reading. Therefore, io.Copy
// will finish its execution and the program will finish
select {
case <-sigs:
return nil, func() {}, io.EOF
default:
}

return r.Read()
})
}))

reader, err := videoTrack.NewEncodedIOReader(x264Params.RTPCodec().MimeType)
must(err)
defer reader.Close()

out, err := os.Create(dest)
must(err)

fmt.Println("Recording... Press Ctrl+c to stop")
_, err = io.Copy(out, reader)
must(err)
videoTrack.Close() // Ideally we should close the track before the io.Copy is done to save every last frame
<-time.After(100 * time.Millisecond) // Give a bit of time for the ffmpeg stream to stop cleanly before the program exits
fmt.Println("Your video has been recorded to", dest)
}
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/blackjack/webcam v0.0.0-20220329180758-ba064708e165
github.com/gen2brain/malgo v0.11.10
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.3.0
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329
github.com/pion/interceptor v0.1.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down
126 changes: 126 additions & 0 deletions pkg/driver/cmdsource/audiocmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cmdsource

import (
"encoding/binary"
"fmt"
"io"
"time"

"github.com/pion/mediadevices/pkg/driver"
"github.com/pion/mediadevices/pkg/io/audio"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pion/mediadevices/pkg/wave"
)

type audioCmdSource struct {
cmdSource
bufferSampleCount int
showStdErr bool
label string
}

func AddAudioCmdSource(label string, command string, mediaProperties []prop.Media, readTimeout uint32, sampleBufferSize int, showStdErr bool) error {
audioCmdSource := &audioCmdSource{
cmdSource: newCmdSource(command, mediaProperties, readTimeout),
bufferSampleCount: sampleBufferSize,
label: label,
showStdErr: showStdErr,
}
if len(audioCmdSource.cmdArgs) == 0 || audioCmdSource.cmdArgs[0] == "" {
return errInvalidCommand // no command specified
}

// register this audio source with the driver manager
err := driver.GetManager().Register(audioCmdSource, driver.Info{
Label: label,
DeviceType: driver.CmdSource,
Priority: driver.PriorityNormal,
})
if err != nil {
return err
}
return nil
}

func (c *audioCmdSource) AudioRecord(inputProp prop.Media) (audio.Reader, error) {
decoder, err := wave.NewDecoder(&wave.RawFormat{
SampleSize: inputProp.SampleSize,
IsFloat: inputProp.IsFloat,
Interleaved: inputProp.IsInterleaved,
})

if err != nil {
return nil, err
}

if c.showStdErr {
// get the command's standard error
stdErr, err := c.execCmd.StderrPipe()
if err != nil {
return nil, err
}
// send standard error to the console as debug logs prefixed with "{command} stdErr >"
go c.logStdIoWithPrefix(fmt.Sprintf("%s stderr > ", c.label+":"+c.cmdArgs[0]), stdErr)
}

// get the command's standard output
stdOut, err := c.execCmd.StdoutPipe()
if err != nil {
return nil, err
}

// add environment variables to the command for each media property
c.addEnvVarsFromStruct(inputProp.Audio, c.showStdErr)

// start the command
if err := c.execCmd.Start(); err != nil {
return nil, err
}

// claclulate the sample size and chunk buffer size (as a multple of the sample size)
sampleSize := inputProp.ChannelCount * inputProp.SampleSize
chunkSize := c.bufferSampleCount * sampleSize
var endienness binary.ByteOrder = binary.LittleEndian
if inputProp.IsBigEndian {
endienness = binary.BigEndian
}

var chunkBuf []byte = make([]byte, chunkSize)
doneChan := make(chan error)
r := audio.ReaderFunc(func() (chunk wave.Audio, release func(), err error) {
go func() {
if _, err := io.ReadFull(stdOut, chunkBuf); err == io.ErrUnexpectedEOF {
doneChan <- io.EOF
} else if err != nil {
doneChan <- err
}
doneChan <- nil
}()

select {
case err := <-doneChan:
if err != nil {
return nil, nil, err
} else {
decodedChunk, err := decoder.Decode(endienness, chunkBuf, inputProp.ChannelCount)
if err != nil {
return nil, nil, err
}
// FIXME: the decoder should also fill this information
switch decodedChunk := decodedChunk.(type) {
case *wave.Float32Interleaved:
decodedChunk.Size.SamplingRate = inputProp.SampleRate
case *wave.Int16Interleaved:
decodedChunk.Size.SamplingRate = inputProp.SampleRate
default:
panic("unsupported format")
}
return decodedChunk, func() {}, err
}
case <-time.After(time.Duration(c.readTimeout) * time.Second):
return nil, func() {}, errReadTimeout
}
})

return r, nil
}
Loading

0 comments on commit 1d228e0

Please sign in to comment.