h264_test.go 3.9 KB

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