ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/devicemodule/audioplayer/report/AudioPlayStateReport.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.ApiConstants; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import com.baidu.duer.dcs.devicemodule.audioplayer.message.AudioPlayerPayload; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import com.baidu.duer.dcs.devicemodule.audioplayer.message.PlaybackFailedPayload; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.devicemodule.audioplayer.message.PlaybackStatePayload; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import com.baidu.duer.dcs.devicemodule.audioplayer.message.PlaybackStutterFinishedPayload; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import com.baidu.duer.dcs.framework.IMessageSender; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import com.baidu.duer.dcs.framework.message.Event; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import com.baidu.duer.dcs.framework.message.Header; |
| TMBOY | 45:2aa9f933c8d2 | 26 | import com.baidu.duer.dcs.framework.message.MessageIdHeader; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import com.baidu.duer.dcs.framework.message.Payload; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import com.baidu.duer.dcs.systeminterface.IMediaPlayer; |
| TMBOY | 45:2aa9f933c8d2 | 29 | |
| TMBOY | 45:2aa9f933c8d2 | 30 | /** |
| TMBOY | 45:2aa9f933c8d2 | 31 | * Audio Player模块各种事件上报的处理,同时维护当前的端状态 |
| TMBOY | 45:2aa9f933c8d2 | 32 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 33 | * Created by guxiuzhong@baidu.com on 2017/6/1. |
| TMBOY | 45:2aa9f933c8d2 | 34 | */ |
| TMBOY | 45:2aa9f933c8d2 | 35 | public class AudioPlayStateReport { |
| TMBOY | 45:2aa9f933c8d2 | 36 | public enum AudioPlayerState { |
| TMBOY | 45:2aa9f933c8d2 | 37 | IDLE, |
| TMBOY | 45:2aa9f933c8d2 | 38 | PLAYING, |
| TMBOY | 45:2aa9f933c8d2 | 39 | PAUSED, |
| TMBOY | 45:2aa9f933c8d2 | 40 | FINISHED, |
| TMBOY | 45:2aa9f933c8d2 | 41 | STOPPED, |
| TMBOY | 45:2aa9f933c8d2 | 42 | BUFFER_UNDERRUN |
| TMBOY | 45:2aa9f933c8d2 | 43 | } |
| TMBOY | 45:2aa9f933c8d2 | 44 | |
| TMBOY | 45:2aa9f933c8d2 | 45 | private AudioPlayerState currentState = AudioPlayerState.FINISHED; |
| TMBOY | 45:2aa9f933c8d2 | 46 | private IMessageSender messageSender; |
| TMBOY | 45:2aa9f933c8d2 | 47 | private String namespace; |
| TMBOY | 45:2aa9f933c8d2 | 48 | private AudioPlayStateReportListener audioPlayStateReportListener; |
| TMBOY | 45:2aa9f933c8d2 | 49 | |
| TMBOY | 45:2aa9f933c8d2 | 50 | public AudioPlayStateReport(String namespace, IMessageSender messageSender, |
| TMBOY | 45:2aa9f933c8d2 | 51 | AudioPlayStateReportListener audioPlayStateReportListener) { |
| TMBOY | 45:2aa9f933c8d2 | 52 | this.namespace = namespace; |
| TMBOY | 45:2aa9f933c8d2 | 53 | this.messageSender = messageSender; |
| TMBOY | 45:2aa9f933c8d2 | 54 | this.audioPlayStateReportListener = audioPlayStateReportListener; |
| TMBOY | 45:2aa9f933c8d2 | 55 | |
| TMBOY | 45:2aa9f933c8d2 | 56 | } |
| TMBOY | 45:2aa9f933c8d2 | 57 | |
| TMBOY | 45:2aa9f933c8d2 | 58 | public AudioPlayerState getState() { |
| TMBOY | 45:2aa9f933c8d2 | 59 | return currentState; |
| TMBOY | 45:2aa9f933c8d2 | 60 | } |
| TMBOY | 45:2aa9f933c8d2 | 61 | |
| TMBOY | 45:2aa9f933c8d2 | 62 | public void playbackResumed() { |
| TMBOY | 45:2aa9f933c8d2 | 63 | currentState = AudioPlayerState.PLAYING; |
| TMBOY | 45:2aa9f933c8d2 | 64 | Event event = createAudioPlayerEvent(ApiConstants.Events.PlaybackResumed.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 65 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 66 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 67 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 68 | } |
| TMBOY | 45:2aa9f933c8d2 | 69 | |
| TMBOY | 45:2aa9f933c8d2 | 70 | public void playbackFinished() { |
| TMBOY | 45:2aa9f933c8d2 | 71 | currentState = AudioPlayerState.FINISHED; |
| TMBOY | 45:2aa9f933c8d2 | 72 | Event event = createAudioPlayerEvent(ApiConstants.Events.PlaybackFinished.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 73 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 74 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 75 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 76 | } |
| TMBOY | 45:2aa9f933c8d2 | 77 | |
| TMBOY | 45:2aa9f933c8d2 | 78 | public void playbackStarted() { |
| TMBOY | 45:2aa9f933c8d2 | 79 | currentState = AudioPlayerState.PLAYING; |
| TMBOY | 45:2aa9f933c8d2 | 80 | Event event = createAudioPlayerEvent(ApiConstants.Events.PlaybackStarted.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 81 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 82 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 83 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | |
| TMBOY | 45:2aa9f933c8d2 | 86 | public void playbackFailed(IMediaPlayer.ErrorType errorType) { |
| TMBOY | 45:2aa9f933c8d2 | 87 | currentState = AudioPlayerState.STOPPED; |
| TMBOY | 45:2aa9f933c8d2 | 88 | long offset = audioPlayStateReportListener.getCurrentOffsetInMilliseconds(); |
| TMBOY | 45:2aa9f933c8d2 | 89 | PlaybackStatePayload playbackStatePayload = |
| TMBOY | 45:2aa9f933c8d2 | 90 | new PlaybackStatePayload(audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 91 | offset, currentState.toString()); |
| TMBOY | 45:2aa9f933c8d2 | 92 | |
| TMBOY | 45:2aa9f933c8d2 | 93 | Header header = new MessageIdHeader(namespace, |
| TMBOY | 45:2aa9f933c8d2 | 94 | ApiConstants.Events.PlaybackFailed.NAME); |
| TMBOY | 45:2aa9f933c8d2 | 95 | Event event = new Event(header, |
| TMBOY | 45:2aa9f933c8d2 | 96 | new PlaybackFailedPayload(audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 97 | playbackStatePayload, errorType)); |
| TMBOY | 45:2aa9f933c8d2 | 98 | |
| TMBOY | 45:2aa9f933c8d2 | 99 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 100 | } |
| TMBOY | 45:2aa9f933c8d2 | 101 | |
| TMBOY | 45:2aa9f933c8d2 | 102 | public void playbackPaused() { |
| TMBOY | 45:2aa9f933c8d2 | 103 | currentState = AudioPlayerState.PAUSED; |
| TMBOY | 45:2aa9f933c8d2 | 104 | Event event = createAudioPlayerEvent(ApiConstants.Events.PlaybackPaused.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 105 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 106 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 107 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 108 | } |
| TMBOY | 45:2aa9f933c8d2 | 109 | |
| TMBOY | 45:2aa9f933c8d2 | 110 | public void playbackNearlyFinished() { |
| TMBOY | 45:2aa9f933c8d2 | 111 | currentState = AudioPlayerState.FINISHED; |
| TMBOY | 45:2aa9f933c8d2 | 112 | Event event = createAudioPlayerEvent(ApiConstants.Events.PlaybackNearlyFinished.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 113 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 114 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 115 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 116 | } |
| TMBOY | 45:2aa9f933c8d2 | 117 | |
| TMBOY | 45:2aa9f933c8d2 | 118 | public void playbackStutterStarted() { |
| TMBOY | 45:2aa9f933c8d2 | 119 | currentState = AudioPlayerState.BUFFER_UNDERRUN; |
| TMBOY | 45:2aa9f933c8d2 | 120 | Event event = createAudioPlayerEvent(ApiConstants.Events.PlaybackStutterStarted.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 121 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 122 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 123 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 124 | } |
| TMBOY | 45:2aa9f933c8d2 | 125 | |
| TMBOY | 45:2aa9f933c8d2 | 126 | public void playbackStutterFinished() { |
| TMBOY | 45:2aa9f933c8d2 | 127 | currentState = AudioPlayerState.PLAYING; |
| TMBOY | 45:2aa9f933c8d2 | 128 | Event event = createAudioPlayerPlaybackStutterFinishedEvent( |
| TMBOY | 45:2aa9f933c8d2 | 129 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 130 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds(), |
| TMBOY | 45:2aa9f933c8d2 | 131 | audioPlayStateReportListener.getStutterDurationInMilliseconds() |
| TMBOY | 45:2aa9f933c8d2 | 132 | ); |
| TMBOY | 45:2aa9f933c8d2 | 133 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 134 | } |
| TMBOY | 45:2aa9f933c8d2 | 135 | |
| TMBOY | 45:2aa9f933c8d2 | 136 | public void playbackStopped() { |
| TMBOY | 45:2aa9f933c8d2 | 137 | currentState = AudioPlayerState.STOPPED; |
| TMBOY | 45:2aa9f933c8d2 | 138 | Event event = createAudioPlayerEvent(ApiConstants.Events.PlaybackStopped.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 139 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 140 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 141 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 142 | } |
| TMBOY | 45:2aa9f933c8d2 | 143 | |
| TMBOY | 45:2aa9f933c8d2 | 144 | public void clearQueueAll() { |
| TMBOY | 45:2aa9f933c8d2 | 145 | Event event = createAudioPlayerPlaybackQueueClearedEvent(); |
| TMBOY | 45:2aa9f933c8d2 | 146 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 147 | if (currentState == AudioPlayerState.PLAYING || currentState == AudioPlayerState.PAUSED |
| TMBOY | 45:2aa9f933c8d2 | 148 | || currentState == AudioPlayerState.BUFFER_UNDERRUN) { |
| TMBOY | 45:2aa9f933c8d2 | 149 | currentState = AudioPlayerState.STOPPED; |
| TMBOY | 45:2aa9f933c8d2 | 150 | Event eventStopped = createAudioPlayerEvent(ApiConstants.Events.PlaybackStopped.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 151 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 152 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 153 | messageSender.sendEvent(eventStopped); |
| TMBOY | 45:2aa9f933c8d2 | 154 | } |
| TMBOY | 45:2aa9f933c8d2 | 155 | } |
| TMBOY | 45:2aa9f933c8d2 | 156 | |
| TMBOY | 45:2aa9f933c8d2 | 157 | public void clearQueueEnqueued() { |
| TMBOY | 45:2aa9f933c8d2 | 158 | Event event = createAudioPlayerPlaybackQueueClearedEvent(); |
| TMBOY | 45:2aa9f933c8d2 | 159 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 160 | } |
| TMBOY | 45:2aa9f933c8d2 | 161 | |
| TMBOY | 45:2aa9f933c8d2 | 162 | private Event createAudioPlayerEvent(String name, String streamToken, |
| TMBOY | 45:2aa9f933c8d2 | 163 | long offsetInMilliseconds) { |
| TMBOY | 45:2aa9f933c8d2 | 164 | Header header = new MessageIdHeader(namespace, name); |
| TMBOY | 45:2aa9f933c8d2 | 165 | Payload payload = new AudioPlayerPayload(streamToken, offsetInMilliseconds); |
| TMBOY | 45:2aa9f933c8d2 | 166 | return new Event(header, payload); |
| TMBOY | 45:2aa9f933c8d2 | 167 | } |
| TMBOY | 45:2aa9f933c8d2 | 168 | |
| TMBOY | 45:2aa9f933c8d2 | 169 | private Event createAudioPlayerPlaybackQueueClearedEvent() { |
| TMBOY | 45:2aa9f933c8d2 | 170 | Header header = new MessageIdHeader(ApiConstants.NAMESPACE, |
| TMBOY | 45:2aa9f933c8d2 | 171 | ApiConstants.Events.PlaybackQueueCleared.NAME); |
| TMBOY | 45:2aa9f933c8d2 | 172 | return new Event(header, new Payload()); |
| TMBOY | 45:2aa9f933c8d2 | 173 | } |
| TMBOY | 45:2aa9f933c8d2 | 174 | |
| TMBOY | 45:2aa9f933c8d2 | 175 | private Event createAudioPlayerPlaybackStutterFinishedEvent(String streamToken, |
| TMBOY | 45:2aa9f933c8d2 | 176 | long offsetInMilliseconds, |
| TMBOY | 45:2aa9f933c8d2 | 177 | long stutterDurationInMilliseconds) { |
| TMBOY | 45:2aa9f933c8d2 | 178 | Header header = new MessageIdHeader(ApiConstants.NAMESPACE, |
| TMBOY | 45:2aa9f933c8d2 | 179 | ApiConstants.Events.PlaybackStutterFinished.NAME); |
| TMBOY | 45:2aa9f933c8d2 | 180 | return new Event(header, new PlaybackStutterFinishedPayload(streamToken, |
| TMBOY | 45:2aa9f933c8d2 | 181 | offsetInMilliseconds, stutterDurationInMilliseconds)); |
| TMBOY | 45:2aa9f933c8d2 | 182 | } |
| TMBOY | 45:2aa9f933c8d2 | 183 | |
| TMBOY | 45:2aa9f933c8d2 | 184 | public void reportProgressDelay() { |
| TMBOY | 45:2aa9f933c8d2 | 185 | currentState = AudioPlayerState.PLAYING; |
| TMBOY | 45:2aa9f933c8d2 | 186 | Event event = createAudioPlayerEvent(ApiConstants.Events.ProgressReportDelayElapsed.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 187 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 188 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 189 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 190 | } |
| TMBOY | 45:2aa9f933c8d2 | 191 | |
| TMBOY | 45:2aa9f933c8d2 | 192 | public void reportProgressInterval() { |
| TMBOY | 45:2aa9f933c8d2 | 193 | currentState = AudioPlayerState.PLAYING; |
| TMBOY | 45:2aa9f933c8d2 | 194 | Event event = createAudioPlayerEvent(ApiConstants.Events.ProgressReportIntervalElapsed.NAME, |
| TMBOY | 45:2aa9f933c8d2 | 195 | audioPlayStateReportListener.getCurrentStreamToken(), |
| TMBOY | 45:2aa9f933c8d2 | 196 | audioPlayStateReportListener.getCurrentOffsetInMilliseconds()); |
| TMBOY | 45:2aa9f933c8d2 | 197 | messageSender.sendEvent(event); |
| TMBOY | 45:2aa9f933c8d2 | 198 | } |
| TMBOY | 45:2aa9f933c8d2 | 199 | |
| TMBOY | 45:2aa9f933c8d2 | 200 | public interface AudioPlayStateReportListener { |
| TMBOY | 45:2aa9f933c8d2 | 201 | String getCurrentStreamToken(); |
| TMBOY | 45:2aa9f933c8d2 | 202 | |
| TMBOY | 45:2aa9f933c8d2 | 203 | long getCurrentOffsetInMilliseconds(); |
| TMBOY | 45:2aa9f933c8d2 | 204 | |
| TMBOY | 45:2aa9f933c8d2 | 205 | long getStutterDurationInMilliseconds(); |
| TMBOY | 45:2aa9f933c8d2 | 206 | } |
| TMBOY | 45:2aa9f933c8d2 | 207 | } |
