Skip to content

Commit

Permalink
feat: add audio support? maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
fuatakgun committed Nov 23, 2023
1 parent 47c132b commit eada05e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions custom_components/eufy_security/eufy_security_api/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, api, serial_no: str, properties: dict, metadata: dict, comman
self.stream_url: str = None

self.video_queue = asyncio.Queue()
self.audio_queue = asyncio.Queue()
self.config = config
self.voices = voices
self.image_last_updated = None
Expand Down Expand Up @@ -84,6 +85,7 @@ async def _handle_livestream_stopped(self, event: Event):
_LOGGER.debug(f"_handle_livestream_stopped - {event}")
self.stream_status = StreamStatus.IDLE
self.video_queue = asyncio.Queue()
self.audio_queue = asyncio.Queue()

async def _handle_rtsp_livestream_started(self, event: Event):
# automatically find this function for respective event
Expand All @@ -99,8 +101,7 @@ async def _handle_livestream_video_data_received(self, event: Event):
await self.video_queue.put(event.data["buffer"]["data"])

async def _handle_livestream_audio_data_received(self, event: Event):
pass
#await self.video_queue.put(event.data["buffer"]["data"])
await self.audio_queue.put(event.data["buffer"]["data"])

async def _initiate_start_stream(self, stream_type) -> bool:
self.set_stream_prodiver(stream_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ class P2PStreamer:
def __init__(self, camera) -> None:
self.camera = camera

async def chunk_generator(self):
async def chunk_generator(self, queue):
while True:
try:
item = await asyncio.wait_for(self.camera.video_queue.get(), timeout=2.5)
item = await asyncio.wait_for(queue.get(), timeout=2.5)
_LOGGER.debug(f"chunk_generator yield data - {len(item)}")
yield bytearray(item)
except TimeoutError as te:
_LOGGER.debug(f"chunk_generator timeout Exception %s - traceback: %s", te, traceback.format_exc())
raise te

async def write_bytes(self):
async def write_bytes(self, queue):
url = GO2RTC_API_URL.format(self.camera.config.rtsp_server_address, GO2RTC_API_PORT)
url = f"{url}?dst={str(self.camera.serial_no)}"

retry = False
try:
async with aiohttp.ClientSession() as session:
resp = await session.post(url, data = self.chunk_generator(), timeout=aiohttp.ClientTimeout(total=None, connect=5))
resp = await session.post(url, data = self.chunk_generator(queue), timeout=aiohttp.ClientTimeout(total=None, connect=5))
_LOGGER.debug(f"write_bytes - post response - {resp.status} - {await resp.text()}")

except (asyncio.exceptions.TimeoutError, asyncio.exceptions.CancelledError) as ex:
Expand Down Expand Up @@ -67,7 +67,9 @@ async def start(self):
"""start streaming thread"""
# send API command to go2rtc to create a new stream
await self.create_stream_on_go2rtc()
asyncio.get_event_loop().create_task(self.write_bytes())
asyncio.get_event_loop().create_task(self.write_bytes(self.camera.video_queue))
asyncio.get_event_loop().create_task(self.write_bytes(self.camera.audio_queue))


async def stop(self):
await self.camera.check_and_stop_livestream()
Expand Down

0 comments on commit eada05e

Please sign in to comment.