Skip to content
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
116 changes: 110 additions & 6 deletions apps/simple-camera/__tests__/visioncamera.session.harness.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
beforeAll,
describe,
expect,
it,
} from 'react-native-harness'
import { Platform } from 'react-native'
import { beforeAll, describe, expect, it } from 'react-native-harness'
import type {
CameraDeviceFactory,
TargetCameraPosition,
Expand Down Expand Up @@ -231,6 +227,114 @@ describe('VisionCamera - Session', () => {
await session.stop()
})

it('starts and reconfigures a session with haptics and system sounds playback enabled', async (context) => {
if (Platform.OS !== 'ios') {
return context.skip('allowHapticsAndSystemSoundsPlayback: iOS only')
}
const device = factory.getDefaultCamera('back')
expect(device).toBeDefined()
if (device == null) throw new Error('no back camera')

const session = await VisionCamera.createCameraSession(false)
const photoOutput = VisionCamera.createPhotoOutput({
targetResolution: CommonResolutions.HD_4_3,
containerFormat: 'jpeg',
quality: 0.8,
qualityPrioritization: 'balanced',
})
const connection = {
input: device,
outputs: [{ output: photoOutput, mirrorMode: 'auto' as const }],
constraints: [],
}

const started = deferred()
const stopped = deferred()
const startSub = session.addOnStartedListener(started.resolve)
const stopSub = session.addOnStoppedListener(stopped.resolve)
const errorSub = session.addOnErrorListener((error) => {
started.reject(error)
stopped.reject(error)
})

try {
const controllers = await session.configure([connection], {
allowHapticsAndSystemSoundsPlayback: true,
})
expect(controllers).toHaveLength(1)

await session.start()
await withTimeout(started.promise, 10_000, 'session start')

const reconfiguredControllers = await session.configure([connection], {
allowHapticsAndSystemSoundsPlayback: false,
})
expect(reconfiguredControllers).toHaveLength(1)

await session.stop()
await withTimeout(stopped.promise, 10_000, 'session stop')
} finally {
await session.stop()
startSub.remove()
stopSub.remove()
errorSub.remove()
}
})

it('starts and reconfigures a session with background audio playback enabled', async (context) => {
if (Platform.OS !== 'ios') {
return context.skip('allowBackgroundAudioPlayback: iOS only')
}
const device = factory.getDefaultCamera('back')
expect(device).toBeDefined()
if (device == null) throw new Error('no back camera')

const session = await VisionCamera.createCameraSession(false)
const photoOutput = VisionCamera.createPhotoOutput({
targetResolution: CommonResolutions.HD_4_3,
containerFormat: 'jpeg',
quality: 0.8,
qualityPrioritization: 'balanced',
})
const connection = {
input: device,
outputs: [{ output: photoOutput, mirrorMode: 'auto' as const }],
constraints: [],
}

const started = deferred()
const stopped = deferred()
const startSub = session.addOnStartedListener(started.resolve)
const stopSub = session.addOnStoppedListener(stopped.resolve)
const errorSub = session.addOnErrorListener((error) => {
started.reject(error)
stopped.reject(error)
})

try {
const controllers = await session.configure([connection], {
allowBackgroundAudioPlayback: true,
})
expect(controllers).toHaveLength(1)

await session.start()
await withTimeout(started.promise, 10_000, 'session start')

const reconfiguredControllers = await session.configure([connection], {
allowBackgroundAudioPlayback: false,
})
expect(reconfiguredControllers).toHaveLength(1)

await session.stop()
await withTimeout(stopped.promise, 10_000, 'session stop')
} finally {
await session.stop()
startSub.remove()
stopSub.remove()
errorSub.remove()
}
})

it('reconfigures a running session with a new output set', async () => {
const device = factory.getDefaultCamera('back')
expect(device).toBeDefined()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ final class HybridCameraSession: HybridCameraSessionSpec {
}
self.session.automaticallyConfiguresCaptureDeviceForWideColor = !hasCustomDynamicRangeConstraint

// Haptics and System Sounds Playback
if let allowHapticsAndSystemSoundsPlayback = config?.allowHapticsAndSystemSoundsPlayback {
let audioSession = AVAudioSession.sharedInstance()
if audioSession.allowHapticsAndSystemSoundsDuringRecording != allowHapticsAndSystemSoundsPlayback {
try audioSession.setAllowHapticsAndSystemSoundsDuringRecording(allowHapticsAndSystemSoundsPlayback)
}
}
// Background Audio Playback
if #available(iOS 18.0, *) {
if let allowBackgroundAudioPlayback = config?.allowBackgroundAudioPlayback {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,34 @@ import type { CameraSession } from './CameraSession.nitro'
*/
export interface CameraSessionConfiguration {
/**
* If enabled, the {@linkcode CameraSession} does not interrupt
* currently playing audio (e.g. music from a different app),
* while recording.
* If enabled, the device may play haptics/vibrations
* and system sounds during while the {@linkcode CameraSession}
* is active.
*
* If disabled (the default), all haptics and system
* sounds will be muted while the {@linkcode CameraSession}
* is active.
*
* @note If {@linkcode allowHapticsAndSystemSoundsPlayback}
* is true, haptics or system sounds may also be captured
* in Video recordings.
* Haptics may also affect stabilization or focus operations.
*
* @default false
* @platform iOS
*/
allowHapticsAndSystemSoundsPlayback?: boolean
/**
* If enabled, the device may play background audio,
* possibly from another app (e.g. Apple Music) while
* the {@linkcode CameraSession} is active.
*
* If disabled (the default), any playing audio will be
* stopped while the {@linkcode CameraSession} is active.
*
* @note If {@linkcode allowBackgroundAudioPlayback} is true,
* background audio may also be captured in Video recordings.
*
* If disabled (the default), any playing audio will be stopped
* when a recording is started.
* @default false
* @platform iOS
*/
Expand Down
Loading