解决iOS 11 ijk播放器退到后台再进入时只有声音没画面问题

问题描述

iOS 11之前,ijk播放器可正常前后台切换拉流播放,但更新iOS 11系统之后,退出后台,再进入时出现概率性没有画面,也许一段时间后会出现画面(此时声音没任何问题).

问题猜想

  1. 业务逻辑问题 -- 不太可能,毕竟iOS 11 之前一切正常;
  2. ijk本身不兼容iOS11问题 --有可能,某一个ijk代码环节出现iOS 11不生效的问题;
  3. 见鬼了;

代码逻辑

  1. 退到后台(有后台播放功能),如果开启了后台播放功能,则ijk退到后台时不进行Pause处理,本身ijk支持 * [self.ijkPlayer setPauseInBackground:NO];* 退到后台不暂停.如果用户关闭后台播放功能后,退到后台再进入前台会走repaly逻辑方法来重新刷流播放.
  2. 问题就出在退到后台进入前台 和 横屏下刷新按钮方法都走同一个repaly方法,而横屏下的刷新正常,退到后台进入前台却出现了声音正常出来,画面延迟出来,概率性不出现.

问题定位与解决

1.因为走的是同一个方法,而横屏下刷新却没任何问题,所以尝试用延迟的方法,在进入前台时在方法中延迟一下调用 replay.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ }); 延迟1s,正常了
这种方法虽然可以解决当前问题,但是不太建议,感觉有点像"用胶水补了下一下鞋子",虽然鞋子能继续穿,不知道哪天还会坏,不知道牢不牢固.

  1. 利用CATransaction flush 显式事务完成后立即提交隐式事务.
在IJKSDLGLView.m文件中,
1. - (BOOL)setupEAGLContext:(EAGLContext *)context 方法的
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderbuffer);
代码下面添加
[CATransaction flush];
2. 在- (void)displayInternal: (SDL_VoutOverlay *) overlay 方法中
IJK_GLES2_Renderer_setGravity(_renderer, _rendererGravity, _backingWidth, _backingHeight);
代码下面也添加
[CATransaction flush];

添加后代码如下:
- (BOOL)setupEAGLContext:(EAGLContext *)context
{
    glGenFramebuffers(1, &_framebuffer);
    glGenRenderbuffers(1, &_renderbuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
    [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_backingWidth);
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_backingHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderbuffer);
    [CATransaction flush];
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE) {
        NSLog(@"failed to make complete framebuffer object %x\n", status);
        return NO;
    }

    GLenum glError = glGetError();
    if (GL_NO_ERROR != glError) {
        NSLog(@"failed to setup GL %x\n", glError);
        return NO;
    }

    return YES;
}

- (void)displayInternal: (SDL_VoutOverlay *) overlay
{
    if (![self setupRenderer:overlay]) {
        if (!overlay && !_renderer) {
            NSLog(@"IJKSDLGLView: setupDisplay not ready\n");
        } else {
            NSLog(@"IJKSDLGLView: setupDisplay failed\n");
        }
        return;
    }

    [[self eaglLayer] setContentsScale:_scaleFactor];

    if (_isRenderBufferInvalidated) {
        NSLog(@"IJKSDLGLView: renderbufferStorage fromDrawable\n");
        _isRenderBufferInvalidated = NO;

        glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
        [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_backingWidth);
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_backingHeight);
        IJK_GLES2_Renderer_setGravity(_renderer, _rendererGravity, _backingWidth, _backingHeight);
        [CATransaction flush];
    }

    glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
    glViewport(0, 0, _backingWidth, _backingHeight);

    if (!IJK_GLES2_Renderer_renderOverlay(_renderer, overlay))
        ALOGE("[EGL] IJK_GLES2_render failed\n");

    glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
    [_context presentRenderbuffer:GL_RENDERBUFFER];

    int64_t current = (int64_t)SDL_GetTickHR();
    int64_t delta   = (current > _lastFrameTime) ? current - _lastFrameTime : 0;
    if (delta <= 0) {
        _lastFrameTime = current;
    } else if (delta >= 1000) {
        _fps = ((CGFloat)_frameCount) * 1000 / delta;
        _frameCount = 0;
        _lastFrameTime = current;
    } else {
        _frameCount++;
    }
}


针对+ (void)flush;方法,官网是这样描述的:
Discussion
Delays the commit until any nested explicit transactions have completed.
Flush is typically called automatically at the end of the current runloop, regardless of the runloop mode. If your application does not have a runloop, you must call this method explicitly.
However, you should attempt to avoid calling flush explicitly. By allowing flush to execute during the runloop your application will achieve better performance, atomic screen updates will be preserved, and transactions and animations that work from transaction to transaction will continue to function.

大体意思就是 : 延迟提交,直到任何嵌套的显式事务完成为止。提交任何现存隐式事务,直到所有嵌套的显式事务完成提交实际效果,其本质作用是对事务的处理安排.

感谢github大神的参考意见.

你可能感兴趣的:(解决iOS 11 ijk播放器退到后台再进入时只有声音没画面问题)