-
Notifications
You must be signed in to change notification settings - Fork 728
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0534556
add hook to restart audio track on device change
olipyskoty ae91929
add tests for useRestartAudioTrackOnDeviceChange hook
olipyskoty ba4098f
add useRestartAudioTrackOnDeviceChange hook to VideoProvider
olipyskoty 7d02c56
update Video Provider test to include useRestartAudioTrackOnDeviceCha…
olipyskoty 4249332
add comment to useRestartAudioTrackOnDeviceChange hook
olipyskoty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...eoProvider/useRestartAudioTrackOnDeviceChange/useRestartAudioTrackOnDeviceChange.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
...ts/VideoProvider/useRestartAudioTrackOnDeviceChange/useRestartAudioTrackOnDeviceChange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)[]) { | ||
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]); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
What do you think?
There was a problem hiding this comment.
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!