Skip to content

Commit

Permalink
further simplify and dedupe media player state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffc committed Nov 11, 2024
1 parent e715eb7 commit b894fda
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,61 @@ public void onEvents(Player p, Player.Events events) {

// Loop over each media player event in this tick and fire it back
// to JS
//
// We don't want to send duplicate messages, so keep track of
// the types we might need to deduplicate
boolean hasPlaybackStateChangedEvent = false;
boolean hasIsPlayingChangedEvent = false;
for (int i = 0; i < events.size(); i++) {
hasPlaybackStateChangedEvent |=
events.get(i) == Player.EVENT_PLAYBACK_STATE_CHANGED;
hasIsPlayingChangedEvent |= events.get(i) == Player.EVENT_IS_PLAYING_CHANGED;
}

for (int i = 0; i < events.size(); i++) {
@Player.Event int e = events.get(i);
ClientEvent.Builder protob = ClientEvent.newBuilder();

// some common info
@Player.Event int playbackState = p.getPlaybackState();
boolean isPlaying = p.isPlaying();

// Don't send duplicate messages:
// - If we have both IS_PLAYING_CHANGED and
// PLAYBACK_STATE_CHANGED events, ignore one or the
// other.
if (hasIsPlayingChangedEvent && hasPlaybackStateChangedEvent) {
if ((isPlaying && e == Player.EVENT_PLAYBACK_STATE_CHANGED)
|| (!isPlaying && e == Player.EVENT_IS_PLAYING_CHANGED)) {
continue;
}
}

switch (e) {
case Player.EVENT_PLAYBACK_STATE_CHANGED:
case Player.EVENT_IS_PLAYING_CHANGED:
ClientEvent.MediaPlayerStateChange.Builder b =
ClientEvent.MediaPlayerStateChange.newBuilder().setPlayer(which_player);
switch (p.getPlaybackState()) {
case Player.STATE_IDLE:
b.setNewState(MediaPlayerState.STATE_IDLE);
break;
case Player.STATE_BUFFERING:
b.setNewState(MediaPlayerState.STATE_BUFFERING);
break;
case Player.STATE_READY:
if (p.isPlaying()) {
b.setNewState(MediaPlayerState.STATE_PLAYING);
} else {
b.setNewState(MediaPlayerState.STATE_PAUSED);
}
break;
case Player.STATE_ENDED:
b.setNewState(MediaPlayerState.STATE_ENDED);
break;

MediaPlayerState newState = null;

if (playbackState == Player.STATE_READY) {
newState =
(isPlaying)
? MediaPlayerState.STATE_PLAYING
: MediaPlayerState.STATE_PAUSED;
} else if (playbackState == Player.STATE_BUFFERING) {
newState = MediaPlayerState.STATE_BUFFERING;
} else if (playbackState == Player.STATE_IDLE
|| playbackState == Player.STATE_ENDED) {
newState = MediaPlayerState.STATE_IDLE;
} else {
Log.w(
"HassmicBackgroundTaskService",
"Unhandled playback state: " + playbackState);
}

b.setNewState(newState);
protob.setMediaPlayerStateChange(b.build());
break;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions app/app/proto/hassmic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,13 @@ export enum MediaPlayerState {
*/
STATE_BUFFERING = 2,
/**
* @generated from protobuf enum value: STATE_READY = 3;
* @generated from protobuf enum value: STATE_PLAYING = 3;
*/
STATE_READY = 3,
STATE_PLAYING = 3,
/**
* @generated from protobuf enum value: STATE_ENDED = 4;
* @generated from protobuf enum value: STATE_PAUSED = 4;
*/
STATE_ENDED = 4,
/**
* @generated from protobuf enum value: STATE_PLAYING = 5;
*/
STATE_PLAYING = 5,
/**
* @generated from protobuf enum value: STATE_PAUSED = 6;
*/
STATE_PAUSED = 6
STATE_PAUSED = 4
}
/**
* The different media players available
Expand Down
3 changes: 1 addition & 2 deletions custom_components/hassmic/hassmic.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _handle_connection_state_change(self, new_state: bool):

def _handle_client_event(self, event: ClientEvent):
"""Handle a state change from the connection manager."""
_LOGGER.debug("Got connection change to state: %s", repr(event))
_LOGGER.debug("Got client event: %s", repr(event))
for e in self._entities:
hce = getattr(e, "handle_client_event", None)
if hce is not None and callable(hce):
Expand Down Expand Up @@ -175,7 +175,6 @@ async def handle_incoming_message(self, reader) -> ClientInfo:
_LOGGER.debug("Got client info: %s", repr(val))

case "client_event":
_LOGGER.debug("Got client event: %s", repr(val))
self._handle_client_event(val)

case "ping":
Expand Down
4 changes: 1 addition & 3 deletions custom_components/hassmic/media_player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ def handle_client_event(self, event: proto.ClientEvent):
self._attr_state = MediaPlayerState.PLAYING
case proto.MediaPlayerState.STATE_PAUSED:
self._attr_state = MediaPlayerState.PAUSED
case proto.MediaPlayerState.STATE_ENDED | proto.MediaPlayerState.STATE_IDLE:
case proto.MediaPlayerState.STATE_IDLE:
self._attr_state = MediaPlayerState.IDLE
case proto.MediaPlayerState.STATE_READY:
pass
case _:
_LOGGER.warning(
"Got unhandled media player state %s", val.new_state
Expand Down
6 changes: 2 additions & 4 deletions custom_components/hassmic/proto/hassmic.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions proto/hassmic.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ enum MediaPlayerState {
STATE_UNKNOWN = 0;
STATE_IDLE = 1;
STATE_BUFFERING = 2;
STATE_READY = 3;
STATE_ENDED = 4;
STATE_PLAYING = 5;
STATE_PAUSED = 6;
STATE_PLAYING = 3;
STATE_PAUSED = 4;
}

// The different media players available
Expand Down

0 comments on commit b894fda

Please sign in to comment.