-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add simple example
- Loading branch information
Showing
8 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Simple | ||
|
||
This is a simple example to show: | ||
|
||
* Publishs webm to session | ||
* Subscribe from session and save to disk | ||
|
||
## Quick Start | ||
|
||
### 1 build | ||
|
||
``` | ||
go build main.go | ||
``` | ||
|
||
### 2 use | ||
|
||
``` | ||
# see ./main --help | ||
./main -file /Volumes/vm/media/djrm480p.webm -addr "localhost:8000" -session 'test room' | ||
``` | ||
|
||
### 3 tips | ||
|
||
* people in the same room will see your movie | ||
* you will find ogg and ivf on your disk if others publish streams in the same room |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"strings" | ||
|
||
log "github.com/pion/ion-log" | ||
sdk "github.com/pion/ion-sdk-go" | ||
"github.com/pion/webrtc/v3" | ||
"github.com/pion/webrtc/v3/pkg/media" | ||
"github.com/pion/webrtc/v3/pkg/media/ivfwriter" | ||
"github.com/pion/webrtc/v3/pkg/media/oggwriter" | ||
) | ||
|
||
func saveToDisk(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { | ||
codec := track.Codec() | ||
var fileWriter media.Writer | ||
var err error | ||
if strings.EqualFold(codec.MimeType, webrtc.MimeTypeOpus) { | ||
log.Infof("Got Opus track, saving to disk as ogg (48 kHz, 2 channels)") | ||
fileWriter, err = oggwriter.New(fmt.Sprintf("%d_%d.ogg", codec.PayloadType, track.SSRC()), 48000, 2) | ||
} else if strings.EqualFold(codec.MimeType, webrtc.MimeTypeVP8) { | ||
log.Infof("Got VP8 track, saving to disk as ivf") | ||
fileWriter, err = ivfwriter.New(fmt.Sprintf("%d_%d.ivf", codec.PayloadType, track.SSRC())) | ||
} | ||
|
||
if err != nil { | ||
log.Errorf("err=%v", err) | ||
fileWriter.Close() | ||
return | ||
} | ||
|
||
for { | ||
rtpPacket, _, err := track.ReadRTP() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err := fileWriter.WriteRTP(rtpPacket); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
// init log | ||
fixByFile := []string{"asm_amd64.s", "proc.go", "icegatherer.go"} | ||
fixByFunc := []string{"AddProducer", "NewClient"} | ||
log.Init("debug", fixByFile, fixByFunc) | ||
|
||
// parse flag | ||
var session, addr, file string | ||
flag.StringVar(&file, "file", "./file.webm", "Path to the file media") | ||
flag.StringVar(&addr, "addr", "localhost:50051", "Ion-sfu grpc addr") | ||
flag.StringVar(&session, "session", "test room", "join session name") | ||
flag.Parse() | ||
|
||
// add stun servers | ||
webrtcCfg := webrtc.Configuration{ | ||
ICEServers: []webrtc.ICEServer{ | ||
webrtc.ICEServer{ | ||
URLs: []string{"stun:stun.stunprotocol.org:3478", "stun:stun.l.google.com:19302"}, | ||
}, | ||
}, | ||
} | ||
|
||
config := sdk.Config{ | ||
Log: log.Config{ | ||
Level: "debug", | ||
}, | ||
WebRTC: sdk.WebRTCTransportConfig{ | ||
Configuration: webrtcCfg, | ||
}, | ||
} | ||
// new sdk engine | ||
e := sdk.NewEngine(addr, config) | ||
|
||
// get a client from engine | ||
c := e.AddClient(addr, session, "client id") | ||
|
||
// subscribe rtp from sessoin | ||
// comment this if you don't need save to file | ||
c.OnTrack = saveToDisk | ||
|
||
// client join a session | ||
err := c.Join(session) | ||
|
||
// publish file to session if needed | ||
if err == nil && file != "" { | ||
err = c.PublishWebm(file) | ||
if err != nil { | ||
log.Errorf("err=%v", err) | ||
} | ||
} else { | ||
log.Errorf("err=%v", err) | ||
} | ||
select {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters