Replies: 2 comments
-
I'm trying to figure out how to do WHEP to stream audio in opus (=no flv=no rtmp). How did you create the whep stream? |
Beta Was this translation helpful? Give feedback.
-
I did some experiments with aiortc and it's perfectly possible to receive a WebRTC+WHEP stream by converting an existing WHEP client, like the one embedded inside MediaMTX, from JavaScript to Python. Working example: import re
import asyncio
import aiohttp
from aiortc import RTCPeerConnection, RTCConfiguration, RTCIceServer, RTCSessionDescription
WHEP_URL = 'http://localhost:8889/stream/whep'
async def run_track(track):
while True:
frame = await track.recv()
print(frame)
async def main():
async with aiohttp.ClientSession() as session:
# get ICE servers
iceServers = []
async with session.options(WHEP_URL) as res:
for k, v in res.headers.items():
if k == "Link":
m = re.match('^<(.+?)>; rel="ice-server"(; username="(.*?)"; credential="(.*?)"; credential-type="password")?', v)
iceServers.append(RTCIceServer(urls=m[1], username=m[2], credential=m[3], credentialType='password'))
# setup peer connection
pc = RTCPeerConnection(RTCConfiguration(iceServers=iceServers))
pc.addTransceiver('video', direction='recvonly')
pc.addTransceiver('audio', direction='recvonly')
# on connection state change callback
@pc.on("connectionstatechange")
async def on_connectionstatechange():
print("Connection state is %s" % pc.connectionState)
# on track callback
@pc.on('track')
async def on_track(track):
print('on track')
task = asyncio.ensure_future(run_track(track))
# generate offer
offer = await pc.createOffer()
await pc.setLocalDescription(offer)
# send offer, set answer
async with session.post(WHEP_URL, headers={'Content-Type': 'application/sdp'}, data=offer.sdp) as res:
answer = await res.text()
await pc.setRemoteDescription(RTCSessionDescription(sdp=answer, type='answer'))
await asyncio.sleep(10000)
asyncio.run(main()) What is missing is the ability to send asynchronous candidates to the server (i.e. trickle ICE) - if you need that, you can go in in the conversion task from JavaScript to Python. |
Beta Was this translation helpful? Give feedback.
-
Question
I'm trying to create a Python client to connect to a MediaMTX server using the WHEP (WebRTC-HTTP Egress Protocol) protocol, but I'm struggling to get started. I have a MediaMTX server running, but I'm not sure how to properly implement the client-side WHEP connection in Python.
My current setup:
I've tried implementing a basic WHEP client based on the protocol description, but I'm not sure if I'm doing it correctly.
Can someone provide guidance on:
Any help or pointers in the right direction would be greatly appreciated. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions