Skip to content

Commit

Permalink
demo优化,添加视频信息。
Browse files Browse the repository at this point in the history
  • Loading branch information
loyinglin committed Dec 8, 2017
1 parent 8ca1ad7 commit 3b91501
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
FDF4F40A1FD7E89300C711AF /* test.mov in Resources */ = {isa = PBXBuildFile; fileRef = FDF4F4031FD7E89300C711AF /* test.mov */; };
FDF4F40B1FD7E89300C711AF /* test.pcm in Resources */ = {isa = PBXBuildFile; fileRef = FDF4F4041FD7E89300C711AF /* test.pcm */; };
FDF4F4161FD8160D00C711AF /* LYPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = FDF4F4141FD8160D00C711AF /* LYPlayer.m */; };
FDF4F4391FDA403E00C711AF /* LYOpenGLView.m in Sources */ = {isa = PBXBuildFile; fileRef = FDF4F4361FDA403E00C711AF /* LYOpenGLView.m */; };
FDF4F43A1FDA403E00C711AF /* Shader.fsh in Resources */ = {isa = PBXBuildFile; fileRef = FDF4F4371FDA403E00C711AF /* Shader.fsh */; };
FDF4F43B1FDA403E00C711AF /* Shader.vsh in Resources */ = {isa = PBXBuildFile; fileRef = FDF4F4381FDA403E00C711AF /* Shader.vsh */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -43,6 +46,10 @@
FDF4F4041FD7E89300C711AF /* test.pcm */ = {isa = PBXFileReference; lastKnownFileType = file; path = test.pcm; sourceTree = "<group>"; };
FDF4F4141FD8160D00C711AF /* LYPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LYPlayer.m; sourceTree = "<group>"; };
FDF4F4151FD8160D00C711AF /* LYPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LYPlayer.h; sourceTree = "<group>"; };
FDF4F4351FDA403E00C711AF /* LYOpenGLView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LYOpenGLView.h; sourceTree = "<group>"; };
FDF4F4361FDA403E00C711AF /* LYOpenGLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LYOpenGLView.m; sourceTree = "<group>"; };
FDF4F4371FDA403E00C711AF /* Shader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = Shader.fsh; sourceTree = "<group>"; };
FDF4F4381FDA403E00C711AF /* Shader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = Shader.vsh; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -75,6 +82,7 @@
FDF4F3E61FD7E84A00C711AF /* LearnAudioUnit */ = {
isa = PBXGroup;
children = (
FDF4F4341FDA403E00C711AF /* GL */,
FDF4F3FD1FD7E89300C711AF /* CommonResource */,
FDF4F4151FD8160D00C711AF /* LYPlayer.h */,
FDF4F4141FD8160D00C711AF /* LYPlayer.m */,
Expand Down Expand Up @@ -106,6 +114,17 @@
path = ../../../CommonResource;
sourceTree = "<group>";
};
FDF4F4341FDA403E00C711AF /* GL */ = {
isa = PBXGroup;
children = (
FDF4F4351FDA403E00C711AF /* LYOpenGLView.h */,
FDF4F4361FDA403E00C711AF /* LYOpenGLView.m */,
FDF4F4371FDA403E00C711AF /* Shader.fsh */,
FDF4F4381FDA403E00C711AF /* Shader.vsh */,
);
path = GL;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -165,12 +184,14 @@
buildActionMask = 2147483647;
files = (
FDF4F4051FD7E89300C711AF /* abc.aac in Resources */,
FDF4F43A1FDA403E00C711AF /* Shader.fsh in Resources */,
FDF4F3F41FD7E84A00C711AF /* LaunchScreen.storyboard in Resources */,
FDF4F4061FD7E89300C711AF /* abc.m4a in Resources */,
FDF4F4091FD7E89300C711AF /* abc.pcm in Resources */,
FDF4F40B1FD7E89300C711AF /* test.pcm in Resources */,
FDF4F4071FD7E89300C711AF /* abc.mp3 in Resources */,
FDF4F3F11FD7E84A00C711AF /* Assets.xcassets in Resources */,
FDF4F43B1FDA403E00C711AF /* Shader.vsh in Resources */,
FDF4F40A1FD7E89300C711AF /* test.mov in Resources */,
FDF4F3EF1FD7E84A00C711AF /* Main.storyboard in Resources */,
FDF4F4081FD7E89300C711AF /* abc.mp4 in Resources */,
Expand All @@ -185,6 +206,7 @@
buildActionMask = 2147483647;
files = (
FDF4F3EC1FD7E84A00C711AF /* ViewController.m in Sources */,
FDF4F4391FDA403E00C711AF /* LYOpenGLView.m in Sources */,
FDF4F3F71FD7E84A00C711AF /* main.m in Sources */,
FDF4F3E91FD7E84A00C711AF /* AppDelegate.m in Sources */,
FDF4F4161FD8160D00C711AF /* LYPlayer.m in Sources */,
Expand Down Expand Up @@ -322,6 +344,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 6X5L546LN9;
INFOPLIST_FILE = LearnAudioUnit/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -336,6 +359,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 6X5L546LN9;
INFOPLIST_FILE = LearnAudioUnit/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -19,29 +20,48 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8i6-lG-94i">
<rect key="frame" x="172" y="205" width="31" height="30"/>
<rect key="frame" x="172" y="120" width="31" height="30"/>
<state key="normal" title="播放"/>
<connections>
<action selector="onClick:" destination="BYZ-38-t0r" eventType="touchUpInside" id="SpV-xs-BjU"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="使用AudioUnit播放CMSampleBuffer" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="60O-QF-GSf">
<rect key="frame" x="50" y="155" width="275" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="使用AudioUnit播放CMSampleBuffer的音频" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="60O-QF-GSf">
<rect key="frame" x="24" y="70" width="327" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="TyD-E1-AzN" customClass="LYOpenGLView">
<rect key="frame" x="0.0" y="292" width="375" height="375"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" secondItem="TyD-E1-AzN" secondAttribute="height" multiplier="1:1" id="V2U-Hl-R9y"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="使用OpenGL播放CMSampleBuffer的视频" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="lj5-uC-YIv">
<rect key="frame" x="30" y="251" width="315" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="TyD-E1-AzN" secondAttribute="bottom" id="36J-Mn-3Tq"/>
<constraint firstItem="8i6-lG-94i" firstAttribute="top" secondItem="60O-QF-GSf" secondAttribute="top" constant="50" id="8ro-4v-0nT"/>
<constraint firstItem="TyD-E1-AzN" firstAttribute="top" secondItem="lj5-uC-YIv" secondAttribute="bottom" constant="20" id="FyA-sO-Ugz"/>
<constraint firstItem="60O-QF-GSf" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="Hud-Qh-76U"/>
<constraint firstItem="60O-QF-GSf" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="135" id="gla-p5-R9P"/>
<constraint firstItem="lj5-uC-YIv" firstAttribute="centerX" secondItem="TyD-E1-AzN" secondAttribute="centerX" id="S9O-nc-mTS"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="TyD-E1-AzN" secondAttribute="trailing" id="Uk4-Gk-D6g"/>
<constraint firstItem="TyD-E1-AzN" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="dzY-93-rnX"/>
<constraint firstItem="60O-QF-GSf" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="50" id="gla-p5-R9P"/>
<constraint firstItem="8i6-lG-94i" firstAttribute="centerX" secondItem="60O-QF-GSf" secondAttribute="centerX" id="hER-sa-IVf"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="mGLView" destination="TyD-E1-AzN" id="7kj-qR-9hG"/>
<outlet property="mPlayButton" destination="8i6-lG-94i" id="K6z-sR-5Yc"/>
</connections>
</viewController>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static OSStatus PlayCallback(void *inRefCon,
memcpy(ioData->mBuffers[i].mData, player->bufferList->mBuffers[i].mData + player->readedSize, ioData->mBuffers[i].mDataByteSize);
player->readedSize += ioData->mBuffers[i].mDataByteSize;
}
NSLog(@"out size: %d", ioData->mBuffers[0].mDataByteSize);
// NSLog(@"out size: %d", ioData->mBuffers[0].mDataByteSize);
}
return noErr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/ALAssetsLibrary.h>
#import "LYPlayer.h"
#import "LYOpenGLView.h"

@interface ViewController () <LYPlayerDelegate>

Expand All @@ -21,12 +22,20 @@ @interface ViewController () <LYPlayerDelegate>
@property (nonatomic , strong) AVAssetReader *mReader;
@property (nonatomic , strong) AVAssetReaderTrackOutput *mReaderAudioTrackOutput;
@property (nonatomic , assign) AudioStreamBasicDescription fileFormat;
// timer
@property (nonatomic , strong) CADisplayLink *mDisplayLink;


@property (nonatomic, strong) LYPlayer *mLYPlayer;
@property (nonatomic, assign) CMBlockBufferRef blockBufferOut;
@property (nonatomic, assign) AudioBufferList audioBufferList;


// gl
@property (nonatomic, strong) IBOutlet LYOpenGLView *mGLView;
@property (nonatomic , strong) AVAssetReaderTrackOutput *mReaderVideoTrackOutput;
@property (nonatomic , strong) CADisplayLink *mDisplayLink;

@property (nonatomic, assign) long mAudioTimeStamp;
@property (nonatomic, assign) long mVideoTimeStamp;
@end

@implementation ViewController
Expand All @@ -35,13 +44,21 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self.mGLView setupGL];
[self.view addSubview:self.mGLView];

self.mDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkCallback:)];
// self.mDisplayLink.frameInterval = 2; //FPS=30
[[self mDisplayLink] addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[self mDisplayLink] setPaused:YES];

[self loadAsset];
}


