mirror of
https://github.com/Kalit31/AR-Video-Streaming-over-WebRTC.git
synced 2026-04-22 23:27:03 +08:00
Fix logic to update ICECandidates to ensure successful WebRTC connection
This commit is contained in:
@@ -0,0 +1 @@
|
||||
bin/*
|
||||
@@ -0,0 +1,25 @@
|
||||
# Variables
|
||||
APP_NAME = main
|
||||
BIN_DIR = bin
|
||||
GO_FILES = *.go
|
||||
|
||||
# Create the bin directory if it doesn't exist
|
||||
.PHONY: build run-server run-client clean all
|
||||
build:
|
||||
mkdir -p $(BIN_DIR) # Create bin directory if it doesn't exist
|
||||
go build -o $(BIN_DIR)/$(APP_NAME) $(GO_FILES)
|
||||
|
||||
# Run the server
|
||||
run-server: build
|
||||
./$(BIN_DIR)/$(APP_NAME) --server
|
||||
|
||||
# Run the client
|
||||
run-client: build
|
||||
./$(BIN_DIR)/$(APP_NAME) --client
|
||||
|
||||
# Clean up build artifacts
|
||||
clean:
|
||||
rm -f $(BIN_DIR)/$(APP_NAME)
|
||||
|
||||
# Default target
|
||||
all: build
|
||||
+63
-9
@@ -15,14 +15,20 @@ type Message struct {
|
||||
|
||||
var (
|
||||
answerChan = make(chan string) // Global variable for the channel
|
||||
userPeerConnection *webrtc.PeerConnection
|
||||
userPeerConnection *webrtc.PeerConnection = nil
|
||||
connectionEstablishedChan = make(chan bool)
|
||||
)
|
||||
|
||||
func createPeerConnection() (*webrtc.PeerConnection, error) {
|
||||
func createPeerConnection(conn *websocket.Conn) (*webrtc.PeerConnection, error) {
|
||||
config := webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{URLs: []string{"stun:stun.l.google.com:19302", "stun:stun1.l.google.com:19302", "stun:stun2.l.google.com:19302"}}, // Add your STUN server here
|
||||
{
|
||||
URLs: []string{
|
||||
"stun:stun.l.google.com:19302",
|
||||
"stun:stun1.l.google.com:19302",
|
||||
"stun:stun2.l.google.com:19302",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -32,6 +38,19 @@ func createPeerConnection() (*webrtc.PeerConnection, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
peerConnection.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
||||
if candidate == nil {
|
||||
return
|
||||
}
|
||||
// Send this candidate to the remote peer
|
||||
fmt.Println("New ICE candidate:", candidate.ToJSON())
|
||||
iceCandidateMsg := Message{
|
||||
Type: "iceCandidate",
|
||||
Content: candidate.ToJSON().Candidate,
|
||||
}
|
||||
conn.WriteJSON(iceCandidateMsg)
|
||||
})
|
||||
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
||||
@@ -45,6 +64,18 @@ func createPeerConnection() (*webrtc.PeerConnection, error) {
|
||||
fmt.Println("Connection failed!")
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/h264"}, "video", "pion")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add the track to the peer connection
|
||||
_, err = peerConnection.AddTrack(videoTrack)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return peerConnection, nil
|
||||
}
|
||||
@@ -88,7 +119,7 @@ func openCameraFeed(peerConnection *webrtc.PeerConnection) error {
|
||||
|
||||
|
||||
func establishConnectionWithPeer(conn *websocket.Conn){
|
||||
peerConnection, err := createPeerConnection()
|
||||
peerConnection, err := createPeerConnection(conn)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -130,7 +161,7 @@ func establishConnectionWithPeer(conn *websocket.Conn){
|
||||
|
||||
func handleOffer(conn *websocket.Conn, msg Message){
|
||||
fmt.Println("Received offer")
|
||||
peerConnection, err := createPeerConnection()
|
||||
peerConnection, err := createPeerConnection(conn)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -165,10 +196,32 @@ func handleOffer(conn *websocket.Conn, msg Message){
|
||||
connectionEstablishedChan <- true
|
||||
}
|
||||
|
||||
func handleAnswer(conn *websocket.Conn, msg Message){
|
||||
func handleAnswer(msg Message){
|
||||
answerChan <- msg.Content
|
||||
}
|
||||
|
||||
func addICECandidate(msg Message){
|
||||
fmt.Println("Received ICE Candidate:", msg.Content)
|
||||
|
||||
if (userPeerConnection == nil){
|
||||
fmt.Println("Peer connection not created yet. Returning...")
|
||||
return
|
||||
}
|
||||
|
||||
// Create a new ICE candidate from the received content
|
||||
candidate := webrtc.ICECandidateInit{
|
||||
Candidate: msg.Content,
|
||||
}
|
||||
|
||||
// Add the ICE candidate to the peer connection
|
||||
if err := userPeerConnection.AddICECandidate(candidate); err != nil {
|
||||
log.Println("Failed to add ICE candidate:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("ICE Candidate added successfully.")
|
||||
}
|
||||
|
||||
func Run() {
|
||||
// Connect to the WebSocket server
|
||||
url := "ws://localhost:8080/ws"
|
||||
@@ -195,7 +248,9 @@ func Run() {
|
||||
} else if (inputMsg.Type == "offer"){
|
||||
go handleOffer(conn, inputMsg)
|
||||
} else if (inputMsg.Type == "answer"){
|
||||
go handleAnswer(conn, inputMsg)
|
||||
go handleAnswer(inputMsg)
|
||||
} else if(inputMsg.Type == "iceCandidate"){
|
||||
go addICECandidate(inputMsg)
|
||||
}
|
||||
}
|
||||
}(conn)
|
||||
@@ -204,14 +259,13 @@ func Run() {
|
||||
Type: "join",
|
||||
Content: "true",
|
||||
}
|
||||
// fmt.Println(msg)
|
||||
conn.WriteJSON(msg)
|
||||
|
||||
|
||||
<-connectionEstablishedChan
|
||||
fmt.Println("Successfully established a WebRTC connection between clients")
|
||||
|
||||
openCameraFeed(userPeerConnection)
|
||||
// openCameraFeed(userPeerConnection)
|
||||
|
||||
select {}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"websocket_tests/client"
|
||||
"websocket_tests/server"
|
||||
server "websocket_tests/signalling_server"
|
||||
)
|
||||
|
||||
func main(){
|
||||
|
||||
@@ -32,3 +32,5 @@ https://github.com/asticode/go-astiav
|
||||
https://jsfiddle.net/z17q28cd/
|
||||
|
||||
ffmpeg -f v4l2 -i /dev/video0 -f mpegts udp://224.0.0.251:5353
|
||||
|
||||
netstat -anu|sort -nk4
|
||||
|
||||
Reference in New Issue
Block a user