Skip to content

Commit

Permalink
server: make broadcaster and orchestrator config endpoint form values…
Browse files Browse the repository at this point in the history
… optional
  • Loading branch information
kyriediculous committed Aug 8, 2020
1 parent 5f57721 commit 2258db6
Showing 1 changed file with 102 additions and 72 deletions.
174 changes: 102 additions & 72 deletions server/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package server

import (
"encoding/json"
"errors"

"flag"
"fmt"
"math/big"
Expand All @@ -25,6 +25,7 @@ import (
lpTypes "github.com/livepeer/go-livepeer/eth/types"
"github.com/livepeer/go-livepeer/monitor"
ffmpeg "github.com/livepeer/lpms/ffmpeg"
"github.com/pkg/errors"
)

var vFlag *glog.Level = flag.Lookup("v").Value.(*glog.Level)
Expand Down Expand Up @@ -85,58 +86,66 @@ func (s *LivepeerServer) cliWebServerHandlers(bindAddr string) *http.ServeMux {
//Set the broadcast config for creating onchain jobs.
mux.HandleFunc("/setBroadcastConfig", func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
glog.Errorf("Parse Form Error: %v", err)
err = errors.Wrapf(err, "Parse form error")
glog.Error(err)
respondWith400(w, err.Error())
return
}

pricePerUnit := r.FormValue("maxPricePerUnit")
pr, err := strconv.ParseInt(pricePerUnit, 10, 64)
if err != nil {
glog.Errorf("Error converting string to int64: %v\n", err)
return
}

pixelsPerUnit := r.FormValue("pixelsPerUnit")
px, err := strconv.ParseInt(pixelsPerUnit, 10, 64)
if err != nil {
glog.Errorf("Error converting string to int64: %v\n", err)
return
}
if px <= 0 {
glog.Errorf("pixels per unit must be greater than 0, provided %d\n", px)
return
}

var price *big.Rat
if pr > 0 {
price = big.NewRat(pr, px)
}
if pricePerUnit != "" && pixelsPerUnit != "" {
pr, err := strconv.ParseInt(pricePerUnit, 10, 64)
if err != nil {
err = errors.Wrapf(err, "Error converting string to int64")
glog.Error(err)
respondWith400(w, err.Error())
return
}

transcodingOptions := r.FormValue("transcodingOptions")
if transcodingOptions == "" {
glog.Errorf("Need to provide transcoding options")
return
}
px, err := strconv.ParseInt(pixelsPerUnit, 10, 64)
if err != nil {
err = errors.Wrapf(err, "Error converting string to int64")
glog.Error(err)
respondWith400(w, err.Error())
return
}
if px <= 0 {
err = errors.Wrapf(err, "pixels per unit must be greater than 0, provided %d\n", px)
glog.Error(err)
respondWith400(w, err.Error())
return
}

profiles := []ffmpeg.VideoProfile{}
for _, pName := range strings.Split(transcodingOptions, ",") {
p, ok := ffmpeg.VideoProfileLookup[pName]
if ok {
profiles = append(profiles, p)
var price *big.Rat
if pr > 0 {
price = big.NewRat(pr, px)
}
}
if len(profiles) == 0 {
glog.Errorf("Invalid transcoding options: %v", transcodingOptions)
return
}
BroadcastCfg.SetMaxPrice(price)
BroadcastJobVideoProfiles = profiles
if price != nil {

BroadcastCfg.SetMaxPrice(price)

glog.Infof("Maximum transcoding price: %d per %q pixels\n", pr, px)
} else {
glog.Info("Maximum transcoding price per pixel not set, broadcaster is currently set to accept ANY price.\n")
}
glog.Infof("Transcode Job Type: %v", BroadcastJobVideoProfiles)

transcodingOptions := r.FormValue("transcodingOptions")
if transcodingOptions != "" {
profiles := []ffmpeg.VideoProfile{}
for _, pName := range strings.Split(transcodingOptions, ",") {
p, ok := ffmpeg.VideoProfileLookup[pName]
if ok {
profiles = append(profiles, p)
}
}
if len(profiles) == 0 {
err := fmt.Errorf("Invalid transcoding options: %v", transcodingOptions)
glog.Error(err)
respondWith400(w, err.Error())
return
}
BroadcastJobVideoProfiles = profiles
glog.Infof("Transcode Job Type: %v", BroadcastJobVideoProfiles)
}
})

mux.HandleFunc("/getBroadcastConfig", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -338,47 +347,62 @@ func (s *LivepeerServer) cliWebServerHandlers(bindAddr string) *http.ServeMux {
//Set transcoder config on-chain.
mux.HandleFunc("/setOrchestratorConfig", func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
glog.Errorf("Parse Form Error: %v", err)
err = errors.Wrapf(err, "Parse form error")
glog.Error(err)
respondWith400(w, err.Error())
return
}

blockRewardCutStr := r.FormValue("blockRewardCut")
if blockRewardCutStr == "" {
glog.Errorf("Need to provide block reward cut")
return
}
blockRewardCut, err := strconv.ParseFloat(blockRewardCutStr, 64)
if err != nil {
glog.Errorf("Cannot convert block reward cut: %v", err)
return
pixels := r.FormValue("pixelsPerUnit")
price := r.FormValue("pricePerUnit")
if pixels != "" && price != "" {
if err := s.setOrchestratorPriceInfo(price, pixels); err != nil {
glog.Error(err)
respondWith400(w, err.Error())
return
}
}

feeShareStr := r.FormValue("feeShare")
if feeShareStr == "" {
glog.Errorf("Need to provide fee share")
return
}
feeShare, err := strconv.ParseFloat(feeShareStr, 64)
if err != nil {
glog.Errorf("Cannot convert fee share: %v", err)
return
var (
blockRewardCut float64
feeShare float64
err error
)
blockRewardCutStr := r.FormValue("blockRewardCut")

if blockRewardCutStr != "" {
blockRewardCut, err = strconv.ParseFloat(blockRewardCutStr, 64)
if err != nil {
err = errors.Wrapf(err, "Cannot convert block reward cut")
glog.Error(err)
respondWith400(w, err.Error())
return
}
}

if err := s.setOrchestratorPriceInfo(r.FormValue("pricePerUnit"), r.FormValue("pixelsPerUnit")); err != nil {
glog.Error(err)
return
feeShareStr := r.FormValue("feeShare")
if feeShareStr != "" {
feeShare, err = strconv.ParseFloat(feeShareStr, 64)
if err != nil {
err = errors.Wrapf(err, "Cannot convert fee share")
glog.Error(err)
respondWith400(w, err.Error())
return
}
}

t, err := s.LivepeerNode.Eth.GetTranscoder(s.LivepeerNode.Eth.Account().Address)
if err != nil {
glog.Error(err)
respondWith500(w, err.Error())
return
}

if t.RewardCut.Cmp(eth.FromPerc(blockRewardCut)) != 0 || t.FeeShare.Cmp(eth.FromPerc(feeShare)) != 0 {
if feeShareStr != "" && blockRewardCutStr != "" && (t.RewardCut.Cmp(eth.FromPerc(blockRewardCut)) != 0 || t.FeeShare.Cmp(eth.FromPerc(feeShare)) != 0) {
tx, err := s.LivepeerNode.Eth.Transcoder(eth.FromPerc(blockRewardCut), eth.FromPerc(feeShare))
if err != nil {
glog.Error(err)
respondWith500(w, err.Error())
return
}

Expand All @@ -387,20 +411,26 @@ func (s *LivepeerServer) cliWebServerHandlers(bindAddr string) *http.ServeMux {
err = s.LivepeerNode.Eth.CheckTx(tx)
if err != nil {
glog.Error(err)
respondWith500(w, err.Error())
return
}
}

serviceURI := r.FormValue("serviceURI")
if _, err := url.ParseRequestURI(serviceURI); err != nil {
glog.Error(err)
return
}

if t.ServiceURI != serviceURI {
if err := s.setServiceURI(serviceURI); err != nil {
if serviceURI != "" {
if _, err := url.ParseRequestURI(serviceURI); err != nil {
glog.Error(err)
respondWith400(w, err.Error())
return
}

if t.ServiceURI != serviceURI {
if err := s.setServiceURI(serviceURI); err != nil {
glog.Error(err)
respondWith500(w, err.Error())
return
}
}
}
})

Expand Down

0 comments on commit 2258db6

Please sign in to comment.