JeffLIrion / python-androidtv

Communicate with an Android TV or Fire TV device via ADB over a network.
MIT License
165 stars 57 forks source link

Media session title #250

Open Drafteed opened 3 years ago

Drafteed commented 3 years ago

Hello @JeffLIrion! Thx for work!

dumpsys media_session return the title of the content being played in the metadata:description field for apps that use media_session API (YouTube, Spotify, ViMu, Netflix, TiviMate etc..).

Is it possible to make this information used in HA media_title, media_artist, media_album if it provided and active? Like Google Cast integration do. Also i see current media position meta.


  6 sessions listeners.
  Global priority session is null
  User Records:
  Record for full_user=0
    Volume key long-press listener: null
    Volume key long-press listener package: 
    Media key listener: null
    Media key listener package: 
    Callback: android.media.session.ICallback$Stub$Proxy@7f2bc79
    Last MediaButtonReceiver: null
    Restored MediaButtonReceiver: null
    Media button session is com.google.android.youtube.tv/starboard_media (userId=0)
    Sessions Stack - have 4 sessions:
      starboard_media com.google.android.youtube.tv/starboard_media (userId=0)
        ownerPid=25681, ownerUid=10048, userId=0
        package=com.google.android.youtube.tv
        launchIntent=null
        mediaButtonReceiver=null
        active=true
        flags=3
        rating type=0
        controllers: 12
        state=PlaybackState {state=3, position=1980379, buffered position=0, speed=1.0, updated=38059080, actions=379, custom actions=[], active item id=-1, error=null}
        audioAttrs=AudioAttributes: usage=USAGE_MEDIA content=CONTENT_TYPE_UNKNOWN flags=0x0 tags= bundle=null
        volumeType=1, controlType=2, max=0, current=0
        metadata:size=5, description=Introducing iPad Pro | Apple, Apple, null
        queueTitle=null, size=0

      spotify-android-tv-media-session com.spotify.tv.android/spotify-android-tv-media-session (userId=0)
        ownerPid=4636, ownerUid=10071, userId=0
        package=com.spotify.tv.android
        launchIntent=null
        mediaButtonReceiver=PendingIntent{8775afa: PendingIntentRecord{fe618ab com.spotify.tv.android broadcastIntent}}
        active=false
        flags=3
        rating type=0
        controllers: 0
        state=PlaybackState {state=0, position=30357, buffered position=0, speed=1.0, updated=36002007, actions=2368383, custom actions=[], active item id=-1, error=null}
        audioAttrs=AudioAttributes: usage=USAGE_MEDIA content=CONTENT_TYPE_UNKNOWN flags=0x0 tags= bundle=null
        volumeType=1, controlType=2, max=0, current=0
        metadata:size=7, description=Counting Days Till Suicide, REDZED, Ecstasy
        queueTitle=null, size=0

      Netflix media session com.netflix.ninja/Netflix media session (userId=0)
        ownerPid=5525, ownerUid=10063, userId=0
        package=com.netflix.ninja
        launchIntent=null
        mediaButtonReceiver=null
        active=false
        flags=3
        rating type=0
        controllers: 0
        state=null
        audioAttrs=AudioAttributes: usage=USAGE_MEDIA content=CONTENT_TYPE_UNKNOWN flags=0x0 tags= bundle=null
        volumeType=1, controlType=2, max=0, current=0
        metadata:size=0, description=null
        queueTitle=null, size=0

      starboard_media com.google.android.youtube.tv/starboard_media (userId=0)
        ownerPid=25681, ownerUid=10048, userId=0
        package=com.google.android.youtube.tv
        launchIntent=null
        mediaButtonReceiver=null
        active=false
        flags=3
        rating type=0
        controllers: 0
        state=PlaybackState {state=0, position=0, buffered position=0, speed=0.0, updated=0, actions=0, custom actions=[], active item id=-1, error=null}
        audioAttrs=AudioAttributes: usage=USAGE_MEDIA content=CONTENT_TYPE_UNKNOWN flags=0x0 tags= bundle=null
        volumeType=1, controlType=2, max=0, current=0
        metadata:size=0, description=null, null, null
        queueTitle=null, size=0

  Audio playback (lastly played comes first)
    uid=10048 packages=com.google.android.youtube.tv
JeffLIrion commented 3 years ago

Short answer: that should work.

Long answer...

When I first created this package, the ADB library was not very reliable for newer devices. In an effort to make it work better, I used a single ADB command for the update function. This wasn't so bad at first, the command was reasonably short. But as more info got collected by the update function, this ADB command grew in complexity.

To accomplish what you're describing, it's probably better to split the update function into multiple ADB commands, since the ADB library that it uses now (adb-shell) is pretty reliable. First, check simple stuff, like if the device is on and awake. Then check stuff like the current app and the fields you mentioned. And maybe instead of using a complicated grep command to parse the dumpsys output on the device, just return the full output and parse it in this package.