mirror of
https://github.com/Danile71/go-rtsp.git
synced 2025-10-07 09:00:56 +08:00
add mp4 file sample
This commit is contained in:
43
example/file/main.go
Normal file
43
example/file/main.go
Normal 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
BIN
example/file/sample.mp4
Normal file
Binary file not shown.
@@ -6,6 +6,7 @@ package rtsp
|
|||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
// Init old version's ffmpeg
|
||||||
func init() {
|
func init() {
|
||||||
C.ffmpeginit()
|
C.ffmpeginit()
|
||||||
}
|
}
|
||||||
|
11
pkt.go
11
pkt.go
@@ -7,26 +7,33 @@ type Packet struct {
|
|||||||
streamIndex int
|
streamIndex int
|
||||||
codecType int
|
codecType int
|
||||||
data []byte
|
data []byte
|
||||||
width int
|
|
||||||
height int
|
// only image
|
||||||
|
width int
|
||||||
|
height int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Height if image or 0 if audio
|
||||||
func (pkt *Packet) Height() int {
|
func (pkt *Packet) Height() int {
|
||||||
return pkt.height
|
return pkt.height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Width if image or 0 if audio
|
||||||
func (pkt *Packet) Width() int {
|
func (pkt *Packet) Width() int {
|
||||||
return pkt.width
|
return pkt.width
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Data encoded jpeg if image or wav if audio
|
||||||
func (pkt *Packet) Data() []byte {
|
func (pkt *Packet) Data() []byte {
|
||||||
return pkt.data
|
return pkt.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsAudio packet
|
||||||
func (pkt *Packet) IsAudio() bool {
|
func (pkt *Packet) IsAudio() bool {
|
||||||
return pkt.codecType == C.AVMEDIA_TYPE_AUDIO
|
return pkt.codecType == C.AVMEDIA_TYPE_AUDIO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsVideo packet
|
||||||
func (pkt *Packet) IsVideo() bool {
|
func (pkt *Packet) IsVideo() bool {
|
||||||
return pkt.codecType == C.AVMEDIA_TYPE_VIDEO
|
return pkt.codecType == C.AVMEDIA_TYPE_VIDEO
|
||||||
}
|
}
|
||||||
|
1
rtsp.go
1
rtsp.go
@@ -1,5 +1,6 @@
|
|||||||
package rtsp
|
package rtsp
|
||||||
|
|
||||||
|
// Open rtsp stream of file
|
||||||
func Open(url string) (*Stream, error) {
|
func Open(url string) (*Stream, error) {
|
||||||
stream := New(url)
|
stream := New(url)
|
||||||
return stream, stream.Setup(Auto)
|
return stream, stream.Setup(Auto)
|
||||||
|
@@ -6,6 +6,7 @@ package rtsp
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -138,7 +139,11 @@ func (stream *Stream) ReadPacket() (pkt *Packet, err error) {
|
|||||||
defer C.av_packet_unref(&packet)
|
defer C.av_packet_unref(&packet)
|
||||||
|
|
||||||
if cerr := C.av_read_frame(stream.formatCtx, &packet); int(cerr) != 0 {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user