Go implementation of the MediaDevices API.
Pion MediaDevices
Go implementation of the MediaDevices API
mediadevices provides access to media input devices like cameras, microphones, and screen capture. It can also be used to encode your video/audio stream to various codec selections. mediadevices abstracts away the complexities of interacting with things like hardware and codecs allowing you to focus on building appilcations, interacting only with an amazingly simple, easy, and elegant API!
go get -u github.com/pion/mediadevicesmediadevices provides a nomicrophone build tag. Use this tag to build without microphone support. This is particularly useful for:
malgo are unavailable.go build -tags nomicrophoneThe following snippet shows how to capture a camera stream and store a frame as a jpeg image:
…| Input | Linux | Mac | Windows |
|---|---|---|---|
| Camera | ✔️ | ✔️ | ✔️ |
| Microphone | ✔️ | ✔️ | ✔️ |
| Screen | ✔️ | ✔️ | ✔️ |
By default, there's no media input registered. This decision was made to allow you to play only what you need. Therefore, you need to import the associated packages for the media inputs. For example, if you want to use a camera, you need to import the camera package as a side effect:
import (
...
_ "github.com/pion/mediadevices/pkg/driver/camera"
)In order to encode your video/audio, mediadevices needs to know what codecs that you want to use and their parameters. To do this, you need to import the associated packages for the codecs, and add them to the codec selector that you'll pass to GetUserMedia:
…Since mediadevices doesn't implement the video/audio codecs, it needs to call the codec libraries from the system through cgo. Therefore, you're required to install the codec libraries before you can use them in mediadevices. In the next section, it shows a list of available codecs, where the packages are defined (documentation linked), and installation instructions.
Note: we do not provide recommendations on choosing one codec or another as it is very complex and can be subjective.
A free software library and application for encoding video streams into the H.264/MPEG-4 AVC compression format.
brew install x264apt install libx264-devA framework to enable H264 hardware encoding for Raspberry Pi or boards that use VideoCore GPUs.
A codec library which supports H.264 encoding and decoding. It is suitable for use in real time applications.
A free software video codec library from the Alliance for Open Media that implements AV1 video coding formats.
brew install svt-av1apt install libsvtav1enc-devA free software video codec library from Google and the Alliance for Open Media that implements VP8/VP9 video coding formats.
brew install libvpxapt install libvpx-devAn open source API that allows applications such as VLC media player or GStreamer to use hardware video acceleration capabilities (currently support VP8/VP9).
apt install libva-devA totally open, royalty-free, highly versatile audio codec.
brew install opusapt install libopus-devResult as of Nov 4, 2020 with Go 1.14 on a Raspberry pi 3, mediadevices can produce video, encode, send across network, and decode at 720p, 30 fps with < 500 ms latency.
The test was taken by capturing a camera stream, decoding the raw frames, encoding the video stream with mmal, and sending the stream through Webrtc.
mediadevices provides an automated driver discovery through GetUserMedia and GetDisplayMedia. The driver discover algorithm works something like:
So, when mediadevices returns failed to find the best driver that fits the constraints error, one of the following conditions might have occured:
import _ github.com/pion/mediadevices/pkg/driver/cameraexport PION_LOG_DEBUG=all to see what was too strict and tune that.RegisterDriverAdapterimport _ github.com/pion/mediadevices/pkg/driver/screen note that you will need to use GetDisplayMedia instead of GetUserMediaSince mediadevices uses cgo to access video/audio codecs, it needs to find these libraries from the system. To accomplish this, pkg-config is used for library discovery.
If you see the following error message at compile time:
# pkg-config --cflags -- vpx
Package vpx was not found in the pkg-config search path.
Perhaps you should add the directory containing `vpx.pc'
to the PKG_CONFIG_PATH environment variable
No package 'vpx' found
pkg-config: exit status 1There are 2 common problems:
.pc files for this codec (reference). In this case, you need to find where the codec library's .pc is stored, and let pkg-config knows with: export PKG_CONFIG_PATH=/path/to/directory.The library can be used with our WebRTC implementation. Please refer to that roadmap to track our major milestones.
Pion has an active community on Discord.
Follow the Pion Twitter and the Pion Bluesky for project updates and important WebRTC news.
We are always looking to support your projects. Please reach out if you have something to build! If you need commercial support or don't want to use public methods you can contact us at [email protected]
Check out the contributing wiki to join the group of amazing people making this project possible
MIT License - see LICENSE for full text
No open issues yet, or sync has not completed.