-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements with concurrent encodings & stopping videos when leaving…
… page
- Loading branch information
1 parent
01e14d1
commit 535d8fe
Showing
16 changed files
with
227 additions
and
110 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 @@ | ||
|
||
|
||
https://bitbucket.org/walterebert/ffmpeg-hls/src/f14ccab90f1884ecee9bbccd025af87a3b2f837a/build.sh?at=default&fileviewer=file-view-default |
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 |
---|---|---|
|
@@ -15,5 +15,4 @@ Running it | |
|
||
License | ||
------- | ||
See LICENSE.txt | ||
|
||
See LICENSE.txt |
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,5 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore | ||
!README.md |
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,110 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
//"net/http" | ||
"bufio" | ||
"crypto/sha1" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"syscall" | ||
) | ||
|
||
type Empty struct{} | ||
|
||
type HttpCommandHandler struct { | ||
tokenChannel chan Empty | ||
cacheDir string | ||
inProgress map[string]string | ||
inProgressMutex *sync.RWMutex | ||
// path string | ||
} | ||
|
||
func NewHttpCommandHandler(workerCount int, cacheDir string) *HttpCommandHandler { | ||
ch := &HttpCommandHandler{make(chan Empty, workerCount), cacheDir, make(map[string]string), new(sync.RWMutex)} | ||
for i := workerCount; i > 0; i-- { | ||
ch.tokenChannel <- Empty{} | ||
} | ||
go ch.start() | ||
return ch | ||
} | ||
|
||
func (s *HttpCommandHandler) start() { | ||
|
||
} | ||
|
||
func (s *HttpCommandHandler) calculateKey(cmd string, args []string) string { | ||
h := sha1.New() | ||
h.Write([]byte(cmd)) | ||
for _, v := range args { | ||
h.Write([]byte(v)) | ||
} | ||
sum := h.Sum(nil) | ||
return fmt.Sprintf("%x", sum) | ||
} | ||
|
||
func (s *HttpCommandHandler) ServeCommand(cmdPath string, args []string, w io.Writer) error { | ||
key := s.calculateKey(cmdPath, args) | ||
token := <-s.tokenChannel | ||
//log.Printf("Token: %v",key) | ||
defer func() { | ||
s.tokenChannel <- token | ||
//log.Printf("Released token") | ||
}() | ||
cachePath := filepath.Join("cache", s.cacheDir, key) | ||
mkerr := os.MkdirAll(filepath.Join("cache", s.cacheDir), 0777) | ||
if mkerr != nil { | ||
log.Printf("Could not create cache dir %v: %v", filepath.Join("cache", s.cacheDir), mkerr) | ||
return mkerr | ||
} | ||
if file, err := os.Open(cachePath); err == nil { | ||
defer file.Close() | ||
_, err = io.Copy(w, file) | ||
if err != nil { | ||
log.Printf("Error copying file to client: %v", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
cacheFile, ferr := os.Create(cachePath) | ||
if ferr != nil { | ||
log.Printf("Could not create cache file %v: %v", cacheFile, ferr) | ||
return ferr | ||
} | ||
defer cacheFile.Close() | ||
log.Printf("Executing %v %v", cmdPath, args) | ||
cmd := exec.Command(cmdPath, args...) | ||
stdout, err := cmd.StdoutPipe() | ||
defer stdout.Close() | ||
if err != nil { | ||
log.Printf("Error opening stdout of command: %v", err) | ||
return err | ||
} | ||
err = cmd.Start() | ||
if err != nil { | ||
log.Printf("Error starting command: %v", err) | ||
return err | ||
} | ||
filew := bufio.NewWriter(cacheFile) | ||
multiw := io.MultiWriter(filew, w) | ||
_, err = io.Copy(multiw, stdout) | ||
if err != nil { | ||
log.Printf("Error copying data to client: %v", err) | ||
cacheFile.Close() | ||
os.Remove(cachePath) | ||
// Ask the process to exit | ||
cmd.Process.Signal(syscall.SIGKILL) | ||
cmd.Process.Wait() | ||
return err | ||
} | ||
cmd.Wait() | ||
filew.Flush() | ||
log.Printf("Streaming done\n"); | ||
return nil | ||
//s.inProgressMutex.Lock() | ||
//s.inProgressMutex.Unlock() | ||
} |
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
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
Oops, something went wrong.