-
Notifications
You must be signed in to change notification settings - Fork 1.4k
refactor(voip): split MediaSessionInstance into Controller + Orchestrator #7112
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
Closed
diegolmello
wants to merge
13
commits into
feat.voip-lib-new
from
refactor/voip-media-session-controller
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
958b7fe
Slice 1: Extract MediaSessionController
diegolmello 148ba9e
Slice 2: Refactor MediaSessionInstance to use MediaSessionController
diegolmello ded71fe
Slice 3: Add navigation callbacks to CallOrchestrator
diegolmello 41d3db2
Slice 4: Add CallResult return types
diegolmello 38b2acb
Configure Jest for media-signaling module transform
diegolmello 9637143
Slice 7: Update integration tests to use new mock structure
diegolmello eb543cc
Slice 6: Write CallOrchestrator boundary tests
diegolmello 6f1c77a
Fix lint errors and update tests
diegolmello 84d4b60
Fix CallOrchestrator listener leak and API issues
diegolmello c2258a6
Fix init() crash and stale newCall listener in CallOrchestrator
diegolmello 11b3792
Fix listener leaks on re-init and duplicate newCall handler
diegolmello 98039c1
chore: gitignore .claude and .superset local tooling dirs
diegolmello fb07a0e
Fix endCall() edge cases where onCallEnded is silently skipped
diegolmello 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 hidden or 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 |
|---|---|---|
|
|
@@ -83,4 +83,8 @@ e2e/e2e_account.ts | |
| **/e2e_account.js | ||
| **/e2e_account.ts | ||
|
|
||
| *.p8 | ||
| *.p8 | ||
|
|
||
| # Local tooling | ||
| .claude/ | ||
| .superset/ | ||
This file contains hidden or 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 hidden or 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 hidden or 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,127 @@ | ||
| import type { IClientMediaCall } from '@rocket.chat/media-signaling'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the unused type import.
🧰 Tools🪛 ESLint[error] 1-1: 'IClientMediaCall' is defined but never used. ( 🤖 Prompt for AI Agents |
||
|
|
||
| import { mediaSessionStore } from './MediaSessionStore'; | ||
| import { MediaSessionController } from './MediaSessionController'; | ||
|
|
||
| jest.mock('./MediaSessionStore', () => ({ | ||
| mediaSessionStore: { | ||
| setWebRTCProcessorFactory: jest.fn(), | ||
| getInstance: jest.fn(), | ||
| dispose: jest.fn(), | ||
| onChange: jest.fn(() => jest.fn()) | ||
| } | ||
| })); | ||
|
|
||
| jest.mock('../sdk', () => ({ | ||
| default: { | ||
| onStreamData: jest.fn(() => ({ stop: jest.fn() })) | ||
| } | ||
| })); | ||
|
|
||
| jest.mock('react-native-device-info', () => ({ | ||
| getUniqueIdSync: () => 'device-123' | ||
| })); | ||
|
|
||
| jest.mock('../../store/auxStore', () => ({ | ||
| store: { | ||
| getState: () => ({ | ||
| settings: { | ||
| VoIP_TeamCollab_Ice_Servers: 'stun:stun.l.google.com:19302', | ||
| VoIP_TeamCollab_Ice_Gathering_Timeout: 5000 | ||
| } | ||
| }), | ||
| subscribe: jest.fn(() => jest.fn()) | ||
| } | ||
| })); | ||
|
|
||
| type MockMediaSignalingSession = { | ||
| userId: string; | ||
| on: jest.Mock; | ||
| setIceGatheringTimeout: jest.Mock; | ||
| }; | ||
|
|
||
| jest.mock('@rocket.chat/media-signaling', () => ({ | ||
| MediaCallWebRTCProcessor: jest.fn().mockImplementation(function (this: unknown) { | ||
| return this; | ||
| }), | ||
| MediaSignalingSession: jest | ||
| .fn() | ||
| .mockImplementation(function MockMediaSignalingSession(this: MockMediaSignalingSession, config: { userId: string }) { | ||
| this.userId = config.userId; | ||
| this.on = jest.fn(); | ||
| this.setIceGatheringTimeout = jest.fn(); | ||
| }) | ||
| })); | ||
|
|
||
| jest.mock('react-native-webrtc', () => ({ | ||
| registerGlobals: jest.fn() | ||
| })); | ||
|
|
||
| describe('MediaSessionController', () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('constructor', () => { | ||
| it('should initialize with empty session', () => { | ||
| const controller = new MediaSessionController('user-123'); | ||
| expect(controller.getSession()).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('configure', () => { | ||
| it('should set WebRTC processor factory with ICE servers', () => { | ||
| const controller = new MediaSessionController('user-123'); | ||
| controller.configure(); | ||
|
|
||
| expect(mediaSessionStore.setWebRTCProcessorFactory).toHaveBeenCalledTimes(1); | ||
| const factory = (mediaSessionStore.setWebRTCProcessorFactory as jest.Mock).mock.calls[0][0]; | ||
| const processor = factory({ | ||
| rtc: { iceServers: [] }, | ||
| iceGatheringTimeout: 5000 | ||
| }); | ||
| expect(processor).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should create session via mediaSessionStore', () => { | ||
| const mockInstance = { startCall: jest.fn() }; | ||
| (mediaSessionStore.getInstance as jest.Mock).mockReturnValue(mockInstance); | ||
|
|
||
| const controller = new MediaSessionController('user-123'); | ||
| controller.configure(); | ||
|
|
||
| expect(mediaSessionStore.getInstance).toHaveBeenCalledWith('user-123'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getSession', () => { | ||
| it('should return null before configure', () => { | ||
| const controller = new MediaSessionController('user-123'); | ||
| expect(controller.getSession()).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return session after configure', () => { | ||
| const mockInstance = { startCall: jest.fn() }; | ||
| (mediaSessionStore.getInstance as jest.Mock).mockReturnValue(mockInstance); | ||
|
|
||
| const controller = new MediaSessionController('user-123'); | ||
| controller.configure(); | ||
|
|
||
| expect(controller.getSession()).toBe(mockInstance); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reset', () => { | ||
| it('should dispose mediaSessionStore and set session to null', () => { | ||
| const mockInstance = { startCall: jest.fn() }; | ||
| (mediaSessionStore.getInstance as jest.Mock).mockReturnValue(mockInstance); | ||
|
|
||
| const controller = new MediaSessionController('user-123'); | ||
| controller.configure(); | ||
| controller.reset(); | ||
|
|
||
| expect(mediaSessionStore.dispose).toHaveBeenCalled(); | ||
| expect(controller.getSession()).toBeNull(); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,87 @@ | ||
| import { | ||
| MediaCallWebRTCProcessor, | ||
| type WebRTCProcessorConfig, | ||
| type MediaSignalingSession | ||
| } from '@rocket.chat/media-signaling'; | ||
| import { registerGlobals } from 'react-native-webrtc'; | ||
|
|
||
| import { mediaSessionStore } from './MediaSessionStore'; | ||
| import { parseStringToIceServers } from './parseStringToIceServers'; | ||
| import { store } from '../../store/auxStore'; | ||
| import type { IceServer } from '../../../definitions/Voip'; | ||
|
|
||
| export class MediaSessionController { | ||
| private userId: string; | ||
| private session: MediaSignalingSession | null = null; | ||
| private iceServers: IceServer[] = []; | ||
| private iceGatheringTimeout: number = 5000; | ||
| private storeTimeoutUnsubscribe: (() => void) | null = null; | ||
| private storeIceServersUnsubscribe: (() => void) | null = null; | ||
|
|
||
| constructor(userId: string) { | ||
| this.userId = userId; | ||
| } | ||
|
|
||
| public configure(): void { | ||
| registerGlobals(); | ||
| this.configureIceServers(); | ||
|
|
||
| mediaSessionStore.setWebRTCProcessorFactory( | ||
| (config: WebRTCProcessorConfig) => | ||
| new MediaCallWebRTCProcessor({ | ||
| ...config, | ||
| rtc: { ...config.rtc, iceServers: this.iceServers }, | ||
| iceGatheringTimeout: this.iceGatheringTimeout | ||
| }) | ||
| ); | ||
|
|
||
| this.session = mediaSessionStore.getInstance(this.userId); | ||
| } | ||
|
|
||
| public getSession(): MediaSignalingSession | null { | ||
| return this.session; | ||
| } | ||
|
|
||
| public refreshSession(): MediaSignalingSession | null { | ||
| this.session = mediaSessionStore.getInstance(this.userId); | ||
| return this.session; | ||
| } | ||
|
|
||
| public reset(): void { | ||
| if (this.storeTimeoutUnsubscribe) { | ||
| this.storeTimeoutUnsubscribe(); | ||
| this.storeTimeoutUnsubscribe = null; | ||
| } | ||
| if (this.storeIceServersUnsubscribe) { | ||
| this.storeIceServersUnsubscribe(); | ||
| this.storeIceServersUnsubscribe = null; | ||
| } | ||
| mediaSessionStore.dispose(); | ||
| this.session = null; | ||
| } | ||
|
|
||
| private getIceServers(): IceServer[] { | ||
| const iceServers = store.getState().settings.VoIP_TeamCollab_Ice_Servers as string; | ||
| return parseStringToIceServers(iceServers); | ||
| } | ||
|
|
||
| private configureIceServers(): void { | ||
| this.iceServers = this.getIceServers(); | ||
| this.iceGatheringTimeout = store.getState().settings.VoIP_TeamCollab_Ice_Gathering_Timeout as number; | ||
|
|
||
| this.storeTimeoutUnsubscribe = store.subscribe(() => { | ||
| const currentTimeout = store.getState().settings.VoIP_TeamCollab_Ice_Gathering_Timeout as number; | ||
| if (currentTimeout !== this.iceGatheringTimeout) { | ||
| this.iceGatheringTimeout = currentTimeout; | ||
| this.session?.setIceGatheringTimeout(this.iceGatheringTimeout); | ||
| } | ||
| }); | ||
|
|
||
| this.storeIceServersUnsubscribe = store.subscribe(() => { | ||
| const currentIceServers = this.getIceServers(); | ||
| if (currentIceServers !== this.iceServers) { | ||
| this.iceServers = currentIceServers; | ||
| } | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Use a mock function for
startCall.The rest parameter here is unused, and ESLint is already flagging it. Switching this to
jest.fn(() => ({ success: true, callId: '' }))removes the lint failure and keeps the mock inspectable.🧰 Tools
🪛 ESLint
[error] 54-54: 'args' is defined but never used. Allowed unused args must match /^_/u.
(
@typescript-eslint/no-unused-vars)🤖 Prompt for AI Agents