github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/egress/types.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package egress
    16  
    17  import "github.com/livekit/protocol/livekit"
    18  
    19  const (
    20  	EgressTypeRoomComposite  = "room_composite"
    21  	EgressTypeWeb            = "web"
    22  	EgressTypeParticipant    = "participant"
    23  	EgressTypeTrackComposite = "track_composite"
    24  	EgressTypeTrack          = "track"
    25  
    26  	OutputTypeFile     = "file"
    27  	OutputTypeStream   = "stream"
    28  	OutputTypeSegments = "segments"
    29  	OutputTypeImages   = "images"
    30  	OutputTypeMultiple = "multiple"
    31  
    32  	Unknown = "unknown"
    33  )
    34  
    35  // Outputs that can be used in egress that are started automatically on room creation
    36  type AutoEncodedOutput interface {
    37  	GetFileOutputs() []*livekit.EncodedFileOutput
    38  	GetSegmentOutputs() []*livekit.SegmentedFileOutput
    39  }
    40  
    41  type EncodedOutput interface {
    42  	AutoEncodedOutput
    43  	GetStreamOutputs() []*livekit.StreamOutput
    44  	GetImageOutputs() []*livekit.ImageOutput
    45  }
    46  
    47  type EncodedOutputDeprecated interface {
    48  	GetFile() *livekit.EncodedFileOutput
    49  	GetStream() *livekit.StreamOutput
    50  	GetSegments() *livekit.SegmentedFileOutput
    51  }
    52  
    53  type DirectOutput interface {
    54  	GetFile() *livekit.DirectFileOutput
    55  	GetWebsocketUrl() string
    56  }
    57  
    58  type UploadRequest interface {
    59  	GetS3() *livekit.S3Upload
    60  	GetGcp() *livekit.GCPUpload
    61  	GetAzure() *livekit.AzureBlobUpload
    62  	GetAliOSS() *livekit.AliOSSUpload
    63  }
    64  
    65  func GetTypes(request interface{}) (string, string) {
    66  	switch req := request.(type) {
    67  	case *livekit.EgressInfo_RoomComposite:
    68  		return EgressTypeRoomComposite, GetOutputType(req.RoomComposite)
    69  
    70  	case *livekit.EgressInfo_Web:
    71  		return EgressTypeWeb, GetOutputType(req.Web)
    72  
    73  	case *livekit.EgressInfo_Participant:
    74  		return EgressTypeParticipant, GetOutputType(req.Participant)
    75  
    76  	case *livekit.EgressInfo_TrackComposite:
    77  		return EgressTypeTrackComposite, GetOutputType(req.TrackComposite)
    78  
    79  	case *livekit.EgressInfo_Track:
    80  		return EgressTypeTrack, GetOutputType(req.Track)
    81  	}
    82  
    83  	return Unknown, Unknown
    84  }
    85  
    86  func GetOutputType(req interface{}) string {
    87  	if r, ok := req.(EncodedOutput); ok {
    88  		outputs := make([]string, 0)
    89  		if len(r.GetFileOutputs()) > 0 {
    90  			outputs = append(outputs, OutputTypeFile)
    91  		}
    92  		if len(r.GetStreamOutputs()) > 0 {
    93  			outputs = append(outputs, OutputTypeStream)
    94  		}
    95  		if len(r.GetSegmentOutputs()) > 0 {
    96  			outputs = append(outputs, OutputTypeSegments)
    97  		}
    98  		if len(r.GetImageOutputs()) > 0 {
    99  			outputs = append(outputs, OutputTypeImages)
   100  		}
   101  
   102  		switch len(outputs) {
   103  		default:
   104  			return OutputTypeMultiple
   105  		case 1:
   106  			return outputs[0]
   107  		case 0:
   108  			if r, ok := req.(EncodedOutputDeprecated); ok {
   109  				if r.GetFile() != nil {
   110  					return OutputTypeFile
   111  				}
   112  				if r.GetStream() != nil {
   113  					return OutputTypeStream
   114  				}
   115  				if r.GetSegments() != nil {
   116  					return OutputTypeSegments
   117  				}
   118  			}
   119  		}
   120  	}
   121  
   122  	if r, ok := req.(DirectOutput); ok {
   123  		if r.GetFile() != nil {
   124  			return OutputTypeFile
   125  		}
   126  		if r.GetWebsocketUrl() != "" {
   127  			return OutputTypeStream
   128  		}
   129  	}
   130  
   131  	return Unknown
   132  }