jrouter/atalk/rtmp/response.go

80 lines
2.1 KiB
Go
Raw Permalink Normal View History

2024-04-08 09:21:35 +10:00
/*
Copyright 2024 Josh Deprez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2024-04-07 14:05:07 +10:00
package rtmp
import (
2024-04-07 14:42:09 +10:00
"bytes"
2024-04-07 14:05:07 +10:00
"encoding/binary"
"fmt"
"github.com/sfiera/multitalk/pkg/ddp"
)
2024-04-07 14:42:09 +10:00
// ResponsePacket represents an RTMP Response packet.
2024-04-07 14:05:07 +10:00
type ResponsePacket struct {
SenderAddr ddp.Addr
Extended bool
RangeStart ddp.Network
RangeEnd ddp.Network
}
2024-04-07 14:42:09 +10:00
// Marshal marshals an RTMP Response packet.
func (rp *ResponsePacket) Marshal() ([]byte, error) {
b := bytes.NewBuffer(nil)
b.Grow(10)
write16(b, rp.SenderAddr.Network)
b.WriteByte(8)
b.WriteByte(byte(rp.SenderAddr.Node))
if !rp.Extended {
return b.Bytes(), nil
}
write16(b, rp.RangeStart)
b.WriteByte(0x80)
write16(b, rp.RangeEnd)
b.WriteByte(0x82)
return b.Bytes(), nil
}
// UnmarshalResponsePacket unmarshals an RTMP Response packet.
2024-04-07 14:05:07 +10:00
func UnmarshalResponsePacket(data []byte) (*ResponsePacket, error) {
if len(data) != 4 && len(data) != 10 {
return nil, fmt.Errorf("invalid input length %d for RTMP Response packet", len(data))
}
if data[2] != 8 {
return nil, fmt.Errorf("unsupported node ID length %d for RTMP Response packet", data[2])
}
rp := &ResponsePacket{
SenderAddr: ddp.Addr{
Network: ddp.Network(binary.BigEndian.Uint16(data[:2])),
Node: ddp.Node(data[3]),
},
}
if len(data) == 4 {
return rp, nil
}
rp.RangeStart = ddp.Network(binary.BigEndian.Uint16(data[4:6]))
if data[6] != 0x80 {
return nil, fmt.Errorf("invalid intermediate byte %x for RTMP Response packet", data[6])
}
rp.RangeEnd = ddp.Network(binary.BigEndian.Uint16(data[7:9]))
if data[9] != 0x82 {
return nil, fmt.Errorf("unsupported version %x for RTMP Response packet", data[9])
}
return rp, nil
}