Try 2 to consolidate mime type (#3407)
* Normalize mime type and add utilities. An attempt to normalize mime type and avoid string compares remembering to do case insensitive search. Not the best solution. Open to ideas. But, define our own mime types (just in case Pion changes things and Pion also does not have red mime type defined which should be easy to add though) and tried to use it everywhere. But, as we get a bunch of callbacks and info from Pion, needed conversion in more places than I anticipated. And also makes it necessary to carry that cognitive load of what comes from Pion and needing to process it properly. * more locations * test * Paul feedback * MimeType type * more consolidation * Remove unused * test * test * mime type as int * use string method * Pass error details and timeouts. (#3402) * go mod tidy (#3408) * Rename CHANGELOG to CHANGELOG.md (#3391) Enables markdown features in this otherwise already markdown'ish formatted document * Update config.go to properly process bool env vars (#3382) Fixes issue https://github.com/livekit/livekit/issues/3381 * fix(deps): update go deps (#3341) Generated by renovateBot Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * Use a Twirp server hook to send API call details to telemetry. (#3401) * Use a Twirp server hook to send API call details to telemetry. * mage generate and clean up * Add project_id * deps * - Redact requests - Do not store responses - Extract top level fields room_name, room_id, participant_identity, participant_id, track_id as appropriate - Store status as int * deps * Update pkg/sfu/mime/mimetype.go * Fix prefer codec test * handle down track mime changes --------- Co-authored-by: Denys Smirnov <dennwc@pm.me> Co-authored-by: Philzen <Philzen@users.noreply.github.com> Co-authored-by: Pablo Fuente Pérez <pablofuenteperez@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Paul Wells <paulwe@gmail.com> Co-authored-by: cnderrauber <zengjie9004@gmail.com>
This commit is contained in:
co-authored by
Denys Smirnov
Philzen
Pablo Fuente Pérez
renovate[bot]
Paul Wells
cnderrauber
parent
045bd785a9
commit
9551c52c85
@@ -17,8 +17,8 @@ package utils
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
"github.com/pion/interceptor"
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/webrtc/v4"
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
func CodecParametersFuzzySearch(needle webrtc.RTPCodecParameters, haystack []webrtc.RTPCodecParameters) (webrtc.RTPCodecParameters, error) {
|
||||
// First attempt to match on MimeType + SDPFmtpLine
|
||||
for _, c := range haystack {
|
||||
if strings.EqualFold(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) &&
|
||||
if mime.IsMimeTypeStringEqual(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) &&
|
||||
c.RTPCodecCapability.SDPFmtpLine == needle.RTPCodecCapability.SDPFmtpLine {
|
||||
return c, nil
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func CodecParametersFuzzySearch(needle webrtc.RTPCodecParameters, haystack []web
|
||||
|
||||
// Fallback to just MimeType
|
||||
for _, c := range haystack {
|
||||
if strings.EqualFold(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) {
|
||||
if mime.IsMimeTypeStringEqual(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
// Copyright 2023 LiveKit, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package utils
|
||||
|
||||
type MimeType int
|
||||
|
||||
const (
|
||||
MimeTypeUnknown MimeType = iota
|
||||
MimeTypeVP8
|
||||
MimeTypeVP9
|
||||
MimeTypeH264
|
||||
MimeTypeAV1
|
||||
MimeTypeH265
|
||||
)
|
||||
|
||||
func MatchMimeType(mimeType string) MimeType {
|
||||
switch len(mimeType) {
|
||||
case 9:
|
||||
switch mimeType[0] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[1] {
|
||||
case 'i', 'I':
|
||||
switch mimeType[2] {
|
||||
case 'd', 'D':
|
||||
switch mimeType[3] {
|
||||
case 'e', 'E':
|
||||
switch mimeType[4] {
|
||||
case 'o', 'O':
|
||||
switch mimeType[5] {
|
||||
case '/':
|
||||
switch mimeType[6] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[7] {
|
||||
case 'p', 'P':
|
||||
switch mimeType[8] {
|
||||
case '8':
|
||||
return MimeTypeVP8
|
||||
case '9':
|
||||
return MimeTypeVP9
|
||||
}
|
||||
}
|
||||
case 'a', 'A':
|
||||
switch mimeType[7] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[8] {
|
||||
case '1':
|
||||
return MimeTypeAV1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case 10:
|
||||
switch mimeType[0] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[1] {
|
||||
case 'i', 'I':
|
||||
switch mimeType[2] {
|
||||
case 'd', 'D':
|
||||
switch mimeType[3] {
|
||||
case 'e', 'E':
|
||||
switch mimeType[4] {
|
||||
case 'o', 'O':
|
||||
switch mimeType[5] {
|
||||
case '/':
|
||||
switch mimeType[6] {
|
||||
case 'h', 'H':
|
||||
switch mimeType[7] {
|
||||
case '2':
|
||||
switch mimeType[8] {
|
||||
case '6':
|
||||
switch mimeType[9] {
|
||||
case '4':
|
||||
return MimeTypeH264
|
||||
case '5':
|
||||
return MimeTypeH265
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return MimeTypeUnknown
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
// Copyright 2023 LiveKit, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func toLowerSwitch(mimeType string) MimeType {
|
||||
switch strings.ToLower(mimeType) {
|
||||
case "video/vp8":
|
||||
return MimeTypeVP8
|
||||
case "video/vp9":
|
||||
return MimeTypeVP9
|
||||
case "video/h264":
|
||||
return MimeTypeH264
|
||||
case "video/av1":
|
||||
return MimeTypeAV1
|
||||
default:
|
||||
return MimeTypeUnknown
|
||||
}
|
||||
}
|
||||
|
||||
func TestMimeTypeMatch(t *testing.T) {
|
||||
require.Equal(t, MimeTypeVP8, MatchMimeType("VIDEO/VP8"), "VIDEO/VP8")
|
||||
require.Equal(t, MimeTypeVP9, MatchMimeType("VIDEO/VP9"), "VIDEO/VP9")
|
||||
require.Equal(t, MimeTypeH264, MatchMimeType("VIDEO/H264"), "VIDEO/H264")
|
||||
require.Equal(t, MimeTypeAV1, MatchMimeType("VIDEO/AV1"), "VIDEO/AV1")
|
||||
}
|
||||
|
||||
func BenchmarkMimeTypeMatch(b *testing.B) {
|
||||
mimeTypes := []string{
|
||||
"video/VP8",
|
||||
"video/VP9",
|
||||
"video/H264",
|
||||
"video/AV1",
|
||||
}
|
||||
|
||||
b.Run("ToLower/switch", func(b *testing.B) {
|
||||
for i := range b.N {
|
||||
_ = toLowerSwitch(mimeTypes[i%len(mimeTypes)])
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("MatchMimeType", func(b *testing.B) {
|
||||
for i := range b.N {
|
||||
_ = MatchMimeType(mimeTypes[i%len(mimeTypes)])
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user