Skip to content

Commit 6767b32

Browse files
committed
update documentations
1 parent 5f99635 commit 6767b32

File tree

5 files changed

+366
-27
lines changed

5 files changed

+366
-27
lines changed

docs/index.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,25 @@ This will create a plugin handle and then destroy it.
5050

5151
Notice that we don't need to call connect or disconnect explicitly. It's managed internally.
5252

53-
### Make Video Calls
53+
### Make Video Calls (Outgoing)
5454

5555
```python
5656
import asyncio
5757
from janus_client import JanusSession, JanusVideoCallPlugin
5858
from aiortc.contrib.media import MediaPlayer, MediaRecorder
59+
from aiortc import RTCConfiguration, RTCIceServer
5960

6061
async def main():
6162
# Create session
6263
session = JanusSession(
6364
base_url="wss://janusmy.josephgetmyip.com/janusbasews/janus",
6465
)
6566

66-
# Create plugin
67-
plugin_handle = JanusVideoCallPlugin()
67+
# Create plugin (optionally with WebRTC configuration)
68+
config = RTCConfiguration(iceServers=[
69+
RTCIceServer(urls='stun:stun.l.google.com:19302')
70+
])
71+
plugin_handle = JanusVideoCallPlugin(pc_config=config)
6872

6973
# Attach to Janus session
7074
await plugin_handle.attach(session=session)
@@ -116,3 +120,62 @@ This example will register to the VideoCall plugin using username `testusernameo
116120

117121
A portion of the screen will be captured and sent in the call media stream.
118122
The incoming media stream will be saved into `videocall_record_out.mp4` file.
123+
124+
### Receive Video Calls (Incoming)
125+
126+
```python
127+
import asyncio
128+
from janus_client import JanusSession, JanusVideoCallPlugin, VideoCallEventType
129+
from aiortc.contrib.media import MediaPlayer, MediaRecorder
130+
131+
async def main():
132+
# Create session
133+
session = JanusSession(
134+
base_url="wss://janusmy.josephgetmyip.com/janusbasews/janus",
135+
)
136+
137+
# Create plugin
138+
plugin_handle = JanusVideoCallPlugin()
139+
140+
# Attach to Janus session
141+
await plugin_handle.attach(session=session)
142+
143+
# Register username
144+
username = "testusernamein"
145+
await plugin_handle.register(username=username)
146+
147+
# Set up event handler for incoming calls
148+
async def on_incoming_call(data):
149+
print(f"Incoming call from {data['username']}")
150+
151+
# Get JSEP from event data
152+
jsep = data['jsep']
153+
154+
# Set up media
155+
player = MediaPlayer("input.mp4")
156+
recorder = MediaRecorder("./videocall_record_in.mp4")
157+
158+
# Accept the call with JSEP
159+
await plugin_handle.accept(jsep, player, recorder)
160+
print("Call accepted")
161+
162+
# Register the event handler
163+
plugin_handle.on_event(VideoCallEventType.INCOMINGCALL, on_incoming_call)
164+
165+
# Wait for incoming calls
166+
print(f"Waiting for calls as '{username}'...")
167+
await asyncio.sleep(60)
168+
169+
# Cleanup
170+
await plugin_handle.destroy()
171+
await session.destroy()
172+
173+
174+
if __name__ == "__main__":
175+
try:
176+
asyncio.run(main())
177+
except KeyboardInterrupt:
178+
pass
179+
```
180+
181+
This example demonstrates the event-driven API for handling incoming calls. The plugin uses callbacks to notify you of incoming calls, and you can accept them by calling `accept()` with the JSEP data from the event.

docs/plugins.md

Lines changed: 212 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,51 @@ if __name__ == "__main__":
4848

4949
## VideoCall Plugin
5050

51-
The VideoCall plugin enables one-to-one video calls between users. It handles user registration, call initiation, and call management.
51+
The VideoCall plugin enables one-to-one video calls between users. It handles user registration, call initiation, and call management using an event-driven architecture.
5252

53-
### VideoCall Usage Example
53+
### VideoCall Features
54+
55+
- **Event-Driven Architecture**: Uses callbacks for handling call events
56+
- **User Registration**: Register unique usernames for receiving calls
57+
- **Call Management**: Initiate, accept, and hang up calls
58+
- **Media Control**: Configure audio/video settings and bitrate
59+
- **WebRTC Configuration**: Optional custom STUN/TURN server configuration
60+
61+
### VideoCall Usage Examples
62+
63+
#### Making Outgoing Calls
5464

