Updates on the seek bar functionality

This commit is contained in:
Alex Ballas
2023-02-06 02:06:01 +02:00
parent 1543941026
commit 8a19bb242b
3 changed files with 198 additions and 4 deletions

View File

@@ -296,8 +296,8 @@ func (p *TVPayload) setNextAVTransportSoapCall(clear bool) error {
return nil
}
// AVTransportActionSoapCall builds and sends the AVTransport actions
func (p *TVPayload) AVTransportActionSoapCall(action string) error {
// PlayPauseStopSoapCall builds and sends the AVTransport actions for Play Pause and Stop.
func (p *TVPayload) PlayPauseStopSoapCall(action string) error {
parsedURLtransport, err := url.Parse(p.ControlURL)
if err != nil {
p.Log().Error().Str("Method", "AVTransportActionSoapCall").Str("Action", "URL Parse").Err(err).Msg("")
@@ -381,6 +381,83 @@ func (p *TVPayload) AVTransportActionSoapCall(action string) error {
return nil
}
// SeekSoapCall builds and sends the AVTransport actions for Seek.
func (p *TVPayload) SeekSoapCall(reltime string) error {
parsedURLtransport, err := url.Parse(p.ControlURL)
if err != nil {
p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "URL Parse").Err(err).Msg("")
return fmt.Errorf("SeekSoapCall parse error: %w", err)
}
var xml []byte
retry := false
xml, err = seekSoapBuild(reltime)
if err != nil {
p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Action Error").Err(err).Msg("")
return fmt.Errorf("SeekSoapCall action error: %w", err)
}
client := &http.Client{}
if retry {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3
retryClient.Logger = nil
client = retryClient.StandardClient()
}
req, err := http.NewRequest("POST", parsedURLtransport.String(), bytes.NewReader(xml))
if err != nil {
p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Prepare POST").Err(err).Msg("")
return fmt.Errorf("SeekSoapCall POST error: %w", err)
}
req.Header = http.Header{
"SOAPAction": []string{`"urn:schemas-upnp-org:service:AVTransport:1#Seek"`},
"content-type": []string{"text/xml"},
"charset": []string{"utf-8"},
"Connection": []string{"close"},
}
headerBytesReq, err := json.Marshal(req.Header)
if err != nil {
p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Header Marshaling").Err(err).Msg("")
return fmt.Errorf("SeekSoapCall Request Marshaling error: %w", err)
}
p.Log().Debug().
Str("Method", "SeekSoapCall").Str("Action", "Seek Request").
RawJSON("Headers", headerBytesReq).
Msg(string(xml))
res, err := client.Do(req)
if err != nil {
p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Do POST").Err(err).Msg("")
return fmt.Errorf("SeekSoapCall Do POST error: %w", err)
}
defer res.Body.Close()
resBytes, err := io.ReadAll(res.Body)
if err != nil {
p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Readall").Err(err).Msg("")
return fmt.Errorf("SeekSoapCall Failed to read response: %w", err)
}
headerBytesRes, err := json.Marshal(res.Header)
if err != nil {
p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Header Marshaling #2").Err(err).Msg("")
return fmt.Errorf("SeekSoapCall Response Marshaling error: %w", err)
}
p.Log().Debug().
Str("Method", "SeekSoapCall").Str("Action", "Seek Response").Str("Status Code", strconv.Itoa(res.StatusCode)).
RawJSON("Headers", headerBytesRes).
Msg(string(resBytes))
return nil
}
// SubscribeSoapCall send a SUBSCRIBE request to the DMR device.
// If we explicitly pass the UUID, then we refresh it instead.
func (p *TVPayload) SubscribeSoapCall(uuidInput string) error {
@@ -1225,7 +1302,7 @@ func (p *TVPayload) SendtoTV(action string) error {
}
}
if err := p.AVTransportActionSoapCall(action); err != nil {
if err := p.PlayPauseStopSoapCall(action); err != nil {
return fmt.Errorf("SendtoTV Play/Stop/Pause action error: %w", err)
}

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/h2non/filetype"
@@ -42,7 +43,8 @@ var (
"image/png": "PNG_LRG",
}
ErrInvalidSeekFlag = errors.New("invalid seek flag")
ErrInvalidSeekFlag = errors.New("invalid seek flag")
ErrInvalidClockFormat = errors.New("invalid clock format")
)
func defaultStreamingFlags() string {
@@ -127,3 +129,44 @@ func GetMimeDetailsFromStream(s io.ReadCloser) (string, error) {
return fmt.Sprintf("%s/%s", kind.MIME.Type, kind.MIME.Subtype), nil
}
func ClockTimeToSeconds(strtime string) (int, error) {
var out int
v := make([]int, 0, 3)
s := strings.Split(strtime, ":")
if len(s) != 3 {
return 0, ErrInvalidClockFormat
}
for _, i := range s {
num, err := strconv.Atoi(i)
if err != nil {
return 0, ErrInvalidClockFormat
}
v = append(v, num)
}
for n, i := range v {
switch n {
case 0:
out += i * 3600
case 1:
out += i * 60
case 2:
out += i
}
}
return out, nil
}
func SecondsToClockTime(secs int) (string, error) {
hours := secs / 3600
secs %= 3600
minutes := secs / 60
secs %= 60
str := fmt.Sprintf("%02d:%02d:%02d", hours, minutes, secs)
return str, nil
}

View File

@@ -76,3 +76,77 @@ func TestBuildContentFeatures(t *testing.T) {
})
}
}
func TestClockTimeToSeconds(t *testing.T) {
tt := []struct {
name string
strtime string
want int
}{
{
`Test #1`,
`00:01:30`,
90,
},
{
`Test #2`,
`00:01:31`,
91,
},
{
`Test #3`,
`01:01:30`,
3690,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
out, err := ClockTimeToSeconds(tc.strtime)
if err != nil {
t.Fatalf("%s: got: %d, want: %d.", tc.name, out, tc.want)
}
if out != tc.want {
t.Fatalf("%s: got: %d, want: %d.", tc.name, out, tc.want)
}
})
}
}
func TestSecondsToClockTime(t *testing.T) {
tt := []struct {
name string
input int
want string
}{
{
`Test #1`,
90,
`00:01:30`,
},
{
`Test #2`,
91,
`00:01:31`,
},
{
`Test #3`,
3690,
`01:01:30`,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
out, err := SecondsToClockTime(tc.input)
if err != nil {
t.Fatalf("%s: got: %s, want: %s.", tc.name, out, tc.want)
}
if out != tc.want {
t.Fatalf("%s: got: %s, want: %s.", tc.name, out, tc.want)
}
})
}
}