Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bitrate parameter to RTSP cameras #184

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions rosys/vision/rtsp_camera/jovision_rtsp_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ def __init__(self, ip: str) -> None:
def settings_url(self) -> str:
return f'http://{self.ip}/cgi-bin/jvsweb.cgi'

@property
def headers(self) -> dict[str, str]:
return {'Cookie': 'passwdRule=1; username=admin; password=admin;'}

def _send_settings(self, settings: list[JovisionCameraSettings]) -> None:
"""
Set the settings for all streams
Expand All @@ -64,32 +60,47 @@ def _send_settings(self, settings: list[JovisionCameraSettings]) -> None:
'digest': '479ead4555b49227dc8812d06970c652',
},
'param': {
'channelid': 0,
'streams': streams,
},
}
params = {
'cmd': json.dumps(cmd),
'_': int(time.time() * 1000), # current time as a timestamp
}
requests.get(self.settings_url, params=params, headers=self.headers, timeout=1) # type: ignore
requests.get(self.settings_url, params=params, timeout=1) # type: ignore

def set_fps(self, stream_id, fps) -> None:
def set_fps(self, stream_id: int, fps: int) -> None:
current_settings = self.get_current_settings()
for settings in current_settings:
if settings.stream_id == stream_id:
settings.fps = fps
settings.fps = int(fps)
break
self._send_settings(current_settings)

def get_fps(self, stream_id) -> int | None:
def get_fps(self, stream_id: int) -> int | None:
current_settings = self.get_current_settings()
for settings in current_settings:
if settings.stream_id == stream_id:
return settings.fps

return None

def get_bitrate(self, stream_id: int) -> int | None:
current_settings = self.get_current_settings()
for settings in current_settings:
if settings.stream_id == stream_id:
return settings.bitrate

return None

def set_bitrate(self, stream_id: int, bitrate: int) -> None:
current_settings = self.get_current_settings()
for settings in current_settings:
if settings.stream_id == stream_id:
settings.bitrate = int(bitrate)
break
self._send_settings(current_settings)

def get_current_settings(self) -> list[JovisionCameraSettings]:
cmd = {
'method': 'stream_get_params',
Expand All @@ -105,7 +116,7 @@ def get_current_settings(self) -> list[JovisionCameraSettings]:
'cmd': json.dumps(cmd),
'_': int(time.time() * 1000),
}
response = requests.get(self.settings_url, params=params, headers=self.headers, timeout=1) # type: ignore
response = requests.get(self.settings_url, params=params, timeout=1) # type: ignore

return [
JovisionCameraSettings(
Expand Down Expand Up @@ -139,7 +150,7 @@ def get_parameter_ranges(self):
'cmd': json.dumps(cmd),
'_': time.time() * 1000,
}
response = requests.get(self.settings_url, params=params, headers=self.headers, timeout=1)
response = requests.get(self.settings_url, params=params, timeout=1)

for stream_id, stream in enumerate(response.json()['result']['all']):
print(f'stream {stream_id}')
Expand Down
15 changes: 14 additions & 1 deletion rosys/vision/rtsp_camera/rtsp_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self,
streaming: bool = True,
fps: int = 5,
jovision_profile: int = 1,
bitrate: int = 4096,
ip: str | None = None,
**kwargs,
) -> None:
Expand All @@ -37,9 +38,11 @@ def __init__(self,
self.ip: str | None = ip

self._register_parameter('jovision_profile', self.get_jovision_profile, self.set_jovision_profile,
min_value=1, max_value=2, step=1, default_value=jovision_profile)
min_value=0, max_value=1, step=1, default_value=jovision_profile)
self._register_parameter('fps', self.get_fps, self.set_fps,
min_value=1, max_value=30, step=1, default_value=fps)
self._register_parameter('bitrate', self.get_bitrate, self.set_bitrate,
min_value=32, max_value=8192, step=1, default_value=bitrate)

def to_dict(self) -> dict[str, Any]:
return super().to_dict() | {
Expand Down Expand Up @@ -127,6 +130,16 @@ def get_jovision_profile(self) -> int | None:

return self.device.get_jovision_profile()

def set_bitrate(self, bitrate: int) -> None:
assert self.device is not None

self.device.set_bitrate(bitrate)

def get_bitrate(self) -> int | None:
assert self.device is not None

return self.device.get_bitrate()

async def _apply_parameters(self, new_values: dict[str, Any], force_set: bool = False) -> None:
await super()._apply_parameters(new_values, force_set)
if self.is_connected:
Expand Down
9 changes: 9 additions & 0 deletions rosys/vision/rtsp_camera/rtsp_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,12 @@ def set_jovision_profile(self, profile: int) -> None:

def get_jovision_profile(self) -> int:
return self.jovision_profile

def set_bitrate(self, bitrate: int) -> None:
if self.settings_interface is not None:
self.settings_interface.set_bitrate(stream_id=self.jovision_profile, bitrate=bitrate)

def get_bitrate(self) -> int | None:
if self.settings_interface is not None:
return self.settings_interface.get_bitrate(stream_id=self.jovision_profile)
return None