|
|
@ -3,6 +3,7 @@ package rtsp
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/penggy/EasyGoLib/utils"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Player struct {
|
|
|
|
type Player struct {
|
|
|
@ -10,14 +11,22 @@ type Player struct {
|
|
|
|
Pusher *Pusher
|
|
|
|
Pusher *Pusher
|
|
|
|
cond *sync.Cond
|
|
|
|
cond *sync.Cond
|
|
|
|
queue []*RTPPack
|
|
|
|
queue []*RTPPack
|
|
|
|
|
|
|
|
queueLimit int
|
|
|
|
|
|
|
|
dropPacketWhenPaused bool
|
|
|
|
|
|
|
|
paused bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewPlayer(session *Session, pusher *Pusher) (player *Player) {
|
|
|
|
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)
|
|
|
|
player = &Player{
|
|
|
|
player = &Player{
|
|
|
|
Session: session,
|
|
|
|
Session: session,
|
|
|
|
Pusher: pusher,
|
|
|
|
Pusher: pusher,
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
queue: make([]*RTPPack, 0),
|
|
|
|
queue: make([]*RTPPack, 0),
|
|
|
|
|
|
|
|
queueLimit: queueLimit,
|
|
|
|
|
|
|
|
dropPacketWhenPaused: dropPacketWhenPaused != 0,
|
|
|
|
|
|
|
|
paused: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session.StopHandles = append(session.StopHandles, func() {
|
|
|
|
session.StopHandles = append(session.StopHandles, func() {
|
|
|
|
pusher.RemovePlayer(player)
|
|
|
|
pusher.RemovePlayer(player)
|
|
|
@ -32,8 +41,18 @@ func (player *Player) QueueRTP(pack *RTPPack) *Player {
|
|
|
|
logger.Printf("player queue enter nil pack, drop it")
|
|
|
|
logger.Printf("player queue enter nil pack, drop it")
|
|
|
|
return player
|
|
|
|
return player
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if player.paused && player.dropPacketWhenPaused {
|
|
|
|
|
|
|
|
return player
|
|
|
|
|
|
|
|
}
|
|
|
|
player.cond.L.Lock()
|
|
|
|
player.cond.L.Lock()
|
|
|
|
player.queue = append(player.queue, pack)
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
player.cond.Signal()
|
|
|
|
player.cond.Signal()
|
|
|
|
player.cond.L.Unlock()
|
|
|
|
player.cond.L.Unlock()
|
|
|
|
return player
|
|
|
|
return player
|
|
|
@ -52,7 +71,11 @@ func (player *Player) Start() {
|
|
|
|
pack = player.queue[0]
|
|
|
|
pack = player.queue[0]
|
|
|
|
player.queue = player.queue[1:]
|
|
|
|
player.queue = player.queue[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queueLen := len(player.queue)
|
|
|
|
player.cond.L.Unlock()
|
|
|
|
player.cond.L.Unlock()
|
|
|
|
|
|
|
|
if player.paused {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
if pack == nil {
|
|
|
|
if pack == nil {
|
|
|
|
if !player.Stoped {
|
|
|
|
if !player.Stoped {
|
|
|
|
logger.Printf("player not stoped, but queue take out nil pack")
|
|
|
|
logger.Printf("player not stoped, but queue take out nil pack")
|
|
|
@ -63,9 +86,23 @@ func (player *Player) Start() {
|
|
|
|
logger.Println(err)
|
|
|
|
logger.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elapsed := time.Now().Sub(timer)
|
|
|
|
elapsed := time.Now().Sub(timer)
|
|
|
|
if elapsed >= 30*time.Second {
|
|
|
|
if player.debugLogEnable && elapsed >= 30*time.Second {
|
|
|
|
logger.Printf("Send a package.type:%d\n", pack.Type)
|
|
|
|
logger.Printf("Player %s, Send a package.type:%d, queue.len=%d\n", player.String(), pack.Type, queueLen)
|
|
|
|
timer = time.Now()
|
|
|
|
timer = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
}
|