mirror of
				https://github.com/aler9/gortsplib
				synced 2025-10-31 10:36:26 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 
 | |
| 	"github.com/aler9/gortsplib"
 | |
| 	"github.com/aler9/gortsplib/pkg/base"
 | |
| 	"github.com/aler9/gortsplib/pkg/headers"
 | |
| )
 | |
| 
 | |
| // This example shows how to
 | |
| // 1. connect to a RTSP server
 | |
| // 2. get tracks published on a path
 | |
| // 3. read only selected tracks
 | |
| 
 | |
| func main() {
 | |
| 	u, err := base.ParseURL("rtsp://myserver/mypath")
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	conn, err := gortsplib.Dial(u.Scheme, u.Host)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	defer conn.Close()
 | |
| 
 | |
| 	_, err = conn.Options(u)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	tracks, baseURL, _, err := conn.Describe(u)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	// start reading only video tracks, skipping audio or application tracks
 | |
| 	for _, t := range tracks {
 | |
| 		if t.Media.MediaName.Media == "video" {
 | |
| 			_, err := conn.Setup(headers.TransportModePlay, baseURL, t, 0, 0)
 | |
| 			if err != nil {
 | |
| 				panic(err)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// play setupped tracks
 | |
| 	_, err = conn.Play(nil)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	// read RTP frames
 | |
| 	err = conn.ReadFrames(func(trackID int, streamType gortsplib.StreamType, payload []byte) {
 | |
| 		fmt.Printf("frame from track %d, type %v, size %d\n", trackID, streamType, len(payload))
 | |
| 	})
 | |
| 	panic(err)
 | |
| }
 | 
