Skip to content

Commit

Permalink
Better isOpen guards
Browse files Browse the repository at this point in the history
isOpen is more accurate than isClosed
  • Loading branch information
singpolyma committed Oct 6, 2024
1 parent 2738faf commit a2949dc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions snikket/jingle/PeerConnection.cpp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ class MediaStreamTrack {
eventLoop = sys.thread.Thread.createWithEventLoop(() -> {
timer = new haxe.Timer(10); // This timer will stop when the audioloop for this track stops
timer.run = () -> {
if (untyped __cpp__("!_gthis->track")) return;
if (!alive || !track.ref.isOpen()) return;

if (audioQ.length > 0 && audioQ[audioQ.length - 1].stamp <= haxe.Timer.stamp()) {
final packet = audioQ.pop();
write(packet.payload, packet.payloadType, packet.clockRate);
Expand Down Expand Up @@ -537,7 +540,6 @@ class MediaStreamTrack {
final format = Lambda.find(supportedAudioFormats, format -> format.clockRate == clockRate && format.channels == channels);
if (format == null) throw "Unsupported audo format: " + clockRate + "/" + channels;
eventLoop.run(() -> {
if (track.ref.isClosed()) return;
final stamp = if (audioQ.length < 1) {
bufferSizeInSeconds = Math.max(bufferSizeInSeconds, bufferSizeInSeconds + 0.1);
haxe.Timer.stamp() + bufferSizeInSeconds;
Expand Down Expand Up @@ -573,6 +575,8 @@ class MediaStreamTrack {

@:allow(snikket)
private function write(payload: Array<cpp.UInt8>, payloadType: cpp.UInt8, clockRate: Int) {
if (untyped __cpp__("!track") || !track.ref.isOpen()) return;

rtpPacketizationConfig.ref.payloadType = payloadType;
rtpPacketizationConfig.ref.clockRate = clockRate;
track.ref.send(cpp.Pointer.ofArray(payload).reinterpret(), payload.length);
Expand All @@ -587,7 +591,7 @@ class MediaStreamTrack {

public function stop() {
alive = false;
if (!track.ref.isClosed()) track.ref.close();
if (track.ref.isOpen()) track.ref.close();
if (untyped __cpp__("opus")) {
OpusDecoder.destroy(opus);
opus = null;
Expand Down

0 comments on commit a2949dc

Please sign in to comment.