forked from yondonfu/comfystream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
287 lines (219 loc) · 8.89 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import asyncio
import argparse
import os
import json
import logging
import wave
import numpy as np
from twilio.rest import Client
from aiohttp import web
from aiortc import (
RTCPeerConnection,
RTCSessionDescription,
RTCConfiguration,
RTCIceServer,
MediaStreamTrack,
)
from aiortc.rtcrtpsender import RTCRtpSender
from pipeline import Pipeline
from utils import patch_loop_datagram
logger = logging.getLogger(__name__)
class VideoStreamTrack(MediaStreamTrack):
kind = "video"
def __init__(self, track: MediaStreamTrack, pipeline):
super().__init__()
self.track = track
self.pipeline = pipeline
async def recv(self):
frame = await self.track.recv()
return await self.pipeline(frame)
class AudioStreamTrack(MediaStreamTrack):
"""
This custom audio track wraps an incoming audio MediaStreamTrack.
It continuously records frames in 10-second chunks and saves each chunk
as a separate WAV file with an incrementing index.
"""
kind = "audio"
def __init__(self, track: MediaStreamTrack):
super().__init__()
self.track = track
self.start_time = None
self.frames = []
self._recording_duration = 10.0 # in seconds
self._chunk_index = 0
self._saving = False
self._lock = asyncio.Lock()
async def recv(self):
frame = await self.track.recv()
return await self.pipeline(frame)
# async def recv(self):
# frame = await self.source.recv()
# # On the first frame, record the start time.
# if self.start_time is None:
# self.start_time = frame.time
# logger.info(f"Audio recording started at time: {self.start_time:.3f}")
# elapsed = frame.time - self.start_time
# self.frames.append(frame)
# logger.info(f"Received audio frame at time: {frame.time:.3f}, total frames: {len(self.frames)}")
# # Check if we've hit 10 seconds and we're not currently saving.
# if elapsed >= self._recording_duration and not self._saving:
# logger.info(f"10 second chunk reached (elapsed: {elapsed:.3f}s). Preparing to save chunk {self._chunk_index}.")
# self._saving = True
# # Handle saving in a background task so we don't block the recv loop.
# asyncio.create_task(self.save_audio())
# return frame
async def save_audio(self):
logger.info(f"Starting to save audio chunk {self._chunk_index}...")
async with self._lock:
# Extract properties from the first frame
if not self.frames:
logger.warning("No frames to save, skipping.")
self._saving = False
return
sample_rate = self.frames[0].sample_rate
layout = self.frames[0].layout
channels = len(layout.channels)
logger.info(f"Audio chunk {self._chunk_index}: sample_rate={sample_rate}, channels={channels}, frames_count={len(self.frames)}")
# Convert all frames to ndarray and concatenate
data_arrays = [f.to_ndarray() for f in self.frames]
data = np.concatenate(data_arrays, axis=1) # shape: (channels, total_samples)
# Interleave channels (if multiple) since WAV expects interleaved samples.
interleaved = data.T.flatten()
# If needed, convert float frames to int16
# interleaved = (interleaved * 32767).astype(np.int16)
filename = f"output_{self._chunk_index}.wav"
logger.info(f"Writing audio chunk {self._chunk_index} to file: {filename}")
with wave.open(filename, 'wb') as wf:
wf.setnchannels(channels)
wf.setsampwidth(2) # 16-bit PCM
wf.setframerate(sample_rate)
wf.writeframes(interleaved.tobytes())
logger.info(f"Audio chunk {self._chunk_index} saved successfully as {filename}")
# Increment the chunk index for the next segment
self._chunk_index += 1
# Reset for next recording chunk
self.frames.clear()
self.start_time = None
self._saving = False
logger.info(f"Ready to record next 10-second chunk. Current chunk index: {self._chunk_index}")
def force_codec(pc, sender, forced_codec):
kind = forced_codec.split("/")[0]
codecs = RTCRtpSender.getCapabilities(kind).codecs
transceiver = next(t for t in pc.getTransceivers() if t.sender == sender)
codecPrefs = [codec for codec in codecs if codec.mimeType == forced_codec]
transceiver.setCodecPreferences(codecPrefs)
def get_twilio_token():
account_sid = os.getenv("TWILIO_ACCOUNT_SID")
auth_token = os.getenv("TWILIO_AUTH_TOKEN")
if account_sid is None or auth_token is None:
return None
client = Client(account_sid, auth_token)
token = client.tokens.create()
return token
def get_ice_servers():
ice_servers = []
token = get_twilio_token()
if token is not None:
# Use Twilio TURN servers
for server in token.ice_servers:
if server["url"].startswith("turn:"):
turn = RTCIceServer(
urls=[server["urls"]],
credential=server["credential"],
username=server["username"],
)
ice_servers.append(turn)
return ice_servers
async def offer(request):
pcs = request.app["pcs"]
workspace = request.app["workspace"]
params = await request.json()
pipeline = Pipeline(params["prompt"], cwd=workspace)
await pipeline.warm()
offer_params = params["offer"]
offer = RTCSessionDescription(sdp=offer_params["sdp"], type=offer_params["type"])
ice_servers = get_ice_servers()
if len(ice_servers) > 0:
pc = RTCPeerConnection(
configuration=RTCConfiguration(iceServers=get_ice_servers())
)
else:
pc = RTCPeerConnection()
pcs.add(pc)
tracks = {"video": None}
# Prefer h264
transceiver = pc.addTransceiver("video")
caps = RTCRtpSender.getCapabilities("video")
prefs = list(filter(lambda x: x.name == "H264", caps.codecs))
transceiver.setCodecPreferences(prefs)
@pc.on("track")
def on_track(track):
logger.info(f"Track received: {track.kind}")
if track.kind == "video":
videoTrack = VideoStreamTrack(track, pipeline)
tracks["video"] = videoTrack
sender = pc.addTrack(videoTrack)
codec = "video/H264"
force_codec(pc, sender, codec)
elif track.kind == "audio":
audioTrack = AudioStreamTrack(track)
tracks["audio"] = audioTrack
pc.addTrack(audioTrack)
@track.on("ended")
async def on_ended():
logger.info(f"{track.kind} track ended")
@pc.on("connectionstatechange")
async def on_connectionstatechange():
logger.info(f"Connection state is: {pc.connectionState}")
if pc.connectionState == "failed":
await pc.close()
pcs.discard(pc)
elif pc.connectionState == "closed":
await pc.close()
pcs.discard(pc)
await pc.setRemoteDescription(offer)
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
return web.Response(
content_type="application/json",
text=json.dumps(
{"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
),
)
def health(_):
return web.Response(content_type="application/json", text="OK")
async def on_startup(app: web.Application):
if app["media_ports"]:
patch_loop_datagram(app["media_ports"])
app["pcs"] = set()
async def on_shutdown(app: web.Application):
pcs = app["pcs"]
coros = [pc.close() for pc in pcs]
await asyncio.gather(*coros)
pcs.clear()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run comfystream server")
parser.add_argument("--port", default=8888, help="Set the signaling port")
parser.add_argument(
"--media-ports", default=None, help="Set the UDP ports for WebRTC media"
)
parser.add_argument("--host", default="127.0.0.1", help="Set the host")
parser.add_argument(
"--workspace", default=None, required=True, help="Set Comfy workspace"
)
parser.add_argument(
"--log-level",
default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="Set the logging level",
)
args = parser.parse_args()
logging.basicConfig(level=args.log_level.upper())
app = web.Application()
app["media_ports"] = args.media_ports.split(",") if args.media_ports else None
app["workspace"] = args.workspace
app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)
app.router.add_post("/offer", offer)
app.router.add_get("/", health)
web.run_app(app, host=args.host, port=int(args.port))