Skip to content

Commit

Permalink
rotation support for ipc track
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Nov 23, 2021
1 parent 6ac4a7a commit d7e27aa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReplayKit

extension CGImagePropertyOrientation {

func toRTCRotation() -> RTCVideoRotation {
public func toRTCRotation() -> RTCVideoRotation {
switch self {
case .up, .upMirrored, .down, .downMirrored: return ._0
case .left, .leftMirrored: return ._90
Expand Down
23 changes: 16 additions & 7 deletions Sources/LiveKit/protos/livekit_ipc.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ public struct IPCMessage {
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
// methods supported on all messages.

/// pixel format
/// CVPixelBuffer's FormatType
public var format: UInt32 = 0

/// RTCVideoRotation
public var rotation: UInt32 = 0

public var width: UInt32 = 0

public var height: UInt32 = 0
Expand Down Expand Up @@ -318,8 +321,9 @@ extension IPCMessage.Buffer.Video: SwiftProtobuf.Message, SwiftProtobuf._Message
public static let protoMessageName: String = IPCMessage.Buffer.protoMessageName + ".Video"
public static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1: .same(proto: "format"),
2: .same(proto: "width"),
3: .same(proto: "height")
2: .same(proto: "rotation"),
3: .same(proto: "width"),
4: .same(proto: "height")
]

public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
Expand All @@ -329,8 +333,9 @@ extension IPCMessage.Buffer.Video: SwiftProtobuf.Message, SwiftProtobuf._Message
// enabled. https://github.com/apple/swift-protobuf/issues/1034
switch fieldNumber {
case 1: try { try decoder.decodeSingularUInt32Field(value: &self.format) }()
case 2: try { try decoder.decodeSingularUInt32Field(value: &self.width) }()
case 3: try { try decoder.decodeSingularUInt32Field(value: &self.height) }()
case 2: try { try decoder.decodeSingularUInt32Field(value: &self.rotation) }()
case 3: try { try decoder.decodeSingularUInt32Field(value: &self.width) }()
case 4: try { try decoder.decodeSingularUInt32Field(value: &self.height) }()
default: break
}
}
Expand All @@ -340,17 +345,21 @@ extension IPCMessage.Buffer.Video: SwiftProtobuf.Message, SwiftProtobuf._Message
if self.format != 0 {
try visitor.visitSingularUInt32Field(value: self.format, fieldNumber: 1)
}
if self.rotation != 0 {
try visitor.visitSingularUInt32Field(value: self.rotation, fieldNumber: 2)
}
if self.width != 0 {
try visitor.visitSingularUInt32Field(value: self.width, fieldNumber: 2)
try visitor.visitSingularUInt32Field(value: self.width, fieldNumber: 3)
}
if self.height != 0 {
try visitor.visitSingularUInt32Field(value: self.height, fieldNumber: 3)
try visitor.visitSingularUInt32Field(value: self.height, fieldNumber: 4)
}
try unknownFields.traverse(visitor: &visitor)
}

public static func ==(lhs: IPCMessage.Buffer.Video, rhs: IPCMessage.Buffer.Video) -> Bool {
if lhs.format != rhs.format {return false}
if lhs.rotation != rhs.rotation {return false}
if lhs.width != rhs.width {return false}
if lhs.height != rhs.height {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
Expand Down
18 changes: 8 additions & 10 deletions Sources/LiveKit/track/capturers/IPCCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class IPCCapturer: VideoCapturer {
init(delegate: RTCVideoCapturerDelegate, ipcName: String) {
self.ipcName = ipcName
super.init(delegate: delegate)
ipcServer.onReceivedData = { _, messageId, data in

ipcServer.onReceivedData = { _, _, data in

guard let message = try? IPCMessage(serializedData: data) else {
logger.warning("Failed to decode ipc message")
return
Expand All @@ -30,23 +30,21 @@ class IPCCapturer: VideoCapturer {
height: Int(videoMessage.height),
pixelFormat: videoMessage.format)

// TODO: handle rotation

delegate.capturer(self.capturer,
didCapture: pixelBuffer,
timeStampNs: bufferMessage.timestampNs,
rotation: ._0)
didCapture: pixelBuffer,
timeStampNs: bufferMessage.timestampNs,
rotation: RTCVideoRotation(rawValue: Int(videoMessage.rotation)) ?? ._0)
}
}
}

override func startCapture() -> Promise<Void> {
super.startCapture().then {
// start listening for ipc messages
self.ipcServer.listen(self.ipcName)
}
}

override func stopCapture() -> Promise<Void> {
super.stopCapture().then {
// stop listening for ipc messages
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/track/capturers/VideoCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class VideoCapturer: MulticastDelegate<VideoCapturerDelegate>, VideoCaptu
}

public func restartCapture() -> Promise<Void> {
stopCapture().recover { error in
stopCapture().recover { _ in
logger.warning("Capturer was already stopped")
}.then {
self.startCapture()
Expand Down
7 changes: 4 additions & 3 deletions livekit_ipc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ message IPCMessage {
}

message Video {
uint32 format = 1; // pixel format
uint32 width = 2;
uint32 height = 3;
uint32 format = 1; // CVPixelBuffer's FormatType
uint32 rotation = 2; // RTCVideoRotation
uint32 width = 3;
uint32 height = 4;
}

message AudioApp {}
Expand Down

0 comments on commit d7e27aa

Please sign in to comment.