add docs (#3)

This commit is contained in:
Daniel
2021-07-23 16:30:28 +03:00
committed by GitHub
parent b292df7d62
commit 675daf6cd5
4 changed files with 80 additions and 39 deletions

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -18,6 +18,12 @@ type decoder struct {
codecType int codecType int
} }
func decodeAVPacket(packet *C.AVPacket) (data []byte) {
data = make([]byte, int(packet.size))
copy(data, *(*[]byte)(unsafe.Pointer(&packet.data)))
return
}
func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) { func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
pkt = &Packet{} pkt = &Packet{}
@@ -47,10 +53,14 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
return return
} }
pkt.duration = int64(frame.pkt_duration)
pkt.position = int64(frame.pkt_pos)
switch decoder.codecType { switch decoder.codecType {
case C.AVMEDIA_TYPE_VIDEO: case C.AVMEDIA_TYPE_VIDEO:
pkt.width = int(frame.width) pkt.width = int(frame.width)
pkt.height = int(frame.height) pkt.height = int(frame.height)
pkt.keyFrame = int(frame.key_frame) == 1
var encPacket C.AVPacket var encPacket C.AVPacket
defer C.av_packet_unref(&encPacket) defer C.av_packet_unref(&encPacket)
@@ -69,8 +79,7 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
} }
} }
pkt.data = make([]byte, int(encPacket.size)) pkt.data = decodeAVPacket(&encPacket)
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
case C.AVMEDIA_TYPE_AUDIO: case C.AVMEDIA_TYPE_AUDIO:
pkt.bitRate = int(decoder.codecCtx.bit_rate) pkt.bitRate = int(decoder.codecCtx.bit_rate)
@@ -108,8 +117,7 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_resample_wav failed: %d", cerr) err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_resample_wav failed: %d", cerr)
return return
} }
pkt.data = make([]byte, int(encPacket.size)) pkt.data = decodeAVPacket(&encPacket)
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
case C.AV_SAMPLE_FMT_S16: case C.AV_SAMPLE_FMT_S16:
var encPacket C.AVPacket var encPacket C.AVPacket
@@ -119,9 +127,7 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_wav failed: %d", cerr) err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_wav failed: %d", cerr)
return return
} }
pkt.data = decodeAVPacket(&encPacket)
pkt.data = make([]byte, int(encPacket.size))
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
case C.AV_SAMPLE_FMT_S32: case C.AV_SAMPLE_FMT_S32:
if decoder.swrContext == nil { if decoder.swrContext == nil {
@@ -153,8 +159,8 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_resample_wav failed: %d", cerr) err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_resample_wav failed: %d", cerr)
return return
} }
pkt.data = make([]byte, int(encPacket.size)) pkt.data = decodeAVPacket(&encPacket)
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
default: default:
err = fmt.Errorf("ffmpeg: audio format %d not supported: %d", frame.format) err = fmt.Errorf("ffmpeg: audio format %d not supported: %d", frame.format)
return return

3
doc.go Normal file
View File

@@ -0,0 +1,3 @@
// Package go-rtsp is a Golang audio/video library based on FFmpeg.
// go-rtsp is powerful library to help convert streams/files to jpeg/wav.
package rtsp

71
pkt.go
View File

@@ -3,57 +3,68 @@ package rtsp
// #include "ffmpeg.h" // #include "ffmpeg.h"
import "C" import "C"
type Packet struct { type Image struct {
streamIndex int
codecType int
data []byte
// only image // only image
width int width int
height int height int
keyFrame bool
}
// Height if image or 0 if audio
func (image *Image) Height() int {
return image.height
}
// Width if image or 0 if audio
func (image *Image) Width() int {
return image.width
}
type Audio struct {
// only audio // only audio
sampleRate int sampleRate int
bitRate int bitRate int
channels int channels int
} }
// Height if image or 0 if audio
func (pkt *Packet) Height() int {
return pkt.height
}
// Width if image or 0 if audio
func (pkt *Packet) Width() int {
return pkt.width
}
// SampleRate if audio or 0 if image // SampleRate if audio or 0 if image
func (pkt *Packet) SampleRate() int { func (audio *Audio) SampleRate() int {
return pkt.sampleRate return audio.sampleRate
} }
// BitRate if audio or 0 if image // BitRate if audio or 0 if image
func (pkt *Packet) BitRate() int { func (audio *Audio) BitRate() int {
return pkt.bitRate return audio.bitRate
} }
// BitRate if audio or 0 if image // Channels if audio or 0 if image
func (pkt *Packet) Channels() int { func (audio *Audio) Channels() int {
return pkt.channels return audio.channels
}
type Packet struct {
streamIndex int
codecType int
data []byte
duration int64
position int64
Image
Audio
} }
// Data encoded jpeg if image or wav if audio // Data encoded jpeg if image or wav if audio
func (pkt *Packet) Data() []byte { func (packet *Packet) Data() []byte {
return pkt.data return packet.data
} }
// IsAudio packet // IsAudio packet
func (pkt *Packet) IsAudio() bool { func (packet *Packet) IsAudio() bool {
return pkt.codecType == C.AVMEDIA_TYPE_AUDIO return packet.codecType == C.AVMEDIA_TYPE_AUDIO
} }
// IsVideo packet // IsVideo packet
func (pkt *Packet) IsVideo() bool { func (packet *Packet) IsVideo() bool {
return pkt.codecType == C.AVMEDIA_TYPE_VIDEO return packet.codecType == C.AVMEDIA_TYPE_VIDEO
} }