Google Chromecast SDK TearDown in the background

Using the iOS sender API, when my app goes in the background, the SDK disconnects all connections, and I cannot run more media files until the app comes to the fore. My application plays audio and is allowed to run and stream in the background. Is it possible to tell the Googlecast platform to open sockets?

Here's the background log:

INFO: action will be canceled

INFO: now in the background

FINE: - [Disable GCKCastSocket] disable

FINE: - [GCKCastSocket doTeardownWithError:] doTeardownWithError

FINE: - [GCKCastSocket doTeardownWithError:] delegate notification that the socket is disconnected

FINE: - [GCKHeartbeatChannel didDisconnect] disconnected - stop the pulse timer if necessary

FINE: - [GCKCastSocket socketDidDisconnect: withError:] socketDidDisconnect: withError: "(null)"

Then, when the application resumes: INFO: go to the foreground

FINE: - [GCKCastSocket doTeardownWithError:] doTeardownWithError

FINE: - [GCKCastSocket doTeardownWithError:] delegate notification that the socket is disconnected

FINE: - [GCKCastSocket connectToHost: port: withTimeout:] Connection to "192.168.1.4" on port 8009l ...

INFO: Active

FINE: - [GCKCastSocket socket: didConnectToHost: port:] socketDidConnect:

FINE: - [GCKCastSocket socketDidSecure:] socketDidSecure:

FINE: - [GCKCastSocket: doneReadData: withTag:] socket, expected message length = 1307

FINE: - [GCKDeviceAuthChannel didReceiveBinaryMessage:] Genuine Google device didDeviceAuthenticated = YES

FINE: - [GCKDeviceManager deviceAuthChannelDidAuthenticate:] ,

FINE: - [ GCKCastSocket: doneReadData: withTag:], = 474

FINE: - [GCKDeviceManager receiverControlChannel: didReceiveStatusForApplication:] (CC1AD845)

FINE: - [GCKDeviceManager connectAndNotifyDidConnectToApplication: launchApplication:] < 0x1467c8f0: GCKApplicationMetadata > (CC1AD845), web-11

FINE: - [ GCKCastSocket: doneReadData: withTag:], = 135

FINE: - [GCKMediaControlChannel didReceiveTextMessage:]: { "type": "MEDIA_STATUS", "status": [], "requestId": 6}

0
2

2.0 API- iOS GCKCastSocket UIApplicationDidEnterBackgroundNotification, , .

:

  • , .
  • .

( ):

  • ,
  • .

. .

+2

, , , ( )

1. dispatch_block_t,

 dispatch_block_t myDummyBackgroundTaskBlock = {
    [[UIApplication sharedApplication] endBackgroundTask:myDummyBackgroundTask];
    myDummyBackgroundTask = UIBackgroundTaskInvalid;
    myDummyBackgroundTask = [app beginBackgroundTaskWithExpirationHandler:myDummyBackgroundTask];
};

2. -

    // foreground
    -(void)handleTasksForApplicationInForeground {
    if(myDummyBackgroundTask) { // reset that task
       [[UIApplication sharedApplication] endBackgroundTask: myDummyBackgroundTask];
       myDummyBackgroundTask = UIBackgroundTaskInvalid;
     }
    }

     // background
     -(void) handleTasksForApplicationInBackground {
         UIDevice *device = [UIDevice currentDevice];
         BOOL backgroundSupported = NO;
        if ([device respondsToSelector:@selector(isMultitaskingSupported)])
         backgroundSupported = device.multitaskingSupported;
        if(backgroundSupported && backgroundEnabled) { // perform a background task

            myDummyBackgroundTaskBlock = ^{
                [[UIApplication sharedApplication] endBackgroundTask: myDummyBackgroundTaskBlock];
                myDummyBackgroundTaskBlock = UIBackgroundTaskInvalid;
           };

           SEL sel = @selector(doDummyBackgroundTask);
           [self doBackgroundTaskAsync:sel];

           [self performSelector:@selector(doBackgroundTaskAsync:) withObject:nil afterDelay:500.0f]; /// LP: this is the funny part since iOS will kill the task after 500 sec.
             }
           }

3. ( , .plist):

    -(void)applicationDidEnterBackground:(UIApplication *)application {
       [self handleTasksForApplicationInBackground];
     }

    -(void)applicationWillEnterForeground:(UIApplication *)application {
       [self handleTasksForApplicationInForeground];
     }

4. , aync .

        -(void) doBackgroundTaskAsync:(SEL)selector {

        @try {
             if( [[UIApplication sharedApplication] backgroundTimeRemaining] < 5 ) { 
               return;
             }

           if(!myDummyBackgroundTaskBlock) { // need to create again on-the-fly
                myDummyBackgroundTaskBlock = ^{
                      [[UIApplication sharedApplication] endBackgroundTask:myDummyBackgroundTask];
                     myDummyBackgroundTask = UIBackgroundTaskInvalid;
                };
             }

            myDummyBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:myDummyBackgroundTaskBlock];
          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

           while ([[UIApplication sharedApplication] backgroundTimeRemaining] > 5.0) {
              int delta = 5.0;
              [self performSelector: selector ];
              sleep(delta);
          }
        });
      }
       @catch (...) {
     }
    }
0

Source: https://habr.com/ru/post/1534736/


All Articles