Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[IOS] do not keep network alive in idle state (paused or stopped).

  • Loading branch information...
commit f4b1dbb7170c80fb1eec3fe2fc45bcd2eeaf6b6a 1 parent 165be20
@ulion ulion authored
Showing with 50 additions and 6 deletions.
  1. +11 −1 xbmc/osx/ios/XBMCController.h
  2. +39 −5 xbmc/osx/ios/XBMCController.mm
View
12 xbmc/osx/ios/XBMCController.h
@@ -28,6 +28,13 @@
@class IOSEAGLView;
+typedef enum
+{
+ IOS_PLAYBACK_STOPPED,
+ IOS_PLAYBACK_PAUSED,
+ IOS_PLAYBACK_PLAYING
+} IOSPlaybackState;
+
@interface XBMCController : UIViewController <UIGestureRecognizerDelegate>
{
UIWindow *m_window;
@@ -46,6 +53,8 @@
bool m_isPlayingBeforeInactive;
bool m_isInterrupted;
UIBackgroundTaskIdentifier m_bgTask;
+ NSTimer *m_networkAutoSuspendTimer;
+ IOSPlaybackState m_playbackState;
}
@property (readonly, nonatomic, getter=isAnimating) BOOL animating;
@property CGPoint lastGesturePoint;
@@ -53,6 +62,7 @@
@property bool touchBeginSignaled;
@property int m_screenIdx;
@property CGSize screensize;
+@property (nonatomic, retain) NSTimer *m_networkAutoSuspendTimer;
// message from which our instance is obtained
- (void) pauseAnimation;
@@ -79,7 +89,7 @@
- (void) deactivateKeyboard:(UIView *)view;
- (void) disableNetworkAutoSuspend;
-- (void) enableNetworkAutoSuspend;
+- (void) enableNetworkAutoSuspend:(id)obj;
- (void) disableSystemSleep;
- (void) enableSystemSleep;
- (void) disableScreenSaver;
View
44 xbmc/osx/ios/XBMCController.mm
@@ -240,6 +240,7 @@ @implementation XBMCController
@synthesize touchBeginSignaled;
@synthesize m_screenIdx;
@synthesize screensize;
+@synthesize m_networkAutoSuspendTimer;
//--------------------------------------------------------------
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
@@ -698,6 +699,7 @@ - (id)initWithFrame:(CGRect)frame withScreen:(UIScreen *)screen
m_isPlayingBeforeInactive = NO;
m_isInterrupted = NO;
m_bgTask = UIBackgroundTaskInvalid;
+ m_playbackState = IOS_PLAYBACK_STOPPED;
m_window = [[UIWindow alloc] initWithFrame:frame];
[m_window setRootViewController:self];
@@ -745,7 +747,8 @@ -(void)viewDidLoad
- (void)dealloc
{
// stop background task
- [self enableNetworkAutoSuspend];
+ [m_networkAutoSuspendTimer invalidate];
+ [self enableNetworkAutoSuspend:nil];
AnnounceReceiver::dealloc();
[m_glView stopAnimation];
@@ -867,10 +870,16 @@ - (void)disableNetworkAutoSuspend
}
// we have to alloc the background task for keep network working after screen lock and dark.
UIBackgroundTaskIdentifier newTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
- m_bgTask = newTask;
+ m_bgTask = newTask;
+
+ if (m_networkAutoSuspendTimer)
+ {
+ [m_networkAutoSuspendTimer invalidate];
+ self.m_networkAutoSuspendTimer = nil;
+ }
}
//--------------------------------------------------------------
-- (void)enableNetworkAutoSuspend
+- (void)enableNetworkAutoSuspend:(id)obj
{
PRINT_SIGNATURE();
if (m_bgTask != UIBackgroundTaskInvalid)
@@ -923,6 +932,7 @@ - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
LOG(@"%s: type %d, subtype: %d", __PRETTY_FUNCTION__, receivedEvent.type, receivedEvent.subtype);
if (receivedEvent.type == UIEventTypeRemoteControl)
{
+ [self disableNetworkAutoSuspend];
switch (receivedEvent.subtype)
{
case UIEventSubtypeRemoteControlTogglePlayPause:
@@ -945,6 +955,7 @@ - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
LOG(@"unhandled subtype: %d", receivedEvent.subtype);
break;
}
+ [self rescheduleNetworkAutoSuspend];
}
}
//--------------------------------------------------------------
@@ -980,8 +991,8 @@ - (void)becomeInactive
m_isPlayingBeforeInactive = YES;
CApplicationMessenger::Get().MediaPauseIfPlaying();
}
- // TODO: only disable network auto-suspend when app is playing
- [self disableNetworkAutoSuspend];
+ // check whether we need disable network auto suspend.
+ [self rescheduleNetworkAutoSuspend];
}
- (void)beginInterruption
@@ -1089,11 +1100,16 @@ - (void)onPlay:(NSDictionary *)item
[[NowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
[dict release];
}
+ m_playbackState = IOS_PLAYBACK_PLAYING;
+ [self disableNetworkAutoSuspend];
}
//--------------------------------------------------------------
- (void)onPause:(NSDictionary *)item
{
PRINT_SIGNATURE();
+ m_playbackState = IOS_PLAYBACK_PAUSED;
+ // schedule set network auto suspend state for save power if idle.
+ [self rescheduleNetworkAutoSuspend];
}
//--------------------------------------------------------------
- (void)onStop:(NSDictionary *)item
@@ -1102,6 +1118,24 @@ - (void)onStop:(NSDictionary *)item
Class NowPlayingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (NowPlayingInfoCenter)
[[NowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
+ m_playbackState = IOS_PLAYBACK_STOPPED;
+ // delay set network auto suspend state in case we are switching playing item.
+ [self rescheduleNetworkAutoSuspend];
+}
+//--------------------------------------------------------------
+- (void)rescheduleNetworkAutoSuspend
+{
+ LOG(@"%s: playback state: %d", __PRETTY_FUNCTION__, m_playbackState);
+ if (m_playbackState == IOS_PLAYBACK_PLAYING)
+ {
+ [self disableNetworkAutoSuspend];
+ return;
+ }
+ if (m_networkAutoSuspendTimer)
+ [m_networkAutoSuspendTimer invalidate];
+
+ int delay = m_playbackState == IOS_PLAYBACK_PAUSED ? 60 : 30; // wait longer if paused than stopped
+ self.m_networkAutoSuspendTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(enableNetworkAutoSuspend:) userInfo:nil repeats:NO];
}
#pragma mark -
Please sign in to comment.
Something went wrong with that request. Please try again.