How do URLs get calculated for external access to media?

I am trying to figure out how to calculate the streaming URL to provide to an external device in order to play streaming media off from the Home Assistant’s local media collection. Does anyone have any pointers on whether this is surfaced somewhere? or where in the code the method to calculate the URL currently exists?

When I play media via the Home Assistant built in Play Media feature, it sends the device a URL that looks like:

https://[my HA machine]:8123/media/local/Alarm.mp3?authSig=[authkey]

But when I use the Media Selector in HA to choose a piece of media, it give me a media_content_id that contains a something like:

media-source://media_source/local/Alarm.mp3

I am trying to write a something that takes in the media URL as a parameter the same way the media_player.start would, but I can’t figure out how to get the local media_source URL converted into a remote URL with an auth token on it for the device.

Is this buried within the HA code in a way that one can’t get at it? Or is there another way to get this accomplished?

I was working on this a bit further and found this script that was helpful…

https://community.home-assistant.io/t/how-to-play-mp3-from-media-browser-folder/298206/24?u=mark4automation

Then the tricky part is packaging that up into a command line, passing it an auth token, getting back the values, parsing things apart, and stitching it all together. Not easy but it eventually works.

Still not sure why if the call to get this is available via the websockets, why can’t one get at it via templates or a service call within automation? Or why doesn’t it come back as part of the media object when you reference a media item?