Skip to content

Commit

Permalink
Fix trackInfo layer
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlidakis committed Dec 18, 2024
1 parent 0c2498c commit 2217b21
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
33 changes: 33 additions & 0 deletions Sources/StreamVideo/Models/PublishOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,39 @@ struct PublishOptions: Sendable, Hashable {
) -> Bool {
lhs.id == rhs.id && lhs.codec == rhs.codec
}

func buildLayers(for trackType: TrackType) -> [Stream_Video_Sfu_Models_VideoLayer] {
let publishOption = Stream_Video_Sfu_Models_PublishOption(
self,
trackType: trackType == .video ? .video : .screenShare
)

let result = publishOption
.videoLayers(spatialLayersRequired: capturingLayers.spatialLayers)
.enumerated()
.map { (offset, element) in
let scaleDownFactor = max(1, offset * 2)
var result = Stream_Video_Sfu_Models_VideoLayer()
result.rid = element.quality.rawValue
result.bitrate = UInt32(bitrate / scaleDownFactor)
result.fps = UInt32(frameRate)
result.videoDimension = .init()
result.videoDimension.width = UInt32(dimensions.width / CGFloat(scaleDownFactor))
result.videoDimension.height = UInt32(dimensions.height / CGFloat(scaleDownFactor))
return result
}
.reversed()
.prepare()

if result.endIndex != capturingLayers.spatialLayers {
log.warning(
"Capturing layers should match the generated trackInfo layers.",
subsystems: .sfu
)
}

return result
}
}

/// Original publish option models received from the server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,34 @@ extension Array where Element: RTCRtpEncodingParameters {
}
}
}

extension Array where Element == Stream_Video_Sfu_Models_VideoLayer {

/// Prepares the array of `RTCRtpEncodingParameters` for use with SVC codecs.
///
/// This method adjusts the encoding parameters if a Scalable Video Codec (SVC)
/// is being used. It filters the encodings to retain only the highest-quality
/// layer (`.full`) and modifies its `rid` (Restriction Identifier) to use
/// the lower-quality layer (`.quarter`) as required by certain SVC scenarios.
///
/// - Parameter isSVC: A Boolean indicating whether an SVC codec is in use.
/// - Returns: The modified array of `RTCRtpEncodingParameters` if SVC is used,
/// or the original array if SVC is not used.
func prepare() -> Self {
enumerated()
.map { (offset, element) in
var element = element
switch offset {
case 0:
element.rid = VideoLayer.Quality.quarter.rawValue
case 1:
element.rid = VideoLayer.Quality.half.rawValue
case 2:
element.rid = VideoLayer.Quality.full.rawValue
default:
break
}
return element
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ final class LocalScreenShareMediaAdapter: LocalMediaAdapting, @unchecked Sendabl
var trackInfo = Stream_Video_Sfu_Models_TrackInfo()
trackInfo.trackType = .screenShare
trackInfo.trackID = transceiver.sender.track?.trackId ?? ""
trackInfo.layers = transceiver
.sender
.parameters
.encodings
.map { Stream_Video_Sfu_Models_VideoLayer($0, publishOptions: publishOptions) }
trackInfo.layers = publishOptions.buildLayers(for: .screenshare)
trackInfo.mid = transceiver.mid
trackInfo.muted = transceiver.sender.track?.isEnabled ?? true
return trackInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,7 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
var trackInfo = Stream_Video_Sfu_Models_TrackInfo()
trackInfo.trackType = .video
trackInfo.trackID = transceiver.sender.track?.trackId ?? ""
trackInfo.layers = transceiver
.sender
.parameters
.encodings
.map { Stream_Video_Sfu_Models_VideoLayer($0, publishOptions: publishOptions) }
trackInfo.layers = publishOptions.buildLayers(for: .video)
trackInfo.mid = transceiver.mid
trackInfo.muted = transceiver.sender.track?.isEnabled ?? true
return trackInfo
Expand Down Expand Up @@ -440,7 +436,7 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
return
}
log.info(
"Update publish quality, enabled rids: \(activeLayers.joined(separator: ","))",
"Update publish quality for publishOptionID:\(videoSender.publishOptionID) codec:\(VideoCodec(videoSender.codec)), enabled rids: \(activeLayers.joined(separator: ","))",
subsystems: .webRTC
)
params.encodings = updatedEncodings
Expand Down

0 comments on commit 2217b21

Please sign in to comment.