Skip to content

Commit

Permalink
finish adding audio connector
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Sep 27, 2024
1 parent e5c31e1 commit 0aa1ea1
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![Build Status](https://github.com/Vonage/vonage-python-sdk/workflows/Build/badge.svg)](https://github.com/Vonage/vonage-python-sdk/actions)
[![Python versions supported](https://img.shields.io/pypi/pyversions/vonage.svg)](https://pypi.python.org/pypi/vonage)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
![Total lines](https://sloc.xyz/github/vonage/vonage-python-sdk)

This is the Python server SDK for Vonage's API. To use it you'll
need a Vonage account. Sign up [for free at vonage.com][signup].
Expand Down
33 changes: 30 additions & 3 deletions video/src/vonage_video/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
from .enums import ArchiveMode, MediaMode, TokenRole
from .audio_connector import (
AudioConnectorData,
AudioConnectorOptions,
AudioConnectorWebSocket,
)
from .captions import CaptionsData, CaptionsOptions
from .enums import (
ArchiveMode,
AudioSampleRate,
LanguageCode,
MediaMode,
P2pPreference,
TokenRole,
)
from .session import SessionOptions, VideoSession
from .signal import SignalData
from .stream import StreamInfo, StreamLayout, StreamLayoutOptions
from .token import TokenOptions

__all__ = [
'MediaMode',
'AudioConnectorData',
'AudioConnectorOptions',
'AudioConnectorWebSocket',
'CaptionsData',
'CaptionsOptions',
'ArchiveMode',
'MediaMode',
'TokenRole',
'TokenOptions',
'P2pPreference',
'LanguageCode',
'AudioSampleRate',
'SessionOptions',
'VideoSession',
'SignalData',
'StreamInfo',
'StreamLayoutOptions',
'StreamLayout',
'TokenOptions',
]
20 changes: 10 additions & 10 deletions video/src/vonage_video/models/audio_connector.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from typing import Optional
from typing import List, Optional

from pydantic import BaseModel, Field
from vonage_video.models.enums import AudioSampleRate


class AudioConnectorWebsocket(BaseModel):
class AudioConnectorWebSocket(BaseModel):
"""The audio connector websocket options.
Args:
uri (str): The URI.
streams (list): The streams.
headers (dict): The headers.
audio_rate (AudioSampleRate): The audio sample rate.
streams (List[str]): Stream IDs to include. If not provided, all streams are included.
headers (dict): The headers to send to your WebSocket server.
audio_rate (AudioSampleRate): The audio sample rate in Hertz.
"""

uri: str
streams: Optional[list] = None
streams: Optional[List[str]] = None
headers: Optional[dict] = None
audio_rate: Optional[AudioSampleRate] = Field(None, serialization_alias='audioRate')

Expand All @@ -26,16 +26,16 @@ class AudioConnectorOptions(BaseModel):
Args:
session_id (str): The session ID.
token (str): The token.
websocket (AudioConnectorWebsocket): The audio connector websocket.
websocket (AudioConnectorWebSocket): The audio connector websocket.
"""

session_id: str = Field(..., serialization_alias='sessionId')
token: str
websocket: AudioConnectorWebsocket
websocket: AudioConnectorWebSocket


class AudioConnectorData(BaseModel):
"""Class containing audio connector ID and audio captioning session ID."""
"""Class containing Audio Connector WebSocket ID and connection ID."""

id: Optional[str] = None
captions_id: Optional[str] = Field(None, serialization_alias='captionsId')
connection_id: Optional[str] = Field(None, validation_alias='connectionId')
2 changes: 1 addition & 1 deletion video/src/vonage_video/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ class LanguageCode(str, Enum):
TH_TH = 'th-TH'


class AudioSampleRate(str, Enum):
class AudioSampleRate(int, Enum):
KHZ_8 = 8000
KHZ_16 = 16000
3 changes: 2 additions & 1 deletion video/src/vonage_video/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pydantic import validate_call
from vonage_http_client.http_client import HttpClient
from vonage_video.models.audio_connector import AudioConnectorData, AudioConnectorOptions
from vonage_video.models.captions import CaptionsData, CaptionsOptions
from vonage_video.models.session import SessionOptions, VideoSession
from vonage_video.models.signal import SignalData
Expand Down Expand Up @@ -252,4 +253,4 @@ def start_audio_connector(self, options: AudioConnectorOptions) -> AudioConnecto
options.model_dump(exclude_none=True, by_alias=True),
)

return AudioConnectorData(audio_connector_id=response['audioConnectorId'])
return AudioConnectorData(**response)
4 changes: 4 additions & 0 deletions video/tests/data/audio_connector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "b3cd31f4-020e-4ba3-9a2a-12d98b8a184f",
"connectionId": "1bf530df-97f4-4437-b6c9-2a66200200c8"
}
70 changes: 70 additions & 0 deletions video/tests/test_audio_connector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from os.path import abspath

import responses
from vonage_http_client import HttpClient
from vonage_video.models.audio_connector import (
AudioConnectorOptions,
AudioConnectorWebSocket,
)
from vonage_video.models.enums import AudioSampleRate, TokenRole
from vonage_video.models.token import TokenOptions
from vonage_video.video import Video

from testutils import build_response, get_mock_jwt_auth

path = abspath(__file__)


video = Video(HttpClient(get_mock_jwt_auth()))


def test_audio_connector_options_model():
options = AudioConnectorOptions(
session_id='test_session_id',
token='test_token',
websocket=AudioConnectorWebSocket(
uri='test_uri',
streams=['test_stream_id'],
headers={'test_header': 'test_value'},
audio_rate=AudioSampleRate.KHZ_16,
),
)

assert options.model_dump(by_alias=True) == {
'sessionId': 'test_session_id',
'token': 'test_token',
'websocket': {
'uri': 'test_uri',
'streams': ['test_stream_id'],
'headers': {'test_header': 'test_value'},
'audioRate': 16000,
},
}


@responses.activate
def test_start_audio_connector():
build_response(
path,
'POST',
'https://video.api.vonage.com/v2/project/test_application_id/connect',
'audio_connector.json',
200,
)

session_id = 'test_session_id'
options = AudioConnectorOptions(
session_id=session_id,
token=video.generate_client_token(
TokenOptions(session_id=session_id, role=TokenRole.MODERATOR)
),
websocket=AudioConnectorWebSocket(
uri='wss://example.com/ws',
audio_rate=AudioSampleRate.KHZ_16,
),
)

audio_connector = video.start_audio_connector(options)

assert audio_connector.id == 'b3cd31f4-020e-4ba3-9a2a-12d98b8a184f'
assert audio_connector.connection_id == '1bf530df-97f4-4437-b6c9-2a66200200c8'

0 comments on commit 0aa1ea1

Please sign in to comment.