mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-17 17:04:22 +00:00
Delete signal dir, webrtc is no longer in use
This commit is contained in:
parent
2b73b5825e
commit
3956a2cedf
5 changed files with 0 additions and 384 deletions
File diff suppressed because one or more lines are too long
|
|
@ -1,31 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package signal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// HTTPSDPServer starts a HTTP Server that consumes SDPs
|
||||
func HTTPSDPServer(port int) chan string {
|
||||
sdpChan := make(chan string)
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := ioutil.ReadAll(r.Body)
|
||||
fmt.Fprintf(w, "done")
|
||||
sdpChan <- string(body)
|
||||
})
|
||||
|
||||
go func() {
|
||||
// nolint: gosec
|
||||
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
return sdpChan
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package signal
|
||||
|
||||
import "github.com/pion/randutil"
|
||||
|
||||
// RandSeq generates a random string to serve as dummy data
|
||||
//
|
||||
// It returns a deterministic sequence of values each time a program is run.
|
||||
// Use rand.Seed() function in your real applications.
|
||||
func RandSeq(n int) string {
|
||||
val, err := randutil.GenerateCryptoRandomString(n, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
121
signal/signal.go
121
signal/signal.go
|
|
@ -1,121 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Package signal contains helpers to exchange the SDP session
|
||||
// description between examples.
|
||||
package signal
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Allows compressing offer/answer to bypass terminal input limits.
|
||||
const compress = false
|
||||
|
||||
// MustReadStdin blocks until input is received from stdin
|
||||
func MustReadStdin() string {
|
||||
filename := "/home/chigozirim/Documents/my-docs/song-recognition/signal/base64.txt"
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
r := bufio.NewReaderSize(file, 16384)
|
||||
|
||||
var in string
|
||||
for {
|
||||
var err error
|
||||
in, err = r.ReadString('\n')
|
||||
if err != io.EOF {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
in = strings.TrimSpace(in)
|
||||
if len(in) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
|
||||
return in
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
212
signal/webrtc.go
212
signal/webrtc.go
|
|
@ -1,212 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//go:build !js
|
||||
// +build !js
|
||||
|
||||
// save-to-disk is a simple application that shows how to record your webcam/microphone using Pion WebRTC and save VP8/Opus to disk.
|
||||
package signal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/pion/interceptor"
|
||||
"github.com/pion/webrtc/v4"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"song-recognition/shazam"
|
||||
|
||||
"github.com/pion/webrtc/v4/pkg/media"
|
||||
)
|
||||
|
||||
func SaveToDisk(i media.Writer, track *webrtc.TrackRemote) {
|
||||
defer func() {
|
||||
if err := i.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
rtpPacket, _, err := track.ReadRTP()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if err := i.WriteRTP(rtpPacket); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SaveToBytes(track *webrtc.TrackRemote) ([]byte, error) {
|
||||
var audioData []byte
|
||||
|
||||
for {
|
||||
rtpPacket, _, err := track.ReadRTP()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Extract audio payload from RTP packet
|
||||
payload := rtpPacket.Payload
|
||||
|
||||
// Append audio payload to audioData
|
||||
audioData = append(audioData, payload...)
|
||||
// fmt.Println("ByteArray: ", audioData)
|
||||
}
|
||||
|
||||
return audioData, nil
|
||||
}
|
||||
|
||||
func MatchSampleAudio(track *webrtc.TrackRemote) ([]primitive.M, error) {
|
||||
// Use time.After to stop after 15 seconds
|
||||
stop := time.After(20 * time.Second)
|
||||
|
||||
// Use a ticker to process sampleAudio every 2 seconds
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
var sampleAudio []byte
|
||||
var matches []shazam.Match
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
// Process sampleAudio every 2 seconds
|
||||
if len(sampleAudio) > 0 {
|
||||
matchess, err := shazam.FindMatches(sampleAudio)
|
||||
matches = matchess
|
||||
if err != nil {
|
||||
fmt.Println("An Error: ", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Reset sampleAudio for fresh input
|
||||
// sampleAudio = nil
|
||||
|
||||
// if len(matches) > 0 {
|
||||
// fmt.Println("FOUND A MATCH! - ", matches)
|
||||
// jsonData, err := json.Marshal(matches)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return "", nil
|
||||
// }
|
||||
// return string(jsonData), nil
|
||||
// }
|
||||
}
|
||||
case <-stop:
|
||||
// Stop after 15 seconds
|
||||
fmt.Println("Stopped after 15 seconds")
|
||||
var matchesChunkTags []primitive.M
|
||||
for _, match := range matches {
|
||||
matchesChunkTags = append(matchesChunkTags, match.ChunkTag)
|
||||
}
|
||||
return matchesChunkTags, nil
|
||||
|
||||
default:
|
||||
// Read RTP packets and accumulate sampleAudio
|
||||
rtpPacket, _, err := track.ReadRTP()
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
return nil, fmt.Errorf("error reading RTP packet: %d", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Extract audio payload from RTP packet
|
||||
payload := rtpPacket.Payload
|
||||
|
||||
sampleAudio = append(sampleAudio, payload...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// nolint:gocognit
|
||||
func SetupWebRTC(encodedOffer string) *webrtc.PeerConnection {
|
||||
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
|
||||
|
||||
// Create a MediaEngine object to configure the supported codec
|
||||
m := &webrtc.MediaEngine{}
|
||||
|
||||
// Setup the codecs you want to use.
|
||||
// We'll use Opus, but you can also define your own
|
||||
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 1, SDPFmtpLine: "", RTCPFeedback: nil},
|
||||
PayloadType: 111,
|
||||
}, webrtc.RTPCodecTypeAudio); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create a InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
|
||||
// This provides NACKs, RTCP Reports and other features.
|
||||
i := &interceptor.Registry{}
|
||||
|
||||
// Register a intervalpli factory
|
||||
// This interceptor sends a PLI every 3 seconds. A PLI causes a keyframe to be generated by the sender.
|
||||
// intervalPliFactory, err := intervalpli.NewReceiverInterceptor()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// i.Add(intervalPliFactory)
|
||||
|
||||
// // Use the default set of Interceptors
|
||||
// if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// Create the API object with the MediaEngine
|
||||
api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i))
|
||||
|
||||
// Prepare the configuration
|
||||
config := webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create a new RTCPeerConnection
|
||||
peerConnection, err := api.NewPeerConnection(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Wait for the offer to be pasted
|
||||
offer := webrtc.SessionDescription{}
|
||||
Decode(encodedOffer, &offer)
|
||||
|
||||
// Set the remote SessionDescription
|
||||
err = peerConnection.SetRemoteDescription(offer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create 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
|
||||
|
||||
return peerConnection
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue