-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCustomVideoSource.swift
More file actions
185 lines (145 loc) · 8.38 KB
/
Copy pathCustomVideoSource.swift
File metadata and controls
185 lines (145 loc) · 8.38 KB
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
177
178
179
180
181
182
183
184
185
//
// CustomVideoSource.swift
// R5ProTestbed
//
// Created by David Heimann on 5/9/16.
// Copyright © 2015 Infrared5, Inc. All rights reserved.
//
// The accompanying code comprising examples for use solely in conjunction with Red5 Pro (the "Example Code")
// is licensed to you by Infrared5 Inc. in consideration of your agreement to the following
// license terms and conditions. Access, use, modification, or redistribution of the accompanying
// code constitutes your acceptance of the following license terms and conditions.
//
// Permission is hereby granted, free of charge, to you to use the Example Code and associated documentation
// files (collectively, the "Software") without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The Software shall be used solely in conjunction with Red5 Pro. Red5 Pro is licensed under a separate end
// user license agreement (the "EULA"), which must be executed with Infrared5, Inc.
// An example of the EULA can be found on our website at: https://account.red5pro.com/assets/LICENSE.txt.
//
// The above copyright notice and this license shall be included in all copies or portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL INFRARED5, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import UIKit
import R5Streaming
@objc(CustomVideoSource)
class CustomVideoSource : R5VideoSource {
var frameDuration : CMTime;
var PTS : CMTime;
var timer : Timer?;
var start: Double!
override init() {
let fpsVal: Int32 = 15;
//setup simple timestamp calculation
self.frameDuration = CMTimeMakeWithSeconds(1.0/Double(fpsVal), preferredTimescale: fpsVal);
self.PTS = CMTime.zero;
self.timer = nil;
super.init();
//initialize the properties used by the stream!
self.bitrate = 256;
self.width = 320;
self.height = 240;
self.orientation = 0;
self.fps = fpsVal;
}
override func startVideoCapture() {
//start a timer to run at desired framerate.
self.timer = Timer.scheduledTimer(timeInterval: 1.0 / Double(self.fps),
target: self,
selector: #selector(capturePixels),
userInfo: nil,
repeats: true);
}
override func stopVideoCapture() {
//stop the capture!
self.timer!.invalidate()
//stop the encoder!
super.stopVideoCapture()
}
@objc func capturePixels( time:Timer ){
if (start == nil || start == 0) {
start = now_ms()
}
//make sure encoding layer is ready for input!
if(self.encoder != nil){
let frameSize: CGSize = CGSize(width: 352, height: 288);
//
// Below is a simple "plasma" style rendering using the PTS as the animation offset
// Using RGB color format - 3 bytes per pixel
//
let time: Float = Float( CMTimeGetSeconds(self.PTS) * 0.6 );
let scale: Float = 0.035;
let componentsPerPixel = 3;
let rgbPixels: UnsafeMutablePointer<__uint8_t> = UnsafeMutablePointer<__uint8_t>.allocate(capacity: Int(frameSize.width * frameSize.height) * componentsPerPixel);
//stride
let bpr: Int = Int(frameSize.width) * componentsPerPixel;
for y in 0..<Int(frameSize.height) {
for x in 0..<Int(frameSize.width) {
var cx: Float = Float(x) / Float(componentsPerPixel) * scale;
var cy = Float(y) * scale;
var v: Float = sinf(cx+time);
v += sinf(cy+time);
v += sinf(cx+cy+time);
cx += scale * sinf(time*0.33);
cy += scale * cosf(time*0.2);
v += sinf(sqrtf(cx*cx + cy*cy + 1.0)+time);
//Set the R, G, B channels to the desired color
rgbPixels[(y * bpr) + (componentsPerPixel*x)] = __uint8_t( max( Double(sinf( v * Float.pi )) * Double(UINT8_MAX), 0) );
rgbPixels[(y * bpr) + (componentsPerPixel*x)+1] = __uint8_t( max( Double(cosf( v * Float.pi )) * Double(UINT8_MAX), 0) );
rgbPixels[(y * bpr) + (componentsPerPixel*x)+2] = 0;
}
}
// Create a pixel buffer
var pixelBuffer: CVPixelBuffer?;
//Create a pixel buffer to hold our bytes with RGB format
var result: OSStatus = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
Int(frameSize.width),
Int(frameSize.height),
kCVPixelFormatType_24RGB,
rgbPixels,
Int(frameSize.width) * componentsPerPixel,
nil,
nil,
nil,
&pixelBuffer);
if(result != kCVReturnSuccess){
NSLog("Failed to get pixel buffer");
}
var videoInfo: CMVideoFormatDescription?;
//Create a description for the pixel buffer
result = CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pixelBuffer!, formatDescriptionOut: &videoInfo);
if(result != kCVReturnSuccess) {
NSLog("Failed to create video info");
}
//increment our timestamp
self.PTS = CMTimeAdd(self.PTS, self.frameDuration);
let now = now_ms()
//Only PTS is needed for the encoder - leave everything else invalid if you want
var timingInfo: CMSampleTimingInfo = CMSampleTimingInfo.invalid;
timingInfo.duration = CMTime.invalid;
timingInfo.decodeTimeStamp = CMTime.invalid;
// timingInfo.presentationTimeStamp = self.PTS;
let aTime:Double = (now-start)/100
timingInfo.presentationTimeStamp = CMTimeMakeWithSeconds(aTime, preferredTimescale: 1000)
var buffer: CMSampleBuffer?;
//Create the sample buffer for the pixel buffer
result = CMSampleBufferCreateForImageBuffer(allocator: kCFAllocatorDefault,
imageBuffer: pixelBuffer!,
dataReady: true, makeDataReadyCallback: nil, refcon: nil,
formatDescription: videoInfo!,
sampleTiming: &timingInfo,
sampleBufferOut: &buffer);
//push the sample buffer to the encoder with type r5_media_type_video_custom
self.encoder.encodeFrame( buffer, of: r5_media_type_video_custom );
//free all our content
free(rgbPixels);
}
}
}