@@ -12,7 +12,7 @@ import ReplayKit
12
12
13
13
class RTCCustomFrameCapturer : RTCVideoCapturer {
14
14
15
- let kNanosecondsPerSecond : Float64 = 1000000000
15
+ let kNanosecondsPerSecond : Float64 = 1_000_000_000
16
16
var nanoseconds : Float64 = 0
17
17
var lastSentFrameTimeStampNanoSeconds : Int64 = 0
18
18
private var targetHeight : Int
@@ -34,10 +34,10 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
34
34
self . targetHeight = height
35
35
self . externalCapture = externalCapture
36
36
37
- //if external capture is enabled videoEnabled and audioEnabled are ignored
37
+ // if external capture is enabled videoEnabled and audioEnabled are ignored
38
38
self . videoEnabled = videoEnabled
39
39
self . audioEnabled = audioEnabled
40
- self . frameRateIntervalNanoSeconds = kNanosecondsPerSecond/ Double( fps)
40
+ self . frameRateIntervalNanoSeconds = kNanosecondsPerSecond / Double( fps)
41
41
self . fps = fps
42
42
43
43
super. init ( delegate: delegate)
@@ -48,8 +48,8 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
48
48
}
49
49
50
50
public func capture( _ pixelBuffer: CVPixelBuffer , rotation: RTCVideoRotation , timeStampNs: Int64 ) {
51
- if ( ( Double ( timeStampNs) - Double( lastSentFrameTimeStampNanoSeconds) ) < frameRateIntervalNanoSeconds ) {
52
- AntMediaClient . verbose ( " Dropping frame because high fps than the configured fps: \( fps) . Incoming timestampNs: \( timeStampNs) last sent timestampNs: \( lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs: \( frameRateIntervalNanoSeconds) " ) ;
51
+ if ( Double ( timeStampNs) - Double( lastSentFrameTimeStampNanoSeconds) ) < frameRateIntervalNanoSeconds {
52
+ AntMediaClient . verbose ( " Dropping frame because high fps than the configured fps: \( fps) . Incoming timestampNs: \( timeStampNs) last sent timestampNs: \( lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs: \( frameRateIntervalNanoSeconds) " )
53
53
return
54
54
}
55
55
@@ -64,7 +64,7 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
64
64
65
65
let rtcPixelBuffer = RTCCVPixelBuffer (
66
66
pixelBuffer: pixelBuffer,
67
- adaptedWidth: scaledWidth,
67
+ adaptedWidth: scaledWidth,
68
68
adaptedHeight: Int32 ( self . targetHeight) ,
69
69
cropWidth: width,
70
70
cropHeight: height,
@@ -84,90 +84,74 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
84
84
85
85
public func capture( _ sampleBuffer: CMSampleBuffer , externalRotation: Int = - 1 ) {
86
86
87
- if ( CMSampleBufferGetNumSamples ( sampleBuffer) != 1 || !CMSampleBufferIsValid( sampleBuffer) ||
88
- !CMSampleBufferDataIsReady( sampleBuffer) )
89
- {
90
- NSLog ( " Buffer is not ready and dropping " ) ;
91
- return ;
87
+ if CMSampleBufferGetNumSamples ( sampleBuffer) != 1 || !CMSampleBufferIsValid( sampleBuffer) || !CMSampleBufferDataIsReady( sampleBuffer) {
88
+ NSLog ( " Buffer is not ready and dropping " )
89
+ return
92
90
}
93
91
94
92
let timeStampNs = CMTimeGetSeconds ( CMSampleBufferGetPresentationTimeStamp ( sampleBuffer) ) * kNanosecondsPerSecond
95
93
96
- if ( ( Double ( timeStampNs) - Double( lastSentFrameTimeStampNanoSeconds) ) < frameRateIntervalNanoSeconds ) {
97
- AntMediaClient . verbose ( " Dropping frame because high fps than the configured fps: \( fps) . Incoming timestampNs: \( timeStampNs) last sent timestampNs: \( lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs: \( frameRateIntervalNanoSeconds) " ) ;
94
+ if ( Double ( timeStampNs) - Double( lastSentFrameTimeStampNanoSeconds) ) < frameRateIntervalNanoSeconds {
95
+ AntMediaClient . verbose ( " Dropping frame because high fps than the configured fps: \( fps) . Incoming timestampNs: \( timeStampNs) last sent timestampNs: \( lastSentFrameTimeStampNanoSeconds) frameRateIntervalNs: \( frameRateIntervalNanoSeconds) " )
98
96
return
99
97
}
100
98
101
99
let _pixelBuffer : CVPixelBuffer ? = CMSampleBufferGetImageBuffer ( sampleBuffer)
102
100
103
101
if let pixelBuffer = _pixelBuffer {
104
- //NSLog("Incoming frame width:\(width) height:\(height) adapted width:\(scaledWidth) height:\(self.targetHeight)")
102
+ // NSLog("Incoming frame width:\(width) height:\(height) adapted width:\(scaledWidth) height:\(self.targetHeight)")
103
+
104
+ var rotation = RTCVideoRotation . _0
105
105
106
- var rotation = RTCVideoRotation . _0;
107
- if ( externalRotation == - 1 ) {
106
+ if externalRotation == - 1 {
108
107
if #available( iOS 11 . 0 , * ) {
109
- if let orientationAttachment = CMGetAttachment ( sampleBuffer, key: RPVideoSampleOrientationKey as CFString , attachmentModeOut: nil ) as? NSNumber
110
- {
108
+ if let orientationAttachment = CMGetAttachment ( sampleBuffer, key: RPVideoSampleOrientationKey as CFString , attachmentModeOut: nil ) as? NSNumber {
111
109
let orientation = CGImagePropertyOrientation ( rawValue: orientationAttachment. uint32Value)
112
110
switch orientation {
113
111
case . up:
114
- rotation = RTCVideoRotation . _0;
115
- break ;
112
+ rotation = RTCVideoRotation . _0
116
113
case . down:
117
- rotation = RTCVideoRotation . _180;
118
- break ;
114
+ rotation = RTCVideoRotation . _180
119
115
case . left:
120
- rotation = RTCVideoRotation . _90;
121
- break ;
116
+ rotation = RTCVideoRotation . _90
122
117
case . right:
123
- rotation = RTCVideoRotation . _270;
124
- break ;
125
-
118
+ rotation = RTCVideoRotation . _270
126
119
default :
127
- NSLog ( " orientation NOT FOUND " ) ;
120
+ NSLog ( " orientation NOT FOUND " )
128
121
}
129
- }
130
- else {
122
+ } else {
131
123
NSLog ( " CANNOT get image rotation " )
132
-
133
124
}
134
125
} else {
135
126
NSLog ( " CANNOT get image rotation becaue iOS version is older than 11 " )
136
127
}
137
- }
138
- else {
139
- rotation = RTCVideoRotation ( rawValue: externalRotation) ?? RTCVideoRotation . _0;
128
+ } else {
129
+ rotation = RTCVideoRotation ( rawValue: externalRotation) ?? RTCVideoRotation . _0
140
130
}
141
131
142
-
143
132
capture ( pixelBuffer, rotation: rotation, timeStampNs: Int64 ( timeStampNs) )
144
- //NSLog("Device orientation width: %d, height:%d ", width, height);
145
- }
146
- else {
147
- NSLog ( " Cannot get image buffer " ) ;
133
+ // NSLog("Device orientation width: %d, height:%d ", width, height);
134
+ } else {
135
+ NSLog ( " Cannot get image buffer " )
148
136
}
149
137
}
150
138
151
- public func startCapture( )
152
- {
153
- if !externalCapture
154
- {
155
- let recorder = RPScreenRecorder . shared ( ) ;
139
+ public func startCapture( ) {
140
+ if !externalCapture {
141
+ let recorder = RPScreenRecorder . shared ( )
156
142
157
143
if #available( iOS 11 . 0 , * ) {
158
- recorder. startCapture { ( buffer, bufferType, error) in
159
- if bufferType == RPSampleBufferType . video && self . videoEnabled
160
- {
144
+ recorder. startCapture { buffer, bufferType, _ in
145
+ if bufferType == RPSampleBufferType . video && self . videoEnabled {
161
146
self . capture ( buffer)
162
- }
163
- else if bufferType == RPSampleBufferType . audioApp && self . audioEnabled {
164
- self . webRTCClient? . deliverExternalAudio ( sampleBuffer: buffer) ;
147
+ } else if bufferType == RPSampleBufferType . audioApp && self . audioEnabled {
148
+ self . webRTCClient? . deliverExternalAudio ( sampleBuffer: buffer)
165
149
166
150
}
167
- } completionHandler: { ( error) in
151
+ } completionHandler: { error in
168
152
guard error == nil else {
169
153
AntMediaClient . printf ( " Screen capturer is not started " )
170
- return ;
154
+ return
171
155
}
172
156
}
173
157
} else {
@@ -178,13 +162,13 @@ class RTCCustomFrameCapturer: RTCVideoCapturer {
178
162
179
163
public func stopCapture( ) {
180
164
if !externalCapture {
181
- let recorder = RPScreenRecorder . shared ( ) ;
182
- if ( recorder. isRecording) {
165
+ let recorder = RPScreenRecorder . shared ( )
166
+ if recorder. isRecording {
183
167
if #available( iOS 11 . 0 , * ) {
184
- recorder. stopCapture { ( error) in
168
+ recorder. stopCapture { error in
185
169
guard error == nil else {
186
- AntMediaClient . printf ( " Cannot stop capture \( String ( describing: error) ) " ) ;
187
- return ;
170
+ AntMediaClient . printf ( " Cannot stop capture \( String ( describing: error) ) " )
171
+ return
188
172
}
189
173
}
190
174
} else {
0 commit comments