forked from emiago/diago
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
63 lines (48 loc) · 1.27 KB
/
utils.go
File metadata and controls
63 lines (48 loc) · 1.27 KB
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024, Emir Aganovic
package diago
import (
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httputil"
"sync"
"github.com/emiago/diago/media"
"github.com/pion/rtp"
)
var rtpBufPool = sync.Pool{
New: func() any {
return make([]byte, media.RTPBufSize)
},
}
func copyWithBuf(reader io.Reader, writer io.Writer, payloadBuf []byte) (int64, error) {
return media.CopyWithBuf(reader, writer, payloadBuf)
}
func closeAndLog(closer io.Closer, msg string) {
if err := closer.Close(); err != nil {
slog.Error(msg, "error", err)
}
}
type rtpWriterBuffer struct {
buf []*rtp.Packet
}
func newRTPWriterBuffer() *rtpWriterBuffer {
return &rtpWriterBuffer{
buf: make([]*rtp.Packet, 0, 1000),
}
}
func (w *rtpWriterBuffer) WriteRTP(p *rtp.Packet) error {
w.buf = append(w.buf, p)
return nil
}
type loggingTransport struct{}
func (s *loggingTransport) RoundTrip(r *http.Request) (*http.Response, error) {
bytes, _ := httputil.DumpRequestOut(r, false)
resp, err := http.DefaultTransport.RoundTrip(r)
// err is returned after dumping the response
respBytes, _ := httputil.DumpResponse(resp, false)
bytes = append(bytes, respBytes...)
slog.Debug(fmt.Sprintf("HTTP Debug:\n%s\n", bytes))
return resp, err
}