github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/ingress/validation_test.go (about) 1 package ingress 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 "github.com/whoyao/protocol/livekit" 8 ) 9 10 func TestValidate(t *testing.T) { 11 info := &livekit.IngressInfo{} 12 13 err := Validate(info) 14 require.Error(t, err) 15 16 info.StreamKey = "stream_key" 17 err = Validate(info) 18 require.Error(t, err) 19 20 info.RoomName = "room_name" 21 err = Validate(info) 22 require.Error(t, err) 23 24 info.ParticipantIdentity = "participant_identity" 25 err = Validate(info) 26 require.NoError(t, err) 27 28 // make sure video parameters are validated. Full validation logic tested in the next test 29 info.Video = &livekit.IngressVideoOptions{} 30 err = Validate(info) 31 require.NoError(t, err) 32 33 info.Video.Source = livekit.TrackSource_MICROPHONE 34 err = Validate(info) 35 require.Error(t, err) 36 37 info.Video.Source = livekit.TrackSource_CAMERA 38 39 // make sure audio parameters are validated. Full validation logic tested in the next test 40 info.Audio = &livekit.IngressAudioOptions{} 41 err = Validate(info) 42 require.NoError(t, err) 43 44 info.Audio.Source = livekit.TrackSource_CAMERA 45 err = Validate(info) 46 require.Error(t, err) 47 48 info.Audio.Source = livekit.TrackSource_SCREEN_SHARE_AUDIO 49 err = Validate(info) 50 require.NoError(t, err) 51 } 52 53 func TestValidateVideoOptionsConsistency(t *testing.T) { 54 video := &livekit.IngressVideoOptions{} 55 err := ValidateVideoOptionsConsistency(video) 56 require.NoError(t, err) 57 58 video.Source = livekit.TrackSource_MICROPHONE 59 err = ValidateVideoOptionsConsistency(video) 60 require.Error(t, err) 61 62 video.Source = livekit.TrackSource_CAMERA 63 video.EncodingOptions = &livekit.IngressVideoOptions_Preset{ 64 Preset: livekit.IngressVideoEncodingPreset(42), 65 } 66 err = ValidateVideoOptionsConsistency(video) 67 require.Error(t, err) 68 69 video.EncodingOptions = &livekit.IngressVideoOptions_Preset{ 70 Preset: livekit.IngressVideoEncodingPreset_H264_1080P_30FPS_1_LAYER, 71 } 72 err = ValidateVideoOptionsConsistency(video) 73 require.NoError(t, err) 74 75 video.EncodingOptions = &livekit.IngressVideoOptions_Options{ 76 Options: &livekit.IngressVideoEncodingOptions{ 77 VideoCodec: livekit.VideoCodec_H264_HIGH, 78 }, 79 } 80 err = ValidateVideoOptionsConsistency(video) 81 require.Error(t, err) 82 83 video.EncodingOptions = &livekit.IngressVideoOptions_Options{ 84 Options: &livekit.IngressVideoEncodingOptions{ 85 VideoCodec: livekit.VideoCodec_DEFAULT_VC, 86 }, 87 } 88 err = ValidateVideoOptionsConsistency(video) 89 require.NoError(t, err) 90 91 video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{ 92 &livekit.VideoLayer{}, 93 } 94 err = ValidateVideoOptionsConsistency(video) 95 require.Error(t, err) 96 97 video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{ 98 &livekit.VideoLayer{ 99 Width: 640, 100 Height: 480, 101 }, 102 } 103 err = ValidateVideoOptionsConsistency(video) 104 require.NoError(t, err) 105 106 video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{ 107 &livekit.VideoLayer{ 108 Width: 641, 109 Height: 480, 110 }, 111 } 112 err = ValidateVideoOptionsConsistency(video) 113 require.Error(t, err) 114 115 video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{ 116 &livekit.VideoLayer{ 117 Width: 640, 118 Height: 480, 119 Quality: livekit.VideoQuality_HIGH, 120 }, 121 &livekit.VideoLayer{ 122 Width: 640, 123 Height: 480, 124 Quality: livekit.VideoQuality_LOW, 125 }, 126 } 127 err = ValidateVideoOptionsConsistency(video) 128 require.Error(t, err) 129 130 video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{ 131 &livekit.VideoLayer{ 132 Width: 640, 133 Height: 480, 134 Quality: livekit.VideoQuality_HIGH, 135 }, 136 &livekit.VideoLayer{ 137 Width: 1280, 138 Height: 720, 139 Quality: livekit.VideoQuality_HIGH, 140 }, 141 } 142 err = ValidateVideoOptionsConsistency(video) 143 require.Error(t, err) 144 145 video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{ 146 &livekit.VideoLayer{ 147 Width: 640, 148 Height: 480, 149 Quality: livekit.VideoQuality_HIGH, 150 }, 151 &livekit.VideoLayer{ 152 Width: 1280, 153 Height: 720, 154 Quality: livekit.VideoQuality_LOW, 155 }, 156 } 157 err = ValidateVideoOptionsConsistency(video) 158 require.Error(t, err) 159 160 video.EncodingOptions.(*livekit.IngressVideoOptions_Options).Options.Layers = []*livekit.VideoLayer{ 161 &livekit.VideoLayer{ 162 Width: 640, 163 Height: 480, 164 Quality: livekit.VideoQuality_LOW, 165 }, 166 &livekit.VideoLayer{ 167 Width: 1280, 168 Height: 720, 169 Quality: livekit.VideoQuality_HIGH, 170 }, 171 } 172 err = ValidateVideoOptionsConsistency(video) 173 require.NoError(t, err) 174 } 175 176 func TestValidateAudioOptionsConsistency(t *testing.T) { 177 audio := &livekit.IngressAudioOptions{} 178 err := ValidateAudioOptionsConsistency(audio) 179 require.NoError(t, err) 180 181 audio.Source = livekit.TrackSource_CAMERA 182 err = ValidateAudioOptionsConsistency(audio) 183 require.Error(t, err) 184 185 audio.Source = livekit.TrackSource_SCREEN_SHARE_AUDIO 186 audio.EncodingOptions = &livekit.IngressAudioOptions_Preset{ 187 Preset: livekit.IngressAudioEncodingPreset(42), 188 } 189 err = ValidateAudioOptionsConsistency(audio) 190 require.Error(t, err) 191 192 audio.EncodingOptions = &livekit.IngressAudioOptions_Preset{ 193 Preset: livekit.IngressAudioEncodingPreset_OPUS_MONO_64KBS, 194 } 195 err = ValidateAudioOptionsConsistency(audio) 196 require.NoError(t, err) 197 198 audio.EncodingOptions = &livekit.IngressAudioOptions_Options{ 199 Options: &livekit.IngressAudioEncodingOptions{ 200 AudioCodec: livekit.AudioCodec_AAC, 201 }, 202 } 203 err = ValidateAudioOptionsConsistency(audio) 204 require.Error(t, err) 205 206 audio.EncodingOptions = &livekit.IngressAudioOptions_Options{ 207 Options: &livekit.IngressAudioEncodingOptions{ 208 AudioCodec: livekit.AudioCodec_OPUS, 209 Channels: 3, 210 }, 211 } 212 err = ValidateAudioOptionsConsistency(audio) 213 require.Error(t, err) 214 215 audio.EncodingOptions.(*livekit.IngressAudioOptions_Options).Options.Channels = 2 216 err = ValidateAudioOptionsConsistency(audio) 217 require.NoError(t, err) 218 }