github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/ingress/validation_test.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 ingress
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/livekit/protocol/livekit"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestValidate(t *testing.T) {
    25  	info := &livekit.IngressInfo{}
    26  
    27  	err := Validate(info)
    28  	require.Error(t, err)
    29  
    30  	info.StreamKey = "stream_key"
    31  	err = Validate(info)
    32  	require.Error(t, err)
    33  
    34  	info.RoomName = "room_name"
    35  	err = Validate(info)
    36  	require.Error(t, err)
    37  
    38  	info.ParticipantIdentity = "participant_identity"
    39  	err = Validate(info)
    40  	require.NoError(t, err)
    41  
    42  	// make sure video parameters are validated. Full validation logic tested in the next test
    43  	info.Video = &livekit.IngressVideoOptions{}
    44  	err = Validate(info)
    45  	require.NoError(t, err)
    46  
    47  	info.Video.Source = livekit.TrackSource_MICROPHONE
    48  	err = Validate(info)
    49  	require.Error(t, err)
    50  
    51  	info.Video.Source = livekit.TrackSource_CAMERA
    52  
    53  	// make sure audio parameters are validated. Full validation logic tested in the next test
    54  	info.Audio = &livekit.IngressAudioOptions{}
    55  	err = Validate(info)
    56  	require.NoError(t, err)
    57  
    58  	info.Audio.Source = livekit.TrackSource_CAMERA
    59  	err = Validate(info)
    60  	require.Error(t, err)
    61  
    62  	info.Audio.Source = livekit.TrackSource_SCREEN_SHARE_AUDIO
    63  	err = Validate(info)
    64  	require.NoError(t, err)
    65  }
    66  
    67  func TestValidateBypassTranscoding(t *testing.T) {
    68  	info := &livekit.IngressInfo{}
    69  
    70  	err := ValidateBypassTranscoding(info)
    71  	require.NoError(t, err)
    72  
    73  	info.BypassTranscoding = true
    74  	err = ValidateBypassTranscoding(info)
    75  	require.Error(t, err)
    76  
    77  	info.InputType = livekit.IngressInput_WHIP_INPUT
    78  	err = ValidateBypassTranscoding(info)
    79  	require.NoError(t, err)
    80  
    81  	info.Video = &livekit.IngressVideoOptions{}
    82  	err = ValidateBypassTranscoding(info)
    83  	require.NoError(t, err)
    84  
    85  	info.Video.EncodingOptions = &livekit.IngressVideoOptions_Preset{}
    86  	err = ValidateBypassTranscoding(info)
    87  	require.Error(t, err)
    88  
    89  	info.Video = nil
    90  
    91  	info.Audio = &livekit.IngressAudioOptions{}
    92  	err = ValidateBypassTranscoding(info)
    93  	require.NoError(t, err)
    94  
    95  	info.Audio.EncodingOptions = &livekit.IngressAudioOptions_Options{
    96  		Options: &livekit.IngressAudioEncodingOptions{},
    97  	}
    98  	err = ValidateBypassTranscoding(info)
    99  	require.Error(t, err)
   100  
   101  }
   102  
   103  func TestValidateEnableTranscoding(t *testing.T) {
   104  	info := &livekit.IngressInfo{}
   105  	T := true
   106  	F := false
   107  
   108  	err := ValidateEnableTranscoding(info)
   109  	require.NoError(t, err)
   110  
   111  	info.InputType = livekit.IngressInput_WHIP_INPUT
   112  	err = ValidateEnableTranscoding(info)
   113  	require.NoError(t, err)
   114  
   115  	info.Audio = &livekit.IngressAudioOptions{}
   116  	info.Audio.EncodingOptions = &livekit.IngressAudioOptions_Options{}
   117  	err = ValidateEnableTranscoding(info)
   118  	require.Error(t, err)
   119  
   120  	info.Audio.EncodingOptions = nil
   121  
   122  	info.EnableTranscoding = &T
   123  	err = ValidateEnableTranscoding(info)
   124  	require.NoError(t, err)
   125  
   126  	info.EnableTranscoding = &F
   127  	err = ValidateEnableTranscoding(info)
   128  	require.NoError(t, err)
   129  
   130  	info.Video = &livekit.IngressVideoOptions{}
   131  	info.Video.EncodingOptions = &livekit.IngressVideoOptions_Preset{}
   132  	err = ValidateEnableTranscoding(info)
   133  	require.Error(t, err)
   134  
   135  	info.Video.EncodingOptions = nil
   136  
   137  	info.InputType = livekit.IngressInput_RTMP_INPUT
   138  	err = ValidateEnableTranscoding(info)
   139  	require.Error(t, err)
   140  
   141  	info.EnableTranscoding = &T
   142  	err = ValidateEnableTranscoding(info)
   143  	require.NoError(t, err)
   144  }
   145  
   146  func TestValidateVideoOptionsConsistency(t *testing.T) {
   147  	video := &livekit.IngressVideoOptions{}
   148  	err := ValidateVideoOptionsConsistency(video)
   149  	require.NoError(t, err)
   150  
   151  	video.Source = livekit.TrackSource_MICROPHONE
   152  	err = ValidateVideoOptionsConsistency(video)
   153  	require.Error(t, err)
   154  
   155  	video.Source = livekit.TrackSource_CAMERA
   156  	video.EncodingOptions = &livekit.IngressVideoOptions_Preset{
   157  		Preset: livekit.IngressVideoEncodingPreset(42),
   158  	}
   159  	err = ValidateVideoOptionsConsistency(video)
   160  	require.Error(t, err)
   161  
   162  	video.EncodingOptions = &livekit.IngressVideoOptions_Preset{
   163  		Preset: livekit.IngressVideoEncodingPreset_H264_1080P_30FPS_1_LAYER,
   164  	}
   165  	err = ValidateVideoOptionsConsistency(video)
   166  	require.NoError(t, err)
   167  
   168  	video.EncodingOptions = &livekit.IngressVideoOptions_Options{
   169  		Options: &livekit.IngressVideoEncodingOptions{
   170  			VideoCodec: livekit.VideoCodec_H264_HIGH,
   171  		},
   172  	}
   173  	err = ValidateVideoOptionsConsistency(video)
   174  	require.Error(t, err)
   175  
   176  	video.EncodingOptions = &livekit.IngressVideoOptions_Options{
   177  		Options: &livekit.IngressVideoEncodingOptions{
   178  			VideoCodec: livekit.VideoCodec_DEFAULT_VC,
   179  		},
   180  	}
   181  	err = ValidateVideoOptionsConsistency(video)
   182  	require.NoError(t, err)
   183  
   184  	video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{
   185  		&livekit.VideoLayer{},
   186  	}
   187  	err = ValidateVideoOptionsConsistency(video)
   188  	require.Error(t, err)
   189  
   190  	video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{
   191  		&livekit.VideoLayer{
   192  			Width:  640,
   193  			Height: 480,
   194  		},
   195  	}
   196  	err = ValidateVideoOptionsConsistency(video)
   197  	require.NoError(t, err)
   198  
   199  	video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{
   200  		&livekit.VideoLayer{
   201  			Width:  641,
   202  			Height: 480,
   203  		},
   204  	}
   205  	err = ValidateVideoOptionsConsistency(video)
   206  	require.Error(t, err)
   207  
   208  	video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{
   209  		&livekit.VideoLayer{
   210  			Width:   640,
   211  			Height:  480,
   212  			Quality: livekit.VideoQuality_HIGH,
   213  		},
   214  		&livekit.VideoLayer{
   215  			Width:   640,
   216  			Height:  480,
   217  			Quality: livekit.VideoQuality_LOW,
   218  		},
   219  	}
   220  	err = ValidateVideoOptionsConsistency(video)
   221  	require.NoError(t, err)
   222  
   223  	video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{
   224  		&livekit.VideoLayer{
   225  			Width:   640,
   226  			Height:  480,
   227  			Quality: livekit.VideoQuality_HIGH,
   228  		},
   229  		&livekit.VideoLayer{
   230  			Width:   1280,
   231  			Height:  720,
   232  			Quality: livekit.VideoQuality_HIGH,
   233  		},
   234  	}
   235  	err = ValidateVideoOptionsConsistency(video)
   236  	require.Error(t, err)
   237  
   238  	video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{
   239  		&livekit.VideoLayer{
   240  			Width:   640,
   241  			Height:  480,
   242  			Quality: livekit.VideoQuality_HIGH,
   243  		},
   244  		&livekit.VideoLayer{
   245  			Width:   1280,
   246  			Height:  720,
   247  			Quality: livekit.VideoQuality_LOW,
   248  		},
   249  	}
   250  	err = ValidateVideoOptionsConsistency(video)
   251  	require.Error(t, err)
   252  
   253  	video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{
   254  		&livekit.VideoLayer{
   255  			Width:   640,
   256  			Height:  480,
   257  			Quality: livekit.VideoQuality_LOW,
   258  		},
   259  		&livekit.VideoLayer{
   260  			Width:   1280,
   261  			Height:  720,
   262  			Quality: livekit.VideoQuality_HIGH,
   263  		},
   264  	}
   265  
   266  	err = ValidateVideoOptionsConsistency(video)
   267  	require.NoError(t, err)
   268  }
   269  
   270  func TestValidateAudioOptionsConsistency(t *testing.T) {
   271  	audio := &livekit.IngressAudioOptions{}
   272  	err := ValidateAudioOptionsConsistency(audio)
   273  	require.NoError(t, err)
   274  
   275  	audio.Source = livekit.TrackSource_CAMERA
   276  	err = ValidateAudioOptionsConsistency(audio)
   277  	require.Error(t, err)
   278  
   279  	audio.Source = livekit.TrackSource_SCREEN_SHARE_AUDIO
   280  	audio.EncodingOptions = &livekit.IngressAudioOptions_Preset{
   281  		Preset: livekit.IngressAudioEncodingPreset(42),
   282  	}
   283  	err = ValidateAudioOptionsConsistency(audio)
   284  	require.Error(t, err)
   285  
   286  	audio.EncodingOptions = &livekit.IngressAudioOptions_Preset{
   287  		Preset: livekit.IngressAudioEncodingPreset_OPUS_MONO_64KBS,
   288  	}
   289  	err = ValidateAudioOptionsConsistency(audio)
   290  	require.NoError(t, err)
   291  
   292  	audio.EncodingOptions = &livekit.IngressAudioOptions_Options{
   293  		Options: &livekit.IngressAudioEncodingOptions{
   294  			AudioCodec: livekit.AudioCodec_AAC,
   295  		},
   296  	}
   297  	err = ValidateAudioOptionsConsistency(audio)
   298  	require.Error(t, err)
   299  
   300  	audio.EncodingOptions = &livekit.IngressAudioOptions_Options{
   301  		Options: &livekit.IngressAudioEncodingOptions{
   302  			AudioCodec: livekit.AudioCodec_OPUS,
   303  			Channels:   3,
   304  		},
   305  	}
   306  	err = ValidateAudioOptionsConsistency(audio)
   307  	require.Error(t, err)
   308  
   309  	audio.EncodingOptions.(*livekit.IngressAudioOptions_Options).Options.Channels = 2
   310  	err = ValidateAudioOptionsConsistency(audio)
   311  	require.NoError(t, err)
   312  }