Home telegram - mediaPlayerNode
Post
Cancel

telegram - mediaPlayerNode

class MediaPlayerNode

播放层UI(AVSampleBufferDisplayLayer)封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
public var videoInHierarchy: Bool = false
public var updateVideoInHierarchy: ((Bool) -> Void)?
private var videoNode: MediaPlayerNodeDisplayNode
private var videoLayer: AVSampleBufferDisplayLayer?
private let videoQueue: Queue
public var snapshotNode: ASDisplayNode? {
    didSet {
        if let snapshotNode = oldValue {
            snapshotNode.removeFromSupernode()
        }
        
        if let snapshotNode = self.snapshotNode {
            snapshotNode.frame = self.bounds
            self.insertSubnode(snapshotNode, at: 0)
        }
    }
}
var takeFrameAndQueue: (Queue, () -> MediaTrackFrameResult)?
var timer: SwiftSignalKit.Timer?
var polling = false
var currentRotationAngle = 0.0
var currentAspect = 1.0


public var state: (timebase: CMTimebase, requestFrames: Bool, rotationAngle: Double, aspect: Double)? {
    didSet {
        self.updateState()
    }
}
private func updateState() {
    if let (timebase, requestFrames, rotationAngle, aspect) = self.state {
        if let videoLayer = self.videoLayer {
            videoQueue.async {
                if videoLayer.controlTimebase !== timebase || videoLayer.status == .failed {
                    videoLayer.flush()
                    videoLayer.controlTimebase = timebase
                }
            }
            
            if !self.currentRotationAngle.isEqual(to: rotationAngle) || !self.currentAspect.isEqual(to: aspect) {
                self.currentRotationAngle = rotationAngle
                self.currentAspect = aspect
                var transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
                if !rotationAngle.isZero {
                    transform = transform.scaledBy(x: CGFloat(aspect), y: CGFloat(1.0 / aspect))
                }
                videoLayer.setAffineTransform(transform)
            }
            
            if self.videoInHierarchy {
                if requestFrames {
                    self.startPolling()
                }
            }
        }
    }
}


private func startPolling() {
    if !self.polling {
        self.polling = true
        self.poll(completion: { [weak self] status in
            self?.polling = false
            
            if let strongSelf = self, let (_, requestFrames, _, _) = strongSelf.state, requestFrames {
                strongSelf.timer?.invalidate()
                switch status {
                    case let .delay(delay):
                        strongSelf.timer = SwiftSignalKit.Timer(timeout: delay, repeat: true, completion: {
                            if let strongSelf = self, let videoLayer = strongSelf.videoLayer, let (_, requestFrames, _, _) = strongSelf.state, requestFrames, strongSelf.videoInHierarchy {
                                if videoLayer.isReadyForMoreMediaData {
                                    strongSelf.timer?.invalidate()
                                    strongSelf.timer = nil
                                    strongSelf.startPolling()
                                }
                            }
                        }, queue: Queue.mainQueue())
                        strongSelf.timer?.start()
                    case .finished:
                        break
                }
            }
        })
    }
}

private func poll(completion: @escaping (PollStatus) -> Void) {
    if let (takeFrameQueue, takeFrame) = self.takeFrameAndQueue, let videoLayer = self.videoLayer, let (timebase, _, _, _) = self.state {
        let layerRef = Unmanaged.passRetained(videoLayer)
        takeFrameQueue.async {
            let status: PollStatus
            do {
                var numFrames = 0
                let layer = layerRef.takeUnretainedValue()
                let layerTime = CMTimeGetSeconds(CMTimebaseGetTime(timebase))
                let rate = CMTimebaseGetRate(timebase)
                var maxTakenTime = layerTime + 0.1
                var finised = false
                loop: while true {
                    let isReady = layer.isReadyForMoreMediaData
                    #if DEBUG
                    if let error = layer.error {
                        print("MediaPlayerNode error: \(error)")
                    }
                    #endif

                    if isReady {
                        switch takeFrame() {
                            case let .restoreState(frames, atTime):
                                layer.flush()
                                for i in 0 ..< frames.count {
                                    let frame = frames[i]
                                    let frameTime = CMTimeGetSeconds(frame.position)
                                    maxTakenTime = frameTime
                                    let attachments = CMSampleBufferGetSampleAttachmentsArray(frame.sampleBuffer, createIfNecessary: true)! as NSArray
                                    let dict = attachments[0] as! NSMutableDictionary
                                    if i == 0 {
                                        CMSetAttachment(frame.sampleBuffer, key: kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding as NSString, value: kCFBooleanTrue as AnyObject, attachmentMode: kCMAttachmentMode_ShouldPropagate)
                                        CMSetAttachment(frame.sampleBuffer, key: kCMSampleBufferAttachmentKey_EndsPreviousSampleDuration as NSString, value: kCFBooleanTrue as AnyObject, attachmentMode: kCMAttachmentMode_ShouldPropagate)
                                    }
                                    if CMTimeCompare(frame.position, atTime) < 0 {
                                        dict.setValue(kCFBooleanTrue as AnyObject, forKey: kCMSampleAttachmentKey_DoNotDisplay as NSString as String)
                                    } else if CMTimeCompare(frame.position, atTime) == 0 {
                                        dict.setValue(kCFBooleanTrue as AnyObject, forKey: kCMSampleAttachmentKey_DisplayImmediately as NSString as String)
                                        dict.setValue(kCFBooleanTrue as AnyObject, forKey: kCMSampleBufferAttachmentKey_EndsPreviousSampleDuration as NSString as String)
                                        //print("restore state to \(frame.position) -> \(frameTime) at \(layerTime) (\(i + 1) of \(frames.count))")
                                    }
                                    layer.enqueue(frame.sampleBuffer)
                                }
                            case let .frame(frame):
                                numFrames += 1
                                let frameTime = CMTimeGetSeconds(frame.position)
                                if rate.isZero {
                                    //print("enqueue \(frameTime) at \(layerTime)")
                                }
                                if frame.resetDecoder {
                                    layer.flush()
                                }

                                if frame.decoded && frameTime < layerTime {
                                    //print("drop frame at \(frameTime) current \(layerTime)")
                                    continue loop
                                }

                                //print("took frame at \(frameTime) current \(layerTime)")
                                maxTakenTime = frameTime
                                layer.enqueue(frame.sampleBuffer)
                            case .skipFrame:
                                break
                            case .noFrames:
                                finised = true
                                break loop
                            case .finished:
                                finised = true
                                break loop
                        }
                    } else {
                        break loop
                    }
                }
                if finised {
                    status = .finished
                } else {
                    status = .delay(max(1.0 / 30.0, maxTakenTime - layerTime))
                }
                //print("took \(numFrames) frames, status \(status)")
            }
            DispatchQueue.main.async {
                layerRef.release()

                completion(status)
            }
        }
    }
}
This post is licensed under CC BY 4.0 by the author.