Giter Club home page Giter Club logo

livekit-server-sdk-python's Introduction

This repo has been deprecated, it's been superceded by https://github.com/livekit/python-sdks/

LiveKit Server SDK

https://pypi.org/project/livekit-server-sdk-python/ Code style: black Imports: isort

API Reference: https://docs.livekit.io/guides/server-api

Examples

Generate Access Token for a Client

import livekit

grant = livekit.VideoGrant(room_join=True, room="My Cool Room")
access_token = livekit.AccessToken("<api key>", "<api secret>", grant=grant, identity="bob", name="Bob")
token = access_token.to_jwt()

Using RoomServiceClient

import livekit

client = livekit.RoomServiceClient("<host>", "<api key>", "<api secret>")
client.mute_published_track(
    room="<room name>",
    identity="Bob",
    track_sid="<track sid>",
    muted=True,
)

Local Development

Make sure you clone with submodules:

$ git clone --recurse-submodules https://github.com/tradablebits/livekit-server-sdk-python.git

Or if you have already cloned:

$ git submodule update --init

Dependencies

  • golang >= 1.17
  • protoc
    • Ubuntu: sudo apt install protobuf-compiler
  • protoc-gen-twirpy
    • go install github.com/verloop/twirpy/protoc-gen-twirpy@latest
    • make sure ~/go/bin is in your $PATH
  • pre-commit

Environment Setup

Set up the python virtual environment:

$ python3 -m venv env
$ source env/bin/activate
$ pip install --editable '.[dev]'
$ pre-commit install

Run tests

The RoomServiceClient tests require a running LiveKit server. See the LiveKit Getting Started page.

The tests use the following environment variables to locate the LiveKit server.

export LIVEKIT_HOST='http://localhost:7880'
export LIVEKIT_API_KEY='<api key>'
export LIVEKIT_API_SECRET='<api secret>'

Run the tests:

$ pytest

Updating protocol

The build.sh script pulls the latest tag and builds

$ ./bin/build.sh

livekit-server-sdk-python's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

livekit-server-sdk-python's Issues

How to use this SDK for egress server

Hi,

I want to use this SDK and with gstreamer for python to do an egress server - called "myegress".

My scenario is:

  • When we send TrackCompositeEgressRequest to livekit server
  • livekit server will send media stream to "myegress"
  • at "myegress" will receive this media stream and I will captured it by OpenCV.
  • And then I will apply AI model detection for captured images

Please advise if I can use this SDK for above scenario?

Basic Instructions as developer - Windows

Can i get basic instructions on how to setup my development env on pycharm and how to i start the server.

i ran the following
i created development.env file and added export LIVEKIT_API_KEY=
export LIVEKIT_API_SECRET=
git clone --recurse-submodules https://github.com/tradablebits/livekit-server-sdk-python.git
python3 -m venv env
source env/Script/activate
pip install --editable . // . is for current folder

i am not sure how do i proceed further, can you guide me on how to start the server,

I also have GoLang installed on my system

It will be great help if you could guide me further. Thanks

Name conflict with Client SDK

Is it possible to rename this project to "livekitserver" or something similar so it does not conflict with the Python Client SDK?

Connect to room and listen for data events

Hi @AlexJMohr
I'm wondering how (if at all possible) I could connect to a room from the backend, and listen to data events.
At the moment the only webhooks livekit supports are on participant leave etc, but not on data messages, I was looking at that option
Not having that ability is quite limiting, as I can send WS data, but cannot receive (meaning I have to receive API calls instead)
I've been using the channels py library as well, but don't want to use two WS systems - so want to get rid of that one entirely

I got issue with RTMP stream

Hi,

I'm using this sdk for my backend. My flow at my backend as below:

  • A request from a client to my backend to create a room
  • The backend create a room
  • The client join room & publish media
  • Livekit server send webhook to backend
  • The backend receive webhook and send request to livekit server for streaming use RTMP

Livekit server: v1.2.5
livekit-server-sdk-python: 0.4.1

In init.py of livekit-server-sdk-python, I added following code:

class TrackServiceClient:
"""
Client to access Egress APIs
"""
def init(self, host: str, api_key: str, api_secret: str):
self._client = TwirpEgressServiceClient(host)
self._api_key = api_key
self._api_secret = api_secret

