diff --git a/Douyu/Base.lproj/Main.storyboard b/Douyu/Base.lproj/Main.storyboard index 003cb9b..2475f18 100644 --- a/Douyu/Base.lproj/Main.storyboard +++ b/Douyu/Base.lproj/Main.storyboard @@ -664,7 +664,7 @@ - + @@ -688,7 +688,7 @@ - + @@ -838,7 +838,7 @@ - + diff --git a/Douyu/Model/DYDanmuProvider.m b/Douyu/Model/DYDanmuProvider.m index 65358db..35d24f3 100644 --- a/Douyu/Model/DYDanmuProvider.m +++ b/Douyu/Model/DYDanmuProvider.m @@ -25,7 +25,6 @@ - (void)loadWithInfo:(DYRoomInfo *)roomInfo { _authSocket = [AuthSocket sharedInstance]; _authSocket.room = roomInfo.roomId; _authSocket.servers = roomInfo.servers; - [_authSocket connectSocketHost]; _danmuSocket = [DanmuSocket sharedInstance]; _danmuSocket.room = _authSocket.room; //weak处理防止block循环 @@ -35,8 +34,8 @@ - (void)loadWithInfo:(DYRoomInfo *)roomInfo { danmuSocket.groupID = groupID; [danmuSocket connectSocketHost]; }; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveMessageNotification:) name:@"kReceiveDYMessageNotification" object:nil]; + [_authSocket connectSocketHost]; } - (void)receiveMessageNotification:(NSNotification *)notification { @@ -78,6 +77,7 @@ - (void)receiveMessageNotification:(NSNotification *)notification { - (void)disconnect{ [_authSocket cutOffSocket]; [_danmuSocket cutOffSocket]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; } diff --git a/Douyu/Model/socket/AuthSocket.m b/Douyu/Model/socket/AuthSocket.m index b060ac8..db28709 100755 --- a/Douyu/Model/socket/AuthSocket.m +++ b/Douyu/Model/socket/AuthSocket.m @@ -89,6 +89,7 @@ - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)ta //断开链接 - (void)onSocketDidDisconnect:(AsyncSocket *)sock{ NSLog(@"---认证服务器断开---"); + self.combieData = nil; } @end diff --git a/Douyu/Model/socket/DanmuSocket.m b/Douyu/Model/socket/DanmuSocket.m index 41d1614..5b4bb85 100755 --- a/Douyu/Model/socket/DanmuSocket.m +++ b/Douyu/Model/socket/DanmuSocket.m @@ -40,14 +40,13 @@ - (void)setServerConfig{ } - (void)connectSocketHost{ - self.socket = [[AsyncSocket alloc]initWithDelegate:self]; + self.socket = [[AsyncSocket alloc] initWithDelegate:self]; NSError *error = nil; NSString *ip = @"openbarrage.douyutv.com"; UInt16 port = 8601; [self.socket connectToHost:ip onPort:port withTimeout:30 error:&error]; - } //心跳包 @@ -86,7 +85,7 @@ - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UIn //断开链接 - (void)onSocketDidDisconnect:(AsyncSocket *)sock{ NSLog(@"----弹幕服务器断开----"); - + self.combieData = nil; } diff --git a/Douyu/Model/socket/DouyuTVSocket.m b/Douyu/Model/socket/DouyuTVSocket.m index 38a193f..a2d83b6 100755 --- a/Douyu/Model/socket/DouyuTVSocket.m +++ b/Douyu/Model/socket/DouyuTVSocket.m @@ -19,7 +19,7 @@ - (void)setServerConfig{ //连接服务器 - (void)connectSocketHost{ - self.socket = [[AsyncSocket alloc]initWithDelegate:self]; + self.socket = [[AsyncSocket alloc] initWithDelegate:self]; [self setServerConfig]; NSError *error = nil; ServerModel *sevCfg = self.server[0]; @@ -34,7 +34,6 @@ - (void)cutOffSocket{ [self.socket writeData:logoutData withTimeout:30 tag:1]; [self.connectTimer invalidate]; [self.socket disconnectAfterWriting]; - } //心跳包 - (void)longConnectToSocket{ diff --git a/Douyu/PlayerViewController.h b/Douyu/PlayerViewController.h index 781379f..7acd200 100644 --- a/Douyu/PlayerViewController.h +++ b/Douyu/PlayerViewController.h @@ -14,9 +14,9 @@ @interface PlayerViewController : NSViewController -@property (assign) mpv_handle *mpv; -@property (weak) IBOutlet MpvClientOGLView *glView; -@property (strong) BarrageRenderer *barrageRenderer; +@property (assign, nonatomic) mpv_handle *mpv; +@property (weak, nonatomic) IBOutlet MpvClientOGLView *glView; +@property (strong, nonatomic) BarrageRenderer *barrageRenderer; - (void)loadPlayerWithInfo:(DYRoomInfo *)info withVideoQuality:(NSInteger)quality; - (void)destroyPlayer; diff --git a/Douyu/PlayerViewController.m b/Douyu/PlayerViewController.m index 948fd78..f9ea96f 100644 --- a/Douyu/PlayerViewController.m +++ b/Douyu/PlayerViewController.m @@ -13,7 +13,7 @@ #define RGB(r,g,b,a) [NSColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a] -#define ColorFromRGBHex(rgbValue,alphaValue) \ +#define ColorFromRGBHex(rgbValue,alphaValue) \ [NSColor colorWithRed:((float) ((rgbValue & 0xFF0000) >> 16)) / 255.0 \ green:((float) ((rgbValue & 0xFF00) >> 8)) / 255.0 \ blue:((float) (rgbValue & 0xFF)) / 255.0 \ @@ -24,10 +24,10 @@ @interface PlayerViewController () { NSTimer *hideCursorTimer; } -@property (weak) IBOutlet NSView *loadingView; -@property (strong) DYRoomInfo *roomInfo; -@property (strong) DYDanmuProvider *danmuProvider; -@property (strong) dispatch_queue_t queue; +@property (weak, nonatomic) IBOutlet NSView *loadingView; +@property (strong, nonatomic) DYRoomInfo *roomInfo; +@property (strong, nonatomic) DYDanmuProvider *danmuProvider; +@property (strong, nonatomic) dispatch_queue_t queue; @end @implementation PlayerViewController @@ -79,7 +79,6 @@ static void glupdate(void *ctx) } - (void)loadPlayerWithInfo:(DYRoomInfo *)info withVideoQuality:(NSInteger)quality { - self.queue = dispatch_queue_create("mpv.quene", DISPATCH_QUEUE_SERIAL); [self.loadingView setWantsLayer:YES]; [self.loadingView.layer setBackgroundColor:ColorFromRGBHex(0xecebeb, 1).CGColor]; self.roomInfo = info; @@ -131,18 +130,11 @@ - (void)hideCursor{ } - (void)loadDanmu { - dispatch_async(dispatch_get_main_queue(), ^{ - BarrageRenderer *renderer = [[BarrageRenderer alloc] init]; - [self.view setWantsLayer:YES]; - [renderer.view setFrame:NSMakeRect(0,0,self.view.frame.size.width,self.view.frame.size.height)]; - [renderer.view setAutoresizingMask:NSViewMaxYMargin|NSViewMinXMargin|NSViewWidthSizable|NSViewMaxXMargin|NSViewHeightSizable|NSViewMinYMargin]; - [self.view addSubview:renderer.view positioned:NSWindowAbove relativeTo:nil]; - [renderer start]; - self.barrageRenderer = renderer; - self.danmuProvider = [[DYDanmuProvider alloc] init]; - self.danmuProvider.delegate = self; - [self.danmuProvider loadWithInfo:self.roomInfo]; - }); + self.danmuProvider = [[DYDanmuProvider alloc] init]; + self.danmuProvider.delegate = self; + [self.danmuProvider loadWithInfo:self.roomInfo]; + [self.barrageRenderer start]; + [self.view addSubview:self.barrageRenderer.view positioned:NSWindowAbove relativeTo:nil]; } - (void)onNewMessage:(NSString *)cmContent :(NSString *)userName :(int)ftype :(int)fsize :(NSColor *)color{ @@ -308,12 +300,9 @@ - (void) mpv_cleanup { mpv_set_wakeup_callback(self.mpv, NULL,NULL); mpv_opengl_cb_uninit_gl(self.glView.mpvGL); - mpv_detach_destroy(self.mpv); self.glView.mpvGL = nil; + mpv_detach_destroy(self.mpv); self.mpv = nil; - [self.glView clearGLContext]; - [self.glView removeFromSuperview]; - self.glView = nil; } - (void)destroyPlayer{ @@ -322,9 +311,7 @@ - (void)destroyPlayer{ hideCursorTimer = nil; [self.danmuProvider disconnect]; [self.barrageRenderer stop]; - [self.barrageRenderer.view.layer removeAllAnimations]; [self.barrageRenderer.view removeFromSuperview]; - self.barrageRenderer = nil; [self mpv_cleanup]; } @@ -354,6 +341,26 @@ - (void)onKeepAspect:(int)keep{ }); } +#pragma mark - lazy init + +- (BarrageRenderer *)barrageRenderer { + if (!_barrageRenderer) { + BarrageRenderer *renderer = [[BarrageRenderer alloc] init]; + [self.view setWantsLayer:YES]; + [renderer.view setFrame:NSMakeRect(0,0,self.view.frame.size.width,self.view.frame.size.height)]; + [renderer.view setAutoresizingMask:NSViewMaxYMargin|NSViewMinXMargin|NSViewWidthSizable|NSViewMaxXMargin|NSViewHeightSizable|NSViewMinYMargin]; + [renderer.view setWantsLayer:YES]; + _barrageRenderer = renderer; + } + return _barrageRenderer; +} + +- (dispatch_queue_t)queue { + if (!_queue) { + _queue = dispatch_queue_create("mpv.quene", DISPATCH_QUEUE_SERIAL); + } + return _queue; +} @end diff --git a/Douyu/ViewController.h b/Douyu/ViewController.h index 1b4dfb6..2678443 100644 --- a/Douyu/ViewController.h +++ b/Douyu/ViewController.h @@ -10,9 +10,9 @@ @interface ViewController : NSViewController -@property (weak) IBOutlet NSButton *playButton; -@property (weak) IBOutlet NSPopUpButton *videoQualityButton; -@property (weak) IBOutlet NSComboBox *roomComboBox; +@property (weak, nonatomic) IBOutlet NSButton *playButton; +@property (weak, nonatomic) IBOutlet NSPopUpButton *videoQualityButton; +@property (weak, nonatomic) IBOutlet NSComboBox *roomComboBox; - (void)reset; diff --git a/Douyu/ViewController.m b/Douyu/ViewController.m index 3d9784a..577271a 100644 --- a/Douyu/ViewController.m +++ b/Douyu/ViewController.m @@ -15,10 +15,10 @@ @interface ViewController () playingActivity; -@property (strong) NSArray *roomHistory; +@property (strong, nonatomic) NSWindowController *playerWindowController; +@property (weak, nonatomic) PlayerViewController *playerViewController; +@property (strong, nonatomic) id playingActivity; +@property (strong, nonatomic) NSArray *roomHistory; @end @@ -28,7 +28,6 @@ - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. - [self.view.window center]; NSInteger videoQuality = [[NSUserDefaults standardUserDefaults] integerForKey:@"videoQuality"]; [self.videoQualityButton selectItemAtIndex:videoQuality]; self.roomComboBox.dataSource = self; @@ -106,15 +105,11 @@ - (void)playWithRoomString:(NSString *)room { [self showError:@"主播不在线"]; return; } - NSWindowController *playerWindowController = [[NSStoryboard storyboardWithName:@"Main" bundle:nil] instantiateControllerWithIdentifier:@"PlayerWindowController"]; - [playerWindowController.window center]; - [playerWindowController.window makeKeyAndOrderFront:nil]; - [playerWindowController.window setDelegate:self]; - self.playerWindowController = playerWindowController; - PlayerViewController *playerViewController = (PlayerViewController *)playerWindowController.contentViewController; + PlayerViewController *playerViewController = (PlayerViewController *)self.playerWindowController.contentViewController; [playerViewController loadPlayerWithInfo:roomInfo withVideoQuality:self.videoQualityButton.indexOfSelectedItem]; self.playerViewController = playerViewController; self.playingActivity = [[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityIdleDisplaySleepDisabled reason:@"playing video"]; + [self.playerWindowController.window makeKeyAndOrderFront:nil]; [self.view.window performClose:nil]; } @@ -148,10 +143,20 @@ - (void)showError:(NSString *)string { - (void)windowWillClose:(NSNotification *)notification{ [self.playerViewController destroyPlayer]; - self.playerWindowController = nil; [[NSProcessInfo processInfo] endActivity:self.playingActivity]; [self reset]; [self.view.window makeKeyAndOrderFront:nil]; } +#pragma mark - lazy init + +- (NSWindowController *)playerWindowController { + if (!_playerWindowController) { + NSWindowController *playerWindowController = [[NSStoryboard storyboardWithName:@"Main" bundle:nil] instantiateControllerWithIdentifier:@"PlayerWindowController"]; + [playerWindowController.window setDelegate:self]; + _playerWindowController = playerWindowController; + } + return _playerWindowController; +} + @end