5565
```python
5666
import asyncio
57-
from janus_client import JanusSession, JanusVideoCallPlugin
67+
from janus_client import JanusSession, JanusVideoCallPlugin, VideoCallEventType
5868
from aiortc.contrib.media import MediaPlayer, MediaRecorder
69+
from aiortc import RTCConfiguration, RTCIceServer
5970

6071
async def main():
6172
session = JanusSession(base_url="wss://example.com/janus")
62-
plugin = JanusVideoCallPlugin()
73+
74+
# Optional: Configure WebRTC settings
75+
config = RTCConfiguration(iceServers=[
76+
RTCIceServer(urls='stun:stun.l.google.com:19302')
77+
])
78+
plugin = JanusVideoCallPlugin(pc_config=config)
6379

6480
async with session:
6581
await plugin.attach(session)
6682

6783
# Register with a username
6884
await plugin.register(username="caller")
6985

86+
# Set up event handlers
87+
async def on_accepted(data):
88+
print(f"Call accepted by {data['username']}")
89+
90+
async def on_hangup(data):
91+
print(f"Call ended: {data.get('reason', 'Unknown')}")
92+
93+
plugin.on_event(VideoCallEventType.ACCEPTED, on_accepted)
94+
plugin.on_event(VideoCallEventType.HANGUP, on_hangup)
95+
7096
# Make a call
7197
player = MediaPlayer("input.mp4")
7298
recorder = MediaRecorder("output.mp4")
@@ -83,6 +109,188 @@ if __name__ == "__main__":
83109
asyncio.run(main())
84110
```
85111

112+
#### Receiving Incoming Calls
113+
114+
```python
115+
import asyncio
116+
from janus_client import JanusSession, JanusVideoCallPlugin, VideoCallEventType
117+
from aiortc.contrib.media import MediaPlayer, MediaRecorder
118+
119+
async def main():
120+
session = JanusSession(base_url="wss://example.com/janus")
121+
plugin = JanusVideoCallPlugin()
122+
123+
async with session:
124+
await plugin.attach(session)
125+
126+
# Register username to receive calls
127+
await plugin.register(username="callee")
128+
129+
# Set up event handler for incoming calls
130+
async def on_incoming_call(data):
131+
print(f"Incoming call from {data['username']}")
132+
133+
# Get JSEP from event data
134+
jsep = data['jsep']
135+
136+
# Set up media
137+
player = MediaPlayer("input.mp4")
138+
recorder = MediaRecorder("output.mp4")
139+
140+
# Accept the call with JSEP
141+
await plugin.accept(jsep, player, recorder)
142+
print("Call accepted")
143+
144+
async def on_hangup(data):
145+
print(f"Call ended: {data.get('reason', 'Unknown')}")
146+
147+
# Register event handlers
148+
plugin.on_event(VideoCallEventType.INCOMINGCALL, on_incoming_call)
149+
plugin.on_event(VideoCallEventType.HANGUP, on_hangup)
150+
151+
# Wait for incoming calls
152+
print("Waiting for calls...")
153+
await asyncio.sleep(300) # Wait 5 minutes
154+
155+
await plugin.destroy()
156+
157+
if __name__ == "__main__":
158+
asyncio.run(main())
159+
```
160+
161+
#### Listing Available Users
162+
163+
```python
164+
import asyncio
165+
from janus_client import JanusSession, JanusVideoCallPlugin
166+
167+
async def main():
168+
session = JanusSession(base_url="wss://example.com/janus")
169+
plugin = JanusVideoCallPlugin()
170+
171+
async with session:
172+
await plugin.attach(session)
173+
174+
# List all registered users
175+
users = await plugin.list_users()
176+
print(f"Available users: {users}")
177+
178+
await plugin.destroy()
179+
180+
if __name__ == "__main__":
181+
asyncio.run(main())
182+
```
183+
184+
#### Media Control During Call
185+
186+
```python
187+
import asyncio
188+
from janus_client import JanusSession, JanusVideoCallPlugin
189+
from aiortc.contrib.media import MediaPlayer, MediaRecorder
190+
191+
async def main():
192+
session = JanusSession(base_url="wss://example.com/janus")
193+
plugin = JanusVideoCallPlugin()
194+
195+
async with session:
196+
await plugin.attach(session)
197+
await plugin.register(username="caller")
198+
199+
# Make a call
200+
player = MediaPlayer("input.mp4")
201+
recorder = MediaRecorder("output.mp4")
202+
await plugin.call(username="callee", player=player, recorder=recorder)
203+
204+
# Wait a bit
205+
await asyncio.sleep(10)
206+
207+
# Mute audio
208+
await plugin.set_media(audio=False)
209+
210+
# Wait a bit more
211+
await asyncio.sleep(5)
212+
213+
# Unmute audio and set bitrate limit
214+
await plugin.set_media(audio=True, bitrate=256000)
215+
216+
# Continue call
217+
await asyncio.sleep(15)
218+
219+
await plugin.hangup()
220+
await plugin.destroy()
221+
222+
if __name__ == "__main__":
223+
asyncio.run(main())
224+
```
225+
226+
### VideoCall Event Types
227+
228+
The plugin supports the following event types via `VideoCallEventType` enum:
229+
230+
- **REGISTERED**: User successfully registered
231+
- **CALLING**: Call is being initiated
232+
- **INCOMINGCALL**: Incoming call received (includes JSEP data)
233+
- **ACCEPTED**: Call was accepted by remote peer
234+
- **UPDATE**: Call parameters updated (may include JSEP for renegotiation)
235+
- **HANGUP**: Call ended
236+
- **SET**: Media settings changed
237+
238+
### VideoCall Best Practices
239+
240+
#### Event Handler Registration
241+
242+
```python
243+
# Register handlers before making or receiving calls
244+
plugin.on_event(VideoCallEventType.INCOMINGCALL, on_incoming_call)
245+
plugin.on_event(VideoCallEventType.ACCEPTED, on_accepted)
246+
plugin.on_event(VideoCallEventType.HANGUP, on_hangup)
247+
248+
# Then proceed with registration and calls
249+
await plugin.register(username="myuser")
250+
```
251+
252+
#### Error Handling
253+
254+
```python
255+
from janus_client import VideoCallError
256+
257+
try:
258+
await plugin.register(username="alice")
259+
await plugin.call(username="bob", player=player, recorder=recorder)
260+
except VideoCallError as e:
261+
print(f"Call failed: {e.error_message} (code: {e.error_code})")
262+
```
263+
264+
#### Resource Cleanup
265+
266+
```python
267+
try:
268+
# Use the plugin
269+
await plugin.register(username="alice")
270+
await plugin.call(username="bob", player=player, recorder=recorder)
271+
await asyncio.sleep(30)
272+
finally:
273+
# Always cleanup
274+
try:
275+
if plugin.in_call:
276+
await plugin.hangup()
277+
except:
278+
pass
279+
await plugin.destroy()
280+
```
281+
282+
#### Handling Call State
283+
284+
```python
285+
# Check if currently in a call
286+
if plugin.in_call:
287+
print("Currently in a call")
288+
289+
# Get registered username
290+
if plugin.username:
291+
print(f"Registered as: {plugin.username}")
292+
```
293+
86294
## VideoRoom Plugin
87295

