├── .gitignore ├── .dockerignore ├── api ├── resource.go ├── go.mod ├── Dockerfile ├── store.go ├── main.go └── go.sum ├── recorder ├── go.mod ├── Dockerfile ├── resource.go ├── watcher.go ├── main.go ├── store.go └── go.sum ├── conf └── nginx │ ├── Dockerfile │ ├── nginx.conf │ └── sites-available │ └── minio.conf ├── Makefile ├── ingest.sh ├── README.md ├── LICENSE ├── frontend ├── nginx.conf └── player │ └── index.html └── docker-compose.yml /.gitignore: -------------------------------------------------------------------------------- 1 | media/* 2 | !media/.keep 3 | bin/* -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | bin 2 | media 3 | README.md 4 | LICENSE 5 | **/.git -------------------------------------------------------------------------------- /api/resource.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "strings" 4 | 5 | type Resource struct { 6 | Path string 7 | } 8 | 9 | func (r *Resource) ObjectName() string { 10 | return strings.Replace(r.Path, "/live/", "", 1) 11 | } 12 | -------------------------------------------------------------------------------- /recorder/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mauricioabreu/now-live/recorder 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/go-ini/ini v1.55.0 // indirect 7 | github.com/minio/minio-go v6.0.14+incompatible 8 | github.com/minio/minio-go/v6 v6.0.55 9 | github.com/rjeczalik/notify v0.9.2 10 | ) 11 | -------------------------------------------------------------------------------- /api/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mauricioabreu/now-live/api 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/go-ini/ini v1.56.0 // indirect 7 | github.com/labstack/echo/v4 v4.1.16 8 | github.com/minio/minio-go v6.0.14+incompatible 9 | github.com/mitchellh/go-homedir v1.1.0 // indirect 10 | ) 11 | -------------------------------------------------------------------------------- /api/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.14 as builder 2 | 3 | ENV GO111MODULE=on \ 4 | CGO_ENABLED=0 5 | 6 | WORKDIR /build 7 | 8 | COPY go.mod go.sum ./ 9 | RUN go mod download 10 | 11 | COPY . . 12 | 13 | RUN go build -o api . 14 | 15 | FROM alpine:3.7 16 | 17 | RUN adduser -S -D -H -h /app api 18 | 19 | USER api 20 | 21 | COPY --from=builder /build/api /app/ 22 | 23 | WORKDIR /app 24 | 25 | CMD ["./api"] -------------------------------------------------------------------------------- /recorder/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.14 as builder 2 | 3 | ENV GO111MODULE=on \ 4 | CGO_ENABLED=0 5 | 6 | WORKDIR /build 7 | 8 | COPY go.mod go.sum ./ 9 | RUN go mod download 10 | 11 | COPY . . 12 | 13 | RUN go build -o recorder . 14 | 15 | FROM alpine:3.7 16 | 17 | RUN adduser -S -D -H -h /app recorder 18 | 19 | USER recorder 20 | 21 | COPY --from=builder /build/recorder /app/ 22 | 23 | WORKDIR /app 24 | 25 | CMD ["./recorder"] -------------------------------------------------------------------------------- /recorder/resource.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "path/filepath" 5 | "strings" 6 | ) 7 | 8 | type Resource struct { 9 | File string 10 | } 11 | 12 | func (r *Resource) ObjectName(root string) string { 13 | path := strings.Replace(r.File, root, "", 1) 14 | if path[0] == '/' { 15 | return path[1:] 16 | } 17 | return filepath.Dir(path) 18 | } 19 | 20 | func (r *Resource) Path() string { 21 | return filepath.Base(r.File) 22 | } 23 | -------------------------------------------------------------------------------- /conf/nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:alpine 2 | 3 | RUN apk add --no-cache curl 4 | 5 | RUN \ 6 | rm -f \ 7 | /etc/nginx/sites-available/minio.conf \ 8 | /etc/nginx/sites-enabled/minio.conf \ 9 | /etc/nginx/sites-enabled/default 10 | 11 | ADD sites-available/ /etc/nginx/sites-available 12 | 13 | COPY nginx.conf /etc/nginx/nginx.conf 14 | 15 | RUN \ 16 | mkdir -p /etc/nginx/sites-enabled && \ 17 | ln -s /etc/nginx/sites-available/minio.conf /etc/nginx/sites-enabled/minio.conf -------------------------------------------------------------------------------- /recorder/watcher.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | "github.com/rjeczalik/notify" 8 | ) 9 | 10 | type Watcher struct { 11 | Path string 12 | EventStream chan notify.EventInfo 13 | } 14 | 15 | func NewWatcher(path string) *Watcher { 16 | return &Watcher{ 17 | Path: fmt.Sprintf("%s/...", path), 18 | EventStream: make(chan notify.EventInfo, 1), 19 | } 20 | } 21 | 22 | func (w *Watcher) Start() { 23 | if err := notify.Watch(w.Path, w.EventStream, notify.InCloseWrite, notify.InMovedTo); err != nil { 24 | log.Fatal(err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: help ingest now-live package recorder 2 | 3 | RECORDER_VERSION=0.0.1 4 | 5 | ingest: ## Produce some video and ingest it in the packager 6 | ./ingest.sh 7 | 8 | now-live: ## Run Now Live platform 9 | docker-compose build 10 | docker-compose up 11 | 12 | down-live: ## Stop Now Live platform 13 | docker-compose down 14 | 15 | recorder-build: ## Build recorder image 16 | docker build --tag recorder:${RECORDER_VERSION} -f Dockerfile-recorder . 17 | 18 | help: ## Lists available commands 19 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' -------------------------------------------------------------------------------- /conf/nginx/nginx.conf: -------------------------------------------------------------------------------- 1 | user nginx; 2 | worker_processes auto; 3 | 4 | error_log /var/log/nginx/error.log warn; 5 | pid /var/run/nginx.pid; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | http { 12 | include /etc/nginx/mime.types; 13 | default_type application/octet-stream; 14 | 15 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 16 | '$status $body_bytes_sent "$http_referer" ' 17 | '"$http_user_agent" "$http_x_forwarded_for"'; 18 | 19 | access_log /var/log/nginx/access.log main; 20 | 21 | sendfile on; 22 | keepalive_timeout 65; 23 | 24 | include /etc/nginx/sites-enabled/minio.conf; 25 | } -------------------------------------------------------------------------------- /recorder/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "regexp" 6 | ) 7 | 8 | var ( 9 | regexTempl = regexp.MustCompile(`^.*\.(m3u8|mp4|aac|ts)$`) 10 | ) 11 | 12 | func main() { 13 | store := NewStore("video") 14 | err := store.CreateBucket("video") 15 | if err != nil { 16 | log.Fatalf("Failed to create bucket:", err) 17 | } 18 | watcher := NewWatcher("/app/media") 19 | watcher.Start() 20 | 21 | done := make(chan struct{}, 1) 22 | 23 | go func() { 24 | for { 25 | select { 26 | case ev := <-watcher.EventStream: 27 | if regexTempl.MatchString(ev.Path()) { 28 | log.Print(ev.Path()) 29 | go store.UploadFile(ev.Path()) 30 | } 31 | } 32 | } 33 | }() 34 | 35 | <-done 36 | log.Println("Done...") 37 | } 38 | -------------------------------------------------------------------------------- /ingest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | docker run --net="host" --rm -v $(pwd):/files jrottenberg/ffmpeg:4.1 -hide_banner -re -f lavfi -i 'testsrc2=size=1280x720:rate=60,format=yuv420p' \ 4 | -f lavfi -i 'sine=frequency=440:sample_rate=48000:beep_factor=4' \ 5 | -c:a libfdk_aac -b:a 128x \ 6 | -c:v libx264 -x264opts keyint=30:min-keyint=30:scenecut=-1 -tune zerolatency \ 7 | -b:v 626k -g 30 -r 30 -s 512x288 -preset superfast -profile:v high -level 4.1 \ 8 | -c:a aac -b:a 32k -f mpegts udp://127.0.0.1:40001 \ 9 | -c:a libfdk_aac -b:a 128x \ 10 | -c:v libx264 -x264opts keyint=30:min-keyint=30:scenecut=-1 -tune zerolatency \ 11 | -b:v 1485k -g 30 -r 30 -s 768x432 -preset superfast -profile:v high -level 4.1 \ 12 | -c:a aac -b:a 32k -f mpegts udp://127.0.0.1:40002 \ 13 | -------------------------------------------------------------------------------- /conf/nginx/sites-available/minio.conf: -------------------------------------------------------------------------------- 1 | upstream minio_servers { 2 | server minio1:9000 max_fails=3 fail_timeout=15; 3 | server minio2:9000 max_fails=3 fail_timeout=15; 4 | server minio3:9000 max_fails=3 fail_timeout=15; 5 | server minio4:9000 max_fails=3 fail_timeout=15; 6 | } 7 | 8 | server { 9 | listen 80; 10 | 11 | location / { 12 | client_max_body_size 20m; 13 | client_body_buffer_size 5m; 14 | proxy_pass http://minio_servers; 15 | proxy_set_header Host $http_host; 16 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 17 | proxy_set_header X-NginX-Proxy true; 18 | proxy_ssl_session_reuse off; 19 | proxy_redirect off; 20 | } 21 | 22 | location ~ ^/healthcheck$ { 23 | default_type text/html; 24 | return 200 "WORKING"; 25 | expires -1; 26 | } 27 | } -------------------------------------------------------------------------------- /api/store.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "log" 6 | 7 | "github.com/minio/minio-go" 8 | ) 9 | 10 | type Store struct { 11 | Client *minio.Client 12 | } 13 | 14 | func NewStore() *Store { 15 | endpoint := "minio_proxy:80" 16 | accessKeyID := "minio" 17 | secretAccessKey := "minio123" 18 | client, err := minio.New(endpoint, accessKeyID, secretAccessKey, false) 19 | if err != nil { 20 | log.Fatal(err) 21 | } 22 | 23 | return &Store{Client: client} 24 | } 25 | 26 | func (s *Store) GetObject(resource string) ([]byte, error) { 27 | reader, err := s.Client.GetObject("video", resource, minio.GetObjectOptions{}) 28 | if err != nil { 29 | return nil, err 30 | } 31 | defer reader.Close() 32 | 33 | stat, err := reader.Stat() 34 | if err != nil { 35 | return nil, err 36 | } 37 | 38 | buf := make([]byte, stat.Size) 39 | _, err = reader.Read(buf) 40 | if err != nil { 41 | if err != io.EOF { 42 | return nil, err 43 | } 44 | } 45 | 46 | return buf, nil 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Now Live - an open streaming platform 2 | 3 | Have some fun streaming videos :-) 4 | 5 | ## What is this? 6 | 7 | I created this project to experiment some video tools. Could this be an open streaming platform in the future? Yes. 8 | 9 | Have you ever used live streaming tools like Youtube, Instagram and Facebook? There is a lot going on behing the scenes. 10 | 11 | *Now Live* assembles some of these tools together, creating a platform where people can stream their videos, packaging it for multiple devices using standard video formats. 12 | 13 | ## How to use? 14 | 15 | `make now-live` will build and run all the tools (packager, storage, frontend servers, etc). 16 | 17 | `make ingest` produce and ingest a sample video so the packager can produce HLS playlists. 18 | 19 | **These two commands must run simultaneously.** 20 | 21 | Now you can point your browser to http://localhost:8080/play and play the sample video. 22 | 23 | ### Components 24 | 25 | #### Packager 26 | 27 | ... 28 | 29 | #### Recorder 30 | 31 | ... 32 | 33 | #### Storage 34 | 35 | ... 36 | 37 | #### Web server 38 | 39 | ... -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Mauricio Antunes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /recorder/store.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/minio/minio-go" 7 | ) 8 | 9 | type Store struct { 10 | Client *minio.Client 11 | bucketName string 12 | } 13 | 14 | func NewStore(bucketName string) *Store { 15 | endpoint := "minio_proxy:80" 16 | accessKeyID := "minio" 17 | secretAccessKey := "minio123" 18 | client, err := minio.New(endpoint, accessKeyID, secretAccessKey, false) 19 | if err != nil { 20 | log.Fatal(err) 21 | } 22 | 23 | return &Store{Client: client, bucketName: bucketName} 24 | } 25 | 26 | func (s *Store) UploadFile(filePath string) { 27 | bucketName := "video" 28 | resource := Resource{File: filePath} 29 | 30 | n, err := s.Client.FPutObject(bucketName, resource.ObjectName("/app/media"), filePath, minio.PutObjectOptions{}) 31 | if err != nil { 32 | log.Fatalln(err) 33 | } 34 | 35 | log.Printf("Successfully uploaded %s of size %d\n", filePath, n) 36 | } 37 | 38 | func (s *Store) CreateBucket(bucketName string) error { 39 | err := s.Client.MakeBucket(bucketName, "us-east-1") 40 | if err != nil { 41 | exists, errExists := s.Client.BucketExists(bucketName) 42 | if errExists == nil && exists { 43 | return nil 44 | } 45 | return errExists 46 | } 47 | return err 48 | } 49 | -------------------------------------------------------------------------------- /api/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "mime" 5 | "net/http" 6 | "path/filepath" 7 | "strconv" 8 | 9 | "github.com/labstack/echo/v4" 10 | ) 11 | 12 | func init() { 13 | mime.AddExtensionType(".ts", "video/mp2t") 14 | mime.AddExtensionType(".m3u8", "application/vnd.apple.mpegurl") 15 | mime.AddExtensionType(".mp4", "video/mp4") 16 | } 17 | 18 | type API struct { 19 | *echo.Echo 20 | store *Store 21 | } 22 | 23 | func NewAPI() *API { 24 | return &API{ 25 | echo.New(), 26 | NewStore(), 27 | } 28 | } 29 | 30 | func main() { 31 | api := NewAPI() 32 | api.GET("/live/*", api.GetResource) 33 | api.Logger.Fatal(api.Start(":1323")) 34 | } 35 | 36 | func (a *API) GetResource(c echo.Context) error { 37 | resource := Resource{Path: c.Request().RequestURI} 38 | object, err := a.store.GetObject(resource.ObjectName()) 39 | if len(object) == 0 { 40 | c.Response().WriteHeader(http.StatusNotFound) 41 | return err 42 | } 43 | 44 | if err != nil { 45 | return err 46 | } 47 | 48 | contentType := mime.TypeByExtension(filepath.Ext(resource.Path)) 49 | c.Response().Header().Set(echo.HeaderContentType, contentType) 50 | c.Response().Header().Set(echo.HeaderContentLength, strconv.Itoa(len(object))) 51 | return c.String(http.StatusOK, string(object)) 52 | } 53 | -------------------------------------------------------------------------------- /frontend/nginx.conf: -------------------------------------------------------------------------------- 1 | events { 2 | worker_connections 1024; 3 | } 4 | 5 | error_log stderr; 6 | 7 | http { 8 | resolver 127.0.0.1 ipv6=off; 9 | 10 | upstream backend { 11 | server api:1323; 12 | } 13 | 14 | proxy_cache_path /tmp levels=1:2 keys_zone=now_live_cache:10m max_size=1g inactive=5m use_temp_path=off; 15 | 16 | server { 17 | listen 8080; 18 | 19 | location / { 20 | proxy_cache now_live_cache; 21 | proxy_cache_lock on; 22 | proxy_cache_lock_timeout 2s; 23 | proxy_cache_use_stale error timeout updating invalid_header; 24 | proxy_ignore_headers Cache-Control; 25 | 26 | if ($request_method = 'OPTIONS') { 27 | add_header 'Access-Control-Allow-Origin' '*'; 28 | add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 29 | add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; 30 | add_header 'Access-Control-Max-Age' 1728000; 31 | add_header 'Content-Type' 'text/plain; charset=utf-8'; 32 | add_header 'Content-Length' 0; 33 | return 204; 34 | } 35 | 36 | if ($request_method = 'GET') { 37 | add_header 'Access-Control-Allow-Origin' '*'; 38 | add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 39 | add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; 40 | add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; 41 | } 42 | 43 | proxy_pass http://backend; 44 | } 45 | 46 | location /play { 47 | alias /usr/local/openresty/nginx; 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /frontend/player/index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |