-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* PUSH_OUT_START trigger handler * UUID generation * studio callback
- Loading branch information
1 parent
2e7f91d
commit e38280c
Showing
7 changed files
with
142 additions
and
1 deletion.
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,59 @@ | ||
package misttriggers | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/livepeer/catalyst-api/clients" | ||
"github.com/livepeer/catalyst-api/errors" | ||
) | ||
|
||
// TriggerPushOutStart responds to PUSH_OUT_START trigger | ||
// This trigger is run right before an outgoing push is started. This trigger is stream-specific and must be blocking. | ||
// The payload for this trigger is multiple lines, each separated by a single newline character (without an ending newline), containing data: | ||
// | ||
// stream name (string) | ||
// push target URI (string) | ||
func (d *MistCallbackHandlersCollection) TriggerPushOutStart(w http.ResponseWriter, req *http.Request, payload []byte) { | ||
lines := strings.Split(strings.TrimSuffix(string(payload), "\n"), "\n") | ||
if len(lines) != 2 { | ||
errors.WriteHTTPBadRequest(w, "Bad request payload", fmt.Errorf("unknown payload '%s'", string(payload))) | ||
return | ||
} | ||
streamName := lines[0] | ||
destination := lines[1] | ||
var destinationToReturn string | ||
switch streamNameToPipeline(streamName) { | ||
case Recording: | ||
destinationToReturn = d.RecordingPushOutStart(w, req, streamName, destination) | ||
default: | ||
destinationToReturn = destination | ||
} | ||
if _, err := w.Write([]byte(destinationToReturn)); err != nil { | ||
log.Printf("TriggerPushOutStart failed to send rewritten url: %v", err) | ||
} | ||
} | ||
|
||
func (d *MistCallbackHandlersCollection) RecordingPushOutStart(w http.ResponseWriter, req *http.Request, streamName, destination string) string { | ||
event := &clients.RecordingEvent{ | ||
Event: "start", | ||
Timestamp: time.Now().UnixMilli(), | ||
StreamName: streamName, | ||
RecordingId: uuid.New().String(), | ||
Hostname: req.Host, | ||
} | ||
pushUrl, err := url.Parse(destination) | ||
if err != nil { | ||
log.Printf("RecordingPushOutStart url.Parse %v", err) | ||
return destination | ||
} | ||
// Add uuid after stream name | ||
pushUrl.Path = strings.Replace(pushUrl.Path, "$stream", "$stream/"+event.RecordingId, 1) | ||
go clients.DefaultCallbackClient.SendRecordingEvent(event) | ||
return pushUrl.String() | ||
} |
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
package misttriggers | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/livepeer/catalyst-api/clients" | ||
"github.com/livepeer/catalyst-api/config" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
|
@@ -18,6 +27,47 @@ func TestPipelineId(t *testing.T) { | |
} | ||
} | ||
|
||
func TestRecordingStart(t *testing.T) { | ||
testStartTime := time.Now().UnixMilli() | ||
mistCallbackHandlers := &MistCallbackHandlersCollection{MistClient: clients.StubMistClient{}} | ||
callbackHappened := make(chan bool, 10) | ||
callbackServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
payload, err := io.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
w.WriteHeader(200) | ||
message := clients.RecordingEvent{} | ||
err = json.Unmarshal(payload, &message) | ||
require.NoError(t, err) | ||
require.Equal(t, "videoSomeStreamName", message.StreamName) | ||
require.Equal(t, "start", message.Event) | ||
require.GreaterOrEqual(t, message.Timestamp, testStartTime) | ||
require.Less(t, message.Timestamp, testStartTime+2) | ||
require.NotEmpty(t, message.RecordingId) | ||
callbackHappened <- true | ||
})) | ||
defer callbackServer.Close() | ||
config.RecordingCallback = callbackServer.URL | ||
|
||
router := httprouter.New() | ||
router.POST("/api/mist/trigger", mistCallbackHandlers.Trigger()) | ||
pushOutTriggerPayload := "videoSomeStreamName\ns3+https://creds:[email protected]/region/livepeer-recordings-bucket/$stream/index.m3u8" | ||
req, _ := http.NewRequest("POST", "/api/mist/trigger", bytes.NewBuffer([]byte(pushOutTriggerPayload))) | ||
req.Header.Set("X-Trigger", "PUSH_OUT_START") | ||
req.Header.Set("Host", "test.livepeer.monster") | ||
rr := httptest.NewRecorder() | ||
router.ServeHTTP(rr, req) | ||
require.Equal(t, 200, rr.Result().StatusCode) | ||
result := rr.Body.String() | ||
require.Equal(t, "s3+https://creds:[email protected]/region/livepeer-recordings-bucket/$stream/", result[:81]) | ||
require.Greater(t, len(result), 92) | ||
require.Equal(t, "/index.m3u8", result[len(result)-11:]) | ||
select { | ||
case <-callbackHappened: | ||
case <-time.After(1 * time.Second): | ||
require.FailNow(t, "no callback happened") | ||
} | ||
} | ||
|
||
type StreamSample struct { | ||
streamName string | ||
expected PipelineId | ||
|
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