Skip to content

Commit

Permalink
Moved to the unstable version of discordgo until they merge the devel…
Browse files Browse the repository at this point in the history
…op branch

Updated the core APIs
Faster seeking
  • Loading branch information
dondish committed Oct 18, 2019
1 parent 1e7a9d3 commit b1994cb
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 17 deletions.
3 changes: 3 additions & 0 deletions core/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type PlaySeekable interface {
// An interface for a track
type Track interface {
GetPlayable() (Playable, error)
GetBitrate() int
GetChannels() int
GetCodec() string
}

// An interface for a track that can be seeked
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module lionPlayer
go 1.13

require (
github.com/bwmarrin/discordgo v0.19.0
github.com/bwmarrin/discordgo v0.0.0-20191018135108-0364b15ee4ec
github.com/dondish/lionPlayer/core v0.0.1
github.com/dondish/lionPlayer/youtube v0.0.2
github.com/gorilla/websocket v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 // indirect
golang.org/x/sys v0.0.0-20191018095205-727590c5006e // indirect
)
2 changes: 1 addition & 1 deletion webm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ go 1.13

require github.com/ebml-go/ebml v0.0.0-20160925193348-ca8851a10894

require github.com/dondish/lionPlayer/core v0.0.1
require github.com/dondish/lionPlayer/core v0.0.2
83 changes: 71 additions & 12 deletions webm/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,40 @@ func (t Track) Chan() <-chan core.Packet {
return t.Output
}

func readUint64(e *ebml.Element) (uint64, error) {
d, err := e.ReadData()
var i int
sz := len(d)
var val uint64
for i = 0; i < sz; i++ {
val <<= 8
val += uint64(d[i])
}
return val, err
}

// get the cluster position from the cuepoint postions element
func getClusterPosition(positions *ebml.Element, trackid uint64) (position uint64, err error) {
var pos *ebml.Element
var trac uint64
var clusterpos *ebml.Element
for pos, err = positions.Next(); err == nil; pos, err = positions.Next() {
trac, err = readUint64(pos)

if trac == trackid {
clusterpos, err = positions.Next()
if err != nil {
return
}
position, err = readUint64(clusterpos)
return
}

_, err = positions.Seek(pos.Size(), 1)
}
return
}

// Seeks to the cluster just before the timecode given
func (t Track) internalSeek(duration time.Duration) error {
if t.cues == 0 {
Expand All @@ -124,23 +158,48 @@ func (t Track) internalSeek(duration time.Duration) error {
if cues.Id != 0x1C53BB6B {
log.Println("wrong cues id", fmt.Sprintf("%#x", cues.Id))
}
var lastcuepoint CuePoint
var cuepoint CuePoint
var offs int64
var prev *ebml.Element
var element *ebml.Element
var tim *ebml.Element
var timecode uint64
for el, err := cues.Next(); err == nil; el, err = cues.Next() { // Go over the cuepoints
lastcuepoint = cuepoint
err = el.Unmarshal(&cuepoint)
prev = element
element = el
tim, err = el.Next()
if err != nil {
return err
}

if time.Duration(cuepoint.Time)*time.Millisecond > duration { // Found the cuepoint that passed the duration given
_, err = t.segment.Seek(t.segment.Offset+12+int64(lastcuepoint.Positions[t.trackId].ClusterPosition), 0)
timecode, err = readUint64(tim)
if err != nil {
return err
}

if time.Duration(timecode)*time.Millisecond > duration { // Found the cuepoint that passed the duration given
prev.Seek(offs, 0)
positions, err := prev.Next()
if err != nil {
return err
}
pos, err := getClusterPosition(positions, t.trackId)
if err != nil {
return err
}
_, err = t.segment.Seek(t.segment.Offset+12+int64(pos), 0)
return err
}
offs, err = el.Seek(0, 1)
_, err = cues.Seek(el.Size(), 1)
}

_, err = t.segment.Seek(t.segment.Offset+12+int64(lastcuepoint.Positions[t.trackId].ClusterPosition), 0)
_, _ = prev.Seek(offs, 0)
positions, err := prev.Next()
if err != nil {
return err
}
pos, err := getClusterPosition(positions, t.trackId)
if err != nil {
return err
}
_, err = t.segment.Seek(t.segment.Offset+12+int64(pos), 0) // last cluster
return err
}

Expand Down Expand Up @@ -317,7 +376,7 @@ func (t Track) Play() {
err = t.internalSeek(seek)
}
}
if err != io.EOF && err != nil {
log.Println("Player Error:", err)
if err != nil && err != io.EOF {
log.Println("play error", err)
}
}
4 changes: 2 additions & 2 deletions youtube/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module github.com/dondish/lionPlayer/youtube

go 1.13

require github.com/dondish/lionPlayer/core v0.0.1
require github.com/dondish/lionPlayer/core v0.0.2

require github.com/dondish/lionPlayer/webm v0.0.4
require github.com/dondish/lionPlayer/webm v0.0.5

require github.com/jeffallen/seekinghttp v0.0.0-20171214161738-f41d11cb25b7
13 changes: 13 additions & 0 deletions youtube/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/dondish/lionPlayer/core"
"github.com/dondish/lionPlayer/webm"
"github.com/jeffallen/seekinghttp"
"strings"
"time"
)

Expand All @@ -42,6 +43,18 @@ type Track struct {
Format *Format
}

func (t Track) GetBitrate() int {
return int(t.Format.Bitrate)
}

func (t Track) GetChannels() int {
return 2
}

func (t Track) GetCodec() string {
return strings.Split(t.Format.Type, "; ")[1]
}

// Return a playable of this track that can be played.
func (t Track) GetPlayable() (core.Playable, error) {
return t.GetPlaySeekable()
Expand Down

0 comments on commit b1994cb

Please sign in to comment.