Use video source timestamp instead of system clock.

This commit is contained in:
aaaron7 2020-11-06 10:50:08 +08:00
parent a717ac0456
commit cf6b98537f
3 changed files with 19 additions and 6 deletions

View File

@ -18,6 +18,7 @@
#import "mediapipe/objc/MPPGraph.h" #import "mediapipe/objc/MPPGraph.h"
#import "mediapipe/objc/MPPLayerRenderer.h" #import "mediapipe/objc/MPPLayerRenderer.h"
#import "mediapipe/objc/MPPPlayerInputSource.h" #import "mediapipe/objc/MPPPlayerInputSource.h"
#import "mediapipe/objc/MPPTimestampConverter.h"
typedef NS_ENUM(NSInteger, MediaPipeDemoSourceMode) { typedef NS_ENUM(NSInteger, MediaPipeDemoSourceMode) {
MediaPipeDemoSourceCamera, MediaPipeDemoSourceCamera,
@ -60,4 +61,7 @@ typedef NS_ENUM(NSInteger, MediaPipeDemoSourceMode) {
// Graph output stream. // Graph output stream.
@property(nonatomic) const char* graphOutputStream; @property(nonatomic) const char* graphOutputStream;
// Convert video time to mediapipe internal Timestamp
@property(nonatomic) MPPTimestampConverter* timestampConverter;
@end @end

View File

@ -76,6 +76,7 @@ static const char* kVideoQueueLabel = "com.google.mediapipe.example.videoQueue";
self.renderer.layer.frame = self.liveView.layer.bounds; self.renderer.layer.frame = self.liveView.layer.bounds;
[self.liveView.layer addSublayer:self.renderer.layer]; [self.liveView.layer addSublayer:self.renderer.layer];
self.renderer.frameScaleMode = MPPFrameScaleModeFillAndCrop; self.renderer.frameScaleMode = MPPFrameScaleModeFillAndCrop;
self.timestampConverter = [[MPPTimestampConverter alloc] init];
dispatch_queue_attr_t qosAttribute = dispatch_queue_attr_make_with_qos_class( dispatch_queue_attr_t qosAttribute = dispatch_queue_attr_make_with_qos_class(
DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, /*relative_priority=*/0); DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, /*relative_priority=*/0);
@ -173,7 +174,8 @@ static const char* kVideoQueueLabel = "com.google.mediapipe.example.videoQueue";
[self.mediapipeGraph sendPixelBuffer:imageBuffer [self.mediapipeGraph sendPixelBuffer:imageBuffer
intoStream:self.graphInputStream intoStream:self.graphInputStream
packetType:MPPPacketTypePixelBuffer]; packetType:MPPPacketTypePixelBuffer
timestamp:[self.timestampConverter timestampForMediaTime:timestamp]];
} }
#pragma mark - MPPGraphDelegate methods #pragma mark - MPPGraphDelegate methods

View File

@ -45,6 +45,8 @@
/// Number of frames currently being processed by the graph. /// Number of frames currently being processed by the graph.
std::atomic<int32_t> _framesInFlight; std::atomic<int32_t> _framesInFlight;
/// Used as a sequential timestamp for MediaPipe.
mediapipe::Timestamp _frameTimestamp;
int64 _frameNumber; int64 _frameNumber;
// Graph config modified to expose requested output streams. // Graph config modified to expose requested output streams.
@ -367,16 +369,21 @@ void CallFrameDelegate(void* wrapperVoid, const std::string& streamName,
timestamp:timestamp timestamp:timestamp
allowOverwrite:NO]; allowOverwrite:NO];
} }
- (BOOL)sendPixelBuffer:(CVPixelBufferRef)imageBuffer - (BOOL)sendPixelBuffer:(CVPixelBufferRef)imageBuffer
intoStream:(const std::string&)inputName intoStream:(const std::string&)inputName
packetType:(MPPPacketType)packetType { packetType:(MPPPacketType)packetType {
uint64_t us = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock:: _GTMDevAssert(_frameTimestamp < mediapipe::Timestamp::Done(),
now().time_since_epoch()).count(); @"Trying to send frame after stream is done.");
if (_frameTimestamp < mediapipe::Timestamp::Min()) {
_frameTimestamp = mediapipe::Timestamp::Min();
} else {
_frameTimestamp++;
}
return [self sendPixelBuffer:imageBuffer return [self sendPixelBuffer:imageBuffer
intoStream:inputName intoStream:inputName
packetType:packetType packetType:packetType
timestamp:mediapipe::Timestamp(us)]; timestamp:_frameTimestamp];
} }
- (void)debugPrintGlInfo { - (void)debugPrintGlInfo {
@ -398,4 +405,4 @@ void CallFrameDelegate(void* wrapperVoid, const std::string& streamName,
NSLog(@"%@", oneExtension); NSLog(@"%@", oneExtension);
} }
@end @end