Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile mediadevices #360

Open
projetoarduino opened this issue Sep 27, 2021 · 0 comments
Open

Compile mediadevices #360

projetoarduino opened this issue Sep 27, 2021 · 0 comments
Labels
question Further information is requested

Comments

@projetoarduino
Copy link

projetoarduino commented Sep 27, 2021

Hi

I follow the examples and the code show this

DeviceID: 77665c83-618f-429d-a1ae-b7b2279ed22d
Video:
Width: 0
Height: 0
FrameRate: 0
FrameFormat:
Audio:
ChannelCount: 1
Latency: 20ms
SampleRate: 48000
SampleSize: 4
IsBigEndian: false
IsFloat: true
IsInterleaved: true
mediadevices/driver/microphone DEBUG: 15:14:05.061486 microphone.go:41: ma_device_init() called with invalid config. Capture channel map is invalid.
panic: miniaudio: invalid device config

my code

`// export GO111MODULE=on
//
//../pkg/mod/github.com/pion/mediadevices@v0.2.0/track.go:342:33: cannot use mtu (type int) as type uint16
// sobre esse erro editar o arquivo track.go
// e colocar isso uma linha acima do erro u := uint16(mtu)

package main

import (
"fmt"
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"io/ioutil"
"os"

"github.com/pion/mediadevices"
//"github.com/pion/mediadevices/examples/internal/signal"
"github.com/pion/mediadevices/pkg/frame"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pion/webrtc/v3"

// If you don't like x264, you can also use vpx by importing as below
// "github.com/pion/mediadevices/pkg/codec/vpx" // This is required to use VP8/VP9 video encoder
// or you can also use openh264 for alternative h264 implementation
//"github.com/pion/mediadevices/pkg/codec/openh264"
// or if you use a raspberry pi like, you can use mmal for using its hardware encoder
// "github.com/pion/mediadevices/pkg/codec/mmal"
"github.com/pion/mediadevices/pkg/codec/opus" // This is required to use opus audio encoder
"github.com/pion/mediadevices/pkg/codec/x264" // This is required to use h264 video encoder

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

)

// Allows compressing offer/answer to bypass terminal input limits.
const compress = false

func main() {
fmt.Println("Cverino2 v0.1")

config := webrtc.Configuration{
	ICEServers: []webrtc.ICEServer{
		{
			URLs: []string{"stun:stun.l.google.com:19302"},
		},
	},
}

// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
Decode(readFile(), &offer)

// Create a new RTCPeerConnection
x264Params, err := x264.NewParams()
if err != nil {
	panic(err)
}
x264Params.BitRate = 500_000 // 500kbps

opusParams, err := opus.NewParams()
if err != nil {
	panic(err)
}
codecSelector := mediadevices.NewCodecSelector(
	mediadevices.WithVideoEncoders(&x264Params),
	mediadevices.WithAudioEncoders(&opusParams),
)

mediaEngine := webrtc.MediaEngine{}
codecSelector.Populate(&mediaEngine)
api := webrtc.NewAPI(webrtc.WithMediaEngine(&mediaEngine))
peerConnection, err := api.NewPeerConnection(config)
if err != nil {
	panic(err)
}

// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
	fmt.Printf("Connection State has changed %s \n", connectionState.String())
})

s, err := mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{
	Video: func(c *mediadevices.MediaTrackConstraints) {
		c.FrameFormat = prop.FrameFormat(frame.FormatI420)
		c.Width = prop.Int(640)
		c.Height = prop.Int(480)
	},
	Audio: func(c *mediadevices.MediaTrackConstraints) {
	},
	Codec: codecSelector,
})
if err != nil {
	panic(err)
}

for _, track := range s.GetTracks() {
	track.OnEnded(func(err error) {
		fmt.Printf("Track (ID: %s) ended with error: %v\n",
			track.ID(), err)
	})

	_, err = peerConnection.AddTransceiverFromTrack(track,
		webrtc.RtpTransceiverInit{
			Direction: webrtc.RTPTransceiverDirectionSendonly,
		},
	)
	if err != nil {
		panic(err)
	}
}

// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
if err != nil {
	panic(err)
}

// Create an answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
	panic(err)
}

// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)

// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(answer)
if err != nil {
	panic(err)
}

// Block until ICE Gathering is complete, disabling trickle ICE
// we do this because we only can exchange one signaling message
// in a production application you should exchange ICE Candidates via OnICECandidate
<-gatherComplete

// Output the answer in base64 so we can paste it in browser
fmt.Println(Encode(*peerConnection.LocalDescription()))

// Block forever
select {}

}

func check(e error) {
if e != nil {
panic(e)
}
}

func readFile() string {
dat, err := os.ReadFile("./dat.txt")
check(err)
return string(dat)
}

// Encode encodes the input in base64
// It can optionally zip the input before encoding
func Encode(obj interface{}) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}

if compress {
	b = zip(b)
}

return base64.StdEncoding.EncodeToString(b)

}

// Decode decodes the input from base64
// It can optionally unzip the input after decoding
func Decode(in string, obj interface{}) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}

if compress {
	b = unzip(b)
}

err = json.Unmarshal(b, obj)
if err != nil {
	panic(err)
}

}

func zip(in []byte) []byte {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
_, err := gz.Write(in)
if err != nil {
panic(err)
}
err = gz.Flush()
if err != nil {
panic(err)
}
err = gz.Close()
if err != nil {
panic(err)
}
return b.Bytes()
}

func unzip(in []byte) []byte {
var b bytes.Buffer
_, err := b.Write(in)
if err != nil {
panic(err)
}
r, err := gzip.NewReader(&b)
if err != nil {
panic(err)
}
res, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
return res
}`

@at-wat at-wat added the question Further information is requested label Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants