Skip to content

Commit 69f111c

Browse files
Add usage code for changeBackgroundReplacementImage in storybook (#961)
1 parent 4e54039 commit 69f111c

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Added
1313

1414
- Add `changeBackgroundReplacementImage` function in the `BackgroundReplacementProvider` to enable the functionality of changing the background replacement image. `changeBackgroundReplacementImage` will take a `Blob` as its parameter and return a `Promise`.
15+
- Add usage of `changeBackgroundReplacementImage` in storybook.
1516

1617
### Removed
1718

src/providers/BackgroundReplacementProvider/docs/BackgroundReplacementProvider.mdx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ For more information, refer to [Video Processing APIs](https://github.com/aws/am
4343

4444
You can access the current `backgroundReplacementProcessor` applied to the video device that is generated when you call `createBackgroundBlurDevice`. You can apply observer notifications to the processor. Refer to [the guide for adding observer notifications to a BackgroundReplacementProcessor](https://github.com/aws/amazon-chime-sdk-js/blob/main/guides/15_Background_Filter_Video_Processor.md#observer-notifications)
4545

46-
You can change the background replacement image by `changeBackgroundReplacementImage`. The background can be set to either a custom color or an image, both of which need to be converted into a `Blob`.
46+
You can change the background replacement image by `changeBackgroundReplacementImage`. The background can be set to either a custom color or an image, both of which need to be converted into a `Blob`. Refer to [BackgroundReplacementDropdown in aws-samples/amazon-chime-sdk](https://github.com/aws-samples/amazon-chime-sdk/blob/main/apps/meeting/src/components/DeviceSelection/CameraDevices/BackgroundReplacementDropdown.tsx) for usage.
4747

4848
One thing to note is that calling `meetingManager.startVideoInputDevice()` with a `Device` type while the current selected video input device is a `VideoTransformDevice`
4949
will automatically stop any processors running within a `DefaultVideoTransformDevice` that was previously running. Lastly, make sure to construct a new `DefaultVideoTransformDevice` using `createBackgroundReplacementDevice`.
@@ -75,6 +75,7 @@ import {
7575
useMeetingManager,
7676
useBackgroundReplacement,
7777
useVideoInputs,
78+
useLogger,
7879
} from 'amazon-chime-sdk-component-library-react';
7980

8081
const App = () => (
@@ -88,11 +89,14 @@ const App = () => (
8889
const MyChild = () => {
8990
const meetingManager = useMeetingManager();
9091
const { selectedDevice } = useVideoInputs();
91-
const [isVideoTransformChecked, setIsVideoTransformCheckOn] = useState(false);
92+
const [isVideoTransformCheckBoxOn, setisVideoTransformCheckBoxOn] =
93+
useState(false);
9294
const {
9395
isBackgroundReplacementSupported,
9496
createBackgroundReplacementDevice,
97+
changeBackgroundReplacementImage,
9598
} = useBackgroundReplacement();
99+
const logger = useLogger();
96100

97101
useEffect(() => {
98102
async function toggleBackgroundReplacement() {
@@ -121,6 +125,19 @@ const MyChild = () => {
121125
setisVideoTransformCheckOn((current) => !current);
122126
};
123127

128+
const changeBackgroundReplacementOption = async (hexColor: string) => {
129+
let current = selectedDevice;
130+
if (current === undefined) {
131+
return;
132+
}
133+
try {
134+
const blob = await createBlob(hexColor); // Refer to https://github.com/aws-samples/amazon-chime-sdk/blob/main/apps/meeting/src/utils/background-replacement.ts#L7 for the implementation.
135+
await changeBackgroundReplacementImage(blob);
136+
} catch (error) {
137+
logger.error(`Error trying to change background replacement image ${error}`);
138+
}
139+
};
140+
124141
return (
125142
<div>
126143
{isBackgroundReplacementSupported && (
@@ -129,6 +146,11 @@ const MyChild = () => {
129146
? 'Background Replacement Enabled'
130147
: 'Enable Background Replacement'}
131148
</button>
149+
<button
150+
onClick={async () => await changeBackgroundReplacementOption('#000000')}
151+
>
152+
'Change background replacement to Black'
153+
</button>
132154
)}
133155
</div>
134156
);

src/providers/BackgroundReplacementProvider/docs/useBackgroundReplacement.mdx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Lastly, make sure to construct a new `DefaultVideoTransformDevice` using `create
1919

2020
You can access the current `backgroundReplacementProcessor` applied to the video device that is generated when you call `createBackgroundBlurDevice`. You can apply observer notifications to the processor. Refer to [the guide for adding observer notifications to a BackgroundReplacementProcessor](https://github.com/aws/amazon-chime-sdk-js/blob/main/guides/15_Background_Filter_Video_Processor.md#observer-notifications)
2121

22-
You can change the background replacement image by `changeBackgroundReplacementImage`. The background can be set to either a custom color or an image, both of which need to be converted into a `Blob`.
22+
You can change the background replacement image by `changeBackgroundReplacementImage`. The background can be set to either a custom color or an image, both of which need to be converted into a `Blob`. Refer to [BackgroundReplacementDropdown in aws-samples/amazon-chime-sdk](https://github.com/aws-samples/amazon-chime-sdk/blob/main/apps/meeting/src/components/DeviceSelection/CameraDevices/BackgroundReplacementDropdown.tsx) for usage.
2323
Background replacement related logs can be found in the browser developer tools when the `BackgroundReplacementProvider` is used within the app component tree.
2424

2525
## Return Value
@@ -60,6 +60,7 @@ import {
6060
useMeetingManager,
6161
useBackgroundReplacement,
6262
useVideoInputs,
63+
useLogger,
6364
} from 'amazon-chime-sdk-component-library-react';
6465

6566
const App = () => (
@@ -78,7 +79,9 @@ const MyChild = () => {
7879
const {
7980
isBackgroundReplacementSupported,
8081
createBackgroundReplacementDevice,
82+
changeBackgroundReplacementImage,
8183
} = useBackgroundReplacement();
84+
const logger = useLogger();
8285

8386
useEffect(() => {
8487
async function toggleBackgroundReplacement() {
@@ -107,6 +110,19 @@ const MyChild = () => {
107110
setisVideoTransformCheckBoxOn((current) => !current);
108111
};
109112

113+
const changeBackgroundReplacementOption = async (hexColor: string) => {
114+
let current = selectedDevice;
115+
if (current === undefined) {
116+
return;
117+
}
118+
try {
119+
const blob = await createBlob(hexColor); // Refer to https://github.com/aws-samples/amazon-chime-sdk/blob/main/apps/meeting/src/utils/background-replacement.ts#L7 for the implementation.
120+
await changeBackgroundReplacementImage(blob);
121+
} catch (error) {
122+
logger.error(`Error trying to change background replacement image ${error}`);
123+
}
124+
};
125+
110126
return (
111127
<div>
112128
{isBackgroundReplacementSupported && (
@@ -115,6 +131,11 @@ const MyChild = () => {
115131
? 'Background Replacement Enabled'
116132
: 'Enable Background Replacement'}
117133
</button>
134+
<button
135+
onClick={async () => await changeBackgroundReplacementOption('#000000')}
136+
>
137+
'Change background replacement to Black'
138+
</button>
118139
)}
119140
</div>
120141
);

0 commit comments

Comments
 (0)