add mp4 file sample

This commit is contained in:
danil_e71
2021-07-22 10:46:08 +03:00
parent 8d800fbbcd
commit 20a759756e
6 changed files with 60 additions and 3 deletions

43
example/file/main.go Normal file
View File

@@ -0,0 +1,43 @@
package main
import (
"net/http"
"github.com/Danile71/go-logger"
"github.com/Danile71/go-rtsp"
"github.com/gorilla/mux"
"github.com/mattn/go-mjpeg"
)
const uri = "./sample.mp4"
func main() {
s := mjpeg.NewStream()
stream, err := rtsp.Open(uri)
if logger.OnError(err) {
return
}
go func() {
for {
pkt, err := stream.ReadPacket()
if logger.OnError(err) {
continue
}
if pkt.IsVideo() {
s.Update(pkt.Data())
}
}
}()
streamHandler := func(w http.ResponseWriter, r *http.Request) {
s.ServeHTTP(w, r)
}
router := mux.NewRouter()
router.HandleFunc("/stream", streamHandler)
http.Handle("/", router)
http.ListenAndServe(":8181", nil)
}

BIN
example/file/sample.mp4 Normal file

Binary file not shown.

View File

@@ -6,6 +6,7 @@ package rtsp
*/
import "C"
// Init old version's ffmpeg
func init() {
C.ffmpeginit()
}

11
pkt.go
View File

@@ -7,26 +7,33 @@ type Packet struct {
streamIndex int
codecType int
data []byte
width int
height int
// only image
width int
height 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
}
// Data encoded jpeg if image or wav if audio
func (pkt *Packet) Data() []byte {
return pkt.data
}
// IsAudio packet
func (pkt *Packet) IsAudio() bool {
return pkt.codecType == C.AVMEDIA_TYPE_AUDIO
}
// IsVideo packet
func (pkt *Packet) IsVideo() bool {
return pkt.codecType == C.AVMEDIA_TYPE_VIDEO
}

View File

@@ -1,5 +1,6 @@
package rtsp
// Open rtsp stream of file
func Open(url string) (*Stream, error) {
stream := New(url)
return stream, stream.Setup(Auto)

View File

@@ -6,6 +6,7 @@ package rtsp
import "C"
import (
"fmt"
"io"
"runtime"
"sync"
"unsafe"
@@ -138,7 +139,11 @@ func (stream *Stream) ReadPacket() (pkt *Packet, err error) {
defer C.av_packet_unref(&packet)
if cerr := C.av_read_frame(stream.formatCtx, &packet); int(cerr) != 0 {
err = fmt.Errorf("ffmpeg: av_read_frame failed: %d", cerr)
if cerr == C.AVERROR_EOF {
err = io.EOF
} else {
err = fmt.Errorf("ffmpeg: av_read_frame failed: %d", cerr)
}
return
}