Skip to content

Commit

Permalink
1. fix video record demo bug (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYng authored and aiyaapp committed Jan 3, 2018
1 parent 1255807 commit f0941a4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 32 deletions.
4 changes: 2 additions & 2 deletions AiyaEffectSDK/AiyaEffectSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
D31A58761FD0F88800ADAA89 /* AySlimFace.mm in Sources */ = {isa = PBXBuildFile; fileRef = D31A58751FD0F88800ADAA89 /* AySlimFace.mm */; };
D31A58791FD10AC900ADAA89 /* AYGPUImageTrackOutput.h in Headers */ = {isa = PBXBuildFile; fileRef = D31A58771FD10AC900ADAA89 /* AYGPUImageTrackOutput.h */; };
D31A587A1FD10AC900ADAA89 /* AYGPUImageTrackOutput.m in Sources */ = {isa = PBXBuildFile; fileRef = D31A58781FD10AC900ADAA89 /* AYGPUImageTrackOutput.m */; };
D343879F1FFC822700C331FD /* libavcodec.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926C21FCE5C1D00A00D12 /* libavcodec.a */; };
D34606911FD13AA5002FF508 /* libyuv.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D34606901FD13AA4002FF508 /* libyuv.a */; };
D34606951FD1744B002FF508 /* libBaseEffects.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D365AF4C1FD00CFC00E60BC0 /* libBaseEffects.a */; };
D365AF4A1FCFFDCA00E60BC0 /* AYGPUImageBeautyFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = D365AF481FCFFDCA00E60BC0 /* AYGPUImageBeautyFilter.h */; };
Expand All @@ -36,7 +37,6 @@
D36926BE1FCE5B6100A00D12 /* libaysdk.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926BD1FCE5B6100A00D12 /* libaysdk.a */; };
D36926C71FCE5C1D00A00D12 /* libavutil.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926C01FCE5C1D00A00D12 /* libavutil.a */; };
D36926C81FCE5C1D00A00D12 /* libavfilter.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926C11FCE5C1D00A00D12 /* libavfilter.a */; };
D36926C91FCE5C1D00A00D12 /* libavcodec.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926C21FCE5C1D00A00D12 /* libavcodec.a */; };
D36926CA1FCE5C1D00A00D12 /* libavformat.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926C31FCE5C1D00A00D12 /* libavformat.a */; };
D36926CB1FCE5C1D00A00D12 /* libavdevice.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926C41FCE5C1D00A00D12 /* libavdevice.a */; };
D36926CC1FCE5C1D00A00D12 /* libswresample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D36926C51FCE5C1D00A00D12 /* libswresample.a */; };
Expand Down Expand Up @@ -365,7 +365,7 @@
D36926D41FCE5C3200A00D12 /* libsoil.a in Frameworks */,
D36926CB1FCE5C1D00A00D12 /* libavdevice.a in Frameworks */,
D36926CC1FCE5C1D00A00D12 /* libswresample.a in Frameworks */,
D36926C91FCE5C1D00A00D12 /* libavcodec.a in Frameworks */,
D343879F1FFC822700C331FD /* libavcodec.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@
<key>AyBeauty.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>18</integer>
<integer>5</integer>
</dict>
<key>AyEffect.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>17</integer>
<integer>4</integer>
</dict>
<key>AySDKCore.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>15</integer>
<integer>2</integer>
</dict>
<key>AyShortVideoEffect.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>19</integer>
<integer>6</integer>
</dict>
<key>AyTrack.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>16</integer>
<integer>3</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
Expand Down
Binary file not shown.
10 changes: 0 additions & 10 deletions demo/AiyaVideoRecord/AiyaVideoRecord/Camera.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ - (void)configSession{
self.session.sessionPreset = AVCaptureSessionPreset1280x720;
}else if ([self.session canSetSessionPreset:AVCaptureSessionPreset640x480]){
self.session.sessionPreset = AVCaptureSessionPreset640x480;
}else if ([self.session canSetSessionPreset:AVCaptureSessionPreset352x288]){
self.session.sessionPreset = AVCaptureSessionPreset352x288;
}

[self.session commitConfiguration];
Expand Down Expand Up @@ -169,14 +167,6 @@ - (void)setCameraPosition:(AVCaptureDevicePosition)position{

self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.camera error:nil];

if ([self.session canSetSessionPreset:AVCaptureSessionPreset1280x720]){
self.session.sessionPreset = AVCaptureSessionPreset1280x720;
}else if ([self.session canSetSessionPreset:AVCaptureSessionPreset640x480]){
self.session.sessionPreset = AVCaptureSessionPreset640x480;
}else if ([self.session canSetSessionPreset:AVCaptureSessionPreset352x288]){
self.session.sessionPreset = AVCaptureSessionPreset352x288;
}

if ([self.session canAddInput:self.videoInput]) {
[self.session addInput:self.videoInput];
}
Expand Down
16 changes: 12 additions & 4 deletions demo/AiyaVideoRecord/AiyaVideoRecord/CameraDataProcess.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
1.0f, 0.0f,
};

static const GLfloat horizontalFlipTextureCoordinates[] = {
1.0f, 0.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
};

static const GLfloat noRotationTextureCoordinates[] = {
0.0f, 0.0f,
1.0f, 0.0f,
Expand Down Expand Up @@ -232,10 +239,11 @@ - (CMSampleBufferRef)process:(CMSampleBufferRef)sampleBuffer{
glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, 0, 0, squareVertices);

if (_mirror) {
glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, 0, 0, verticalFlipTextureCoordinates);
}else {
glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, 0, 0, noRotationTextureCoordinates);
}else {
glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, 0, 0, verticalFlipTextureCoordinates);
}

glUniformMatrix4fv(transformMatrixUniform, 1, GL_FALSE, transformMatrix);
glUniformMatrix3fv(conversionMatrixUniform, 1, GL_FALSE, kAYColorConversion601FullRangeDefault);

Expand Down Expand Up @@ -293,8 +301,8 @@ - (CMSampleBufferRef)process:(CMSampleBufferRef)sampleBuffer{
glEnableVertexAttribArray(textureCoordinateAttribute2);

glVertexAttribPointer(positionAttribute2, 2, GL_FLOAT, 0, 0, squareVertices);
glVertexAttribPointer(textureCoordinateAttribute2, 2, GL_FLOAT, 0, 0, noRotationTextureCoordinates);
glVertexAttribPointer(textureCoordinateAttribute2, 2, GL_FLOAT, 0, 0, horizontalFlipTextureCoordinates);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glFinish();
Expand Down
19 changes: 15 additions & 4 deletions demo/AiyaVideoRecord/AiyaVideoRecord/MediaWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ @implementation MediaWriter
static const int kAudioBitRate = 64000;

// 视频的码率
static const int kVideoBitRateFactor = 2.05; // videoBitRate = width * height * kVideoBitRateFactor;
static const int kVideoBitRateFactor = 4; // videoBitRate = width * height * kVideoBitRateFactor;

// 帧率
static const int kVideoFrameRate = 30;
Expand Down Expand Up @@ -127,12 +127,15 @@ - (BOOL)setupAudioWithSettings:(CMSampleBufferRef)sampleBuffer{
[self.assetWriter addInput:self.assetWriterAudioInput];
} else {
NSLog(@"couldn't add asset writer audio input");
self.assetWriterAudioInput = nil;
return NO;
}

} else {

self.assetWriterAudioInput = nil;
NSLog(@"couldn't apply audio output settings");
return NO;

}

Expand Down Expand Up @@ -170,13 +173,17 @@ - (BOOL)setupVideoWithSettings:(CVImageBufferRef)pixelBuffer {

[self.assetWriter addInput:self.assetWriterVideoInput];
} else {

self.assetWriterVideoInput = nil;
NSLog(@"couldn't add asset writer video input");
return NO;
}

} else {

self.assetWriterVideoInput = nil;
NSLog(@"couldn't apply video output settings");
return NO;

}

Expand All @@ -192,10 +199,12 @@ - (void)writeAudioSampleBuffer:(CMSampleBufferRef)sampleBuffer{

if (!self.audioAlreadySetup){
self.audioAlreadySetup = [self setupAudioWithSettings:sampleBuffer];
NSLog(@"设置音频参数");

if (!self.audioAlreadySetup) {
NSLog(@"设置音频参数失败");
return;
} else {
NSLog(@"设置音频参数成功");
}
}

Expand Down Expand Up @@ -230,7 +239,7 @@ - (void)writeAudioSampleBuffer:(CMSampleBufferRef)sampleBuffer{
CFRetain(sampleBuffer);

dispatch_async(writerQueue, ^{
if (self.assetWriterAudioInput.readyForMoreMediaData) {
if (self.assetWriterAudioInput && self.assetWriterAudioInput.readyForMoreMediaData) {

CMSampleBufferRef adjustedSampleBuffer = [self adjustTime:sampleBuffer by:self.firstTime];

Expand Down Expand Up @@ -273,9 +282,11 @@ - (void)writeVideoPixelBuffer:(CVPixelBufferRef)pixelBuffer time:(CMTime)frameTi

if (!self.videoAlreadySetup){
self.videoAlreadySetup = [self setupVideoWithSettings:pixelBuffer];
NSLog(@"设置视频参数");
if (!self.videoAlreadySetup){
NSLog(@"设置视频参数失败");
return;
} else {
NSLog(@"设置视频参数成功");
}
}

Expand Down
57 changes: 50 additions & 7 deletions demo/AiyaVideoRecord/AiyaVideoRecord/Preview.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
@"attribute vec4 position;\n"
"attribute vec2 inputTextureCoordinate;\n"
"varying mediump vec2 v_texCoord;\n"
"uniform mediump mat4 transformMatrix;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
" gl_Position = transformMatrix * position;\n"
" v_texCoord = inputTextureCoordinate;\n"
"}\n";

Expand All @@ -42,11 +43,11 @@
1.0f, 0.0f,
};

static const GLfloat rotateRightTextureCoordinates[] = {
0.0f, 1.0f,
static const GLfloat noRotationTextureCoordinates[] = {
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};

@interface Preview () {
Expand All @@ -59,7 +60,8 @@ @interface Preview () {
GLuint program;
GLint positionAttribute, textureCoordinateAttribute;
GLint inputTextureUniform;

GLint transformMatrixUniform;

GLuint bgraTexture;
}
@end
Expand Down Expand Up @@ -106,6 +108,8 @@ - (void)initGL{
positionAttribute = glGetAttribLocation(program, [@"position" UTF8String]);
textureCoordinateAttribute = glGetAttribLocation(program, [@"inputTextureCoordinate" UTF8String]);
inputTextureUniform = glGetUniformLocation(program, [@"mTexture" UTF8String]);
transformMatrixUniform = glGetUniformLocation(program, [@"transformMatrix" UTF8String]);

});

}
Expand All @@ -129,6 +133,26 @@ - (void)render:(CVPixelBufferRef)pixelBuffer{

glUseProgram(program);

CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0, 0, 1);
GLfloat transformMatrix[16];
transformMatrix[0] = (GLfloat)transform3D.m11;
transformMatrix[1] = (GLfloat)transform3D.m21;
transformMatrix[2] = (GLfloat)transform3D.m31;
transformMatrix[3] = (GLfloat)transform3D.m41;
transformMatrix[4] = (GLfloat)transform3D.m12;
transformMatrix[5] = (GLfloat)transform3D.m22;
transformMatrix[6] = (GLfloat)transform3D.m32;
transformMatrix[7] = (GLfloat)transform3D.m42;
transformMatrix[8] = (GLfloat)transform3D.m13;
transformMatrix[9] = (GLfloat)transform3D.m23;
transformMatrix[10] = (GLfloat)transform3D.m33;
transformMatrix[11] = (GLfloat)transform3D.m43;
transformMatrix[12] = (GLfloat)transform3D.m14;
transformMatrix[13] = (GLfloat)transform3D.m24;
transformMatrix[14] = (GLfloat)transform3D.m34;
transformMatrix[15] = (GLfloat)transform3D.m44;
glUniformMatrix4fv(transformMatrixUniform, 1, GL_FALSE, transformMatrix);

glActiveTexture(GL_TEXTURE1);

if (!bgraTexture) {
Expand All @@ -154,7 +178,7 @@ - (void)render:(CVPixelBufferRef)pixelBuffer{
glEnableVertexAttribArray(textureCoordinateAttribute);

glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, 0, 0, squareVertices);
glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, 0, 0, rotateRightTextureCoordinates);
glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, 0, 0, verticalFlipTextureCoordinates);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// ----------旋转图像为正向 绘制结束----------
Expand All @@ -171,6 +195,25 @@ - (void)render:(CVPixelBufferRef)pixelBuffer{

glUseProgram(program);

transform3D = CATransform3DMakeRotation(0, 0, 0, 1);
transformMatrix[0] = (GLfloat)transform3D.m11;
transformMatrix[1] = (GLfloat)transform3D.m21;
transformMatrix[2] = (GLfloat)transform3D.m31;
transformMatrix[3] = (GLfloat)transform3D.m41;
transformMatrix[4] = (GLfloat)transform3D.m12;
transformMatrix[5] = (GLfloat)transform3D.m22;
transformMatrix[6] = (GLfloat)transform3D.m32;
transformMatrix[7] = (GLfloat)transform3D.m42;
transformMatrix[8] = (GLfloat)transform3D.m13;
transformMatrix[9] = (GLfloat)transform3D.m23;
transformMatrix[10] = (GLfloat)transform3D.m33;
transformMatrix[11] = (GLfloat)transform3D.m43;
transformMatrix[12] = (GLfloat)transform3D.m14;
transformMatrix[13] = (GLfloat)transform3D.m24;
transformMatrix[14] = (GLfloat)transform3D.m34;
transformMatrix[15] = (GLfloat)transform3D.m44;
glUniformMatrix4fv(transformMatrixUniform, 1, GL_FALSE, transformMatrix);

CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(CGSizeMake(height, width), CGRectMake(0, 0, backingWidth, backingHeight));

CGFloat heightScaling, widthScaling;
Expand Down Expand Up @@ -207,7 +250,7 @@ - (void)render:(CVPixelBufferRef)pixelBuffer{

glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, 0, 0, squareVertices);

glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, 0, 0, verticalFlipTextureCoordinates);
glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, 0, 0, noRotationTextureCoordinates);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Expand Down

0 comments on commit f0941a4

Please sign in to comment.