Skip to content

Commit

Permalink
弹幕服务优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Grayon committed Dec 29, 2017
1 parent 038db64 commit b34f10c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Douyu/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@
<scene sceneID="R2V-B0-nI4">
<objects>
<windowController id="B8D-0N-5wS" sceneMemberID="viewController">
<window key="window" title="斗鱼播放器" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA">
<window key="window" title="斗鱼播放器" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="320" height="85"/>
Expand All @@ -688,7 +688,7 @@
<objects>
<customObject id="B6E-pG-5mQ" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<windowController storyboardIdentifier="PlayerWindowController" id="RsE-Gh-Bc5" sceneMemberID="viewController">
<window key="window" title="Douyu" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" oneShot="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="MbN-wX-phc" customClass="PlayerWindow">
<window key="window" title="Douyu" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="MbN-wX-phc" customClass="PlayerWindow">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowCollectionBehavior key="collectionBehavior" fullScreenPrimary="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
Expand Down Expand Up @@ -838,7 +838,7 @@
</viewController>
<customObject id="4JE-uo-EyL" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="564.5" y="726"/>
<point key="canvasLocation" x="615" y="757"/>
</scene>
</scenes>
<resources>
Expand Down
4 changes: 2 additions & 2 deletions Douyu/Model/DYDanmuProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -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循环
Expand All @@ -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 {
Expand Down Expand Up @@ -78,6 +77,7 @@ - (void)receiveMessageNotification:(NSNotification *)notification {
- (void)disconnect{
[_authSocket cutOffSocket];
[_danmuSocket cutOffSocket];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}


Expand Down
1 change: 1 addition & 0 deletions Douyu/Model/socket/AuthSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)ta
//断开链接
- (void)onSocketDidDisconnect:(AsyncSocket *)sock{
NSLog(@"---认证服务器断开---");
self.combieData = nil;
}

@end
5 changes: 2 additions & 3 deletions Douyu/Model/socket/DanmuSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

}

//心跳包
Expand Down Expand Up @@ -86,7 +85,7 @@ - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UIn
//断开链接
- (void)onSocketDidDisconnect:(AsyncSocket *)sock{
NSLog(@"----弹幕服务器断开----");

self.combieData = nil;
}


Expand Down
3 changes: 1 addition & 2 deletions Douyu/Model/socket/DouyuTVSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -34,7 +34,6 @@ - (void)cutOffSocket{
[self.socket writeData:logoutData withTimeout:30 tag:1];
[self.connectTimer invalidate];
[self.socket disconnectAfterWriting];

}
//心跳包
- (void)longConnectToSocket{
Expand Down
6 changes: 3 additions & 3 deletions Douyu/PlayerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 31 additions & 24 deletions Douyu/PlayerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -24,10 +24,10 @@ @interface PlayerViewController ()<DYDanmuProviderDelegate> {
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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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];
}

Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions Douyu/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 17 additions & 12 deletions Douyu/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ @interface ViewController ()<NSWindowDelegate,NSComboBoxDataSource,NSComboBoxDel
NSTimer *resizingTimer;
}

@property (strong) NSWindowController *playerWindowController;
@property (weak) PlayerViewController *playerViewController;
@property (strong) id <NSObject> playingActivity;
@property (strong) NSArray<DYRoomHistoryData *> *roomHistory;
@property (strong, nonatomic) NSWindowController *playerWindowController;
@property (weak, nonatomic) PlayerViewController *playerViewController;
@property (strong, nonatomic) id <NSObject> playingActivity;
@property (strong, nonatomic) NSArray<DYRoomHistoryData *> *roomHistory;

@end

Expand All @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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

0 comments on commit b34f10c

Please sign in to comment.