mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 07:06:58 +08:00
rename 'read' into 'play', 'publish' into 'record' (#463)
This commit is contained in:
140
examples/client-play-format-h265/h265_decoder.go
Normal file
140
examples/client-play-format-h265/h265_decoder.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #cgo pkg-config: libavcodec libavutil libswscale
|
||||
// #include <libavcodec/avcodec.h>
|
||||
// #include <libavutil/imgutils.h>
|
||||
// #include <libswscale/swscale.h>
|
||||
import "C"
|
||||
|
||||
func frameData(frame *C.AVFrame) **C.uint8_t {
|
||||
return (**C.uint8_t)(unsafe.Pointer(&frame.data[0]))
|
||||
}
|
||||
|
||||
func frameLineSize(frame *C.AVFrame) *C.int {
|
||||
return (*C.int)(unsafe.Pointer(&frame.linesize[0]))
|
||||
}
|
||||
|
||||
// h265Decoder is a wrapper around FFmpeg's H265 decoder.
|
||||
type h265Decoder struct {
|
||||
codecCtx *C.AVCodecContext
|
||||
srcFrame *C.AVFrame
|
||||
swsCtx *C.struct_SwsContext
|
||||
dstFrame *C.AVFrame
|
||||
dstFramePtr []uint8
|
||||
}
|
||||
|
||||
// newH265Decoder allocates a new h265Decoder.
|
||||
func newH265Decoder() (*h265Decoder, error) {
|
||||
codec := C.avcodec_find_decoder(C.AV_CODEC_ID_H265)
|
||||
if codec == nil {
|
||||
return nil, fmt.Errorf("avcodec_find_decoder() failed")
|
||||
}
|
||||
|
||||
codecCtx := C.avcodec_alloc_context3(codec)
|
||||
if codecCtx == nil {
|
||||
return nil, fmt.Errorf("avcodec_alloc_context3() failed")
|
||||
}
|
||||
|
||||
res := C.avcodec_open2(codecCtx, codec, nil)
|
||||
if res < 0 {
|
||||
C.avcodec_close(codecCtx)
|
||||
return nil, fmt.Errorf("avcodec_open2() failed")
|
||||
}
|
||||
|
||||
srcFrame := C.av_frame_alloc()
|
||||
if srcFrame == nil {
|
||||
C.avcodec_close(codecCtx)
|
||||
return nil, fmt.Errorf("av_frame_alloc() failed")
|
||||
}
|
||||
|
||||
return &h265Decoder{
|
||||
codecCtx: codecCtx,
|
||||
srcFrame: srcFrame,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// close closes the decoder.
|
||||
func (d *h265Decoder) close() {
|
||||
if d.dstFrame != nil {
|
||||
C.av_frame_free(&d.dstFrame)
|
||||
}
|
||||
|
||||
if d.swsCtx != nil {
|
||||
C.sws_freeContext(d.swsCtx)
|
||||
}
|
||||
|
||||
C.av_frame_free(&d.srcFrame)
|
||||
C.avcodec_close(d.codecCtx)
|
||||
}
|
||||
|
||||
func (d *h265Decoder) decode(nalu []byte) (image.Image, error) {
|
||||
nalu = append([]uint8{0x00, 0x00, 0x00, 0x01}, []uint8(nalu)...)
|
||||
|
||||
// send NALU to decoder
|
||||
var avPacket C.AVPacket
|
||||
avPacket.data = (*C.uint8_t)(C.CBytes(nalu))
|
||||
defer C.free(unsafe.Pointer(avPacket.data))
|
||||
avPacket.size = C.int(len(nalu))
|
||||
res := C.avcodec_send_packet(d.codecCtx, &avPacket)
|
||||
if res < 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// receive frame if available
|
||||
res = C.avcodec_receive_frame(d.codecCtx, d.srcFrame)
|
||||
if res < 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// if frame size has changed, allocate needed objects
|
||||
if d.dstFrame == nil || d.dstFrame.width != d.srcFrame.width || d.dstFrame.height != d.srcFrame.height {
|
||||
if d.dstFrame != nil {
|
||||
C.av_frame_free(&d.dstFrame)
|
||||
}
|
||||
|
||||
if d.swsCtx != nil {
|
||||
C.sws_freeContext(d.swsCtx)
|
||||
}
|
||||
|
||||
d.dstFrame = C.av_frame_alloc()
|
||||
d.dstFrame.format = C.AV_PIX_FMT_RGBA
|
||||
d.dstFrame.width = d.srcFrame.width
|
||||
d.dstFrame.height = d.srcFrame.height
|
||||
d.dstFrame.color_range = C.AVCOL_RANGE_JPEG
|
||||
res = C.av_frame_get_buffer(d.dstFrame, 1)
|
||||
if res < 0 {
|
||||
return nil, fmt.Errorf("av_frame_get_buffer() failed")
|
||||
}
|
||||
|
||||
d.swsCtx = C.sws_getContext(d.srcFrame.width, d.srcFrame.height, C.AV_PIX_FMT_YUV420P,
|
||||
d.dstFrame.width, d.dstFrame.height, (int32)(d.dstFrame.format), C.SWS_BILINEAR, nil, nil, nil)
|
||||
if d.swsCtx == nil {
|
||||
return nil, fmt.Errorf("sws_getContext() failed")
|
||||
}
|
||||
|
||||
dstFrameSize := C.av_image_get_buffer_size((int32)(d.dstFrame.format), d.dstFrame.width, d.dstFrame.height, 1)
|
||||
d.dstFramePtr = (*[1 << 30]uint8)(unsafe.Pointer(d.dstFrame.data[0]))[:dstFrameSize:dstFrameSize]
|
||||
}
|
||||
|
||||
// convert color space from YUV420 to RGBA
|
||||
res = C.sws_scale(d.swsCtx, frameData(d.srcFrame), frameLineSize(d.srcFrame),
|
||||
0, d.srcFrame.height, frameData(d.dstFrame), frameLineSize(d.dstFrame))
|
||||
if res < 0 {
|
||||
return nil, fmt.Errorf("sws_scale() failed")
|
||||
}
|
||||
|
||||
// embed frame into an image.Image
|
||||
return &image.RGBA{
|
||||
Pix: d.dstFramePtr,
|
||||
Stride: 4 * (int)(d.dstFrame.width),
|
||||
Rect: image.Rectangle{
|
||||
Max: image.Point{(int)(d.dstFrame.width), (int)(d.dstFrame.height)},
|
||||
},
|
||||
}, nil
|
||||
}
|
122
examples/client-play-format-h265/main.go
Normal file
122
examples/client-play-format-h265/main.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/bluenviron/gortsplib/v4"
|
||||
"github.com/bluenviron/gortsplib/v4/pkg/base"
|
||||
"github.com/bluenviron/gortsplib/v4/pkg/format"
|
||||
"github.com/bluenviron/gortsplib/v4/pkg/format/rtph265"
|
||||
"github.com/pion/rtp"
|
||||
)
|
||||
|
||||
// This example shows how to
|
||||
// 1. connect to a RTSP server
|
||||
// 2. check if there's an H265 media stream
|
||||
// 3. decode the H264 media stream into RGBA frames
|
||||
|
||||
// This example requires the FFmpeg libraries, that can be installed with this command:
|
||||
// apt install -y libavformat-dev libswscale-dev gcc pkg-config
|
||||
|
||||
func main() {
|
||||
c := gortsplib.Client{}
|
||||
|
||||
// parse URL
|
||||
u, err := base.ParseURL("rtsp://localhost:8554/mystream")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// connect to the server
|
||||
err = c.Start(u.Scheme, u.Host)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
// find available medias
|
||||
desc, _, err := c.Describe(u)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// find the H265 media and format
|
||||
var forma *format.H265
|
||||
medi := desc.FindFormat(&forma)
|
||||
if medi == nil {
|
||||
panic("media not found")
|
||||
}
|
||||
|
||||
// setup RTP/H265 -> H265 decoder
|
||||
rtpDec, err := forma.CreateDecoder()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// setup H265 -> raw frames decoder
|
||||
frameDec, err := newH265Decoder()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer frameDec.close()
|
||||
|
||||
// if VPS, SPS and PPS are present into the SDP, send them to the decoder
|
||||
if forma.VPS != nil {
|
||||
frameDec.decode(forma.VPS)
|
||||
}
|
||||
if forma.SPS != nil {
|
||||
frameDec.decode(forma.SPS)
|
||||
}
|
||||
if forma.PPS != nil {
|
||||
frameDec.decode(forma.PPS)
|
||||
}
|
||||
|
||||
// setup a single media
|
||||
_, err = c.Setup(desc.BaseURL, medi, 0, 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// called when a RTP packet arrives
|
||||
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
||||
// decode timestamp
|
||||
pts, ok := c.PacketPTS(medi, pkt)
|
||||
if !ok {
|
||||
log.Printf("waiting for timestamp")
|
||||
return
|
||||
}
|
||||
|
||||
// extract access units from RTP packets
|
||||
au, err := rtpDec.Decode(pkt)
|
||||
if err != nil {
|
||||
if err != rtph265.ErrNonStartingPacketAndNoPrevious && err != rtph265.ErrMorePacketsNeeded {
|
||||
log.Printf("ERR: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
for _, nalu := range au {
|
||||
// convert NALUs into RGBA frames
|
||||
img, err := frameDec.decode(nalu)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// wait for a frame
|
||||
if img == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("decoded frame with PTS %v and size %v", pts, img.Bounds().Max)
|
||||
}
|
||||
})
|
||||
|
||||
// start playing
|
||||
_, err = c.Play(nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// wait until a fatal error
|
||||
panic(c.Wait())
|
||||
}
|
Reference in New Issue
Block a user