github.com/pion/webrtc/v3@v3.2.24/internal/fmtp/h264_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package fmtp
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestH264FMTPParse(t *testing.T) {
    12  	testCases := map[string]struct {
    13  		input    string
    14  		expected FMTP
    15  	}{
    16  		"OneParam": {
    17  			input: "key-name=value",
    18  			expected: &h264FMTP{
    19  				parameters: map[string]string{
    20  					"key-name": "value",
    21  				},
    22  			},
    23  		},
    24  		"OneParamWithWhiteSpeces": {
    25  			input: "\tkey-name=value ",
    26  			expected: &h264FMTP{
    27  				parameters: map[string]string{
    28  					"key-name": "value",
    29  				},
    30  			},
    31  		},
    32  		"TwoParams": {
    33  			input: "key-name=value;key2=value2",
    34  			expected: &h264FMTP{
    35  				parameters: map[string]string{
    36  					"key-name": "value",
    37  					"key2":     "value2",
    38  				},
    39  			},
    40  		},
    41  		"TwoParamsWithWhiteSpeces": {
    42  			input: "key-name=value;  \n\tkey2=value2 ",
    43  			expected: &h264FMTP{
    44  				parameters: map[string]string{
    45  					"key-name": "value",
    46  					"key2":     "value2",
    47  				},
    48  			},
    49  		},
    50  	}
    51  	for name, testCase := range testCases {
    52  		testCase := testCase
    53  		t.Run(name, func(t *testing.T) {
    54  			f := Parse("video/h264", testCase.input)
    55  			if !reflect.DeepEqual(testCase.expected, f) {
    56  				t.Errorf("Expected Fmtp params: %v, got: %v", testCase.expected, f)
    57  			}
    58  
    59  			if f.MimeType() != "video/h264" {
    60  				t.Errorf("Expected MimeType of video/h264, got: %s", f.MimeType())
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestH264FMTPCompare(t *testing.T) {
    67  	consistString := map[bool]string{true: "consist", false: "inconsist"}
    68  
    69  	testCases := map[string]struct {
    70  		a, b    string
    71  		consist bool
    72  	}{
    73  		"Equal": {
    74  			a:       "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
    75  			b:       "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
    76  			consist: true,
    77  		},
    78  		"EqualWithWhitespaceVariants": {
    79  			a:       "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
    80  			b:       "  level-asymmetry-allowed=1;  \npacketization-mode=1;\t\nprofile-level-id=42e01f",
    81  			consist: true,
    82  		},
    83  		"EqualWithCase": {
    84  			a:       "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
    85  			b:       "level-asymmetry-allowed=1;packetization-mode=1;PROFILE-LEVEL-ID=42e01f",
    86  			consist: true,
    87  		},
    88  		"OneHasExtraParam": {
    89  			a:       "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
    90  			b:       "packetization-mode=1;profile-level-id=42e01f",
    91  			consist: true,
    92  		},
    93  		"DifferentProfileLevelIDVersions": {
    94  			a:       "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
    95  			b:       "packetization-mode=1;profile-level-id=42e029",
    96  			consist: true,
    97  		},
    98  		"Inconsistent": {
    99  			a:       "packetization-mode=1;profile-level-id=42e029",
   100  			b:       "packetization-mode=0;profile-level-id=42e029",
   101  			consist: false,
   102  		},
   103  		"Inconsistent_MissingPacketizationMode": {
   104  			a:       "packetization-mode=1;profile-level-id=42e029",
   105  			b:       "profile-level-id=42e029",
   106  			consist: false,
   107  		},
   108  		"Inconsistent_MissingProfileLevelID": {
   109  			a:       "packetization-mode=1;profile-level-id=42e029",
   110  			b:       "packetization-mode=1",
   111  			consist: false,
   112  		},
   113  		"Inconsistent_InvalidProfileLevelID": {
   114  			a:       "packetization-mode=1;profile-level-id=42e029",
   115  			b:       "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=41e029",
   116  			consist: false,
   117  		},
   118  	}
   119  	for name, testCase := range testCases {
   120  		testCase := testCase
   121  		check := func(t *testing.T, a, b string) {
   122  			aa := Parse("video/h264", a)
   123  			bb := Parse("video/h264", b)
   124  			c := aa.Match(bb)
   125  			if c != testCase.consist {
   126  				t.Errorf(
   127  					"'%s' and '%s' are expected to be %s, but treated as %s",
   128  					a, b, consistString[testCase.consist], consistString[c],
   129  				)
   130  			}
   131  
   132  			// test reverse case here
   133  			c = bb.Match(aa)
   134  			if c != testCase.consist {
   135  				t.Errorf(
   136  					"'%s' and '%s' are expected to be %s, but treated as %s",
   137  					a, b, consistString[testCase.consist], consistString[c],
   138  				)
   139  			}
   140  		}
   141  		t.Run(name, func(t *testing.T) {
   142  			check(t, testCase.a, testCase.b)
   143  		})
   144  	}
   145  }