mirror of https://github.com/EasyDarwin/EasyDarwin
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.4 KiB
72 lines
1.4 KiB
package rtsp
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Player struct {
|
|
*Session
|
|
Pusher *Pusher
|
|
cond *sync.Cond
|
|
queue []*RTPPack
|
|
}
|
|
|
|
func NewPlayer(session *Session, pusher *Pusher) (player *Player) {
|
|
player = &Player{
|
|
Session: session,
|
|
Pusher: pusher,
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
queue: make([]*RTPPack, 0),
|
|
}
|
|
session.StopHandles = append(session.StopHandles, func() {
|
|
pusher.RemovePlayer(player)
|
|
player.cond.Broadcast()
|
|
})
|
|
return
|
|
}
|
|
|
|
func (player *Player) QueueRTP(pack *RTPPack) *Player {
|
|
logger := player.logger
|
|
if pack == nil {
|
|
logger.Printf("player queue enter nil pack, drop it")
|
|
return player
|
|
}
|
|
player.cond.L.Lock()
|
|
player.queue = append(player.queue, pack)
|
|
player.cond.Signal()
|
|
player.cond.L.Unlock()
|
|
return player
|
|
}
|
|
|
|
func (player *Player) Start() {
|
|
logger := player.logger
|
|
timer := time.Unix(0, 0)
|
|
for !player.Stoped {
|
|
var pack *RTPPack
|
|
player.cond.L.Lock()
|
|
if len(player.queue) == 0 {
|
|
player.cond.Wait()
|
|
}
|
|
if len(player.queue) > 0 {
|
|
pack = player.queue[0]
|
|
player.queue = player.queue[1:]
|
|
}
|
|
player.cond.L.Unlock()
|
|
if pack == nil {
|
|
if !player.Stoped {
|
|
logger.Printf("player not stoped, but queue take out nil pack")
|
|
}
|
|
continue
|
|
}
|
|
if err := player.SendRTP(pack); err != nil {
|
|
logger.Println(err)
|
|
}
|
|
elapsed := time.Now().Sub(timer)
|
|
if elapsed >= 30*time.Second {
|
|
logger.Printf("Send a package.type:%d\n", pack.Type)
|
|
timer = time.Now()
|
|
}
|
|
}
|
|
}
|