Skip to content

Commit 07436e4

Browse files
committed
Various fixes
1 parent de6f737 commit 07436e4

File tree

8 files changed

+113
-109
lines changed

8 files changed

+113
-109
lines changed

submodules/Camera/Sources/Camera.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,19 @@ private final class CameraContext {
379379
return
380380
}
381381

382+
var front = false
383+
if #available(iOS 13.0, *) {
384+
front = connection.inputPorts.first?.sourceDevicePosition == .front
385+
}
386+
382387
if sampleBuffer.type == kCMMediaType_Video {
383388
Queue.mainQueue().async {
384-
self.videoOutput?.push(sampleBuffer)
389+
self.videoOutput?.push(sampleBuffer, mirror: front)
385390
}
386391
}
387392

388393
let timestamp = CACurrentMediaTime()
389394
if timestamp > self.lastSnapshotTimestamp + 2.5, !mainDeviceContext.output.isRecording || !self.savedSnapshot {
390-
var front = false
391-
if #available(iOS 13.0, *) {
392-
front = connection.inputPorts.first?.sourceDevicePosition == .front
393-
}
394395
self.savePreviewSnapshot(pixelBuffer: pixelBuffer, front: front)
395396
self.lastSnapshotTimestamp = timestamp
396397
self.savedSnapshot = true
@@ -1140,13 +1141,13 @@ public enum CameraRecordingError {
11401141
}
11411142

