Skip to content

Commit

Permalink
[Fix]hmr mode CAN_NOT_PUBLISH_MULTIPLE_VIDEO_TRACKS issue (#167)
Browse files Browse the repository at this point in the history
* fix: #155
  • Loading branch information
guoxianzhe authored Oct 30, 2023
1 parent 25e8bc7 commit 74c25f8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
32 changes: 23 additions & 9 deletions packages/agora-rtc-react/src/hooks/useJoin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { IAgoraRTCClient, IAgoraRTCError, UID } from "agora-rtc-sdk-ng";
import { useState } from "react";
import { useRef, useState } from "react";

import { AgoraRTCReactError } from "../error";
import { useRTCClient } from "../hooks/useRTCClient";
import type { AsyncTaskRunner } from "../misc/utils";
import { createAsyncTaskRunner, joinDisposers } from "../misc/utils";
import type { FetchArgs } from "../types";

import { useAsyncEffect, useIsUnmounted } from "./tools";
Expand Down Expand Up @@ -47,6 +49,7 @@ export function useJoin(
): { data: UID; isLoading: boolean; isConnected: boolean; error: AgoraRTCReactError | null } {
const resolvedClient = useRTCClient(client);
const isConnected = useIsConnected(client);
const runnerRef = useRef<AsyncTaskRunner | undefined>();

const [isLoading, setIsLoading] = useState(false);
const [joinResult, setJoinResult] = useState<UID>(0);
Expand Down Expand Up @@ -80,15 +83,26 @@ export function useJoin(
if (!isUnmountRef.current) {
setIsLoading(false);
}
return () => {
for (const track of resolvedClient.localTracks) {
if (track.isPlaying) {
track.stop();

const runner = (runnerRef.current ||= createAsyncTaskRunner());

return joinDisposers([
() => {
runner.dispose();
},
() => {
runner.run(() => resolvedClient.unpublish(resolvedClient.localTracks));
},
() => {
for (const track of resolvedClient.localTracks) {
if (track.isPlaying) {
track.stop();
}
track.close();
}
track.close();
}
return resolvedClient.leave();
};
runner.run(() => resolvedClient.leave());
},
]);
}
}, [ready, client]);
return {
Expand Down
5 changes: 5 additions & 0 deletions packages/agora-rtc-react/src/hooks/usePublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export function usePublish(
const track = filterTracks[i];
if (track) {
if (canPublish(track)) {
await resolvedClient.unpublish(
pubTracks.current.filter(
pubTrack => pubTrack?.trackMediaType === track.trackMediaType,
) as ILocalTrack[],
);
try {
if (!isUnmountRef.current) {
setIsLoading(true);
Expand Down
4 changes: 2 additions & 2 deletions packages/agora-rtc-react/src/hooks/useRemoteAudioTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ export function useRemoteAudioTracks(
user,
mediaType: "audio",
});
nextTracks.current.splice(i, 1);
i--;
}
nextTracks.current.splice(i, 1);
i--;
}
}
if (unsubscribeList.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions packages/agora-rtc-react/src/hooks/useRemoteVideoTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ export function useRemoteVideoTracks(
user,
mediaType: "video",
});
nextTracks.current.splice(i, 1);
i--;
}
nextTracks.current.splice(i, 1);
i--;
}
}
if (unsubscribeList.length > 0) {
Expand Down

0 comments on commit 74c25f8

Please sign in to comment.