| index 943c1350e..a1bfd6836 100644 |
| --- a/docs/site/live-streams-bring-your-own-livekit.md |
| +++ b/docs/site/live-streams-bring-your-own-livekit.md |
| @@ -10,14 +10,15 @@ You need: |
| |
| - A LiveKit Cloud project. Its settings page gives you the project URL, such as `wss://shop-a1b2c3.livekit.cloud`, and an API key and secret. |
| - An object store for the app, made with `carlos store create --app shop`, on a deployment with media conversion turned on. The store must pass the check on the media conversion page. A store made with `carlos store create` and left alone passes. |
| -- A Service credential for media conversion, as in [the media conversion example](/docs/media-conversion#an-example). |
| +- A Service credential for media conversion, as in [the media conversion example](/docs/media-conversion#an-example). The same credential gets your app a write credential for each recording. |
| +- Write credentials turned on for your deployment. Your operator does that. If they're off, `carlos store credential` says so. |
| |
| What the platform needs from your app: |
| |
| - **No egress setting.** Instances can already reach public addresses, and LiveKit Cloud's API and project hosts are public. `carlos apps set --egress-allow` is only for private addresses, and it takes IP addresses, not hostnames. |
| - **A public route for LiveKit's webhook.** LiveKit calls your app when a recording is done. Any path on your app's own hostname works, such as `https://shop.example.com/livekit/webhook`. A host behind [carlos gate](/docs/cli#carlos-gate) can't receive it, because the gate asks every request for the password. |
| -- **The store's region in every recording request.** If you leave both the region and endpoint out, LiveKit asks S3 for the bucket's location, and the store's credential isn't allowed to ask. |
| -- **No tags on recordings.** LiveKit can tag the files it uploads. Leave that empty: the store's credential can't tag objects, so S3 refuses the upload. |
| +- **The store's region in every recording request.** If you leave both the region and endpoint out, LiveKit asks S3 for the bucket's location, and a write credential isn't allowed to ask. The platform gives you the region with the credential. |
| +- **No tags on recordings.** LiveKit can tag the files it uploads. Leave that empty: a write credential can't tag objects, so S3 refuses the upload. |
| |
| ## How it fits together |
| |
| @@ -26,18 +27,21 @@ What the platform needs from your app: |
| carlos env set --app shop LIVEKIT_URL=wss://shop-a1b2c3.livekit.cloud |
| carlos secrets set --app shop LIVEKIT_API_KEY=APIa1b2c3d4 |
| carlos secrets set --app shop LIVEKIT_API_SECRET - < livekit-secret.txt |
| +carlos env set --app shop CARLOS_CONSOLE=https://console.example.com |
| +carlos env set --app shop CARLOS_ACCOUNT=<account> |
| add https://shop.example.com/livekit/webhook as a webhook in the LiveKit project |
| |
| # per stream, on the app's server |
| 1. mint a token for the streamer and one for each viewer -> the browsers join the room |
| -2. start an Egress to the app's store -> LiveKit records the room |
| -3. LiveKit POSTs egress_ended to /livekit/webhook -> the recording's key |
| -4. POST the key to media conversion, poll until done -> the files to play |
| +2. ask the platform for a write credential -> it can only upload to recordings/<id>/ |
| +3. start an Egress with it -> LiveKit records the room |
| +4. LiveKit POSTs egress_ended to /livekit/webhook -> the recording's key |
| +5. POST the key to media conversion, poll until done -> the files to play |
| ``` |
| |
| -The API key and secret are secrets, so they go in with `carlos secrets set`. The `-` reads the secret from the file, so it never appears on your command line. The project URL isn't secret. Your browser code needs it too. |
| +The API key and secret are secrets, so they go in with `carlos secrets set`. The `-` reads the secret from the file, so it never appears on your command line. The project URL isn't secret. Your browser code needs it too. `CARLOS_CONSOLE` is your console's origin and `<account>` is your account ID, as on the media conversion page. |
| |
| -The examples below are in Go, with LiveKit's server SDK (`github.com/livekit/server-sdk-go/v2`) and its protocol package (`github.com/livekit/protocol`). LiveKit's other server SDKs have the same calls. |
| +The examples below are in Go, with LiveKit's server SDK (`github.com/livekit/server-sdk-go/v2`, checked against v2.20.1) and its protocol package (`github.com/livekit/protocol`, v1.54.1). LiveKit's other server SDKs have the same calls. |
| |
| ## Tokens for the streamer and the viewers |
| |
| @@ -81,40 +85,100 @@ To stop someone chatting, your server calls `UpdateParticipant` on LiveKit's roo |
| |
| ## Recording into your store |
| |
| -Your server starts an Egress, LiveKit's recorder, and points it at the app's store with the store's own credentials. The platform delivers them to every instance as `CARLOS_STORE_BUCKET`, `CARLOS_STORE_REGION`, `CARLOS_STORE_ACCESS_KEY` and `CARLOS_STORE_SECRET_KEY`. |
| +Your server starts an Egress, LiveKit's recorder, and points it at the app's store. LiveKit gets a write credential, not the store's own credential. It can only upload files under one folder you name, and only until it expires. |
| |
| -A room composite Egress records the room as one MP4, laid out the way a viewer sees it: |
| +Ask the platform for a write credential just before each recording, with the Service credential: |
| |
| ```go |
| import ( |
| + "bytes" |
| "context" |
| + "encoding/json" |
| + "fmt" |
| + "net/http" |
| "os" |
| + "time" |
| |
| "github.com/livekit/protocol/livekit" |
| - lksdk "github.com/livekit/server-sdk-go/v2" |
| ) |
| |
| -func storeUpload() *livekit.S3Upload { |
| - return &livekit.S3Upload{ |
| - AccessKey: os.Getenv("CARLOS_STORE_ACCESS_KEY"), |
| - Secret: os.Getenv("CARLOS_STORE_SECRET_KEY"), |
| - Region: os.Getenv("CARLOS_STORE_REGION"), // required: see Before you start |
| - Bucket: os.Getenv("CARLOS_STORE_BUCKET"), |
| - // No Tagging. The store refuses tagged uploads. |
| +// recordingUpload asks the platform for a credential that can only upload |
| +// under prefix, for ttl, in the shape LiveKit takes. |
| +func recordingUpload(ctx context.Context, prefix string, ttl time.Duration) (*livekit.S3Upload, error) { |
| + body, err := json.Marshal(map[string]any{"access": "write", "prefix": prefix, "ttl_seconds": int(ttl.Seconds())}) |
| + if err != nil { |
| + return nil, err |
| + } |
| + url := os.Getenv("CARLOS_CONSOLE") + "/api/cli/apps/" + os.Getenv("CARLOS_ACCOUNT") + "/shop/store/credentials" |
| + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) |
| + if err != nil { |
| + return nil, err |
| + } |
| + req.Header.Set("Authorization", "Bearer "+os.Getenv("CARLOS_SERVICE_TOKEN")) |
| + req.Header.Set("Content-Type", "application/json") |
| + resp, err := http.DefaultClient.Do(req) |
| + if err != nil { |
| + return nil, err |
| } |
| + defer resp.Body.Close() |
| + if resp.StatusCode != http.StatusOK { |
| + return nil, fmt.Errorf("write credential: %s", resp.Status) |
| + } |
| + var c struct { |
| + AccessKey string `json:"access_key"` |
| + Secret string `json:"secret"` |
| + SessionToken string `json:"session_token"` |
| + Region string `json:"region"` |
| + Bucket string `json:"bucket"` |
| + } |
| + if err := json.NewDecoder(resp.Body).Decode(&c); err != nil { |
| + return nil, err |
| + } |
| + return &livekit.S3Upload{ |
| + AccessKey: c.AccessKey, |
| + Secret: c.Secret, |
| + SessionToken: c.SessionToken, |
| + Region: c.Region, // required: see Before you start |
| + Bucket: c.Bucket, |
| + // No Tagging. A write credential can't tag, so S3 refuses the upload. |
| + }, nil |
| +} |
| +``` |
| + |
| +Give each recording a folder of its own. A write credential can replace a file already in its folder, but it can't touch anything else. |
| + |
| +Then start the Egress with LiveKit's `StartEgress` call. A template source records the room as one MP4, laid out the way a viewer sees it: |
| + |
| +```go |
| +import ( |
| + "context" |
| + "os" |
| + "time" |
| + |
| + "github.com/livekit/protocol/livekit" |
| + lksdk "github.com/livekit/server-sdk-go/v2" |
| +) |
| + |
| +func egressClient() *lksdk.EgressClient { |
| + return lksdk.NewEgressClient(os.Getenv("LIVEKIT_URL"), os.Getenv("LIVEKIT_API_KEY"), os.Getenv("LIVEKIT_API_SECRET")) |
| } |
| |
| // startRecording records the whole room under recordings/<id>/. |
| func startRecording(ctx context.Context, room, id string) (string, error) { |
| - egress := lksdk.NewEgressClient(os.Getenv("LIVEKIT_URL"), os.Getenv("LIVEKIT_API_KEY"), os.Getenv("LIVEKIT_API_SECRET")) |
| - info, err := egress.StartRoomCompositeEgress(ctx, &livekit.RoomCompositeEgressRequest{ |
| + upload, err := recordingUpload(ctx, "recordings/"+id+"/", 4*time.Hour) |
| + if err != nil { |
| + return "", err |
| + } |
| + info, err := egressClient().StartEgress(ctx, &livekit.StartEgressRequest{ |
| RoomName: room, |
| - Layout: "speaker", |
| - FileOutputs: []*livekit.EncodedFileOutput{{ |
| - FileType: livekit.EncodedFileType_MP4, |
| - Filepath: "recordings/" + id + "/", |
| - Output: &livekit.EncodedFileOutput_S3{S3: storeUpload()}, |
| + Source: &livekit.StartEgressRequest_Template{Template: &livekit.TemplateSource{Layout: "speaker"}}, |
| + Outputs: []*livekit.Output{{ |
| + Config: &livekit.Output_File{File: &livekit.FileOutput{ |
| + FileType: livekit.EncodedFileType_MP4, |
| + Filepath: "recordings/" + id + "/", |
| + }}, |
| }}, |
| + Storage: &livekit.StorageConfig{Provider: &livekit.StorageConfig_S3{S3: upload}}, |
| }) |
| if err != nil { |
| return "", err |
| @@ -123,24 +187,35 @@ func startRecording(ctx context.Context, room, id string) (string, error) { |
| } |
| ``` |
| |
| -`Filepath` ends in `/`, so LiveKit names the file itself inside that folder, as `<room>-<time>.mp4`. That also keeps a retried recording from overwriting the first try. Don't record under `derived/`: media conversion won't take a source there. |
| +`Filepath` ends in `/`, so LiveKit names the file itself inside that folder, as `<room>-<time>.mp4`. That also keeps a retried recording from overwriting the first try. Don't record under `derived/`: media conversion won't take a source there, and the platform won't issue a write credential for it. |
| |
| -A track Egress records one track as it arrives. Video is not re-encoded. Audio is re-encoded as stereo Opus. LiveKit picks the file type from the track: WebM or MP4 for video, and Ogg for audio. Media conversion reads all three. Use it when you only need the streamer's camera or microphone: |
| +The credential has to last until LiveKit has uploaded the file, which it does when the recording stops. LiveKit Cloud stops a room recording at 3 hours, so 4 hours covers the recording and the upload. |
| + |
| +A media source records one video track as it arrives, without re-encoding it. LiveKit picks the file type from the track's codec, so leave `FileType` unset. Media conversion reads WebM and MP4. Use it when you only need the streamer's camera: |
| |
| ```go |
| -info, err := egress.StartTrackEgress(ctx, &livekit.TrackEgressRequest{ |
| +upload, err := recordingUpload(ctx, "recordings/"+id+"/", 13*time.Hour) |
| +if err != nil { |
| + return err |
| +} |
| +info, err := egressClient().StartEgress(ctx, &livekit.StartEgressRequest{ |
| RoomName: room, |
| - TrackId: trackID, |
| - Output: &livekit.TrackEgressRequest_File{File: &livekit.DirectFileOutput{ |
| - Filepath: "recordings/" + id + "/", |
| - Output: &livekit.DirectFileOutput_S3{S3: storeUpload()}, |
| + Source: &livekit.StartEgressRequest_Media{Media: &livekit.MediaSource{ |
| + Video: &livekit.MediaSource_VideoTrackId{VideoTrackId: trackID}, |
| }}, |
| + Encoding: &livekit.StartEgressRequest_Preset{Preset: livekit.EncodingOptionsPreset_PASSTHROUGH}, |
| + Outputs: []*livekit.Output{{ |
| + Config: &livekit.Output_File{File: &livekit.FileOutput{Filepath: "recordings/" + id + "/"}}, |
| + }}, |
| + Storage: &livekit.StorageConfig{Provider: &livekit.StorageConfig_S3{S3: upload}}, |
| }) |
| ``` |
| |
| +LiveKit Cloud stops a track recording at 12 hours, so that credential asks for 13. The longest a write credential can last is 24 hours. |
| + |
| LiveKit uploads the file when the recording stops. It also writes a small JSON manifest beside the file. Set `DisableManifest: true` on the output if you don't want it. |
| |
| -If your app also gets `CARLOS_STORE_ENDPOINT`, use that S3-compatible endpoint as `Endpoint` and set `ForcePathStyle: true`. LiveKit has to be able to reach that endpoint over the internet. |
| +Write credentials need AWS S3. If your app gets `CARLOS_STORE_ENDPOINT`, its store is on an S3-compatible server, and the platform can't issue one for it. |
| |
| ## When the recording is done |
| |
| @@ -207,7 +282,7 @@ Conversion counts against your account's [media allowance](/docs/media-conversio |
| If LiveKit can't upload the recording to your store, the recording isn't in your store and there is nothing to convert. Two common causes: |
| |
| - **The store is full.** A store past its plan's storage allowance refuses new objects, recordings included. |
| -- **The credential changed while the stream ran.** LiveKit uses the credential it was given when the recording started, and uploads at the end. If you run `carlos store rotate --finish --app shop` during a stream, the old credential stops working and the upload fails. Finish a rotation only when no recording is running. |
| +- **The write credential stopped working before the upload.** LiveKit uses the credential it was given when the recording started, and uploads at the end. The upload fails if the recording runs longer than the credential lasts, or if you run `carlos store credential --revoke --app shop` during a stream. Rotating the store's own credential with `carlos store rotate` doesn't affect write credentials. |
| |
| Without a backup copy, the webhook reports `EGRESS_FAILED`, and its error says the upload failed. With backup storage, it reports `EGRESS_COMPLETE` with `backup_storage_used` set instead. |
| |
| @@ -219,7 +294,7 @@ If your store has [virus scanning](/docs/cli#carlos-store-scan), a recording is |
| |
| ## Privacy: a third party in the media path |
| |
| -With this setup, LiveKit Cloud carries every stream. The video, the audio and the chat pass through LiveKit's servers, and LiveKit records the stream before it reaches your store. LiveKit also holds your store's credential while a recording runs. That credential can read, overwrite and delete every object in your app's store, not only recordings. It cannot change object tags or bucket settings. |
| +With this setup, LiveKit Cloud carries every stream. The video, the audio and the chat pass through LiveKit's servers, and LiveKit records the stream before it reaches your store. LiveKit also holds a write credential while a recording runs. It can only upload files to that recording's folder, until it expires. It can't read, list or delete anything in your store. |
| |
| The platform doesn't tell your users any of this. Whether and how to tell them is your app's decision. If you do, say it where people decide to stream or watch: near the start of your app's description, and on anything the stream produces, such as the recording's page. |
| |
| @@ -231,5 +306,4 @@ The platform bills your account as usual for the instance, the recording's stora |
| |
| ## Not yet supported |
| |
| -- **A credential that can only write recordings.** LiveKit needs your store's one credential, which can also read, overwrite and delete every other object in the store. The platform can't yet give an app a narrower credential to hand to a third party. |
| - **Converting recordings longer than 2 hours.** LiveKit Cloud limits a room-composite file to 3 hours, and media conversion converts up to 2 hours of video up to 1080p. To keep a longer stream playable, stop the recording and start a new one before it reaches 2 hours. Each part converts on its own. |