11421143
public class CameraVideoOutput {
1143-
private let sink: (CMSampleBuffer) -> Void
1144+
private let sink: (CMSampleBuffer, Bool) -> Void
11441145

1145-
public init(sink: @escaping (CMSampleBuffer) -> Void) {
1146+
public init(sink: @escaping (CMSampleBuffer, Bool) -> Void) {
11461147
self.sink = sink
11471148
}
11481149

1149-
func push(_ buffer: CMSampleBuffer) {
1150-
self.sink(buffer)
1150+
func push(_ buffer: CMSampleBuffer, mirror: Bool) {
1151+
self.sink(buffer, mirror)
11511152
}
11521153
}

submodules/TelegramUI/Components/CameraScreen/Sources/CameraCollage.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ final class CameraCollage {
234234
self.uniqueIds.append(Int64.random(in: .min ... .max))
235235
}
236236

237+
func getItem(id: Int64) -> CaptureResult? {
238+
guard let index = self.uniqueIds.firstIndex(where: { $0 == id }) else {
239+
return nil
240+
}
241+
return self.results[index]
242+
}
243+
237244
private func checkResults() {
238245
self.results = self.results.filter { $0.content != nil }
239246
}

submodules/TelegramUI/Components/CameraScreen/Sources/CameraScreen.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ enum CameraScreenTransition {
130130
case animateIn
131131
case animateOut
132132
case finishedAnimateIn
133+
case flashModeChanged
133134
}
134135

135136
private let cancelButtonTag = GenericComponentViewTag()
@@ -1134,6 +1135,9 @@ private final class CameraScreenComponent: CombinedComponent {
11341135

11351136
let flashContentComponent: AnyComponentWithIdentity<Empty>
11361137
if component.hasAppeared {
1138+
let animationHint = context.transition.userData(CameraScreenTransition.self)
1139+
let shouldAnimateIcon = component.cameraState.flashModeDidChange && animationHint == .flashModeChanged
1140+
11371141
let flashIconName: String
11381142
switch component.cameraState.flashMode {
11391143
case .off:
@@ -1157,7 +1161,7 @@ private final class CameraScreenComponent: CombinedComponent {
11571161
LottieAnimationComponent(
11581162
animation: LottieAnimationComponent.AnimationItem(
11591163
name: flashIconName,
1160-
mode: !component.cameraState.flashModeDidChange ? .still(position: .end) : .animating(loop: false),
1164+
mode: shouldAnimateIcon ? .animating(loop: false) : .still(position: .end),
11611165
range: nil,
11621166
waitForCompletion: false
11631167
),
@@ -1318,7 +1322,7 @@ private final class CameraScreenComponent: CombinedComponent {
13181322
state?.updateCollageGrid(grid)
13191323
}
13201324
),
1321-
availableSize: CGSize(width: nextButtonX, height: 40.0),
1325+
availableSize: CGSize(width: nextButtonX + 4.0, height: 40.0),
13221326
transition: .immediate
13231327
)
13241328
context.add(collageCarousel
@@ -2103,7 +2107,11 @@ public class CameraScreenImpl: ViewController, CameraScreen {
21032107
let previousState = self.cameraState
21042108
self.cameraState = self.cameraState.updatedPosition(position).updatedFlashMode(flashMode)
21052109
if !self.animatingDualCameraPositionSwitch {
2106-
self.requestUpdateLayout(transition: .easeInOut(duration: 0.2))
2110+
var transition: ComponentTransition = .easeInOut(duration: 0.2)
2111+
if previousState.flashMode != flashMode {
2112+
transition = transition.withUserData(CameraScreenTransition.flashModeChanged)
2113+
}
2114+
self.requestUpdateLayout(transition: transition)
21072115
}
21082116

21092117
if previousState.position != self.cameraState.position {
@@ -2255,15 +2263,15 @@ public class CameraScreenImpl: ViewController, CameraScreen {
22552263
case .began:
22562264
break
22572265
case .changed:
2258-
if case .none = self.cameraState.recording, self.cameraState.collageProgress.isZero {
2266+
if case .none = self.cameraState.recording {
22592267
if case .compact = layout.metrics.widthClass {
22602268
switch controller.mode {
22612269
case .story:
2262-
if (translation.x < -10.0 || self.isDismissing) && self.hasAppeared {
2270+
if (translation.x < -10.0 || self.isDismissing) && self.hasAppeared && self.cameraState.collageProgress.isZero {
22632271
self.isDismissing = true
22642272
let transitionFraction = 1.0 - max(0.0, translation.x * -1.0) / self.frame.width
22652273
controller.updateTransitionProgress(transitionFraction, transition: .immediate)
2266-
} else if translation.y < -10.0 && abs(translation.y) > abs(translation.x) {
2274+
} else if translation.y < -10.0 && abs(translation.y) > abs(translation.x) && self.cameraState.collageProgress < 1.0 {
22672275
controller.presentGallery(fromGesture: true)
22682276
gestureRecognizer.isEnabled = false
22692277
gestureRecognizer.isEnabled = true
@@ -2592,7 +2600,7 @@ public class CameraScreenImpl: ViewController, CameraScreen {
25922600
view.animateOutToEditor(transition: transition)
25932601
}
25942602

2595-
Queue.mainQueue().after(1.0, {
2603+
Queue.mainQueue().after(2.0, {
25962604
if self.cameraState.isCollageEnabled {
25972605
self.collage = nil
25982606
if let collageView = self.collageView {

submodules/TelegramUI/Components/CameraScreen/Sources/CameraVideoLayer.swift

Lines changed: 53 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -247,96 +247,66 @@ final class CameraVideoLayer: MetalEngineSubjectLayer, MetalEngineSubject {
247247
computeEncoder.endEncoding()
248248
})
249249

250-
if !self.blurredLayer.isHidden {
251-
guard let downscaledTexture = self.downscaledTexture?.get(context: context), let blurredHorizontalTexture = self.blurredHorizontalTexture?.get(context: context), let blurredVerticalTexture = self.blurredVerticalTexture?.get(context: context) else {
252-
return
250+
guard let downscaledTexture = self.downscaledTexture?.get(context: context), let blurredHorizontalTexture = self.blurredHorizontalTexture?.get(context: context), let blurredVerticalTexture = self.blurredVerticalTexture?.get(context: context) else {
251+
return
252+
}
253+
254+
let blurredTexture = context.compute(state: BlurState.self, inputs: rgbaTexture.placeholer, downscaledTexture.placeholer, blurredHorizontalTexture.placeholer, blurredVerticalTexture.placeholer, commands: { commandBuffer, blurState, rgbaTexture, downscaledTexture, blurredHorizontalTexture, blurredVerticalTexture -> MTLTexture? in
255+
guard let rgbaTexture, let downscaledTexture, let blurredHorizontalTexture, let blurredVerticalTexture else {
256+
return nil
253257
}
254258

255-
let blurredTexture = context.compute(state: BlurState.self, inputs: rgbaTexture.placeholer, downscaledTexture.placeholer, blurredHorizontalTexture.placeholer, blurredVerticalTexture.placeholer, commands: { commandBuffer, blurState, rgbaTexture, downscaledTexture, blurredHorizontalTexture, blurredVerticalTexture -> MTLTexture? in
256-
guard let rgbaTexture, let downscaledTexture, let blurredHorizontalTexture, let blurredVerticalTexture else {
257-
return nil
258-
}
259-
260-
blurState.downscaleKernel.encode(commandBuffer: commandBuffer, sourceTexture: rgbaTexture, destinationTexture: downscaledTexture)
261-
262-
do {
263-
guard let computeEncoder = commandBuffer.makeComputeCommandEncoder() else {
264-
return nil
265-
}
266-
267-
let threadgroupSize = MTLSize(width: 16, height: 16, depth: 1)
268-
let threadgroupCount = MTLSize(width: (downscaledTexture.width + threadgroupSize.width - 1) / threadgroupSize.width, height: (downscaledTexture.height + threadgroupSize.height - 1) / threadgroupSize.height, depth: 1)
269-
270-
computeEncoder.setComputePipelineState(blurState.computePipelineStateHorizontal)
271-
computeEncoder.setTexture(downscaledTexture, index: 0)
272-
computeEncoder.setTexture(blurredHorizontalTexture, index: 1)
273-
computeEncoder.dispatchThreadgroups(threadgroupCount, threadsPerThreadgroup: threadgroupSize)
274-
275-
computeEncoder.setComputePipelineState(blurState.computePipelineStateVertical)
276-
computeEncoder.setTexture(blurredHorizontalTexture, index: 0)
277-
computeEncoder.setTexture(blurredVerticalTexture, index: 1)
278-
computeEncoder.dispatchThreadgroups(threadgroupCount, threadsPerThreadgroup: threadgroupSize)
279-
280-
computeEncoder.endEncoding()
281-
}
282-
283-
return blurredVerticalTexture
284-
})
259+
blurState.downscaleKernel.encode(commandBuffer: commandBuffer, sourceTexture: rgbaTexture, destinationTexture: downscaledTexture)
285260

286-
context.renderToLayer(spec: renderSpec, state: RenderState.self, layer: self.blurredLayer, inputs: blurredTexture, commands: { encoder, placement, blurredTexture in
287-
guard let blurredTexture else {
288-
return
261+
do {
262+
guard let computeEncoder = commandBuffer.makeComputeCommandEncoder() else {
263+
return nil
289264
}
290-
let effectiveRect = placement.effectiveRect
291-
292-
var rect = SIMD4<Float>(Float(effectiveRect.minX), Float(effectiveRect.minY), Float(effectiveRect.width), Float(effectiveRect.height))
293-
encoder.setVertexBytes(&rect, length: 4 * 4, index: 0)
294265

295-
var mirror = SIMD2<UInt32>(
296-
videoTextures.mirrorDirection.contains(.horizontal) ? 1 : 0,
297-
videoTextures.mirrorDirection.contains(.vertical) ? 1 : 0
298-
)
299-
encoder.setVertexBytes(&mirror, length: 2 * 4, index: 1)
266+
let threadgroupSize = MTLSize(width: 16, height: 16, depth: 1)
267+
let threadgroupCount = MTLSize(width: (downscaledTexture.width + threadgroupSize.width - 1) / threadgroupSize.width, height: (downscaledTexture.height + threadgroupSize.height - 1) / threadgroupSize.height, depth: 1)
300268

301-
encoder.setFragmentTexture(blurredTexture, index: 0)
269+
computeEncoder.setComputePipelineState(blurState.computePipelineStateHorizontal)
270+
computeEncoder.setTexture(downscaledTexture, index: 0)
271+
computeEncoder.setTexture(blurredHorizontalTexture, index: 1)
272+
computeEncoder.dispatchThreadgroups(threadgroupCount, threadsPerThreadgroup: threadgroupSize)
302273

303-
var brightness: Float = 0.85
304-
var saturation: Float = 1.3
305-
var overlay: SIMD4<Float> = SIMD4<Float>()
306-
encoder.setFragmentBytes(&brightness, length: 4, index: 0)
307-
encoder.setFragmentBytes(&saturation, length: 4, index: 1)
308-
encoder.setFragmentBytes(&overlay, length: 4 * 4, index: 2)
274+
computeEncoder.setComputePipelineState(blurState.computePipelineStateVertical)
275+
computeEncoder.setTexture(blurredHorizontalTexture, index: 0)
276+
computeEncoder.setTexture(blurredVerticalTexture, index: 1)
277+
computeEncoder.dispatchThreadgroups(threadgroupCount, threadsPerThreadgroup: threadgroupSize)
309278

310-
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
311-
})
312-
}
313-
314-
// context.renderToLayer(spec: renderSpec, state: RenderState.self, layer: self, inputs: rgbaTexture.placeholer, commands: { encoder, placement, rgbaTexture in
315-
// guard let rgbaTexture else {
316-
// return
317-
// }
318-
//
319-
// let effectiveRect = placement.effectiveRect
320-
//
321-
// var rect = SIMD4<Float>(Float(effectiveRect.minX), Float(effectiveRect.minY), Float(effectiveRect.width), Float(effectiveRect.height))
322-
// encoder.setVertexBytes(&rect, length: 4 * 4, index: 0)
323-
//
324-
// var mirror = SIMD2<UInt32>(
325-
// videoTextures.mirrorDirection.contains(.horizontal) ? 1 : 0,
326-
// videoTextures.mirrorDirection.contains(.vertical) ? 1 : 0
327-
// )
328-
// encoder.setVertexBytes(&mirror, length: 2 * 4, index: 1)
329-
//
330-
// encoder.setFragmentTexture(rgbaTexture, index: 0)
331-
//
332-
// var brightness: Float = 1.0
333-
// var saturation: Float = 1.0
334-
// var overlay: SIMD4<Float> = SIMD4<Float>()
335-
// encoder.setFragmentBytes(&brightness, length: 4, index: 0)
336-
// encoder.setFragmentBytes(&saturation, length: 4, index: 1)
337-
// encoder.setFragmentBytes(&overlay, length: 4 * 4, index: 2)
338-
//
339-
// encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
340-
// })
279+
computeEncoder.endEncoding()
280+
}
281+
282+
return blurredVerticalTexture
283+
})
284+
285+
context.renderToLayer(spec: renderSpec, state: RenderState.self, layer: self.blurredLayer, inputs: blurredTexture, commands: { encoder, placement, blurredTexture in
286+
guard let blurredTexture else {
287+
return
288+
}
289+
let effectiveRect = placement.effectiveRect
290+
291+
var rect = SIMD4<Float>(Float(effectiveRect.minX), Float(effectiveRect.minY), Float(effectiveRect.width), Float(effectiveRect.height))
292+
encoder.setVertexBytes(&rect, length: 4 * 4, index: 0)
293+
294+
var mirror = SIMD2<UInt32>(
295+
videoTextures.mirrorDirection.contains(.horizontal) ? 1 : 0,
296+
videoTextures.mirrorDirection.contains(.vertical) ? 1 : 0
297+
)
298+
encoder.setVertexBytes(&mirror, length: 2 * 4, index: 1)
299+
300+
encoder.setFragmentTexture(blurredTexture, index: 0)
301+
302+
var brightness: Float = 0.85
303+
var saturation: Float = 1.3
304+
var overlay: SIMD4<Float> = SIMD4<Float>()
305+
encoder.setFragmentBytes(&brightness, length: 4, index: 0)
306+
encoder.setFragmentBytes(&saturation, length: 4, index: 1)
307+
encoder.setFragmentBytes(&overlay, length: 4 * 4, index: 2)
308+
309+
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
310+
})
341311
}
342312
}

submodules/TelegramUI/Components/CameraScreen/Sources/CameraVideoSource.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ final class CameraVideoSource: VideoSource {
2121
public init?() {
2222
self.device = MetalEngine.shared.device
2323

24-
self.cameraVideoOutput = CameraVideoOutput(sink: { [weak self] buffer in
25-
self?.push(buffer)
24+
self.cameraVideoOutput = CameraVideoOutput(sink: { [weak self] buffer, mirror in
25+
self?.push(buffer, mirror: mirror)
2626
})
2727

2828
CVMetalTextureCacheCreate(nil, nil, self.device, nil, &self.textureCache)
@@ -41,7 +41,7 @@ final class CameraVideoSource: VideoSource {
4141
}
4242
}
4343

44-
private func push(_ sampleBuffer: CMSampleBuffer) {
44+
private func push(_ sampleBuffer: CMSampleBuffer, mirror: Bool) {
4545
guard let buffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
4646
return
4747
}
@@ -71,7 +71,7 @@ final class CameraVideoSource: VideoSource {
7171
uv: uvTexture
7272
)),
7373
dataBuffer: Output.NativeDataBuffer(pixelBuffer: buffer),
74-
mirrorDirection: [],
74+
mirrorDirection: mirror ? [.vertical] : [],
7575
sourceId: self.sourceId
7676
)
7777

submodules/TelegramUI/Components/CameraScreen/Sources/CollageIconComponent.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ final class CollageIconCarouselComponent: Component {
213213
self.state = state
214214

215215
let inset: CGFloat = 27.0
216-
let spacing: CGFloat = 8.0
216+
let spacing: CGFloat = availableSize.width > 290.0 ? 7.0 : 8.0
217217
var contentWidth: CGFloat = inset
218218
let buttonSize = CGSize(width: 40.0, height: 40.0)
219219

@@ -275,7 +275,7 @@ final class CollageIconCarouselComponent: Component {
275275
self.clippingView.frame = CGRect(origin: .zero, size: availableSize)
276276

277277
if self.clippingView.mask == nil {
278-
if let maskImage = generateGradientImage(size: CGSize(width: 42.0, height: 10.0), colors: [UIColor.clear, UIColor.black, UIColor.black, UIColor.clear], locations: [0.0, 0.3, 0.7, 1.0], direction: .horizontal) {
278+
if let maskImage = generateGradientImage(size: CGSize(width: 42.0, height: 10.0), colors: [UIColor.clear, UIColor.black, UIColor.black, UIColor.clear], locations: [0.0, 0.2, 0.8, 1.0], direction: .horizontal) {
279279
let maskView = UIImageView(image: maskImage.stretchableImage(withLeftCapWidth: 13, topCapHeight: 0))
280280
self.clippingView.mask = maskView
281281
}

submodules/TelegramUI/Components/MediaEditor/Sources/MediaEditor.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,11 @@ public final class MediaEditor {
11491149

11501150
public func setVideoIsMuted(_ videoIsMuted: Bool) {
11511151
self.player?.isMuted = videoIsMuted
1152+
if !self.values.collage.isEmpty {
1153+
for player in self.additionalPlayers {
1154+
player.isMuted = videoIsMuted
1155+
}
1156+
}
11521157
self.updateValues(mode: .skipRendering) { values in
11531158
return values.withUpdatedVideoIsMuted(videoIsMuted)
11541159
}
@@ -1857,9 +1862,11 @@ public final class MediaEditor {
18571862

18581863
public func collageItemIndexForTrackId(_ trackId: Int32) -> Int? {
18591864
var collageIndex = -1
1860-
var trackIndex = 0
1865+
var trackIndex = -1
18611866
for item in self.values.collage {
1862-
if case .videoFile = item.content {
1867+
if case .main = item.content {
1868+
trackIndex += 1
1869+
} else if case .videoFile = item.content {
18631870
trackIndex += 1
18641871
} else if case .asset(_, true) = item.content {
18651872
trackIndex += 1

0 commit comments

Comments
 (0)