Working cli example with video, audio, messaging

This commit is contained in:
Kacper Rączy 2023-11-22 19:23:16 -08:00
parent 9015f66574
commit fa9ebc0869
1 changed files with 23 additions and 14 deletions

View File

@ -7,11 +7,11 @@ import json
import logging
import aiortc
from aiortc.mediastreams import VideoStreamTrack
from aiortc.contrib.media import MediaPlayer
from aiortc.mediastreams import VideoStreamTrack, AudioStreamTrack
from bodyrtc import WebRTCOfferBuilder, WebRTCAnswerBuilder
from bodyrtc.stream import StreamingOffer
from bodyrtc.info import parse_info_from_offer
async def async_input():
@ -37,13 +37,10 @@ async def run_answer(args):
payload = json.loads(raw_payload)
offer = StreamingOffer(**payload)
if args.input_video:
player = MediaPlayer(args.input_video)
video_tracks = [player.video() for _ in offer.video]
else:
player = None
video_tracks = [VideoStreamTrack() for _ in offer.video]
audio_tracks = []
info = parse_info_from_offer(offer.sdp)
assert len(offer.video) == info.n_expected_camera_tracks
video_tracks = [VideoStreamTrack() for _ in offer.video]
audio_tracks = [AudioStreamTrack()] if info.expected_audio_track else []
stream_builder = WebRTCAnswerBuilder(offer.sdp)
for cam, track in zip(offer.video, video_tracks, strict=True):
@ -64,18 +61,29 @@ async def run_offer(args):
stream_builder = WebRTCOfferBuilder(StdioConnectionProvider)
for cam in args.cameras:
stream_builder.offer_to_receive_video_stream(cam)
stream_builder.add_messaging()
if args.audio:
stream_builder.offer_to_receive_audio_stream()
if args.messaging:
stream_builder.add_messaging()
stream = stream_builder.stream()
_ = await stream.start()
await stream.wait_for_connection()
print("Connection established and all tracks are ready")
tracks = [stream.get_incoming_video_track(cam, False) for cam in args.cameras]
video_tracks = [stream.get_incoming_video_track(cam, False) for cam in args.cameras]
audio_track, channel = None, None
if stream.has_incoming_audio_track():
audio_track = stream.get_incoming_audio_track(False)
if stream.has_messaging_channel():
channel = stream.get_messaging_channel()
while True:
try:
frames = await asyncio.gather(*[track.recv() for track in tracks])
frames = await asyncio.gather(*[track.recv() for track in video_tracks])
for key, frame in zip(args.cameras, frames, strict=True):
print("Received frame from", key, frame.time)
if audio_track:
frame = await audio_track.recv()
print("Received frame from audio", frame.time)
except aiortc.mediastreams.MediaStreamError:
return
print("=====================================")
@ -86,10 +94,11 @@ if __name__ == "__main__":
subparsers = parser.add_subparsers(dest="command", required=True)
offer_parser = subparsers.add_parser("offer", description="Create offer stream")
offer_parser.add_argument("cameras", metavar="CAMERA", type=str, nargs="+", default=["driver"], help="Camera types to stream")
offer_parser.add_argument("--audio", action="store_true", help="Offer to receive audio")
offer_parser.add_argument("--messaging", action="store_true", help="Add messaging support")
offer_parser.add_argument("cameras", metavar="CAMERA", type=str, nargs="+", default=[], help="Camera types to stream")
answer_parser = subparsers.add_parser("answer", description="Create answer stream")
answer_parser.add_argument("--input-video", type=str, required=False, help="Stream from video file instead")
args = parser.parse_args()