-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #120
- Loading branch information
Showing
6 changed files
with
173 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
exclude_paths: | ||
- examples/examples.json |
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,13 @@ | ||
# Examples | ||
This directory contains some example use-cases of Pions WebRTC. | ||
|
||
## Instructions | ||
You can host the examples locally on your machine as follows: | ||
``` sh | ||
go get github.com/pions/webrtc | ||
cd $GOPATH/src/github.com/pions/webrtc/examples | ||
go run examples.go | ||
``` | ||
Note: you can change the port of the server using the ``--address`` flag. | ||
|
||
Finally, browse to [localhost](http://localhost) to browse through the examples. |
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,13 @@ | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<title>{{ .Title }}</title> | ||
<link rel="stylesheet" type="text/css" href="demo.css"> | ||
<script src="demo.js"></script> | ||
</head> | ||
<body> | ||
<div> | ||
{{ template "demo.html" }} | ||
</div> | ||
</body> | ||
</html> |
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,97 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Examples represents the examples loaded from examples.json. | ||
type Examples []Example | ||
|
||
// Examples represents an example loaded from examples.json. | ||
type Example struct { | ||
Title string `json:"title"` | ||
Link string `json:"link"` | ||
Description string `json:"description"` | ||
Type string `json:"type"` | ||
} | ||
|
||
func main() { | ||
addr := flag.String("address", ":80", "Address to host the HTTP server on.") | ||
flag.Parse() | ||
|
||
log.Println("Listening on", *addr) | ||
err := serve(*addr) | ||
if err != nil { | ||
log.Fatalf("Failed to serve: %v", err) | ||
} | ||
} | ||
|
||
func serve(addr string) error { | ||
// Load the examples | ||
examples, err := getExamples() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Load the templates | ||
homeTemplate := template.Must(template.ParseFiles("index.html")) | ||
|
||
// Serve the required pages | ||
// DIY 'mux' to avoid additional dependencies | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
parts := strings.Split(r.URL.Path, "/") | ||
if len(parts) > 3 && // 1 / example:2 / link:3 / [ static: 4 ] | ||
parts[1] == "example" { | ||
exampleLink := parts[2] | ||
for _, example := range *examples { | ||
if example.Link != exampleLink { | ||
continue | ||
} | ||
fiddle := filepath.Join(exampleLink, "jsfiddle") | ||
if len(parts[3]) != 0 { | ||
http.StripPrefix("/example/"+exampleLink+"/", http.FileServer(http.Dir(fiddle))).ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
temp := template.Must(template.ParseFiles("example.html")) | ||
_, err = temp.ParseFiles(filepath.Join(fiddle, "demo.html")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
temp.Execute(w, example) | ||
return | ||
} | ||
} | ||
|
||
// Serve the main page | ||
homeTemplate.Execute(w, examples) | ||
}) | ||
|
||
// Start the server | ||
return http.ListenAndServe(addr, nil) | ||
} | ||
|
||
// getExamples loads the examples from the examples.json file. | ||
func getExamples() (*Examples, error) { | ||
file, err := os.Open("./examples.json") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list examples (please run in the examples folder): %v", err) | ||
} | ||
|
||
var examples Examples | ||
err = json.NewDecoder(file).Decode(&examples) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse examples: %v", err) | ||
} | ||
|
||
return &examples, nil | ||
} |
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,32 @@ | ||
[ | ||
{ | ||
"title": "Data channels", | ||
"link": "data-channels", | ||
"description": "data-channels is a pion-WebRTC application that shows how you can send/recv DataChannel messages from a web browser.", | ||
"type": "browser" | ||
}, | ||
{ | ||
"title": "Data channels create", | ||
"link": "data-channels-create", | ||
"description": "data-channels-create is a pion-WebRTC application that shows how you can send/recv DataChannel messages from a web browser. The difference with the data-channels example is that the datachannel is initialized from the pion side in this example.", | ||
"type": "browser" | ||
}, | ||
{ | ||
"title": "Gstreamer receive", | ||
"link": "gstreamer-receive", | ||
"description": "gstreamer-receive is a simple application that shows how to receive media using pion-WebRTC and play live using GStreamer.", | ||
"type": "browser" | ||
}, | ||
{ | ||
"title": "Gstreamer send", | ||
"link": "gstreamer-send", | ||
"description": "gstreamer-send is a simple application that shows how to send video to your browser using pion-WebRTC and GStreamer.", | ||
"type": "browser" | ||
}, | ||
{ | ||
"title": "Save-to-disk", | ||
"link": "save-to-disk", | ||
"description": "save-to-disk is a simple application that shows how to record your webcam using pion-WebRTC and save to disk.", | ||
"type": "browser" | ||
} | ||
] |
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,15 @@ | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<title>pions/webrtc examples!</title> | ||
</head> | ||
<body> | ||
<ol> | ||
{{range .}} | ||
<li><a href="/example/{{ .Link }}/">{{ .Title }}</a>: {{ .Description }}</li> | ||
{{else}} | ||
<li><strong>No examples found!</strong></li> | ||
{{end}} | ||
</ol> | ||
</body> | ||
</html> |