2020-11-07 21:04:26 -08:00
2020-10-27 21:08:12 -07:00
2020-02-24 18:57:42 -08:00
2020-11-06 14:41:06 -08:00
2020-10-30 17:32:40 -07:00
2020-02-24 18:57:42 -08:00
2020-01-05 22:12:44 -08:00
2020-11-06 14:41:06 -08:00
2019-11-19 21:04:32 -08:00
2020-01-05 22:12:44 -08:00
2020-11-01 15:14:08 -08:00
2020-10-02 01:42:01 -04:00
2020-02-14 21:44:38 -08:00
2020-11-03 07:27:32 +00:00
2020-11-03 07:27:32 +00:00
2020-06-02 12:07:05 -04:00
2020-10-30 17:32:40 -07:00
2020-10-30 00:33:55 -07:00
2020-10-30 00:33:55 -07:00
2020-11-06 14:41:06 -08:00
2020-02-18 16:36:28 -08:00
2020-11-02 22:12:43 -08:00
2020-11-01 15:31:36 -08:00
2020-10-30 00:33:55 -07:00
2020-10-30 00:33:55 -07:00
2020-11-02 22:28:01 -08:00


Pion MediaDevices

Go implementation of the MediaDevices API

Slack Widget Build status GoDoc Coverage Status License: MIT


MediaDevices provides access to connected media input devices like cameras and microphones, as well as screen sharing. It can also be used to encode your video/audio stream to various codec selections.

The focus of the project has been to seek out a simple and elegant design for writing media pipelines.

Install

go get -u github.com/pion/mediadevices

Usage

The following snippet shows how to capture a camera stream and store a frame as a jpeg image:

package main

import (
	"image/jpeg"
	"os"

	"github.com/pion/mediadevices"
	"github.com/pion/mediadevices/pkg/prop"

	// This is required to register camera adapter
	_ "github.com/pion/mediadevices/pkg/driver/camera" 
	// Note: If you don't have a camera or your adapters are not supported,
	//       you can always swap your adapters with our dummy adapters below.
	// _ "github.com/pion/mediadevices/pkg/driver/videotest"
)

func main() {
	stream, _ := mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{
		Video: func(constraint *mediadevices.MediaTrackConstraints) {
			// Query for ideal resolutions
			constraint.Width = prop.Int(600)
			constraint.Height = prop.Int(400)
		},
	})

	// Since track can represent audio as well, we need to cast it to 
	// *mediadevices.VideoTrack to get video specific functionalities
	track := stream.GetVideoTracks()[0]
	videoTrack := track.(*mediadevices.VideoTrack)
	defer videoTrack.Close()

	// Create a new video reader to get the decoded frames. Release is used 
	// to return the buffer to hold frame back to the source so that the buffer 
	// can be reused for the next frames.
	videoReader := videoTrack.NewReader(false)
	frame, release, _ := videoReader.Read()
	defer release()

	// Since frame is the standard image.Image, it's compatible with Go standard 
	// library. For example, capturing the first frame and store it as a jpeg image.
	output, _ := os.Create("frame.jpg")
	jpeg.Encode(output, frame, nil)
}


More Examples

  • Webrtc - Use Webrtc to create a realtime peer-to-peer video call
  • Face Detection - Use a machine learning algorithm to detect faces in a camera stream
  • RTP Stream - Capture camera stream, encode it in H264/VP8/VP9, and send it to a RTP server
  • HTTP Broadcast - Broadcast camera stream through HTTP with MJPEG

Available Media Inputs

Input Linux Mac Windows
Camera ✔️ ✔️ ✔️
Microphone ✔️ ✔️ ✔️
Screen ✔️ ✖️ ✖️

Available Codecs

Video Codecs

x264

A free software library and application for encoding video streams into the H.264/MPEG-4 AVC compression format.

mmal

A framework to enable H264 hardware encoding for Raspberry Pi or boards that use VideoCore GPUs.

openh264

A codec library which supports H.264 encoding and decoding. It is suitable for use in real time applications.

vpx

A free software video codec library from Google and the Alliance for Open Media that implements VP8/VP9 video coding formats.

vaapi

An open source API that allows applications such as VLC media player or GStreamer to use hardware video acceleration capabilities (currently support VP8/VP9).

Audio Codecs

opus

A totally open, royalty-free, highly versatile audio codec.

Benchmark

Result as of Nov 4, 2020 with Go 1.14 on a Raspberry pi 3, mediadevices can produce a 720p at 30 fps with <500ms latency video.

The test was taken by capturing a camera stream, decode raw frames, encode the video stream with mmal to H264, and send the stream through Webrtc.

FAQ

Failed to find the best driver that fits the constraints

mediadevices provides an automated driver discovery through GetUserMedia and GetDisplayMedia. In an oversimplified explanation, the discovery algorithm as followed:

  1. Open all registered drivers
  2. Get all properties (property describes what a driver is capable of, e.g. resolution, frame rate, etc.) from opened drivers
  3. Find the best property that meets the criteria

So, when mediadevices returns failed to find the best driver that fits the constraints error, one of the following conditions might have occured:

  • In your program, the driver has never been imported as a side effect, e.g. import _ github.com/pion/mediadevices/pkg/driver/camera
  • Your constraint is too strict that there's no driver can fullfil your requirements. In this case, you can try to turn up the debug level by specifying the following environment variable: export PION_LOG_DEBUG=all
  • Your driver is not supported/implemented. In this case, you can either wait for the maintainers to implement it. Or, you can implement it yourself and register it through RegisterDriverAdapter

Failed to find vpx/x264/mmal/opus codecs

Since mediadevices uses cgo to access video/audio codecs, it needs to find these libraries from the system. To do that, mediadevices uses pkg-config 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 1

There are 2 common problems:

  • The required codec library is not installed (vpx in this example). In this case, please refer to the available codecs.
  • Pkg-config fails to find the .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.

Community

Pion has an active community on the Slack.

Follow the Pion Twitter 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 team@pion.ly

Contributing

Check out the contributing wiki to join the group of amazing people making this project possible:

License

MIT License - see LICENSE for full text

Description
MediaDevices API 的 Go 实现,提供对媒体输入设备(如摄像头、麦克风和屏幕捕获)的访问。它还可用于将您的视频/音频流编码为各种编解码器选择。 抽象出与硬件和编解码器等交互的复杂性,让您专注于构建应用程序,仅与极其简单、轻松和优雅的 API 进行交互!
Readme MIT 50 MiB
Languages
Go 67.2%
C 25.6%
C++ 4.5%
Objective-C 1.7%
Makefile 0.6%
Other 0.3%