Skip to content

Commit

Permalink
Fixed: change logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
KWANG HYOUN KIM committed Aug 9, 2019
1 parent de572ef commit 0522963
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 29 deletions.
12 changes: 8 additions & 4 deletions DLGPlayer/DLGPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ - (id)init {
}

- (void)dealloc {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"DLGPlayer dealloc");
}
}
Expand Down Expand Up @@ -292,8 +292,8 @@ - (void)readFrame {

while (self.playing && !self.closing && !self.decoder.isEOF && !self.requestSeek) {
@autoreleasepool {
if (DLGPlayerDebugEnabled) {
NSLog(@"DLGPlayer bufferedDuration: %f, tempDuration: %f, maxBufferDuration: %f -> ", self.bufferedDuration, tempDuration, self.maxBufferDuration);
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"DLGPlayer bufferedDuration: %f, tempDuration: %f, maxBufferDuration: %f", self.bufferedDuration, tempDuration, self.maxBufferDuration);
}

if (self.bufferedDuration + tempDuration > self.maxBufferDuration) {
Expand All @@ -308,7 +308,7 @@ - (void)readFrame {
[self.aframes removeAllObjects];
dispatch_semaphore_signal(self.aFramesLock);
}
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"DLGPlayer drop frames beacuse buffer duration is over than max duration.");
}
} else {
Expand Down Expand Up @@ -476,6 +476,10 @@ - (void)render {

// Sync audio with video

if (DLGPlayerUtils.debugEnabled) {
NSLog(@"DLGPlayer render -> frame.duration: %f", frame.duration);
}

double syncTime = [self syncTime];
NSTimeInterval t = MAX(frame.duration + syncTime, 0.01);

Expand Down
4 changes: 2 additions & 2 deletions DLGPlayer/DLGPlayerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ - (void)notifyPlayerError:(NSNotification *)notif {
strongSelf.status = DLGPlayerStatusNone;
});

if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"Player decoder error: %@", error);
}
} else if ([error.domain isEqualToString:DLGPlayerErrorDomainAudioManager]) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"Player audio error: %@", error);
}
// I am not sure what will cause the audio error,
Expand Down
4 changes: 2 additions & 2 deletions DLGPlayer/DLGSimplePlayerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ - (void)notifyPlayerError:(NSNotification *)notif {
strongSelf.status = DLGPlayerStatusNone;
});

if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"Player decoder error: %@", error);
}
} else if ([error.domain isEqualToString:DLGPlayerErrorDomainAudioManager]) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"Player audio error: %@", error);
}
}
Expand Down
8 changes: 4 additions & 4 deletions DLGPlayer/codec/DLGPlayerAudioManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ - (void)initVars {
}

- (void)dealloc {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"DLGPlayerAudioManager dealloc");
}
[self unregisterNotifications];
Expand Down Expand Up @@ -113,14 +113,14 @@ - (BOOL)open:(NSError **)error {
}

if (![session setPreferredIOBufferDuration:_bufferDuration error:&rawError]) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"setPreferredIOBufferDuration: %.4f, error: %@", _bufferDuration, rawError);
}
}

double prefferedSampleRate = PREFERRED_SAMPLE_RATE;
if (![session setPreferredSampleRate:prefferedSampleRate error:&rawError]) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"setPreferredSampleRate: %.4f, error: %@", prefferedSampleRate, rawError);
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ - (BOOL)initAudioUnitWithSampleRate:(double)sampleRate andRenderCallback:(AURend

streamDescr.mSampleRate = sampleRate;
status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &streamDescr, size);
if (status != noErr && DLGPlayerDebugEnabled) {
if (status != noErr && DLGPlayerUtils.debugEnabled) {
NSLog(@"FAILED to set audio sample rate: %f, error: %d", sampleRate, (int)status);
}

Expand Down
20 changes: 10 additions & 10 deletions DLGPlayer/codec/DLGPlayerDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ @interface DLGPlayerDecoder () {
@implementation DLGPlayerDecoder

- (void)dealloc {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"DLGPlayerDecoder dealloc");
}
}
Expand Down Expand Up @@ -385,7 +385,7 @@ - (NSArray *)readFrames {
if (ret < 0) {
if (ret == AVERROR_EOF) self.isEOF = YES;
char *e = av_err2str(ret);
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"read frame error: %s", e);
}
break;
Expand Down Expand Up @@ -465,7 +465,7 @@ - (NSArray *)readFrames {
- (NSArray<DLGPlayerVideoFrame *> *)handleVideoPacket:(AVPacket *)packet byContext:(AVCodecContext *)context andFrame:(AVFrame *)frame andSwsContext:(struct SwsContext *)swsctx andSwsFrame:(AVFrame *)swsframe {
int ret = avcodec_send_packet(context, packet);
if (ret != 0) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"avcodec_send_packet: %d", ret);
}
return nil;
Expand All @@ -477,7 +477,7 @@ - (NSArray *)readFrames {
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
break;
} else if (ret < 0) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"avcodec_receive_frame: %d", ret);
}
break;
Expand Down Expand Up @@ -536,7 +536,7 @@ - (NSArray *)readFrames {
- (NSArray<DLGPlayerAudioFrame *> *)handleAudioPacket:(AVPacket *)packet byContext:(AVCodecContext *)context andFrame:(AVFrame *)frame andSwrContext:(SwrContext *)swrctx andSwrBuffer:(void **)swrbuf andSwrBufferSize:(int *)swrbufsize {
int ret = avcodec_send_packet(context, packet);
if (ret != 0) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"avcodec_send_packet: %d", ret);
}
return nil;
Expand All @@ -548,7 +548,7 @@ - (NSArray *)readFrames {
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
break;
} else if (ret < 0) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"avcodec_receive_frame: %d", ret);
}
break;
Expand Down Expand Up @@ -578,7 +578,7 @@ - (NSArray *)readFrames {
Byte *o[2] = { *swrbuf, 0 };
samplesPerChannel = swr_convert(swrctx, o, samples, (const uint8_t **)frame->data, frame->nb_samples);
if (samplesPerChannel < 0) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"failed to resample audio");
}
return nil;
Expand All @@ -587,7 +587,7 @@ - (NSArray *)readFrames {
data = *swrbuf;
} else {
if (context->sample_fmt != AV_SAMPLE_FMT_S16) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"invalid audio format");
}
return nil;
Expand Down Expand Up @@ -629,7 +629,7 @@ - (void)seek:(double)position {
avcodec_flush_buffers(m_pVideoCodecContext);
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval dt = end - start;
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"seek video: %.4f, start: %.4f, end: %.4f", dt, start, end);
}
} else if (_hasAudio) {
Expand All @@ -639,7 +639,7 @@ - (void)seek:(double)position {
avcodec_flush_buffers(m_pAudioCodecContext);
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval dt = end - start;
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"seek audio: %.4f, start: %.4f, end: %.4f", dt, start, end);
}
}
Expand Down
4 changes: 2 additions & 2 deletions DLGPlayer/common/DLGPlayerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

