Kane610 / axis

Python library enabling easy communication with Axis devices and its metadatastream
MIT License
30 stars 14 forks source link

Support for speakers as media_player entity in HA #126

Open WhistleMaster opened 2 years ago

WhistleMaster commented 2 years ago

Hi,

Thanks for the great work !

I've recently installed a new AXIS I8016-LVE and I was wondering if the internal speaker could be seen in HA as a media_player entity. The Synology surveillance client can send any sound file to the speaker, so there may be a way I guess. Then, that could also be applied to other AXIS Network Speakers such as the C1410.

What information would you need that could help ?

Thanks ! Kind regards,

WM

Kane610 commented 2 years ago

It's on my list to investigate. I'm rewriting deconz integration right now. Then I'll solve some bugs with unifi then I hope to get back to the Axis integration

WhistleMaster commented 2 years ago

Great news ! I would be happy to test and provide information if needed.

Kane610 commented 2 years ago

Don't expect it to happen before summer though :/

I have too much to do which is the main issue

WhistleMaster commented 2 years ago

Summer is the best time to test ;-) No worries !

WhistleMaster commented 1 year ago

I'm doing some tests in the meanwhile and I can send sample sounds to the internal speaker with the following command:

ffmpeg -i <sound_file.mp3> -probesize 32 -analyzeduration 32 -c:a pcm_mulaw -ab 128k -ac 1 -ar 16000 -f wav -chunked_post 0 -content_type audio/axis-mulaw-128 "http://<user>:<password>@<cam_ip>/axis-cgi/audio/transmit.cgi"

Kane610 commented 1 year ago

Oo wow! Awesome job.

WhistleMaster commented 1 year ago

Thanks ! Just some good old google-fu to be honest 😇 nothing more. I would like to help to implement that in your add-on but not sure if that really helps... It's a start but I guess that's something that should use some POST to send the sound file to the transmit.cgi and see it as a media_player entity.

Kane610 commented 1 year ago

If you can just give me a proof of concept in python code I can formalize it afterwards

WhistleMaster commented 1 year ago

I've tried to do something like that, as a PoC, which works fine:

import requests
from requests.auth import HTTPDigestAuth
from pydub import AudioSegment
from pydub.utils import mediainfo

# Set Auth
auth = HTTPDigestAuth("<USER>", "<PASSWORD>")

# Set the server URL
server_url = "http://<IP>/axis-cgi/audio/transmit.cgi"

# Open the MP3 file
input_file = AudioSegment.from_mp3("sound.mp3")

# Convert the MP3 file to WAV format
input_file = input_file.split_to_mono()[0]
input_file = input_file.set_frame_rate(16000)
input_file = input_file.export(
    "output.wav", format="wav", codec="pcm_mulaw", bitrate="128k")

# Print some info about the WAV
print(mediainfo("output.wav"))

# Read the WAV file and get its data
with open("output.wav", "rb") as f:
    file_data = f.read()

# Set the headers for the HTTP POST request
headers = {
    "Content-Type": "audio/axis-mulaw-128",
    "Content-Length": str(len(file_data)),
}

try:
    # Send the POST request with the file data and headers
    response = requests.post(server_url, data=file_data,
                             auth=auth, headers=headers)
    print(response.text)
    response.raise_for_status()
except requests.exceptions.HTTPError as errh:
    print("Http Error:", errh)
except requests.exceptions.ConnectionError as errc:
    print("Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
    print("Timeout Error:", errt)
except requests.exceptions.RequestException as err:
    print("OOps: Something Else", err)

I've also found some documentation here: https://www.axis.com/vapix-library/subjects/t10100065/section/t10036015/display

Kane610 commented 1 year ago

Sweet! I've taken my first small steps into the refactoring of the Axis library so once I'm further into that I can start looking at adding new things as it would be counterproductive to expand something that will be rewritten shortly after.

jonoberheide commented 1 week ago

FYI, I posted a PR for speaker support in HA here: https://github.com/home-assistant/core/pull/130163

Kane610 commented 1 week ago

Awesome 🎉