2020-01-25 04:47:37 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
2020-02-04 03:46:30 +13:00
|
|
|
"sync"
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
"github.com/pion/webrtc/v2/pkg/media"
|
|
|
|
"n.eko.moe/neko/internal/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Peer struct {
|
|
|
|
id string
|
|
|
|
engine webrtc.MediaEngine
|
|
|
|
api *webrtc.API
|
|
|
|
video *webrtc.Track
|
|
|
|
audio *webrtc.Track
|
|
|
|
connection *webrtc.PeerConnection
|
2020-02-04 03:46:30 +13:00
|
|
|
mu sync.Mutex
|
2020-01-25 04:47:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *Peer) WriteAudioSample(sample types.Sample) error {
|
|
|
|
if err := peer.audio.WriteSample(media.Sample(sample)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *Peer) WriteVideoSample(sample types.Sample) error {
|
|
|
|
if err := peer.video.WriteSample(media.Sample(sample)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *Peer) WriteData(v interface{}) error {
|
2020-02-04 03:46:30 +13:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
2020-01-25 04:47:37 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *Peer) Destroy() error {
|
|
|
|
if peer.connection != nil && peer.connection.ConnectionState() == webrtc.PeerConnectionStateConnected {
|
|
|
|
if err := peer.connection.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|