forked from emiago/diago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playback_url.go
169 lines (140 loc) · 4.09 KB
/
playback_url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024, Emir Aganovic
// Copyright (C) 2024 Emir Aganovic
package diago
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"mime"
"net/http"
"strconv"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func (p *AudioPlayback) PlayURL(urlStr string) (int64, error) {
var written int64
err := p.playURL(urlStr, &written)
if errors.Is(err, io.EOF) {
return written, nil
}
return written, err
}
func (p *AudioPlayback) playURL(urlStr string, written *int64) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return err
}
req.Header.Add("Range", "bytes=0-1023") // Try with range request
res, err := client.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusPartialContent {
return fmt.Errorf("non 200 received. code=%d", res.StatusCode)
}
contType := res.Header.Get("Content-Type")
mimeType, _, err := mime.ParseMediaType(contType)
if err != nil {
return err
}
switch mimeType {
case "audio/wav", "audio/x-wav", "audio/wav-x", "audio/vnd.wave":
default:
return fmt.Errorf("unsuported content type %q", contType)
}
// Check can be streamed
if res.StatusCode == http.StatusPartialContent {
// acceptRanges := res.Header.Get("Accept-Ranges")
// if acceptRanges != "bytes" {
// return fmt.Errorf("header Accept-Ranges != bytes. Value=%q", acceptRanges)
// }
contentRange := res.Header.Get("Content-Range")
ind := strings.LastIndex(contentRange, "/")
if ind < 0 {
return fmt.Errorf("full audio size in Content-Range not present")
}
maxSize, err := strconv.ParseInt(contentRange[ind+1:], 10, 64)
if err != nil {
return err
}
if maxSize <= 0 {
return fmt.Errorf("parsing audio size failed")
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
// WAV header size is 44 bytes so we have more than enough
reader, writer := io.Pipe()
defer reader.Close()
defer writer.Close()
// BETTER DESIGN needed
httpPartial := func(log zerolog.Logger, res *http.Response, writer io.Writer) error {
chunk, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("reading chunk stopped: %w", err)
}
res.Body.Close()
if _, err := writer.Write(chunk); err != nil {
return err
}
var start int64 = 1024
var offset int64 = 64 * 1024 // 512K
for ; start < maxSize; start += offset {
end := min(start+offset-1, maxSize)
log.Debug().Int64("start", start).Int64("end", end).Int64("max", maxSize).Msg("Reading chunk size")
// Range is inclusive
rangeHDR := fmt.Sprintf("bytes=%d-%d", start, end)
req.Header.Set("Range", rangeHDR) // Try with range request
res, err = client.Do(req)
if err != nil {
return fmt.Errorf("failed to request range: %w", err)
}
if res.StatusCode == http.StatusRequestedRangeNotSatisfiable && res.ContentLength == 0 {
break
}
if res.StatusCode != http.StatusPartialContent {
return fmt.Errorf("expected partial content response: code=%d", res.StatusCode)
}
chunk, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("reading chunk stopped: %w", err)
}
res.Body.Close()
if _, err := writer.Write(chunk); err != nil {
return err
}
}
return nil
}
httpErr := make(chan error)
go func() {
// TODO have here context logger
err := httpPartial(log.Logger, res, writer)
writer.Close()
httpErr <- err
}()
n, err := p.streamWav(reader, p.writer)
*written = n
p.totalWritten += n
// There is no reason having http goroutine still running
// First make sure http goroutine exited and join errors
err = errors.Join(<-httpErr, err)
return err
}
// // We need some stream wave implementation
samples, err := io.ReadAll(res.Body)
if err != nil {
return err
}
defer res.Body.Close()
wavBuf := bytes.NewReader(samples)
n, err := p.streamWav(wavBuf, p.writer)
*written = n
p.totalWritten += n
return err
}