Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Karolk99 committed Oct 1, 2024
1 parent 0deb6c8 commit 7d7d4d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 72 deletions.
78 changes: 20 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

# Fishjam Python Server SDK

[![CircleCI](https://dl.circleci.com/status-badge/img/gh/fishjam-cloud/python-server-sdk/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/fishjam-cloud/python-server-sdk/tree/main)

Python server SDK for the [Fishjam Media Server](https://github.com/fishjam-cloud/fishjam).
Python server SDK for the [Fishjam Cloud](https://fishjam.io/).

Read the docs [here](https://fishjam-cloud.github.io/python-server-sdk)

Expand All @@ -16,42 +14,37 @@ pip install fishjam-server-sdk

## Usage

The SDK exports two main classes for interacting with Fishjam server: `RoomApi` and `Notifier`.
The SDK exports two main classes for interacting with Fishjam server:
`FishjamClient` and `Notifier`.

`RoomApi` wraps http REST api calls, while `Notifier` is responsible for receiving real-time updates from the server.
`FishjamClient` wraps http REST api calls, while `Notifier` is responsible for receiving real-time updates from the server.

#### RoomApi
#### FishjamClient

Create a `RoomApi` instance, providing the fishjam server address and api token
Create a `FishjamClient` instance, providing the fishjam server address and api token

```python
from fishjam import RoomApi
from fishjam import FishjamClient

room_api = RoomApi(server_address="localhost:5002", server_api_token="development")
fishjam_client = FishjamClient(fishjam_url="localhost:5002", management_token="development")
```

You can use it to interact with Fishjam, manage rooms, peers and components
You can use it to interact with Fishjam Cloud to manage rooms and peers

```python
# Create a room
fishjam_address, room = room_api.create_room(video_codec="h264", webhook_url="http://localhost:5000/webhook")
# '127.0.0.1:5002', Room(components=[], config=RoomConfig(max_peers=None, video_codec=<RoomConfigVideoCodec.H264: 'h264'>, webhook_url='http://localhost:5000/webhook'), id='1d905478-ccfc-44d6-a6e7-8ccb1b38d955', peers=[])

# Add peer to the room
from fishjam import PeerOptionsWebRTC
options = RoomOptions(video_codec="h264", webhook_url="http://localhost:5000/webhook")
room = room_api.create_room(options=options)

peer_token, peer_webrtc = room_api.add_peer(room.id, options=PeerOptionsWebRTC())
# 'M8TUGhj-L11KpyG-2zBPIo', Peer(id='b1232c7e-c969-4450-acdf-ea24f3cdd7f6', status=<PeerStatus.DISCONNECTED: 'disconnected'>, type='webrtc')
# Room(components=[], config=RoomConfig(max_peers=None, video_codec=<RoomConfigVideoCodec.H264: 'h264'>, webhook_url='http://localhost:5000/webhook'), id='1d905478-ccfc-44d6-a6e7-8ccb1b38d955', peers=[])

# Add component to the room
from fishjam import ComponentOptionsHLS
# Add peer to the room
peer, token = room_api.create_peer(room.id)

component_hls = room_api.add_component(room.id, options=ComponentOptionsHLS())
# ComponentHLS(id='5f062447-a9f7-45ed-8d1b-511f77dc78ae', properties=ComponentPropertiesHLS(low_latency=False, persistent=False, playable=False, subscribe_mode=<ComponentPropertiesHLSSubscribeMode.AUTO: 'auto'>, target_window_duration=None), type='hls')
# Peer(id='b1232c7e-c969-4450-acdf-ea24f3cdd7f6', status=<PeerStatus.DISCONNECTED: 'disconnected'>, type='webrtc'), 'M8TUGhj-L11KpyG-2zBPIo'
```

All methods in `RoomApi` may raise one of the exceptions deriving from `fishjam.errors.HTTPError`. They are defined in
`fishjam.errors`.
All methods in `FishjamClient` may raise one of the exceptions deriving from `fishjam.errors.HTTPError`. They are defined in `fishjam.errors`.

#### Notifier

Expand All @@ -65,19 +58,14 @@ Create `Notifier` instance
```python
from fishjam import Notifier

notifier = Notifier(server_address='localhost:5002', server_api_token='development')
notifier = Notifier(fishjam_url='localhost:5002', management_token='development')
```

Then define handlers for incoming messages

Then define a handler for incoming messages
```python
@notifier.on_server_notification
def handle_notification(server_notification):
print(f'Received a notification: {server_notification}')

@notifier.on_metrics
def handle_metrics(metrics_report):
print(f'Received WebRTC metrics: {metrics_report}')
```

After that you can start the notifier
Expand All @@ -90,40 +78,14 @@ async def test_notifier():
await notifier.wait_ready()

# Create a room to trigger a server notification
room_api = RoomApi()
room_api.create_room()
fishjam_client = FishjamClient()
fishjam_client.create_room()

await notifier_task

asyncio.run(test_notifier())

# Received a notification: ServerMessageRoomCreated(room_id='69a3fd1a-6a4d-47bc-ae54-0c72b0d05e29')
# Received WebRTC metrics: ServerMessageMetricsReport(metrics='{}')
```

#### Cluster of Fishjams

The cluster of fishjams has got embedded load balancer, which means that a new room will be created on fishjam with the
least usage. At the moment to modify this specific room you must communicate with the fishjam on which this room was
created.

```python
room_api = RoomApi(server_address='localhost:5002')

# Create a room to trigger a server notification with h264 as a codec,
# that allow to use HLS.
address, room = room_api.create_room(video_codec="h264")

# Create new room api with returned fishjam address as a room could be
# created on a different fishjam instance
# (if you communicate with a cluster of fishjams)
new_room_api = RoomApi(server_address=address)

# Add HLS component with manual subscribe mode, we use here `new_room_api` as we are sure that this API refers to the fishjam on which this room was created.
_hls_component = new_room_api.add_component(
room.id,
ComponentOptionsHLS(subscribe_mode=ComponentOptionsHLSSubscribeMode.MANUAL),
)
```

## Testing
Expand Down
13 changes: 0 additions & 13 deletions poetry_scripts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -30,18 +29,6 @@ def run_tests():
)
check_exit_code("docker compose -f docker-compose-test.yaml down")


def run_examples():
print("Start examples")

examples = os.listdir("./examples")

for example in examples:
check_exit_code(f"python ./examples/{example}")
print(f"After example from file: {example}")
print("All examples run without errors")


def run_local_test():
check_exit_code('poetry run pytest -m "not file_component_sources"')

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ lint = "poetry_scripts:run_linter"
fix_lint = "poetry_scripts:run_linter_fix"
generate_docs = "poetry_scripts:generate_docs"
update_client = "poetry_scripts:update_client"
examples = "poetry_scripts:run_examples"

[tool.ruff]
select = ["F", "I"]
Expand Down

0 comments on commit 7d7d4d0

Please sign in to comment.