Skip to content

Commit 084e3ce

Browse files
committed
Adapt pixelbuffer to width/height in RTCCustomFrameCapturer:capture(pixelBuffer...)
1 parent c3198b1 commit 084e3ce

File tree

5 files changed

+45
-39
lines changed

5 files changed

+45
-39
lines changed

ScreenShare/ScreenShare.entitlements

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<plist version="1.0">
44
<dict>
55
<key>com.apple.security.application-groups</key>
6-
<array/>
6+
<array>
7+
<string>group.io.antmedia.ios.webrtc.sample</string>
8+
</array>
79
</dict>
810
</plist>

WebRTC-Sample-App/WebRTC-Sample-App.entitlements

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<plist version="1.0">
44
<dict>
55
<key>com.apple.security.application-groups</key>
6-
<array/>
6+
<array>
7+
<string>group.io.antmedia.ios.webrtc.sample</string>
8+
</array>
79
</dict>
810
</plist>

WebRTC-Sample-App/WebRTC-Sample-AppDebug.entitlements

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<plist version="1.0">
44
<dict>
55
<key>com.apple.security.application-groups</key>
6-
<array/>
6+
<array>
7+
<string>group.io.antmedia.ios.webrtc.sample</string>
8+
</array>
79
</dict>
810
</plist>

WebRTCiOSSDK/api/AntMediaClient.swift

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ open class AntMediaClient: NSObject, AntMediaClientProtocol {
5454

5555

5656
internal static var isDebug: Bool = false
57+
internal static var isVerbose: Bool = false
5758
public weak var delegate: AntMediaClientDelegate?
5859

5960
private var wsUrl: String!
@@ -1073,6 +1074,13 @@ open class AntMediaClient: NSObject, AntMediaClientProtocol {
10731074
}
10741075
}
10751076

1077+
public static func verbose(_ msg:String) {
1078+
if (AntMediaClient.isVerbose) {
1079+
debugPrint("--> AntMediaSDK[verbose]: " + msg);
1080+
}
1081+
1082+
}
1083+
10761084
public func getStreamInfo()
10771085
{
10781086
if (self.isWebSocketConnected)

WebRTCiOSSDK/api/webrtc/RTCCustomFrameCapturer.swift

+28-36
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,36 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
5454
public func capture(_ pixelBuffer: CVPixelBuffer, rotation:RTCVideoRotation, timeStampNs: Int64 )
5555
{
5656
if ((Double(timeStampNs) - Double(lastSentFrameTimeStampNanoSeconds)) < frameRateIntervalNanoSeconds ) {
57-
AntMediaClient.printf("Dropping frame because high fps than the configured fps: \(fps). Incoming timestampNs:\(timeStampNs) last sent timestampNs:\(lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs:\(frameRateIntervalNanoSeconds)");
57+
AntMediaClient.verbose("Dropping frame because high fps than the configured fps: \(fps). Incoming timestampNs:\(timeStampNs) last sent timestampNs:\(lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs:\(frameRateIntervalNanoSeconds)");
5858
return;
5959

6060
}
61+
62+
let width = Int32(CVPixelBufferGetWidth(pixelBuffer))
63+
let height = Int32(CVPixelBufferGetHeight(pixelBuffer))
64+
65+
var scaledWidth = (width * Int32(self.targetHeight)) / height;
66+
if (scaledWidth % 2 == 1) {
67+
scaledWidth+=1;
68+
}
69+
6170
let rtcPixelBuffer = RTCCVPixelBuffer(
62-
pixelBuffer: pixelBuffer)
63-
64-
let rtcVideoFrame = RTCVideoFrame(buffer: rtcPixelBuffer,
65-
66-
rotation: rotation, timeStampNs: Int64(timeStampNs))
71+
pixelBuffer: pixelBuffer,
72+
adaptedWidth:scaledWidth,
73+
adaptedHeight: Int32(self.targetHeight),
74+
cropWidth: width,
75+
cropHeight: height,
76+
cropX: 0,
77+
cropY: 0)
6778

68-
self.delegate?.capturer(self, didCapture: rtcVideoFrame.newI420())
69-
lastSentFrameTimeStampNanoSeconds = timeStampNs;
79+
let rtcVideoFrame = RTCVideoFrame(
80+
buffer: rtcPixelBuffer,
81+
rotation: rotation,
82+
timeStampNs: Int64(timeStampNs)
83+
)
7084

85+
self.delegate?.capturer(self, didCapture: rtcVideoFrame.newI420())
86+
lastSentFrameTimeStampNanoSeconds = Int64(timeStampNs);
7187
}
7288

7389
public func capture(_ sampleBuffer: CMSampleBuffer, externalRotation:Int = -1) {
@@ -83,25 +99,16 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
8399
kNanosecondsPerSecond;
84100

85101
if ((Double(timeStampNs) - Double(lastSentFrameTimeStampNanoSeconds)) < frameRateIntervalNanoSeconds ) {
86-
AntMediaClient.printf("Dropping frame because high fps than the configured fps: \(fps). Incoming timestampNs:\(timeStampNs) last sent timestampNs:\(lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs:\(frameRateIntervalNanoSeconds)");
102+
AntMediaClient.verbose("Dropping frame because high fps than the configured fps: \(fps). Incoming timestampNs:\(timeStampNs) last sent timestampNs:\(lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs:\(frameRateIntervalNanoSeconds)");
87103
return;
88104

89105
}
90106

91107
let _pixelBuffer:CVPixelBuffer? = CMSampleBufferGetImageBuffer(sampleBuffer);
92108

109+
93110
if let pixelBuffer = _pixelBuffer
94111
{
95-
96-
97-
let width = Int32(CVPixelBufferGetWidth(pixelBuffer))
98-
let height = Int32(CVPixelBufferGetHeight(pixelBuffer))
99-
100-
var scaledWidth = (width * Int32(self.targetHeight)) / height;
101-
if (scaledWidth % 2 == 1) {
102-
scaledWidth+=1;
103-
}
104-
105112
//NSLog("Incoming frame width:\(width) height:\(height) adapted width:\(scaledWidth) height:\(self.targetHeight)")
106113

107114
var rotation = RTCVideoRotation._0;
@@ -140,24 +147,9 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
140147
rotation = RTCVideoRotation(rawValue:externalRotation) ?? RTCVideoRotation._0;
141148
}
142149

143-
//NSLog("Device orientation width: %d, height:%d ", width, height);
144150

145-
let rtcPixelBuffer = RTCCVPixelBuffer(
146-
pixelBuffer: pixelBuffer,
147-
adaptedWidth:scaledWidth,
148-
adaptedHeight: Int32(self.targetHeight),
149-
cropWidth: width,
150-
cropHeight: height,
151-
cropX: 0,
152-
cropY: 0)
153-
154-
let rtcVideoFrame = RTCVideoFrame(buffer: rtcPixelBuffer,
155-
156-
rotation: rotation, timeStampNs: Int64(timeStampNs))
157-
158-
self.delegate?.capturer(self, didCapture: rtcVideoFrame.newI420())
159-
lastSentFrameTimeStampNanoSeconds = Int64(timeStampNs);
160-
151+
capture(pixelBuffer, rotation: rotation, timeStampNs: Int64(timeStampNs))
152+
//NSLog("Device orientation width: %d, height:%d ", width, height);
161153
}
162154
else {
163155
NSLog("Cannot get image buffer");

0 commit comments

Comments
 (0)