Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。
package nativecapture
import (
"encoding/binary"
"errors"
"io"
"time"
"unicode/utf8"
"github.com/TNTcraftHIM/Piik/internal/media/encoded"
)
// ErrSourceCanceled means the user dismissed the system source-selection dialog.
var ErrSourceCanceled = errors.New("capture source selection canceled")
const (
envelopeHeaderBytes = 32
maxMediaBytes = encoded.MaxAccessUnitBytes
maxStatusBytes = 4 * 1024
maxControlBytes = 64
MaxVideoDimension = 8192
MaxVideoPixels = 36_864 * 16 * 16
MaxVideoMacroblocksPerSecond = 2_073_600
MaxVideoFramerate = 240 // Bound capture scheduling independently of codec throughput.
// Coded pictures may include alignment outside the visible crop. Keep this
// separate from visible dimensions and within H.264 Level 5.2 storage.
MaxCodedVideoDimension = 8192
MaxCodedVideoPixels = 36_864 * 16 * 16
)
// ValidVideoSize bounds the decoded picture in the current capture contract.
func ValidVideoSize(width, height uint32) bool {
return width >= 2 && width <= MaxVideoDimension && width%2 == 0 &&
height >= 2 && height <= MaxVideoDimension && height%2 == 0 &&
uint64((width+15)/16)*uint64((height+15)/16)*256 <= MaxVideoPixels
}
// ValidVideoFormat keeps both current encoders within the shared receive
// ceiling (H.264 Level 5.2), including non-preset shapes and frame rates.
func ValidVideoFormat(width, height, framerate uint32) bool {
return ValidVideoSize(width, height) && framerate >= 1 && framerate <= MaxVideoFramerate &&
uint64((width+15)/16)*uint64((height+15)/16)*uint64(framerate) <= MaxVideoMacroblocksPerSecond
}
func ValidCodedVideoSize(width, height uint64) bool {
return width >= 2 && width <= MaxCodedVideoDimension && width%2 == 0 &&
height >= 2 && height <= MaxCodedVideoDimension && height%2 == 0 &&
width*height <= MaxCodedVideoPixels
}
// SupportedH264ProfileLevelID bounds capture and receive to the same coding
// tools. Automatic encoders may select a lower level for a smaller output.
func SupportedH264ProfileLevelID(value string) bool {
level, valid := encoded.H264ConstrainedBaselineLevel(value)
if !valid {
return false
}
switch level {
case 10, 11, 12, 13, 20, 21, 22, 30, 31, 32, 40, 41, 42, 50, 51, 52:
return true
default:
return false
}
}
type FrameKind uint8
const (
FramePCM FrameKind = 1
FrameH264 FrameKind = 2
FrameStatus FrameKind = 3
FrameVP8 FrameKind = 4
FrameBegin FrameKind = 5
FrameLayerUnavailable FrameKind = 6
FrameControl FrameKind = 7
)
type Frame struct {
Kind FrameKind
// KeyFrame denotes independent recovery, including H264 SPS/PPS when needed.
KeyFrame bool
Layer int
Width uint32
Height uint32
Timestamp time.Duration
Duration time.Duration
Data []byte
}
func readFrame(input io.Reader) (Frame, error) {
var header [envelopeHeaderBytes]byte
if _, err := io.ReadFull(input, header[:]); err != nil {
return Frame{}, err
}
frame, size, err := parseFrameHeader(header[:])
if err != nil {
return Frame{}, err
}
frame.Data = make([]byte, size)
if _, err = io.ReadFull(input, frame.Data); err != nil {
return Frame{}, err
}
if err = validateFrameData(frame); err != nil {
return Frame{}, err
}
return frame, nil
}
func parseFrameHeader(header []byte) (Frame, uint32, error) {
kind := FrameKind(header[5])
size := binary.BigEndian.Uint32(header[28:32])
maximum := uint32(maxMediaBytes)
if kind == FrameStatus || kind == FrameLayerUnavailable {
maximum = maxStatusBytes
} else if kind == FrameControl {
maximum = maxControlBytes
}
if string(header[:4]) != "SMED" || header[4] != 2 ||
kind < FramePCM || kind > FrameControl || size > maximum ||
(kind == FrameBegin) != (size == 0) {
return Frame{}, 0, errors.New("native capture frame header is invalid")
}
timestamp100ns := binary.BigEndian.Uint64(header[8:16])
duration100ns := binary.BigEndian.Uint64(header[16:24])
width := uint32(binary.BigEndian.Uint16(header[24:26]))
height := uint32(binary.BigEndian.Uint16(header[26:28]))
layer := int(header[7])
isVideo := kind == FrameH264 || kind == FrameVP8
isTimed := isVideo || kind == FramePCM || kind == FrameBegin
const maxTime100ns = uint64(1<<63-1) / 100
if timestamp100ns > maxTime100ns || duration100ns > maxTime100ns ||
(isTimed && duration100ns == 0) || (!isTimed && (timestamp100ns != 0 || duration100ns != 0)) ||
(isVideo && (header[6] > 1 || layer >= maxOutputs || !ValidVideoSize(width, height))) ||
(!isVideo && (header[6] != 0 || width != 0 || height != 0)) ||
(kind == FrameLayerUnavailable && layer >= maxOutputs) ||
(!isVideo && kind != FrameLayerUnavailable && layer != 0) {
return Frame{}, 0, errors.New("native capture media frame is invalid")
}
return Frame{
Kind: kind,
KeyFrame: isVideo && header[6] == 1,
Layer: layer,
Width: width,
Height: height,
Timestamp: time.Duration(timestamp100ns) * 100 * time.Nanosecond,
Duration: time.Duration(duration100ns) * 100 * time.Nanosecond,
}, size, nil
}
func validateFrameData(frame Frame) error {
if frame.Kind == FrameLayerUnavailable && !utf8.Valid(frame.Data) {
return errors.New("native unavailable output diagnostic is invalid")
}
if frame.Kind == FrameControl {
for _, value := range frame.Data {
if value < 32 || value > 126 {
return errors.New("native control payload is not ASCII")
}
}
}
return nil
}
func writeFrame(output io.Writer, frame Frame) error {
if frame.Timestamp < 0 || frame.Duration < 0 || frame.Layer < 0 || frame.Layer > 255 ||
frame.Width > 65_535 || frame.Height > 65_535 || len(frame.Data) > maxMediaBytes {
return errors.New("native capture frame is outside its envelope")
}
var header [envelopeHeaderBytes]byte
copy(header[:], "SMED")
header[4], header[5], header[7] = 2, byte(frame.Kind), byte(frame.Layer)
if frame.KeyFrame {
header[6] = 1
}
binary.BigEndian.PutUint64(header[8:16], uint64(frame.Timestamp/(100*time.Nanosecond)))
binary.BigEndian.PutUint64(header[16:24], uint64(frame.Duration/(100*time.Nanosecond)))
binary.BigEndian.PutUint16(header[24:26], uint16(frame.Width))
binary.BigEndian.PutUint16(header[26:28], uint16(frame.Height))
binary.BigEndian.PutUint32(header[28:32], uint32(len(frame.Data)))
if _, _, err := parseFrameHeader(header[:]); err != nil {
return err
}
if err := validateFrameData(frame); err != nil {
return err
}
for _, data := range [][]byte{header[:], frame.Data} {
if len(data) == 0 {
continue
}
n, err := output.Write(data)
if err != nil {
return err
}
if n != len(data) {
return io.ErrShortWrite
}
}
return nil
}