private final long syncTimer(long startMS) { if (mInSeekingAction || mInSyncAction) { return 0; } mInSyncAction = true; long d = 0; long time = startMS - mTimeBase; if (!mDanmakusVisible || mRenderingState.nothingRendered || mInWaitingState) { timer.update(time); mRemainingTime = 0; } else { long gapTime = time - timer.currMillisecond; long averageTime = Math.max(mFrameUpdateRate, getAverageRenderingTime()); if (gapTime > 2000 || mRenderingState.consumingTime > mCordonTime || averageTime > mCordonTime) { d = gapTime; gapTime = 0; } else { d = averageTime + gapTime / mFrameUpdateRate; d = Math.max(mFrameUpdateRate, d); d = Math.min(mCordonTime, d); long a = d - mLastDeltaTime; if (a > 3 && a < 8 && mLastDeltaTime >= mFrameUpdateRate && mLastDeltaTime <= mCordonTime) { d = mLastDeltaTime; } gapTime -= d; mLastDeltaTime = d; } mRemainingTime = gapTime; timer.add(d); // Log.e("DrawHandler", time+"|d:" + d + "RemaingTime:" + mRemainingTime + // ",gapTime:" + gapTime + ",rtim:" + mRenderingState.consumingTime + ",average:" + // averageTime); } if (mCallback != null) { mCallback.updateTimer(timer); } mInSyncAction = false; return d; }
private void updateInCurrentThread() { if (quitFlag) { return; } long startMS = SystemClock.uptimeMillis(); long d = syncTimer(startMS); if (d < 0) { removeMessages(UPDATE); sendEmptyMessageDelayed(UPDATE, 60 - d); return; } d = mDanmakuView.drawDanmakus(); removeMessages(UPDATE); if (d > mCordonTime2) { // this situation may be cuased by ui-thread waiting of DanmakuView, so we // sync-timer at once timer.add(d); mDrawTimes.clear(); } if (!mDanmakusVisible) { waitRendering(INDEFINITE_TIME); return; } else if (mRenderingState.nothingRendered && mIdleSleep) { long dTime = mRenderingState.endTime - timer.currMillisecond; if (dTime > 500) { waitRendering(dTime - 10); return; } } if (d < mFrameUpdateRate) { sendEmptyMessageDelayed(UPDATE, mFrameUpdateRate - d); return; } sendEmptyMessage(UPDATE); }
@Override public void handleMessage(Message msg) { int what = msg.what; switch (what) { case PREPARE: if (mParser == null || !mDanmakuView.isViewReady()) { sendEmptyMessageDelayed(PREPARE, 100); } else { prepare( new Runnable() { @Override public void run() { mReady = true; if (mCallback != null) { mCallback.prepared(); } } }); } break; case START: Long startTime = (Long) msg.obj; if (startTime != null) { pausedPostion = startTime.longValue(); } else { pausedPostion = 0; } case RESUME: quitFlag = false; if (mReady) { mTimeBase = System.currentTimeMillis() - pausedPostion; timer.update(pausedPostion); removeMessages(RESUME); sendEmptyMessage(UPDATE); drawTask.start(); } else { sendEmptyMessageDelayed(RESUME, 100); } break; case SEEK_POS: Long deltaMs = (Long) msg.obj; mTimeBase -= deltaMs; timer.update(System.currentTimeMillis() - mTimeBase); if (drawTask != null) drawTask.seek(timer.currMillisecond); pausedPostion = timer.currMillisecond; removeMessages(RESUME); sendEmptyMessage(RESUME); break; case UPDATE: if (quitFlag) { break; } long startMS = System.currentTimeMillis(); long d = timer.update(startMS - mTimeBase); if (mCallback != null) { mCallback.updateTimer(timer); } if (d < 0) { removeMessages(UPDATE); sendEmptyMessageDelayed(UPDATE, 60 - d); break; } d = mDanmakuView.drawDanmakus(); removeMessages(UPDATE); if (d == -1) { // reduce refresh rate sendEmptyMessageDelayed(UPDATE, 100); break; } if (d <= 16) { sendEmptyMessage(UPDATE); SystemClock.sleep(16 - d); break; } sendEmptyMessage(UPDATE); break; case SHOW_DANMAKUS: Long start = (Long) msg.obj; if (start == null && drawTask != null) { drawTask.requestClear(); } else if (start != null && drawTask != null) { drawTask.start(); drawTask.seek(start); drawTask.requestClear(); obtainMessage(START, start).sendToTarget(); } mDanmakusVisible = true; break; case HIDE_DANMAKUS: if (mDanmakuView != null) { mDanmakuView.clear(); } Boolean quitDrawTask = (Boolean) msg.obj; if (quitDrawTask.booleanValue() && this.drawTask != null) { this.drawTask.quit(); } mDanmakusVisible = false; if (!quitDrawTask) { break; } case PAUSE: case QUIT: removeCallbacksAndMessages(null); quitFlag = true; mDrawTimes.clear(); pausedPostion = timer.currMillisecond; if (what == QUIT) { if (this.drawTask != null) { this.drawTask.quit(); } if (this.getLooper() != Looper.getMainLooper()) this.getLooper().quit(); if (mParser != null) { mParser.release(); } } break; } }
@Override public void handleMessage(Message msg) { int what = msg.what; switch (what) { case PREPARE: if (mParser == null || !mDanmakuView.isViewReady()) { sendEmptyMessageDelayed(PREPARE, 100); } else { prepare( new Runnable() { @Override public void run() { mReady = true; if (mCallback != null) { mCallback.prepared(); } } }); } break; case SHOW_DANMAKUS: mDanmakusVisible = true; Long start = (Long) msg.obj; boolean resume = false; if (drawTask != null) { if (start == null) { timer.update(getCurrentTime()); drawTask.requestClear(); } else { drawTask.start(); drawTask.seek(start); drawTask.requestClear(); resume = true; } } if (quitFlag && mDanmakuView != null) { mDanmakuView.drawDanmakus(); } notifyRendering(); if (!resume) { break; } case START: Long startTime = (Long) msg.obj; if (startTime != null) { pausedPosition = startTime; } else { pausedPosition = 0; } case RESUME: quitFlag = false; if (mReady) { mRenderingState.reset(); mDrawTimes.clear(); mTimeBase = SystemClock.uptimeMillis() - pausedPosition; timer.update(pausedPosition); removeMessages(RESUME); sendEmptyMessage(UPDATE); drawTask.start(); notifyRendering(); mInSeekingAction = false; } else { sendEmptyMessageDelayed(RESUME, 100); } break; case SEEK_POS: quitFlag = true; quitUpdateThread(); Long position = (Long) msg.obj; long deltaMs = position - timer.currMillisecond; mTimeBase -= deltaMs; timer.update(SystemClock.uptimeMillis() - mTimeBase); mContext.mGlobalFlagValues.updateMeasureFlag(); if (drawTask != null) drawTask.seek(timer.currMillisecond); pausedPosition = timer.currMillisecond; removeMessages(RESUME); sendEmptyMessage(RESUME); break; case UPDATE: if (mUpdateInNewThread) { updateInNewThread(); } else { updateInCurrentThread(); } break; case NOTIFY_DISP_SIZE_CHANGED: mContext.mDanmakuFactory.notifyDispSizeChanged(mContext); Boolean updateFlag = (Boolean) msg.obj; if (updateFlag != null && updateFlag) { mContext.mGlobalFlagValues.updateMeasureFlag(); } break; case HIDE_DANMAKUS: mDanmakusVisible = false; if (mDanmakuView != null) { mDanmakuView.clear(); } if (this.drawTask != null) { this.drawTask.requestClear(); this.drawTask.requestHide(); } Boolean quitDrawTask = (Boolean) msg.obj; if (quitDrawTask && this.drawTask != null) { this.drawTask.quit(); } if (!quitDrawTask) { break; } case PAUSE: removeMessages(UPDATE); case QUIT: if (what == QUIT) { removeCallbacksAndMessages(null); } quitFlag = true; syncTimerIfNeeded(); if (mThread != null) { notifyRendering(); quitUpdateThread(); } pausedPosition = timer.currMillisecond; if (what == QUIT) { if (this.drawTask != null) { this.drawTask.quit(); } if (mParser != null) { mParser.release(); } if (this.getLooper() != Looper.getMainLooper()) this.getLooper().quit(); } break; case NOTIFY_RENDERING: notifyRendering(); break; case UPDATE_WHEN_PAUSED: if (quitFlag && mDanmakuView != null) { drawTask.requestClear(); mDanmakuView.drawDanmakus(); notifyRendering(); } break; case CLEAR_DANMAKUS_ON_SCREEN: if (drawTask != null) { drawTask.clearDanmakusOnScreen(getCurrentTime()); } break; } }
@Override protected void initTimer(DanmakuTimer timer) { mTimer = timer; mCacheTimer = new DanmakuTimer(); mCacheTimer.update(timer.currMillisecond); }