|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import asyncio |
15 | 18 | from typing import TYPE_CHECKING, List, Union |
| 19 | + |
16 | 20 | from ._ffi_client import FfiHandle, FfiClient |
17 | 21 | from ._proto import ffi_pb2 as proto_ffi |
18 | 22 | from ._proto import track_pb2 as proto_track |
19 | 23 | from ._proto import stats_pb2 as proto_stats |
20 | 24 |
|
21 | 25 | if TYPE_CHECKING: |
| 26 | + from .audio_ring_buffer import AudioRingBuffer |
22 | 27 | from .audio_source import AudioSource |
| 28 | + from .participant import LocalParticipant |
23 | 29 | from .video_source import VideoSource |
24 | 30 |
|
| 31 | +PRE_CONNECT_AUDIO_BUFFER_TOPIC = "lk.agent.pre-connect-audio-buffer" |
| 32 | + |
25 | 33 |
|
26 | 34 | class Track: |
27 | 35 | def __init__(self, owned_info: proto_track.OwnedTrack): |
@@ -68,26 +76,80 @@ async def get_stats(self) -> List[proto_stats.RtcStats]: |
68 | 76 |
|
69 | 77 |
|
70 | 78 | class LocalAudioTrack(Track): |
71 | | - def __init__(self, info: proto_track.OwnedTrack): |
| 79 | + def __init__(self, info: proto_track.OwnedTrack, source: AudioSource | None = None): |
72 | 80 | super().__init__(info) |
| 81 | + self._source = source |
| 82 | + self._preconnect_buffer: AudioRingBuffer | None = None |
| 83 | + self._participant: LocalParticipant | None = None |
| 84 | + self._publication_sid: str | None = None |
| 85 | + self._send_lock = asyncio.Lock() |
73 | 86 |
|
74 | 87 | @staticmethod |
75 | | - def create_audio_track(name: str, source: "AudioSource") -> "LocalAudioTrack": |
| 88 | + def create_audio_track(name: str, source: AudioSource) -> LocalAudioTrack: |
76 | 89 | req = proto_ffi.FfiRequest() |
77 | 90 | req.create_audio_track.name = name |
78 | 91 | req.create_audio_track.source_handle = source._ffi_handle.handle |
79 | 92 |
|
80 | 93 | resp = FfiClient.instance.request(req) |
81 | | - return LocalAudioTrack(resp.create_audio_track.track) |
| 94 | + return LocalAudioTrack(resp.create_audio_track.track, source=source) |
82 | 95 |
|
83 | | - def mute(self): |
| 96 | + @property |
| 97 | + def has_preconnect_buffer(self) -> bool: |
| 98 | + return self._preconnect_buffer is not None |
| 99 | + |
| 100 | + def start_preconnect_buffer(self, *, max_duration: float = 10.0) -> None: |
| 101 | + if self._source is None: |
| 102 | + raise RuntimeError("track has no audio source") |
| 103 | + |
| 104 | + from .audio_ring_buffer import AudioRingBuffer |
| 105 | + |
| 106 | + self._preconnect_buffer = AudioRingBuffer( |
| 107 | + max_duration=max_duration, |
| 108 | + sample_rate=self._source.sample_rate, |
| 109 | + num_channels=self._source.num_channels, |
| 110 | + ) |
| 111 | + self._source._set_preconnect_buffer(self._preconnect_buffer) |
| 112 | + |
| 113 | + def stop_preconnect_buffer(self) -> None: |
| 114 | + if self._source is not None: |
| 115 | + self._source._set_preconnect_buffer(None) |
| 116 | + self._preconnect_buffer = None |
| 117 | + |
| 118 | + async def send_preconnect_buffer(self, *, destination_identity: str) -> None: |
| 119 | + if self._participant is None: |
| 120 | + raise RuntimeError("track is not published") |
| 121 | + if self._preconnect_buffer is None: |
| 122 | + raise RuntimeError("preconnect buffer is not active") |
| 123 | + |
| 124 | + async with self._send_lock: |
| 125 | + data = self._preconnect_buffer.capture() |
| 126 | + if not data: |
| 127 | + return |
| 128 | + |
| 129 | + assert self._source is not None |
| 130 | + writer = await self._participant.stream_bytes( |
| 131 | + "preconnect-buffer", |
| 132 | + topic=PRE_CONNECT_AUDIO_BUFFER_TOPIC, |
| 133 | + mime_type="application/octet-stream", |
| 134 | + destination_identities=[destination_identity], |
| 135 | + attributes={ |
| 136 | + "trackId": self._publication_sid or self.sid, |
| 137 | + "sampleRate": str(self._source.sample_rate), |
| 138 | + "channels": str(self._source.num_channels), |
| 139 | + }, |
| 140 | + ) |
| 141 | + |
| 142 | + await writer.write(data) |
| 143 | + await writer.aclose() |
| 144 | + |
| 145 | + def mute(self) -> None: |
84 | 146 | req = proto_ffi.FfiRequest() |
85 | 147 | req.local_track_mute.track_handle = self._ffi_handle.handle |
86 | 148 | req.local_track_mute.mute = True |
87 | 149 | FfiClient.instance.request(req) |
88 | 150 | self._info.muted = True |
89 | 151 |
|
90 | | - def unmute(self): |
| 152 | + def unmute(self) -> None: |
91 | 153 | req = proto_ffi.FfiRequest() |
92 | 154 | req.local_track_mute.track_handle = self._ffi_handle.handle |
93 | 155 | req.local_track_mute.mute = False |
|
0 commit comments