88296
The VideoRoom plugin enables multi-party video conferencing. It supports room management, publishing, and subscribing to multiple video feeds.

docs/reference.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ Complete API reference for all classes, methods, and types in the Python Janus C
5858
separate_signature: true
5959
show_signature_annotations: true
6060

61+
#### VideoCallError Exception
62+
63+
::: janus_client.plugin_video_call.VideoCallError
64+
options:
65+
show_root_heading: true
66+
show_source: false
67+
docstring_section_style: table
68+
69+
#### VideoCallEventType Enum
70+
71+
::: janus_client.plugin_video_call.VideoCallEventType
72+
options:
73+
show_root_heading: true
74+
show_source: false
75+
docstring_section_style: table
76+
6177
### VideoRoom Plugin
6278

6379
::: janus_client.plugin_video_room.JanusVideoRoomPlugin
@@ -69,17 +85,25 @@ Complete API reference for all classes, methods, and types in the Python Janus C
6985
separate_signature: true
7086
show_signature_annotations: true
7187

72-
#### VideoRoom State Class
88+
#### VideoRoomError Exception
89+
90+
::: janus_client.plugin_video_room.VideoRoomError
91+
options:
92+
show_root_heading: true
93+
show_source: false
94+
docstring_section_style: table
95+
96+
#### VideoRoomEventType Enum
7397

74-
::: janus_client.plugin_video_room.JanusVideoRoomPlugin.State
98+
::: janus_client.plugin_video_room.VideoRoomEventType
7599
options:
76100
show_root_heading: true
77101
show_source: false
78102
docstring_section_style: table
79103

80-
#### AllowedAction Enum
104+
#### ParticipantType Enum
81105

82-
::: janus_client.plugin_video_room.AllowedAction
106+
::: janus_client.plugin_video_room.ParticipantType
83107
options:
84108
show_root_heading: true
85109
show_source: false

0 commit comments

Comments
 (0)