ashellunts / ffmpeg-to-webrtc Goto Github PK
View Code? Open in Web Editor NEWStream video from ffmpeg to web(rtc)
License: MIT License
Stream video from ffmpeg to web(rtc)
License: MIT License
Hello how are you?
Now I need to receive the sound from the browser microphone, this code receives the RTP Track and sends it via STDIN to ffmpeg, but I don't hear any sound, what am I doing wrong
package main
import (
"fmt"
"time"
"os"
"os/exec"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
//gst "gstreamer-sink"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
// Prepare the configuration
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
if err != nil {
panic(err)
}
// Set a handler for when a new remote track starts, this handler creates a gstreamer pipeline
// for the given codec
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
go func() {
ticker := time.NewTicker(time.Second * 3)
for range ticker.C {
rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
if rtcpSendErr != nil {
fmt.Println(rtcpSendErr)
}
}
}()
//codecName := strings.Split(track.Codec().RTPCodecCapability.MimeType, "/")[1]
//fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), codecName)
//pipeline := gst.CreatePipeline(track.PayloadType(), strings.ToLower(codecName))
//pipeline.Start()
buf := make([]byte, 1400)
chBuff := make(chan []byte, 1400)
go playTrack(chBuff)
for {
i, _, readErr := track.Read(buf)
if readErr != nil {
panic(err)
}
chBuff <- buf[:i]
//pipeline.Push(buf[:i])
//fmt.Printf("%x", buf[:i])
//fmt.Println(track.PayloadType())
}
})
// 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())
})
// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
Decode(MustReadStdin(), &offer)
// 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 playTrack(ch <-chan []byte){
//cmd := exec.Command("ffmpeg", "-i", "pipe:0", "-f", "alsa", "default")
cmd:= exec.Command("ffmpeg", "-i", "pipe:0", "-c:a", "copy", "-sample_fmt", "s16p", "-ssrc", "1", "-payload_type", "111", "-b", "96k", "-f", "alsa", "default")
cmd.Stderr = os.Stderr // bind log stream to stderr
//cmd.Stdout = resultBuffer // stdout result will be written here
stdin, err := cmd.StdinPipe() // Open stdin pipe
check(err)
err = cmd.Start() // Start a process on another goroutine
check(err)
for {
_, err = stdin.Write(<-ch) // pump audio data to stdin pipe
check(err)
}
err = stdin.Close() // close the stdin, or ffmpeg will wait forever
check(err)
err = cmd.Wait() // wait until ffmpeg finish
check(err)
}
I'm trying to use this to make a web remote desktop client, and when using (edit: not sure this is the issue) I just get -preset ultrafast -tune zerolatency
All video frames parsed and sent
I am trying to stream my cctv streams to webrtc i have alredy tested many commands none of them works.
It returns "All video frames parsed and sent"
Please help me.
I am using windows planning to deploy on linux.
Thanks for the great example h264, could you help me to upload the audio with ffmpeg?
When using the h.264 encoding of the warehouse demo, the native delay is about 10 seconds. But if I switch to vp8, the basic delay is about 200 milliseconds. Why is this? I just switched h264reader.NewReader
to ivfreader.NewWith
Hi, where do I find the SDP from ffmpeg-to-webrtc?
I assume it's meant to appear in the commandline?
Nothing seems to appear after such as command is executed;
go run . -rtbufsize 100M -i test.mp4 -pix_fmt yuv420p -c:v libx264 -bsf:v h264_mp4toannexb -b:v 2M -max_delay 0 -bf 0 -f h264 -
I added ffmpeg.exe into the src directory.
Thanks!
As in the title.
I once looked for a solution to push h.264 stream from IP camera without reencoding via WebRTC and failed completely. I don't remember the details, but it got stuck in my head that every solution required re-encoding.
Reencoding multiple 1080p streams would kill the server and I'd like to push several streams via WebRTC at the same time.
Sorry if my question is incomplete at some point - I'm definitely a beginner when it comes to streaming.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.