-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARWAppDelegate.m
623 lines (493 loc) · 20 KB
/
ARWAppDelegate.m
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//
// ARWAppDelegate.m
// ARWave
//
// Created by おもちメタル on 2013/08/06.
// Copyright (c) 2013年 com.omochimetaru. All rights reserved.
//
#import "ARWAppDelegate.h"
#import "ARWLib.h"
#import "ARWGLView.h"
static CVReturn
ARWAppDelegateDisplayLinkOutputCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp* now,
const CVTimeStamp* outputTime,
CVOptionFlags flagsIn,
CVOptionFlags* flagsOut,
void* displayLinkContext){
@autoreleasepool {
ARWAppDelegate * this = (__bridge ARWAppDelegate *)displayLinkContext;
return [this displayLinkOutputHandler:displayLink
now:now
outputTime:outputTime flagsIn:flagsIn flagsOut:flagsOut];
}
}
@interface ARWAppDelegate()<ARWGLViewDelegate,AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureAudioDataOutputSampleBufferDelegate>
//ビュー
@property(nonatomic,strong)IBOutlet NSWindow *window;
@property(nonatomic,strong)NSView * rootView;
@property(nonatomic,strong)ARWGLView * glView;
//GL
@property(nonatomic,strong)NSOpenGLPixelFormat * glPixelFormat;
@property(nonatomic,strong)NSOpenGLContext * glContext;
//ディスプレイリンク
@property(nonatomic,assign)CVDisplayLinkRef displayLink;
@property(nonatomic,assign)int displayLinkCounter;
@property(nonatomic,assign)int displayLinkInterval;
//メインループ
@property(nonatomic,assign)uint64_t frameCount;
@property(nonatomic,strong)NSDate * fpsCountStartTime;
@property(nonatomic,assign)int fpsFrameCount;
@property(nonatomic,assign)int actualFps;
@property(nonatomic,strong)NSDate * prevUpdateTime;
@property(nonatomic,assign)BOOL updating;
@property(nonatomic,assign)int fpsPrintCounter;
@property(nonatomic,assign)int fpsPrintInterval;
//カメラ
@property(nonatomic,strong)AVCaptureSession * videoCaptureSession;
@property(nonatomic,strong)AVCaptureDevice * captureCamera;
@property(nonatomic,strong)AVCaptureVideoDataOutput * videoCaptureOutput;
@property(nonatomic,strong)ARWGLTexture * cameraYTexture;
@property(nonatomic,strong)ARWGLTexture * cameraUVTexture;
@property(nonatomic,strong)ARWTripleBuffering * cameraBuffering;
@property(nonatomic,strong)ARWGLCameraRenderer * cameraRenderer;
//サウンド
@property(nonatomic,strong)AVCaptureSession * audioCaptureSession;
@property(nonatomic,strong)AVCaptureDevice * captureMic;
@property(nonatomic,assign)AudioStreamBasicDescription audioCaptureFormat;
@property(nonatomic,strong)AVCaptureAudioDataOutput * audioCaptureOutput;
@property(nonatomic,strong)NSMutableData * capturedAudioStream;
//表示分
@property(nonatomic,strong)NSMutableData * waveAudioStream;
@property(nonatomic,assign)float waveAudioSampleInterval;
@property(nonatomic,assign)float waveAudioMaxTimeLen;
@end
@implementation ARWAppDelegate
-(void)awakeFromNib{
NSExceptionHandler *exceptionHandler = [NSExceptionHandler defaultExceptionHandler];
unsigned int handlingMask = NSLogAndHandleEveryExceptionMask;
[exceptionHandler setExceptionHandlingMask:handlingMask];
[exceptionHandler setDelegate:self];
}
-(BOOL)exceptionHandler:(NSExceptionHandler *)sender shouldLogException:(NSException *)exception mask:(NSUInteger)aMask{
return NO;
}
- (BOOL)exceptionHandler:(NSExceptionHandler *)sender shouldHandleException:(NSException *)exception
mask:(unsigned int)aMask{
NSLog(@"Exception: %@",exception);
NSLog(@"%@",[NSThread callStackSymbols]);
[NSApp terminate:self];
return NO;
}
-(void)windowWillClose:(NSNotification *)notification{
[[NSApplication sharedApplication]terminate:self];
}
-(void)initOpenGL{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorSize,24,
NSOpenGLPFAAlphaSize,8,
NSOpenGLPFADepthSize,32,
0
};
self.glPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
if(!self.glPixelFormat)@throw ARWGenericExceptionMake(@"OpenGLPixelFormat init failed");
self.glContext = [[NSOpenGLContext alloc]initWithFormat:self.glPixelFormat shareContext:nil];
if(!self.glContext)@throw ARWGenericExceptionMake(@"OpenGLContext init failed");
int value = 1;
[self.glContext setValues:&value forParameter:NSOpenGLCPSwapInterval];
[self.glContext makeCurrentContext];
const char *glVer = (const char *)ARWGLCallRet(const GLubyte *,glGetString,GL_VERSION);
const char *slVer = (const char *)ARWGLCallRet(const GLubyte *,glGetString,GL_SHADING_LANGUAGE_VERSION);
ARWLogInfo(@"GL: %s, GLSL: %s",glVer,slVer);
}
-(NSString *)selectCameraUniqueID{
NSArray * devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
ARWLogInfo(@"[Camera List]");
for(AVCaptureDevice * device in devices){
ARWLogInfo(@"[%@]/[%@]/[%@]",device.localizedName,device.modelID,device.uniqueID);
}
if(devices.count == 0)return nil;
return [(AVCaptureDevice *)devices[0] uniqueID];
}
-(AVCaptureDevice *)selectCamera{
NSString * cameraID = [self selectCameraUniqueID];
if(!cameraID)return nil;
return [AVCaptureDevice deviceWithUniqueID:cameraID];
}
-(NSString *)selectMicUniqueID{
NSArray * devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
ARWLogInfo(@"[Mic List]");
for(AVCaptureDevice * device in devices){
ARWLogInfo(@"[%@]/[%@]/[%@]",device.localizedName,device.modelID,device.uniqueID);
}
if(devices.count == 0)return nil;
return [(AVCaptureDevice *)devices[0] uniqueID];
}
-(AVCaptureDevice *)selectMic{
NSString * micID = [self selectMicUniqueID];
if(!micID)return nil;
return [AVCaptureDevice deviceWithUniqueID:micID];
}
-(BOOL)initVideoCaptureWithError:(NSError **)error{
//カメラ
self.captureCamera = [self selectCamera];
if(!self.captureCamera){
if(error)*error = ARWErrorMake(ARWErrorNoCamera,nil,nil);
return NO;
}
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:self.captureCamera error:error];
if(!input)return NO;
self.videoCaptureOutput = [[AVCaptureVideoDataOutput alloc]init];
self.videoCaptureOutput.alwaysDiscardsLateVideoFrames = NO;
NSMutableDictionary * videoSettings = [NSMutableDictionary dictionary];
videoSettings[(__bridge id)kCVPixelBufferPixelFormatTypeKey] = @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
self.videoCaptureOutput.videoSettings = videoSettings;
[self.videoCaptureOutput setSampleBufferDelegate:self
queue:dispatch_queue_create("video queue", DISPATCH_QUEUE_SERIAL)];
self.videoCaptureSession = [[AVCaptureSession alloc]init];
self.videoCaptureSession.sessionPreset = AVCaptureSessionPresetHigh;
[self.videoCaptureSession addInput:input];
[self.videoCaptureSession addOutput:self.videoCaptureOutput];
self.cameraRenderer = [[ARWGLCameraRenderer alloc]init];
return YES;
}
-(BOOL)initAudioCaptureWithError:(NSError **)error{
self.captureMic = [self selectMic];
if(!self.captureMic){
if(error)*error = ARWErrorMake(ARWErrorNoMic,nil,nil);
return NO;
}
if(!self.captureMic.connected){
if(error)*error = ARWErrorMake(ARWErrorNotConnected,nil,@"%@",self.captureMic.localizedName);
return NO;
}
//モノラルfloat
_audioCaptureFormat = *CMAudioFormatDescriptionGetStreamBasicDescription(self.captureMic.activeFormat.formatDescription);
_audioCaptureFormat.mFormatID = kAudioFormatLinearPCM;
//ここだけ元の値を考慮
_audioCaptureFormat.mSampleRate = MIN(48000,self.audioCaptureFormat.mSampleRate);
_audioCaptureFormat.mChannelsPerFrame = 1;
_audioCaptureFormat.mBitsPerChannel = sizeof(float) * 8;
_audioCaptureFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:self.captureMic error:error];
if(!input)return NO;
self.audioCaptureOutput = [[AVCaptureAudioDataOutput alloc]init];
NSMutableDictionary * audioSettings = [NSMutableDictionary dictionary];
audioSettings[AVFormatIDKey] = @(kAudioFormatLinearPCM);
audioSettings[AVSampleRateKey] = @(_audioCaptureFormat.mSampleRate);
audioSettings[AVLinearPCMIsFloatKey] = @((_audioCaptureFormat.mFormatFlags & kLinearPCMFormatFlagIsFloat) != 0);
audioSettings[AVNumberOfChannelsKey] = @(_audioCaptureFormat.mChannelsPerFrame);
audioSettings[AVLinearPCMBitDepthKey] = @(_audioCaptureFormat.mBitsPerChannel);
self.audioCaptureOutput.audioSettings = audioSettings;
[self.audioCaptureOutput setSampleBufferDelegate:self
queue:dispatch_queue_create("audio queue", DISPATCH_QUEUE_SERIAL)];
self.audioCaptureSession = [[AVCaptureSession alloc]init];
[self.audioCaptureSession addInput:input];
[self.audioCaptureSession addOutput:self.audioCaptureOutput];
self.capturedAudioStream = [[NSMutableData alloc]init];
return YES;
}
//キャプチャスレッドからメインスレにポストされる
-(void)initCameraBufferingWithWidth:(uint32_t)width height:(uint32_t)height{
if(!self.cameraBuffering){
//二重初期化防止
ARWLogInfo(@"capture init %d x %d",width,height);
self.cameraBuffering = [[ARWTripleBuffering alloc]initWithBufferCreator:^id{
ARWImage * image = [[ARWImage alloc]init];
image.format = ARWImageFormatYUV420;
image.width = width;
image.height = height;
image.data = [NSMutableData dataWithLength:width*height*1.5];
return image;
}];
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
NSError * error;
if(!^(NSError ** error){
self.rootView = self.window.contentView;
//メインループ
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
CVDisplayLinkSetOutputCallback(_displayLink,
&ARWAppDelegateDisplayLinkOutputCallback,
(__bridge void *)(self));
self.frameCount = 0;
self.fpsCountStartTime = [NSDate date];
self.fpsFrameCount = 0;
self.actualFps = 0;
self.prevUpdateTime = [NSDate date];
self.updating = NO;
self.displayLinkCounter = 0;
self.displayLinkInterval = 1;
self.fpsPrintCounter = 0;
self.fpsPrintInterval = 10;
//GL
[self initOpenGL];
[self.glContext makeCurrentContext];
self.glView = [[ARWGLView alloc]initWithFrame:self.rootView.bounds
pixelFormat:self.glPixelFormat];
[self.glView setOpenGLContext:self.glContext];
[self.rootView addSubview:self.glView];
self.glView.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
self.glView.delegate = self;
if(![self initVideoCaptureWithError:error])return NO;
if(![self initAudioCaptureWithError:error])return NO;
//開始
CVDisplayLinkStart(_displayLink);
[self.videoCaptureSession startRunning];
[self.audioCaptureSession startRunning];
self.waveAudioStream = [[NSMutableData alloc]init];
self.waveAudioSampleInterval = 0.001;
self.waveAudioMaxTimeLen = 1.f;
return YES;
}(&error)){
ARWLogError(@"%@",ARWErrorDump(error));
}
}
-(void)applicationWillTerminate:(NSNotification *)notification{
[self.audioCaptureSession stopRunning];
[self.videoCaptureSession stopRunning];
CVDisplayLinkRelease(_displayLink);
self.glContext = nil;
}
-(void)videoCaptureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
if(CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly)){
ARWLogError(@"CVPixelBufferLockBaseAddress failed");
return;
}
int w = (int)CVPixelBufferGetWidthOfPlane(pixelBuffer,0);
int h = (int)CVPixelBufferGetHeightOfPlane(pixelBuffer,0);
if(!self.cameraBuffering){
dispatch_async(dispatch_get_main_queue(), ^{
//二重防止されてる
[self initCameraBufferingWithWidth:w height:h];
});
}else{
ARWImage * backBuffer = [self.cameraBuffering lock];
uint8_t * d = (uint8_t *)[backBuffer.data mutableBytes];
{
uint8_t * s = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer,0);
int stride = (int)CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
for(int yi = 0;yi<h;yi++){
memcpy(d,s,w);
d += w;
s += stride;
}
}
{
uint8_t * s = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer,1);
int stride = (int)CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1);
for(int yi=0;yi<h/2;yi++){
memcpy(d,s,w);
d += w;
s += stride;
}
}
//ARWLogInfo(@"write %@ %d x %d",backBuffer,w,h);
[self.cameraBuffering unlock];
}
if(CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly)){
ARWLogError(@"CVPixelBufferUnlockBaseAddress failed");
return;
}
}
-(void)audioCaptureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
//const AudioStreamBasicDescription *asbd = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription);
int64_t numSample = CMSampleBufferGetNumSamples(sampleBuffer);
//
// if(asbd->mFormatID != kAudioFormatLinearPCM ||
// asbd->mSampleRate != self.audioCaptureFormat.mSampleRate){
// ARWLogError(@"audio capture error: format: %@, sampleRate: %f",ARWFourCharCodeToString(asbd->mFormatID),asbd->mSampleRate);
// return;
// }
CMBlockBufferRef blockBuffer;
AudioBufferList audioBufferList;
if(CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList,sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer)){
ARWLogError(@"CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer failed");
return;
}
uint64_t dataLen = CMBlockBufferGetDataLength(blockBuffer);
@synchronized(self){
uint64_t oldLen = self.capturedAudioStream.length;
[self.capturedAudioStream setLength:oldLen + dataLen];
if(CMBlockBufferCopyDataBytes(blockBuffer,0,dataLen,
(uint8_t *)self.capturedAudioStream.mutableBytes + oldLen)){
ARWLogError(@"CMBlockBufferCopyDataBytes failed");
CFRelease(blockBuffer);
return;
}
//0.1秒分までしか溜め込まない
uint64_t maxSampleNum = self.audioCaptureFormat.mSampleRate * 0.1;
uint64_t sampleNum = self.capturedAudioStream.length / sizeof(float);
if(sampleNum > maxSampleNum){
uint64_t deleteLen = (sampleNum - maxSampleNum) * sizeof(float);
[self.capturedAudioStream replaceBytesInRange:NSMakeRange(0,deleteLen) withBytes:NULL length:0];
}
//ARWLogInfo(@"capture sample %ld",self.capturedAudioStream.length / sizeof(float));
//ARWLogInfo(@"captured audio %ld bytes",self.capturedAudioStream.length);
}
CFRelease(blockBuffer);
}
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
if(captureOutput == self.videoCaptureOutput){
[self videoCaptureOutput:captureOutput didOutputSampleBuffer:sampleBuffer fromConnection:connection];
}else if(captureOutput == self.audioCaptureOutput){
[self audioCaptureOutput:captureOutput didOutputSampleBuffer:sampleBuffer fromConnection:connection];
}
}
-(void)updateWithDeltaTime:(double)deltaTime{
[self.glContext makeCurrentContext];
if(self.cameraBuffering){
[self.cameraBuffering swap];
ARWImage * cameraImage = [self.cameraBuffering front];
if(!self.cameraYTexture){
self.cameraYTexture = [[ARWGLTexture alloc]init];
[self.cameraYTexture setImageWithWidth:cameraImage.width
height:cameraImage.height
internalFormat:GL_LUMINANCE];
self.cameraUVTexture = [[ARWGLTexture alloc]init];
[self.cameraUVTexture setImageWithWidth:cameraImage.width/2
height:cameraImage.height/2
internalFormat:GL_LUMINANCE_ALPHA];
}
[self.cameraYTexture setSubImageWithFormat:GL_LUMINANCE type:GL_UNSIGNED_BYTE data:cameraImage.data.bytes];
int yLen = cameraImage.width * cameraImage.height;
[self.cameraUVTexture setSubImageWithFormat:GL_LUMINANCE_ALPHA type:GL_UNSIGNED_BYTE
data:(uint8_t *)cameraImage.data.bytes + yLen];
}
//このフレームの分を吸着
@synchronized(self){
float sampleRate = self.audioCaptureFormat.mSampleRate;
int numSample = MIN(deltaTime * sampleRate,self.capturedAudioStream.length / sizeof(float));
float * pSample = (float *)self.capturedAudioStream.bytes;
float sampleTime = 0.f;
float sampleSum = 0.f;
int sampleSumCount = 0;
//ミリ秒単位にする
for(int si = 0;si < numSample;si++){
sampleTime += 1.f / sampleRate;
sampleSum += *pSample;
sampleSumCount++;
pSample++;
if(sampleTime >= self.waveAudioSampleInterval){
sampleTime -= self.waveAudioSampleInterval;
float waveValue = sampleSum / (float)sampleSumCount;
sampleSum = 0;
sampleSumCount = 0;
[self.waveAudioStream appendBytes:&waveValue length:sizeof(float)];
}
}
//ARWLogInfo(@"wave sample %ld",self.waveAudioStream.length / sizeof(float));
}
//1秒分をキープ
int keepSampleNum = self.waveAudioMaxTimeLen / self.waveAudioSampleInterval; //1kHzで1Sec
int64_t waveNum = self.waveAudioStream.length / sizeof(float);
if(waveNum > keepSampleNum){
uint64_t deleteLen = (waveNum-keepSampleNum)*sizeof(float);
[self.waveAudioStream replaceBytesInRange:NSMakeRange(0,deleteLen) withBytes:NULL length:0];
}
[NSOpenGLContext clearCurrentContext];
}
-(void)render{
[self.glContext makeCurrentContext];
ARWGLCall(glClearColor,0.f,0.f,0.2f,1.f);
ARWGLCall(glClear,GL_COLOR_BUFFER_BIT);
float w = CGRectGetWidth(self.glView.bounds);
float h = CGRectGetHeight(self.glView.bounds);
CGRect bounds = self.glView.bounds;
ARWGLCall(glViewport,0, 0,w,h);
ARWGLCall(glMatrixMode,GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w,h,0, -1.0,1.0);
ARWGLCall(glMatrixMode,GL_MODELVIEW);
glLoadIdentity();
if(self.cameraYTexture){
glPushMatrix();
CGRect cameraFrame = ARWRectAspectFill(CGRectMake(0, 0, w, h),
CGSizeMake(self.cameraYTexture.width, self.cameraYTexture.height),
ARWLayoutGravityCenter);
glTranslatef(CGRectGetMinX(cameraFrame),CGRectGetMinY(cameraFrame), 0.f);
glScalef(CGRectGetWidth(cameraFrame)/2.f, CGRectGetHeight(cameraFrame)/2.f, 1.f);
glTranslatef(1.f, 1.f, 0.f);
[self.cameraRenderer renderWithYTexture:self.cameraYTexture
uvTexture:self.cameraUVTexture vFlip:NO];
glPopMatrix();
}
{
int maxNum = (int)(self.waveAudioMaxTimeLen/self.waveAudioSampleInterval);
float vertices[maxNum*3];
float * v = vertices + 0;
float * waveValue = (float *)self.waveAudioStream.bytes;
int num = (int)(self.waveAudioStream.length / sizeof(float));
for(int i=0;i<maxNum;i++){
v[0] = i/(float)(maxNum-1);
if(i < num){
v[1] = waveValue[i];
if(i+1 == num){
//ARWLogInfo(@"[%d] = %f",i,waveValue[i]);
}
}else{
v[1] = 0.f;
}
v[2] = 0.f;
v+=3;
}
glPushMatrix();
glTranslatef(0, h/2.f, 0.f);
glScalef(w, h/2.f, 1.f);
ARWGLCall(glColor3f,0.1, 0.9, 0.1);
ARWGLCall(glEnableClientState,GL_VERTEX_ARRAY);
ARWGLCall(glVertexPointer,3,GL_FLOAT,sizeof(float)*3,vertices);
ARWGLCall(glDrawArrays,GL_LINE_STRIP, 0, maxNum);
ARWGLCall(glDisableClientState,GL_VERTEX_ARRAY);
glPopMatrix();
}
[self.glContext flushBuffer];
[NSOpenGLContext clearCurrentContext];
}
-(void)glViewDidUpdate:(ARWGLView *)glView{
//これでチラつかない
[self render];
}
-(void)updateFrameHandler{
self.frameCount++;
self.fpsFrameCount++;
NSDate * now = [NSDate date];
if([now timeIntervalSinceDate:self.fpsCountStartTime] >= 1.0){
self.fpsCountStartTime = now;
self.actualFps = self.fpsFrameCount;
self.fpsFrameCount = 0;
self.fpsPrintCounter ++;
if(self.fpsPrintCounter >= self.fpsPrintInterval){
self.fpsPrintCounter = 0;
ARWLogInfo(@"fps %d",self.actualFps);
}
}
double deltaTime = [now timeIntervalSinceDate:self.prevUpdateTime];
self.prevUpdateTime = now;
[self updateWithDeltaTime:deltaTime];
[self render];
}
-(CVReturn)displayLinkOutputHandler:(CVDisplayLinkRef)displayLink
now:(const CVTimeStamp*) now
outputTime:(const CVTimeStamp*) outputTime
flagsIn:(CVOptionFlags)flagsIn
flagsOut:(CVOptionFlags*)flagsOut{
self.displayLinkCounter++;
if(self.displayLinkCounter >= self.displayLinkInterval){
self.displayLinkCounter = 0;
if(!self.updating){
self.updating = YES;
dispatch_async(dispatch_get_main_queue(), ^{
[self updateFrameHandler];
self.updating = NO;
});
}
}
return kCVReturnSuccess;
}
@end