- (void)loadAsset {
NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:[[NSBundle mainBundle] URLForResource:@"abc" withExtension:@"mp4"] options:inputOptions];
AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mov"] options:inputOptions];
__weak typeof(self) weakSelf = self;
[inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
Expand All @@ -63,7 +80,6 @@ - (AVAssetReader*)createAssetReader
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:self.mAsset error:&error];

NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];

[outputSettings setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
[outputSettings setObject:@(16) forKey:AVLinearPCMBitDepthKey];
[outputSettings setObject:@(NO) forKey:AVLinearPCMIsBigEndianKey];
Expand Down Expand Up @@ -97,7 +113,11 @@ - (AVAssetReader*)createAssetReader
}
}


outputSettings = [NSMutableDictionary dictionary];
[outputSettings setObject:@(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) forKey:(id)kCVPixelBufferPixelFormatTypeKey];
self.mReaderVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[self.mAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] outputSettings:outputSettings];
self.mReaderVideoTrackOutput.alwaysCopiesSampleData = NO;
[assetReader addOutput:self.mReaderVideoTrackOutput];

return assetReader;
}
Expand All @@ -116,6 +136,8 @@ - (void)startPlay
else {
NSLog(@"Start reading success.");
[self.mLYPlayer play];
[self.mDisplayLink setPaused:NO];
self.mAudioTimeStamp = self.mVideoTimeStamp = 0;
}
}

