Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video 4601 fix audio input device #487

Merged
merged 5 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/VideoProvider/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { renderHook } from '@testing-library/react-hooks';
import { Room, TwilioError } from 'twilio-video';
import { VideoProvider } from './index';
import useLocalTracks from './useLocalTracks/useLocalTracks';
import useRestartAudioTrackOnDeviceChange from './useRestartAudioTrackOnDeviceChange/useRestartAudioTrackOnDeviceChange';
import useRoom from './useRoom/useRoom';
import useHandleRoomDisconnection from './useHandleRoomDisconnection/useHandleRoomDisconnection';
import useHandleTrackPublicationFailed from './useHandleTrackPublicationFailed/useHandleTrackPublicationFailed';
Expand All @@ -23,6 +24,7 @@ jest.mock('./useLocalTracks/useLocalTracks', () =>
);
jest.mock('./useHandleRoomDisconnection/useHandleRoomDisconnection');
jest.mock('./useHandleTrackPublicationFailed/useHandleTrackPublicationFailed');
jest.mock('./useRestartAudioTrackOnDeviceChange/useRestartAudioTrackOnDeviceChange');

describe('the VideoProvider component', () => {
it('should correctly return the Video Context object', () => {
Expand Down Expand Up @@ -57,6 +59,7 @@ describe('the VideoProvider component', () => {
expect.any(Function)
);
expect(useHandleTrackPublicationFailed).toHaveBeenCalledWith(mockRoom, expect.any(Function));
expect(useRestartAudioTrackOnDeviceChange).toHaveBeenCalledWith(result.current.localTracks);
});

it('should call the onError function when there is an error', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/components/VideoProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AttachVisibilityHandler from './AttachVisibilityHandler/AttachVisibilityH
import useHandleRoomDisconnection from './useHandleRoomDisconnection/useHandleRoomDisconnection';
import useHandleTrackPublicationFailed from './useHandleTrackPublicationFailed/useHandleTrackPublicationFailed';
import useLocalTracks from './useLocalTracks/useLocalTracks';
import useRestartAudioTrackOnDeviceChange from './useRestartAudioTrackOnDeviceChange/useRestartAudioTrackOnDeviceChange';
import useRoom from './useRoom/useRoom';
import useScreenShareToggle from './useScreenShareToggle/useScreenShareToggle';

Expand Down Expand Up @@ -72,6 +73,7 @@ export function VideoProvider({ options, children, onError = () => {} }: VideoPr
toggleScreenShare
);
useHandleTrackPublicationFailed(room, onError);
useRestartAudioTrackOnDeviceChange(localTracks);

return (
<VideoContext.Provider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { renderHook } from '@testing-library/react-hooks';
import useRestartAudioTrackOnDeviceChange from './useRestartAudioTrackOnDeviceChange';

let mockAddEventListener = jest.fn();
let mockRemoveEventListener = jest.fn();

// @ts-ignore
navigator.mediaDevices = {
addEventListener: mockAddEventListener,
removeEventListener: mockRemoveEventListener,
};

describe('the useHandleTrackPublicationFailed hook', () => {
afterEach(jest.clearAllMocks);

it('should not restart the audio track if mediaStreamTrack readyState has not ended', () => {
const localTrack = [{ kind: 'audio', mediaStreamTrack: { readyState: 'live' }, restart: jest.fn() }];
renderHook(() => useRestartAudioTrackOnDeviceChange(localTrack as any));

// call handleDeviceChange function:
mockAddEventListener.mock.calls[0][1]();

expect(localTrack[0].restart).not.toHaveBeenCalled();
});

it('should restart the audio track if mediaStreamTrack readyState has ended', () => {
const localTrack = [{ kind: 'audio', mediaStreamTrack: { readyState: 'ended' }, restart: jest.fn() }];
renderHook(() => useRestartAudioTrackOnDeviceChange(localTrack as any));

// call handleDeviceChange function:
mockAddEventListener.mock.calls[0][1]();

expect(localTrack[0].restart).toHaveBeenCalled();
});

it('should remove the event handler when component unmounts', () => {
const { unmount } = renderHook(() => useRestartAudioTrackOnDeviceChange([]));
unmount();

expect(mockRemoveEventListener).toHaveBeenCalledWith('devicechange', expect.any(Function));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { LocalAudioTrack, LocalVideoTrack } from 'twilio-video';
import { useEffect } from 'react';

/*
* If a user has published an audio track from an external audio input device and
* disconnects the device, the published audio track will be stopped and the user
* will no longer be heard by other participants.
*
* To prevent this issue, this hook will re-acquire a mediaStreamTrack from the system's
* default audio device when it detects that the published audio device has been disconnected.
*/

export default function useRestartAudioTrackOnDeviceChange(localTracks: (LocalAudioTrack | LocalVideoTrack)[]) {
Comment on lines +12 to +13
Copy link
Contributor

@timmydoza timmydoza Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to have a comment here to explain the purpose of the hook. I was thinking something like this:

Suggested change
export default function useRestartAudioTrackOnDeviceChange(localTracks: (LocalAudioTrack | LocalVideoTrack)[]) {
// If a user has published an audio track from an external audio input device and disconnects the device, the published
// audio track will be stopped and the user will no longer be heard be other participants. To prevent this issue, this hook
// will re-acquire a mediaStreamTrack from the system's default audio device when it detects that the published audio device
// has been disconnected.
export default function useRestartAudioTrackOnDeviceChange(localTracks: (LocalAudioTrack | LocalVideoTrack)[]) {

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that is a good idea! i reformatted the comment but kept your wording. let me know if i should change it back!

const audioTrack = localTracks.find(track => track.kind === 'audio');

useEffect(() => {
const handleDeviceChange = () => {
if (audioTrack?.mediaStreamTrack.readyState === 'ended') {
audioTrack.restart();
}
};

navigator.mediaDevices.addEventListener('devicechange', handleDeviceChange);

return () => {
navigator.mediaDevices.removeEventListener('devicechange', handleDeviceChange);
};
}, [audioTrack]);
}