def _create_context(self, **grant_kwargs):
    grant = VideoGrant(**grant_kwargs)
    access_token = AccessToken(
        self._api_key,
        self._api_secret,
        grant=grant,
        ttl=timedelta(minutes=10),
    )
    return Context(headers={"Authorization": f"Bearer {access_token.to_jwt()}"})

def list_egress(
    self,
    room_name: str
):
    """
    Do list egress.
    """
    ctx = self._create_context(room_admin=True, room=room_name)
    request = ListEgressRequest(room_name=room_name)
    self._client.ListEgress(ctx=ctx, request=request)

def track_egress(
    self,
    room_name: str,
    track_id: str,
    rtmpUrl: str
):
    """
    Do track egress.
    """
    ctx = self._create_context(room_admin=True, room_record=True, room=room_name)
    rtmpOutput = StreamOutput(protocol=1, urls=[rtmpUrl])
    request = TrackCompositeEgressRequest(room_name=room_name, video_track_id=track_id, stream=rtmpOutput)
    self._client.StartTrackEgress(ctx=ctx, request=request)

I called track_egress with rtmpUrl = "rtmp://esw-rtmp.livekit:1935/live/test-room1"

But I got below error at my backend:

raise exceptions.TwirpServerException.from_json(resp.json())
twirp.exceptions.TwirpServerException: the protobuf request could not be decoded

And on livekit server logs:

2023-01-04T09:52:26.579Z INFO livekit service/twirp.go:111 API Egress.StartTrackEgress {"service": "Egress", "method": "StartTrackEgress", "duration": "67.808µs", "status": "400", "error": "the protobuf request could not be decoded", "code": "malformed"}

How can I fixed it ? please advise this.

I fixed it, I called wrong function in track_egress, it should be: StartTrackCompositeEgress

For example: I used livekit-cli for stream request as below and it worked:

{
"room_name": "test-room1",
"video_track_id": "TR_VCHQ3DH3JauRdJ",
"stream": {
"urls": [
"rtmp://esw-rtmp.livekit:1935/live/test"
]
}
}

Support for identity.name

Hi and thanks for making this
I'm using it and works fine for me
Wanted to ask if I'm missing something or this feature isn't supported:

When you create a token, along with the identity and other stuff, you can also give that user a name (in the participant info)
https://docs.livekit.io/server/room-management/#participantinfo

That name is used in the front-end
displayName = participant.name || participant.identity;
But I don't see anywhere in this package the name
For example, this php package seems to have it
https://github.com/agence104/livekit-server-sdk-php/blob/f6965a4d593356ffa762648c4acbb2cbfe048251/src/AccessToken.php#L89
I presume it's a new feature of livekit, perhaps

Does this sdk have WebhookReceiver support?

Hi, it's me again :)
Still a user of this package, and thanks again for making it.
I find myself in need of using the Webhook and unsure (can't see it anyway) if this package has support for webhooks

EDIT: I imagine I can simply look at the token, decode it to validate it and do what ever I need to do, even if not directly supported in this library

Missing attributes on VideoGrant

Hi, thanks for building this SDK. 💯
My problem is that I want to create a camera-only token and send back to client, in the doc I have read that we can use the attribute canPublishSources, I found it in the NodeJS package but for our python we don't have that. Am I missing something ?
image
image

Authentication with egress server

Hello, thanks for the Python SDK!

Is there any example of sending a request to the Egress server?
I am somehow trying to send TrackEgressRequest after joining a room below, while the server responds "status": "401", "error": "permissions denied", "code": "unauthenticated".

It is really helpful If there are any guides about how to use the EgressClient, thanks!

egress_client = livekit.twirp.EgressClient(address=MY_LIVEKIT_SERVER)

response = egress_client.StartTrackEgress(
    ctx=twirp.context.Context(),
    request=livekit.egress.TrackEgressRequest(
        room_name=MY_ROOM,
        track_id=MY_TRACK,
        websocket_url=MY_WEBSOCKET_SERVER,
    ),
    headers={"Authorization": f"Bearer {MY_TOKEN}"},
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.