Expand All @@ -137,9 +159,9 @@ - (void)didReceiveMemoryWarning {
- (AudioBufferList *)onRequestAudioData {
CMSampleBufferRef sampleBuffer = [self.mReaderAudioTrackOutput copyNextSampleBuffer];
size_t bufferListSizeNeededOut = 0;
if (self.blockBufferOut != nil) {
CFRelease(self.blockBufferOut);
}
// if (self.blockBufferOut != nil) {
// CFRelease(self.blockBufferOut);
// }
if (!sampleBuffer) {
return NULL;
}
Expand All @@ -154,12 +176,44 @@ - (AudioBufferList *)onRequestAudioData {
if (err) {
NSLog(@"CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer error: %d", err);
}

CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
long timeStamp = (1000 * presentationTimeStamp.value) / presentationTimeStamp.timescale;
NSLog(@"audio timestamp %lu", timeStamp);
self.mAudioTimeStamp = timeStamp;


return &_audioBufferList;
}


- (void)displayLinkCallback:(CADisplayLink *)sender {
// if (self.mVideoTimeStamp < self.mAudioTimeStamp) {
[self renderVideo];
// }
}

- (void)renderVideo {
CMSampleBufferRef videoSamepleBuffer = [self.mReaderVideoTrackOutput copyNextSampleBuffer];

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(videoSamepleBuffer);
if (pixelBuffer) {
[self.mGLView displayPixelBuffer:pixelBuffer];


CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(videoSamepleBuffer);
long timeStamp = (1000 * presentationTimeStamp.value) / presentationTimeStamp.timescale;
NSLog(@"video timestamp %lu", timeStamp);
self.mVideoTimeStamp = timeStamp;

CFRelease(pixelBuffer);
}
}


- (void)onPlayToEnd:(LYPlayer *)player {
self.mPlayButton.enabled = YES;
// self.mDisplayLink.paused = YES;
}

- (void)printAudioStreamBasicDescription:(AudioStreamBasicDescription)asbd {
Expand Down

0 comments on commit 3b91501

Please sign in to comment.