Skip to content

Commit 512a3a6

Browse files
feat/#40 :: 비디오 캡쳐 기능 추가
- 시즌 1235125호 비디오 캡쳐 기능 추가 - 이번엔 연결 안된다고 안터져요 Co-Authored-By: Youngkyu Song <[email protected]>
1 parent fc7e04b commit 512a3a6

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Foundation
2+
import WebRTC
3+
4+
public class CapturableVideoView: RTCMTLVideoView {
5+
public var capturedImage: UIImage?
6+
7+
public override func setSize(_ size: CGSize) {
8+
super.setSize(size)
9+
}
10+
11+
public override func renderFrame(_ frame: RTCVideoFrame?) {
12+
guard let frame = frame else { return }
13+
14+
super.renderFrame(frame)
15+
capturedImage = convertFrameToImage(frame)
16+
}
17+
18+
private func convertFrameToImage(_ frame: RTCVideoFrame) -> UIImage? {
19+
// frame의 버퍼를 CVPixelBuffer로 가져옴
20+
guard let pixelBuffer = (frame.buffer as? RTCCVPixelBuffer)?.pixelBuffer else { return nil }
21+
22+
// CVPixelBuffer를 CIImage로 변환
23+
let ciImage = CIImage(cvPixelBuffer: pixelBuffer).oriented(.right)
24+
25+
// CIImage를 UIImage로 변환
26+
let context = CIContext(options: nil)
27+
if let cgImage = context.createCGImage(ciImage, from: ciImage.extent) {
28+
return UIImage(cgImage: cgImage)
29+
}
30+
return nil
31+
}
32+
}

PhotoGether/DataLayer/PhotoGetherData/PhotoGetherData/ConnectionClientImpl.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public final class ConnectionClientImpl: ConnectionClient {
99

1010
public var receivedDataPublisher = PassthroughSubject<Data, Never>()
1111

12-
public var remoteVideoView: UIView = RTCMTLVideoView()
13-
public var localVideoView: UIView = RTCMTLVideoView()
12+
public var remoteVideoView: UIView = CapturableVideoView()
13+
public var localVideoView: UIView = CapturableVideoView()
1414

1515
public var peerID: String = ""
1616
public var roomID: String = ""
@@ -40,6 +40,27 @@ public final class ConnectionClientImpl: ConnectionClient {
4040
self.webRTCService.sendData(data)
4141
}
4242

43+
public func captureVideos() -> [UIImage] {
44+
let localCaptureImage = getCapturedVideos(isLocal: true)
45+
let remoteCaptureImage = getCapturedVideos(isLocal: false)
46+
47+
return [localCaptureImage, remoteCaptureImage]
48+
}
49+
50+
private func getCapturedVideos(isLocal: Bool) -> UIImage {
51+
let targetVideo = isLocal ? self.localVideoView : self.remoteVideoView
52+
53+
guard let videoView = targetVideo as? CapturableVideoView else {
54+
return UIImage()
55+
}
56+
57+
guard let capturedImage = videoView.capturedImage else {
58+
return UIImage()
59+
}
60+
61+
return capturedImage
62+
}
63+
4364
private func connect() {
4465
self.signalingService.connect()
4566
}
@@ -112,7 +133,7 @@ extension ConnectionClientImpl: WebRTCServiceDelegate {
112133
) {
113134
// TODO: 피어커넥션 연결 상태 변경에 따른 처리
114135
}
115-
136+
116137
/// peerConnection의 remoteDataChannel 에 데이터가 수신되면 호출됨
117138
public func webRTCService(
118139
_ service: WebRTCService,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import UIKit
2+
import PhotoGetherDomainInterface
3+
4+
public final class CaptureVideosUseCaseImpl: CaptureVideosUseCase {
5+
public func execute() -> [UIImage] {
6+
let localImage = [connectionRepository.clients[0].captureVideos()[0]]
7+
let remoteImages = connectionRepository.clients.map { $0.captureVideos()[1] }
8+
9+
return localImage + remoteImages
10+
}
11+
12+
private let connectionRepository: ConnectionRepository
13+
14+
public init(connectionRepository: ConnectionRepository) {
15+
self.connectionRepository = connectionRepository
16+
}
17+
}

PhotoGether/DomainLayer/PhotoGetherDomain/PhotoGetherDomainInterface/ConnectionClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public protocol ConnectionClient {
1010

1111
func sendOffer()
1212
func sendData(data: Data)
13+
func captureVideos() -> [UIImage]
1314
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import UIKit
2+
3+
public protocol CaptureVideosUseCase {
4+
func execute() -> [UIImage]
5+
}

0 commit comments

Comments
 (0)