Skip to content

stream.GetVideoTracks() is panicing #666

@Onyz107

Description

@Onyz107

Your environment.

  • Version: v0.7.2
  • Browser: 1.84.132 Chromium: 142.0.7444.60 (Official Build) (64-bit)
  • Other Information -
[signal 0xc0000005 code=0x0 addr=0x30 pc=0x7ff6ff9de64d]

goroutine 1 [running]:
main.main()
        C:/Users/zaid2/OneDrive/Desktop/GoProjects/Practice/main.go:25 +0x2d
exit status 2

What did you do?

I simply copied and pasted the example usage code in the README.md and ran the program.

What did you expect?

I expected it to save the snapshot as a PNG to the disk

What happened?

It complained about a memory address or a nil pointer dereference on line 25 which is:
track := stream.GetVideoTracks()[0]

Full code:

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"
)

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)
}

EDIT: I added a check to mediadevices.GetUserMedia:

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

And this is the result:

PS C:\Users\zaid2\OneDrive\Desktop\GoProjects\Practice> go run .\main.go
panic: failed to open device: failed to connect filters

goroutine 1 [running]:
main.main()
        C:/Users/zaid2/OneDrive/Desktop/GoProjects/Practice/main.go:23 +0x145
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions