-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shell command media source drivers
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
Showing
13 changed files
with
863 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cmdsource | ||
recorded.h264 |
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,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 |
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,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) | ||
} |
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,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 | ||
} |
Oops, something went wrong.