Skip to content

Commit

Permalink
Fix packet fragmentation in python tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
tcamise-gpsw committed May 9, 2024
1 parent e354b19 commit 4e4ab17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

"""USB / wireless webcam demo"""

import sys
import argparse
import asyncio
import logging
from typing import Final

from rich.console import Console

from open_gopro import Params, WiredGoPro, WirelessGoPro
from open_gopro.api.params import WebcamFOV, WebcamResolution
from open_gopro.constants import WebcamError, WebcamStatus
from open_gopro.demos.gui.components.util import display_video_blocking
from open_gopro.gopro_base import GoProBase
Expand Down Expand Up @@ -39,6 +42,7 @@ async def poll_for_status() -> bool:
response = (await gopro.http_command.webcam_status()).data
if response.error != WebcamError.SUCCESS:
# Something bad happened
console.print(f"[yellow]Received webcam error: {response.error}")
return False
if response.status in statuses:
# We found the desired status
Expand All @@ -51,7 +55,7 @@ async def poll_for_status() -> bool:
return False


async def main(args: argparse.Namespace) -> None:
async def main(args: argparse.Namespace) -> int:
logger = setup_logging(__name__, args.log)
gopro: GoProBase | None = None

Expand Down Expand Up @@ -79,7 +83,9 @@ async def main(args: argparse.Namespace) -> None:
await wait_for_webcam_status(gopro, {WebcamStatus.OFF})

console.print("[blue]Starting webcam...")
await gopro.http_command.webcam_start()
if (status := (await gopro.http_command.webcam_start()).data.error) != WebcamError.SUCCESS:
console.print(f"[red]Couldn't start webcam: {status}")
return -1
await wait_for_webcam_status(gopro, {WebcamStatus.HIGH_POWER_PREVIEW})

# Start player
Expand All @@ -97,6 +103,8 @@ async def main(args: argparse.Namespace) -> None:
if gopro:
await gopro.close()

return 0


def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Setup and view a GoPro webcam using TS protocol.")
Expand All @@ -117,7 +125,7 @@ def parse_arguments() -> argparse.Namespace:

# Needed for poetry scripts defined in pyproject.toml
def entrypoint() -> None:
asyncio.run(main(parse_arguments()))
sys.exit(asyncio.run(main(parse_arguments())))


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def yield_fragmented_packets(payload: bytes) -> Generator[bytes, None, None]:
is_first_packet = True

# Build initial length header
if length < (2**5 - 1):
header = bytearray([length])
elif length < (2**13 - 1):
if length < (2**13 - 1):
header = bytearray((length | 0x2000).to_bytes(2, "big", signed=False))
elif length < (2**16 - 1):
header = bytearray((length | 0x6400).to_bytes(2, "big", signed=False))
Expand Down

0 comments on commit 4e4ab17

Please sign in to comment.