#import <Foundation/Foundation.h>

static BOOL DLGPlayerDebugEnabled = NO;

@interface DLGPlayerUtils : NSObject

+ (BOOL)createError:(NSError **)error withDomain:(NSString *)domain andCode:(NSInteger)code andMessage:(NSString *)message;
+ (BOOL)createError:(NSError **)error withDomain:(NSString *)domain andCode:(NSInteger)code andMessage:(NSString *)message andRawError:(NSError *)rawError;
+ (NSString *)localizedString:(NSString *)name;
+ (NSString *)durationStringFromSeconds:(int)seconds;
+ (BOOL)debugEnabled;
+ (void)setDebugEnabled:(BOOL)enabled;

@end
11 changes: 11 additions & 0 deletions DLGPlayer/common/DLGPlayerUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "DLGPlayerUtils.h"
#import "DLGPlayerDef.h"

static BOOL debugEnabled = NO;

@implementation DLGPlayerUtils

+ (BOOL)createError:(NSError **)error withDomain:(NSString *)domain andCode:(NSInteger)code andMessage:(NSString *)message {
Expand Down Expand Up @@ -51,4 +53,13 @@ + (NSString *)durationStringFromSeconds:(int)seconds {
return ms;
}

+ (BOOL)debugEnabled {
return debugEnabled;
}

+ (void)setDebugEnabled:(BOOL)enabled {
debugEnabled = enabled;
}


@end
10 changes: 5 additions & 5 deletions DLGPlayer/view/DLGPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ - (void)createGLProgram {
// Create program
GLuint program = glCreateProgram();
if (program == 0) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"FAILED to create program.");
}
return;
Expand Down Expand Up @@ -212,7 +212,7 @@ - (void)createGLProgram {
if (length > 1) {
char *log = malloc(sizeof(char) * length);
glGetProgramInfoLog(program, length, NULL, log);
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"FAILED to link program, error: %s", log);
}
free(log);
Expand Down Expand Up @@ -424,7 +424,7 @@ + (GLuint)loadShader:(GLenum)type withString:(NSString *)shaderString {
// 1. Create shader
GLuint shader = glCreateShader(type);
if (shader == 0) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"FAILED to create shader.");
}
return 0;
Expand All @@ -447,7 +447,7 @@ + (GLuint)loadShader:(GLenum)type withString:(NSString *)shaderString {
if (length > 1) {
char *log = malloc(sizeof(char) * length);
glGetShaderInfoLog(shader, length, NULL, log);
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"FAILED to compile shader, error: %s", log);
}
free(log);
Expand All @@ -463,7 +463,7 @@ + (GLuint)loadShader:(GLenum)type withFile:(NSString *)shaderFile {
NSError *error = nil;
NSString *shaderString = [NSString stringWithContentsOfFile:shaderFile encoding:NSUTF8StringEncoding error:&error];
if (shaderString == nil) {
if (DLGPlayerDebugEnabled) {
if (DLGPlayerUtils.debugEnabled) {
NSLog(@"FAILED to load shader file: %@, Error: %@", shaderFile, error);
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions DLGPlayerDemo/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class RootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// DLGPlayerUtils.setDebugEnabled(true)

// navigationItem.leftBarButtonItem = .init(title: "close", style: .plain, target: self, action: #selector(leftBarButtonItemClicked))
}
override func viewWillAppear(_ animated: Bool) {
Expand Down

0 comments on commit 0522963

Please sign in to comment.