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.

108 lines
2.7 KiB

6 years ago
package rtsp
import (
"sync"
6 years ago
"time"
"github.com/penggy/EasyGoLib/utils"
6 years ago
)
type Player struct {
*Session
Pusher *Pusher
cond *sync.Cond
queue []*RTPPack
queueLimit int
dropPacketWhenPaused bool
paused bool
6 years ago
}
func NewPlayer(session *Session, pusher *Pusher) (player *Player) {
queueLimit := utils.Conf().Section("rtsp").Key("player_queue_limit").MustInt(0)
dropPacketWhenPaused := utils.Conf().Section("rtsp").Key("drop_packet_when_paused").MustInt(0)
6 years ago
player = &Player{
Session: session,
Pusher: pusher,
cond: sync.NewCond(&sync.Mutex{}),
queue: make([]*RTPPack, 0),
queueLimit: queueLimit,
dropPacketWhenPaused: dropPacketWhenPaused != 0,
paused: false,
6 years ago
}
session.StopHandles = append(session.StopHandles, func() {
pusher.RemovePlayer(player)
player.cond.Broadcast()
})
return
}
func (player *Player) QueueRTP(pack *RTPPack) *Player {
logger := player.logger
6 years ago
if pack == nil {
logger.Printf("player queue enter nil pack, drop it")
6 years ago
return player
}
if player.paused && player.dropPacketWhenPaused {
return player
}
6 years ago
player.cond.L.Lock()
player.queue = append(player.queue, pack)
if oldLen := len(player.queue); player.queueLimit > 0 && oldLen > player.queueLimit {
player.queue = player.queue[1:]
if player.debugLogEnable {
len := len(player.queue)
logger.Printf("Player %s, QueueRTP, exceeds limit(%d), drop %d old packets, current queue.len=%d\n", player.String(), player.queueLimit, oldLen - len, len)
}
}
6 years ago
player.cond.Signal()
player.cond.L.Unlock()
return player
}
func (player *Player) Start() {
logger := player.logger
6 years ago
timer := time.Unix(0, 0)
6 years ago
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:]
}
queueLen := len(player.queue)
6 years ago
player.cond.L.Unlock()
if player.paused {
continue
}
6 years ago
if pack == nil {
if !player.Stoped {
logger.Printf("player not stoped, but queue take out nil pack")
6 years ago
}
continue
}
if err := player.SendRTP(pack); err != nil {
logger.Println(err)
6 years ago
}
6 years ago
elapsed := time.Now().Sub(timer)
if player.debugLogEnable && elapsed >= 30*time.Second {
logger.Printf("Player %s, Send a package.type:%d, queue.len=%d\n", player.String(), pack.Type, queueLen)
6 years ago
timer = time.Now()
}
6 years ago
}
}
func (player *Player) Pause(paused bool) {
if paused {
player.logger.Printf("Player %s, Pause\n", player.String())
} else {
player.logger.Printf("Player %s, Play\n", player.String())
}
player.cond.L.Lock()
if paused && player.dropPacketWhenPaused && len(player.queue) > 0 {
player.queue = make([]*RTPPack, 0)
}
player.paused = paused
player.cond.L.Unlock()
}