ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/devicemodule/audioplayer/report/AudioPlayerProgressReporter.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.devicemodule.audioplayer.report; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import com.baidu.duer.dcs.devicemodule.audioplayer.message.PlayPayload; |
| TMBOY | 45:2aa9f933c8d2 | 19 | |
| TMBOY | 45:2aa9f933c8d2 | 20 | import java.util.concurrent.Executors; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import java.util.concurrent.ScheduledExecutorService; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import java.util.concurrent.ScheduledFuture; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import java.util.concurrent.TimeUnit; |
| TMBOY | 45:2aa9f933c8d2 | 24 | |
| TMBOY | 45:2aa9f933c8d2 | 25 | /** |
| TMBOY | 45:2aa9f933c8d2 | 26 | * 当服务器返回Play指令有progressReportDelayInMilliseconds, |
| TMBOY | 45:2aa9f933c8d2 | 27 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 28 | * ProgressReportDelayElapsed和ProgressReportIntervalElapsed事件的上报 |
| TMBOY | 45:2aa9f933c8d2 | 29 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 30 | * Created by guxiuzhong@baidu.com on 2017/5/22. |
| TMBOY | 45:2aa9f933c8d2 | 31 | */ |
| TMBOY | 45:2aa9f933c8d2 | 32 | public class AudioPlayerProgressReporter { |
| TMBOY | 45:2aa9f933c8d2 | 33 | private final ScheduledExecutorService eventScheduler = Executors.newScheduledThreadPool(1); |
| TMBOY | 45:2aa9f933c8d2 | 34 | private ScheduledFuture<?> progressReportDelayFuture; |
| TMBOY | 45:2aa9f933c8d2 | 35 | private ScheduledFuture<?> progressReportIntervalFuture; |
| TMBOY | 45:2aa9f933c8d2 | 36 | private final Runnable progressReportDelayRunnable; |
| TMBOY | 45:2aa9f933c8d2 | 37 | private final Runnable progressReportIntervalRunnable; |
| TMBOY | 45:2aa9f933c8d2 | 38 | private final AudioPlayerTimer audioPlayerTimer; |
| TMBOY | 45:2aa9f933c8d2 | 39 | private long progressReportDelay; |
| TMBOY | 45:2aa9f933c8d2 | 40 | private long progressReportInterval; |
| TMBOY | 45:2aa9f933c8d2 | 41 | private boolean isSetup; |
| TMBOY | 45:2aa9f933c8d2 | 42 | |
| TMBOY | 45:2aa9f933c8d2 | 43 | public AudioPlayerProgressReporter(Runnable progressReportDelayRunnable, |
| TMBOY | 45:2aa9f933c8d2 | 44 | Runnable progressReportIntervalRunnable, |
| TMBOY | 45:2aa9f933c8d2 | 45 | AudioPlayerTimer audioPlayerTimer) { |
| TMBOY | 45:2aa9f933c8d2 | 46 | if (progressReportDelayRunnable == null |
| TMBOY | 45:2aa9f933c8d2 | 47 | || progressReportIntervalRunnable == null |
| TMBOY | 45:2aa9f933c8d2 | 48 | || audioPlayerTimer == null) { |
| TMBOY | 45:2aa9f933c8d2 | 49 | throw new IllegalArgumentException("All arguments must be provided."); |
| TMBOY | 45:2aa9f933c8d2 | 50 | } |
| TMBOY | 45:2aa9f933c8d2 | 51 | this.progressReportDelayRunnable = progressReportDelayRunnable; |
| TMBOY | 45:2aa9f933c8d2 | 52 | this.progressReportIntervalRunnable = progressReportIntervalRunnable; |
| TMBOY | 45:2aa9f933c8d2 | 53 | this.audioPlayerTimer = audioPlayerTimer; |
| TMBOY | 45:2aa9f933c8d2 | 54 | this.isSetup = false; |
| TMBOY | 45:2aa9f933c8d2 | 55 | } |
| TMBOY | 45:2aa9f933c8d2 | 56 | |
| TMBOY | 45:2aa9f933c8d2 | 57 | public synchronized void setup(PlayPayload.ProgressReport progressReport) { |
| TMBOY | 45:2aa9f933c8d2 | 58 | if (progressReport == null) { |
| TMBOY | 45:2aa9f933c8d2 | 59 | String errorMessage = "ProgressReport must not be null."; |
| TMBOY | 45:2aa9f933c8d2 | 60 | throw new IllegalArgumentException(errorMessage); |
| TMBOY | 45:2aa9f933c8d2 | 61 | } |
| TMBOY | 45:2aa9f933c8d2 | 62 | if (isSetup) { |
| TMBOY | 45:2aa9f933c8d2 | 63 | String errorMessage = "AudioPlayerProgressReporter has already been setup. " |
| TMBOY | 45:2aa9f933c8d2 | 64 | + "Please disable it before setting it up again."; |
| TMBOY | 45:2aa9f933c8d2 | 65 | throw new IllegalStateException(errorMessage); |
| TMBOY | 45:2aa9f933c8d2 | 66 | } |
| TMBOY | 45:2aa9f933c8d2 | 67 | |
| TMBOY | 45:2aa9f933c8d2 | 68 | cancelEvents(); |
| TMBOY | 45:2aa9f933c8d2 | 69 | progressReportDelay = progressReport.progressReportDelayInMilliseconds; |
| TMBOY | 45:2aa9f933c8d2 | 70 | progressReportInterval = progressReport.progressReportIntervalInMilliseconds; |
| TMBOY | 45:2aa9f933c8d2 | 71 | isSetup = true; |
| TMBOY | 45:2aa9f933c8d2 | 72 | } |
| TMBOY | 45:2aa9f933c8d2 | 73 | |
| TMBOY | 45:2aa9f933c8d2 | 74 | public synchronized void disable() { |
| TMBOY | 45:2aa9f933c8d2 | 75 | isSetup = false; |
| TMBOY | 45:2aa9f933c8d2 | 76 | cancelEvents(); |
| TMBOY | 45:2aa9f933c8d2 | 77 | progressReportDelay = 0; |
| TMBOY | 45:2aa9f933c8d2 | 78 | progressReportInterval = 0; |
| TMBOY | 45:2aa9f933c8d2 | 79 | } |
| TMBOY | 45:2aa9f933c8d2 | 80 | |
| TMBOY | 45:2aa9f933c8d2 | 81 | public synchronized void start() { |
| TMBOY | 45:2aa9f933c8d2 | 82 | cancelEvents(); |
| TMBOY | 45:2aa9f933c8d2 | 83 | |
| TMBOY | 45:2aa9f933c8d2 | 84 | if (!isSetup) { |
| TMBOY | 45:2aa9f933c8d2 | 85 | String errorMessage = "AudioPlayerProgressReporter cannot be started " |
| TMBOY | 45:2aa9f933c8d2 | 86 | + "because it has not been setup yet."; |
| TMBOY | 45:2aa9f933c8d2 | 87 | throw new IllegalStateException(errorMessage); |
| TMBOY | 45:2aa9f933c8d2 | 88 | } |
| TMBOY | 45:2aa9f933c8d2 | 89 | |
| TMBOY | 45:2aa9f933c8d2 | 90 | long currentOffsetIntoTrack = audioPlayerTimer.getOffsetInMilliseconds(); |
| TMBOY | 45:2aa9f933c8d2 | 91 | |
| TMBOY | 45:2aa9f933c8d2 | 92 | long timeUntilDelayReport = progressReportDelay - currentOffsetIntoTrack; |
| TMBOY | 45:2aa9f933c8d2 | 93 | if (timeUntilDelayReport > 0) { |
| TMBOY | 45:2aa9f933c8d2 | 94 | scheduleDelayEvent(timeUntilDelayReport); |
| TMBOY | 45:2aa9f933c8d2 | 95 | } |
| TMBOY | 45:2aa9f933c8d2 | 96 | |
| TMBOY | 45:2aa9f933c8d2 | 97 | long timeUntilIntervalReport = progressReportInterval == 0 ? 0 : |
| TMBOY | 45:2aa9f933c8d2 | 98 | progressReportInterval - (currentOffsetIntoTrack % progressReportInterval); |
| TMBOY | 45:2aa9f933c8d2 | 99 | if (timeUntilIntervalReport > 0) { |
| TMBOY | 45:2aa9f933c8d2 | 100 | scheduleIntervalEvent(timeUntilIntervalReport, progressReportInterval); |
| TMBOY | 45:2aa9f933c8d2 | 101 | } |
| TMBOY | 45:2aa9f933c8d2 | 102 | } |
| TMBOY | 45:2aa9f933c8d2 | 103 | |
| TMBOY | 45:2aa9f933c8d2 | 104 | public synchronized void stop() { |
| TMBOY | 45:2aa9f933c8d2 | 105 | cancelEvents(); |
| TMBOY | 45:2aa9f933c8d2 | 106 | } |
| TMBOY | 45:2aa9f933c8d2 | 107 | |
| TMBOY | 45:2aa9f933c8d2 | 108 | public synchronized boolean isSetup() { |
| TMBOY | 45:2aa9f933c8d2 | 109 | return isSetup; |
| TMBOY | 45:2aa9f933c8d2 | 110 | } |
| TMBOY | 45:2aa9f933c8d2 | 111 | |
| TMBOY | 45:2aa9f933c8d2 | 112 | private void scheduleDelayEvent(long delay) { |
| TMBOY | 45:2aa9f933c8d2 | 113 | progressReportDelayFuture = eventScheduler.schedule(progressReportDelayRunnable, delay, |
| TMBOY | 45:2aa9f933c8d2 | 114 | TimeUnit.MILLISECONDS); |
| TMBOY | 45:2aa9f933c8d2 | 115 | } |
| TMBOY | 45:2aa9f933c8d2 | 116 | |
| TMBOY | 45:2aa9f933c8d2 | 117 | private void scheduleIntervalEvent(long delay, long interval) { |
| TMBOY | 45:2aa9f933c8d2 | 118 | progressReportIntervalFuture = eventScheduler.scheduleAtFixedRate( |
| TMBOY | 45:2aa9f933c8d2 | 119 | progressReportIntervalRunnable, delay, interval, TimeUnit.MILLISECONDS); |
| TMBOY | 45:2aa9f933c8d2 | 120 | } |
| TMBOY | 45:2aa9f933c8d2 | 121 | |
| TMBOY | 45:2aa9f933c8d2 | 122 | private void cancelEvents() { |
| TMBOY | 45:2aa9f933c8d2 | 123 | if (progressReportDelayFuture != null && !progressReportDelayFuture.isDone()) { |
| TMBOY | 45:2aa9f933c8d2 | 124 | progressReportDelayFuture.cancel(false); |
| TMBOY | 45:2aa9f933c8d2 | 125 | } |
| TMBOY | 45:2aa9f933c8d2 | 126 | if (progressReportIntervalFuture != null && !progressReportIntervalFuture.isDone()) { |
| TMBOY | 45:2aa9f933c8d2 | 127 | progressReportIntervalFuture.cancel(false); |
| TMBOY | 45:2aa9f933c8d2 | 128 | } |
| TMBOY | 45:2aa9f933c8d2 | 129 | } |
| TMBOY | 45:2aa9f933c8d2 | 130 | } |
