-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add video SDK v2 and video stream (#1017)
* add: ethers support (#952) * fix: fix viem support, add ethers support * fix: remove @pushprotocol/socket dependency from restapi * fix: changed ethers fn * fix: fix ether changes * fix: fix subscribev2 * chore: readme changes * fix: fix ethers provider issue * fix: fix channel.update * feat(video-v2): add video v2 class and stream (#930) * feat(video-v2): add highlevel video class * feat(video): add video stream * fix(sendnotification): modify rules.access.data to be an object & code cleanup * fix(video): remove signer from input, throw err if signer, decrypted pgp key not found * feat(video): add sendNotification calls in connect, disconnect methods * fix(videonotificationrules): typo in VideoNotificationRules interface * feat(video stream): handle connect, retry internally from the SDK * feat(video connect): remove the connect method from the videov2 SDK * fix(videov2): create push signer instance to get chain id in initialize() * fix(video stream): add backwards compatibilty to rules object for older SDK versions * fix(video stream): fix bug in rules object creation * feat(video): update param names for video.initialize() * feat(video): add internal event handlers for stream in video SDK --------- Co-authored-by: Mohammed S <[email protected]> --------- Co-authored-by: Aman Gupta <[email protected]> Co-authored-by: Mohammed S <[email protected]>
- Loading branch information
1 parent
8e2cec9
commit 6ae974b
Showing
17 changed files
with
650 additions
and
56 deletions.
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
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
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,146 @@ | ||
import { ENV } from '../constants'; | ||
import CONSTANTS from '../constantsV2'; | ||
import { SignerType, VideoCallData, VideoCallStatus } from '../types'; | ||
import { Signer as PushSigner } from '../helpers'; | ||
|
||
import { Video as VideoV1, initVideoCallData } from '../video/Video'; | ||
import { VideoV2 } from '../video/VideoV2'; | ||
import { VideoInitializeOptions } from './pushAPITypes'; | ||
import { VideoEvent, VideoEventType } from '../pushstream/pushStreamTypes'; | ||
import { produce } from 'immer'; | ||
import { endStream } from '../video/helpers/mediaToggle'; | ||
|
||
export class Video { | ||
constructor( | ||
private account: string, | ||
private env: ENV, | ||
private decryptedPgpPvtKey?: string, | ||
private signer?: SignerType | ||
) {} | ||
|
||
async initialize( | ||
onChange: (fn: (data: VideoCallData) => VideoCallData) => void, | ||
options: VideoInitializeOptions | ||
) { | ||
const { stream, config, media } = options; | ||
|
||
if (!this.signer) { | ||
throw new Error('Signer is required for push video'); | ||
} | ||
|
||
if (!this.decryptedPgpPvtKey) { | ||
throw new Error( | ||
'PushSDK was initialized in readonly mode. Video functionality is not available.' | ||
); | ||
} | ||
|
||
const chainId = await new PushSigner(this.signer).getChainId(); | ||
|
||
if (!chainId) { | ||
throw new Error('Chain Id not retrievable from signer'); | ||
} | ||
|
||
// Initialize the video instance with the provided options | ||
const videoV1Instance = new VideoV1({ | ||
signer: this.signer!, | ||
chainId, | ||
pgpPrivateKey: this.decryptedPgpPvtKey!, | ||
env: this.env, | ||
setData: onChange, | ||
}); | ||
|
||
// Create the media stream with the provided options | ||
await videoV1Instance.create({ | ||
...(media && { | ||
stream: media, | ||
}), | ||
...(config?.audio && { | ||
audio: config.audio, | ||
}), | ||
...(config?.video && { | ||
video: config.video, | ||
}), | ||
}); | ||
|
||
// Setup video event handlers | ||
stream.on(CONSTANTS.STREAM.VIDEO, (data: VideoEvent) => { | ||
const { | ||
address, | ||
signal, | ||
meta: { rules }, | ||
} = data.peerInfo; | ||
|
||
const chatId = rules.access.data.chatId; | ||
|
||
// If the event is RequestVideo, update the video call 'data' state with the incoming call data | ||
if (data.event === VideoEventType.RequestVideo) { | ||
videoV1Instance.setData((oldData) => { | ||
return produce(oldData, (draft) => { | ||
draft.local.address = this.account; | ||
draft.incoming[0].address = address; | ||
draft.incoming[0].status = VideoCallStatus.RECEIVED; | ||
draft.meta.chatId = chatId!; | ||
draft.meta.initiator.address = address; | ||
draft.meta.initiator.signal = signal; | ||
}); | ||
}); | ||
} | ||
|
||
// Check if the chatId from the incoming video event matches the chatId of the current video instance | ||
if (chatId && chatId === videoV1Instance.data.meta.chatId) { | ||
// If the event is DenyVideo, destroy the local stream & reset the video call data | ||
if (data.event === VideoEventType.DenyVideo) { | ||
// destroy the local stream | ||
if (videoV1Instance.data.local.stream) { | ||
endStream(videoV1Instance.data.local.stream); | ||
} | ||
|
||
videoV1Instance.setData(() => initVideoCallData); | ||
} | ||
|
||
// If the event is ApproveVideo or RetryApproveVideo, connect to the video | ||
if ( | ||
data.event === VideoEventType.ApproveVideo || | ||
data.event === VideoEventType.RetryApproveVideo | ||
) { | ||
videoV1Instance.connect({ peerAddress: address, signalData: signal }); | ||
} | ||
|
||
// If the event is RetryRequestVideo and the current instance is the initiator, send a request | ||
if ( | ||
data.event === VideoEventType.RetryRequestVideo && | ||
videoV1Instance.isInitiator() | ||
) { | ||
videoV1Instance.request({ | ||
senderAddress: this.account, | ||
recipientAddress: address, | ||
rules, | ||
retry: true, | ||
}); | ||
} | ||
|
||
// If the event is RetryRequestVideo and the current instance is not the initiator, accept the request | ||
if ( | ||
data.event === VideoEventType.RetryRequestVideo && | ||
!videoV1Instance.isInitiator() | ||
) { | ||
videoV1Instance.acceptRequest({ | ||
signalData: signal, | ||
senderAddress: this.account, | ||
recipientAddress: address, | ||
rules, | ||
retry: true, | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
// Return an instance of the video v2 class | ||
return new VideoV2({ | ||
videoV1Instance, | ||
account: this.account, | ||
decryptedPgpPvtKey: this.decryptedPgpPvtKey!, | ||
env: this.env, | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.