Skip to content

Refactor/android native side untangle - #1553

Draft
mdydek wants to merge 5 commits into
mainfrom
refactor/android-native-side-untangle
Draft

mdydek wants to merge 5 commits into
mainfrom
refactor/android-native-side-untangle

Conversation

@mdydek

@mdydek mdydek commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Introduced changes

For now draft, because I do not have an access to the physical android and i can't test the bluetooth connection and reroute handling

Checklist

  • Linked relevant issue
  • Targets the right branch (release/X.Y only for fixes to the released version)
  • Updated relevant documentation
  • Added/Conducted relevant tests
  • Performed self-review of the code
  • Updated Web Audio API coverage
  • Added support for web
  • Updated old arch android spec file

Summary by CodeRabbit

  • Improvements
    • Improved Android audio routing and communication-device handling, including coordination between recording and session routes.
    • Updated Android audio-session, permissions, notification, and event handling.
    • Added clearer descriptions for available audio devices.
    • Standardized Android log tags for easier identification.
  • Documentation
    • Updated Android architecture and integration guidance to reflect the current audio and notification behavior.

mdydek and others added 5 commits December 4, 2026 15:07
…iptions

- AudioRouteEvents is the one subscription to device add/remove, the
  communication-device change (12+) and the SCO audio broadcast;
  MediaSessionManager.RouteEventDispatch hands each event to the input
  router, the communication session and the JS routeChange emitter.
  Replaces DeviceChangeListener, CommunicationDeviceCallbacks and the
  listeners BluetoothScoLink registered itself. An active communication
  session no longer emits each device change twice or a spurious
  NewDeviceAvailable on activation.
- SystemSubscription / BroadcastSubscription: idempotent register and
  unregister; MediaSessionManager keeps them in one list that cleanup()
  unregisters.
- MediaSessionManager.initialize split into notifications, audio session
  and routing steps; shared permission status check, getDevicesInfo built
  from one device-array helper.
- NotificationRegistry tracks active notifications as a set and creates
  them through getOrPut.
- AudioFocusListener and VolumeChangeListener send their events through
  one helper each; the volume event reuses getSystemVolume (0 instead of
  NaN when the stream maximum is 0).
- AudioAPIModule drops its unused LifecycleEventListener.
- Every logcat tag starts with "RNAudioAPI." (LogTag.PREFIX in Kotlin,
  RN_AUDIO_API_LOG_TAG in C++).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…uting and session classes

Pure move: AudioInputRouter, BluetoothScoLink, AudioRouteEvents and
RouteChangeReason go to system/routing; AudioFocusListener,
CommunicationSession and VolumeChangeListener to system/session.
MediaSessionManager held no media session, so it is now AudioSystem.
Classes named in JNI descriptors (NativeInputRouting,
NativeRecorderControl, NativeFileInfo) and in consuming apps' manifests
(CentralizedForegroundService) stay in system.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… session and routing

AudioSystem is wiring only: it builds AudioPermissions,
AudioNotifications, AudioSession and AudioRouting, registers their
module-lifetime subscriptions as one list, and releases session,
subscriptions, then routing on cleanup, in the same order as before.

- AudioSession owns focus, the communication session, volume and the
  main-thread posting its callers relied on.
- AudioRouting owns the input router, the route-event dispatch and
  devicesInfo; it depends on the session, never the reverse.
- AudioNotifications owns the channel, the registry and the playback
  receiver; CHANNEL_ID moves there. Receivers and the foreground service,
  which Android can start before initialize, use notificationsIfReady.
- AudioDeviceDescriptions builds the device maps JS receives.

No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AudioEventEmitter wraps the module's weak reference and is the only
caller of invokeHandlerWithEventNameAndEventBody. Focus, volume, route,
playback and recording notification events go through it, so no
component holds AudioAPIModule. PlaybackNotificationReceiver takes the
emitter in its constructor instead of a static strong reference to the
module, and RecordingNotification no longer throws when the module is
already gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…session

CommunicationSession (speaker / earpiece) and BluetoothScoLink (a
headset mic's SCO device) both set and cleared the communication device
without knowing of each other: the end of a headset-mic recording
cleared the session's speaker request, and activating or deactivating a
session during such a recording took its SCO link away, which the
recorder then fought back for.

CommunicationRoute is now the only caller of setCommunicationDevice /
clearCommunicationDevice. It keeps both requests; the recording's wins,
the session's is kept meanwhile and re-applied when the recording ends,
and the device is cleared only when neither holds one. A session request
made during the recording resolves at once and takes effect afterwards.
Below Android 12 nothing changes: there is no communication device and
no session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Android audio subsystem replaces MediaSessionManager with AudioSystem and coordinated session, routing, permission, and notification components. Route requests and event handling are reorganized. Kotlin and C++ Android log tags now use a shared RNAudioAPI. prefix.

Changes: Android audio subsystem

Layer / File(s) Summary
System ownership and module integration
.claude/skills/turbo-modules/SKILL.md, CLAUDE.md, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/AudioAPIModule.kt, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/{AudioSystem,AudioPermissions,AudioDeviceDescriptions,AudioEventEmitter,SystemSubscription,MediaSessionManager,PermissionRequestListener}.kt
AudioAPIModule delegates operations to AudioSystem and its components. AudioSystem initializes shared components and cleans them up; MediaSessionManager is removed.
Communication routing and route events
packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/CommunicationRoute.kt, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/{CommunicationDeviceCallbacks,DeviceChangeListener,NativeInputRouting}.kt, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/routing/*
CommunicationRoute stores session and capture requests, with capture requests taking priority. AudioRouteEvents and AudioRouting handle device and SCO changes, and BluetoothScoLink uses CommunicationRoute for capture routing.
Audio session and focus handling
packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/session/*, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/VolumeChangeListener.kt
AudioSession coordinates focus, communication sessions, volume observation, and device operations. CommunicationSession delegates route requests, and focus and volume events use AudioEventEmitter.
Notification ownership and event dispatch
.claude/skills/thread-safety-itc/SKILL.md, .claude/skills/turbo-modules/SKILL.md, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/*, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/CentralizedForegroundService.kt
AudioNotifications owns notification setup and registry operations. Playback and recording notification events use AudioEventEmitter; recording notification handling uses AudioSystem.notifications.
Android log-tag convention
.claude/skills/utilities/SKILL.md, packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/LogTag.kt, packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/*, packages/react-native-audio-api/common/cpp/audioapi/{core/effects/ConvolverNode.cpp,core/utils/AudioFileWriter.cpp,utils/AndroidLogTag.h,utils/Benchmark.hpp}
Kotlin and C++ logging use the RNAudioAPI. prefix. The utilities skill documents the prefix and examples.

Estimated code review effort: 4 (Complex) | ~60 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant AudioRouteEvents
  participant AudioRouting
  participant AudioInputRouter
  participant AudioEventEmitter
  participant AudioSession
  AudioRouteEvents->>AudioRouting: Send device and communication-route changes
  AudioRouting->>AudioInputRouter: Update input and Bluetooth routing
  AudioRouting->>AudioEventEmitter: Emit device route-change reason
  AudioRouting->>AudioSession: Notify communication-device change
Loading

Possibly related issues

  • software-mansion/react-native-audio-api#1548 — The changes centralize communication-device requests and give capture requests priority. They do not state that Bluetooth SCO headsets are excluded from automatic input selection, so the issue objective is only partly addressed.

Suggested reviewers: kimchouard, maciejmakowski2003, closetcaiman

Merge Risk: Minimal · up to 75457

This refactor reorganizes the Android audio system into separate components while preserving behavior. No concrete regression was found. Bluetooth routing was not tested on a physical device, so manual verification is still advisable before release.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Refactor/android native side untangle" matches the main change: the PR splits Android responsibilities into new classes (AudioSystem, AudioSession, AudioRouting, AudioNotifications, AudioPe…
Description check Passed The description follows the template's Introduced changes section and checklist. It lists the split of responsibilities, the CommunicationSession/BluetoothScoLink fix, the RNAudioAPI log tag prefix, a…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Refactor/android native side untangle" matches the main change: the PR splits Android responsibilities into new classes (AudioSystem, AudioSession, AudioRouting, AudioNotifications, AudioPermissions) and removes MediaSessionManager. It is a short, single phrase, but it uses the branch-style "Refactor/" prefix and the word "untangle" is not specific about what changed. It is still related to the main change, so it passes.

Full details: Description check

Explanation

The description follows the template's Introduced changes section and checklist. It lists the split of responsibilities, the CommunicationSession/BluetoothScoLink fix, the RNAudioAPI log tag prefix, and the stacked follow-up work. It also states the PR is a draft because the author cannot test Bluetooth on a physical device. The Closes # issue link is left blank, the Breaking changes section is empty, and most checklist items are unchecked, including tests and documentation. Those are gaps, but the core content is present, so the check passes.

Resolution

Consider adding the linked issue number under Closes #, filling in or removing the Breaking changes section, and checking off completed items such as documentation and self-review.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Clang (23.1.4)

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidAudioPlayer.cpp

Clang-Tidy produced no recognizable diagnostics; analysis is incomplete

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidRecorderInput.cpp

Clang-Tidy produced no recognizable diagnostics; analysis is incomplete

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidRecorderInput.h

Clang-Tidy produced no recognizable diagnostics; analysis is incomplete

  • 4 others

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6961521e-a0be-4da0-808e-378d77074c80
Commits

Reviewing files that changed from the base of the PR and between 2b70a87 and 754575d.

Files selected for processing: 43

  • .claude/skills/thread-safety-itc/SKILL.md
  • .claude/skills/turbo-modules/SKILL.md
  • .claude/skills/utilities/SKILL.md
  • CLAUDE.md
  • packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidAudioPlayer.cpp
  • packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidRecorderInput.cpp
  • packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidRecorderInput.h
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/AudioAPIModule.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/LogTag.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/AudioDeviceDescriptions.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/AudioEventEmitter.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/AudioPermissions.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/AudioSystem.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/CentralizedForegroundService.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/CommunicationDeviceCallbacks.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/CommunicationRoute.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/DeviceChangeListener.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/ForegroundServiceManager.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/MediaSessionManager.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/NativeInputRouting.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/PermissionRequestListener.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/SystemSubscription.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/VolumeChangeListener.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/ArtworkLoader.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/AudioNotifications.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/NotificationRegistry.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/PlaybackNotification.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/PlaybackNotificationReceiver.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/RecordingNotification.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/notification/RecordingNotificationReceiver.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/routing/AudioInputRouter.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/routing/AudioRouteEvents.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/routing/AudioRouting.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/routing/BluetoothScoLink.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/routing/RouteChangeReason.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/session/AudioFocusListener.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/session/AudioSession.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/session/CommunicationSession.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/session/VolumeChangeListener.kt
  • packages/react-native-audio-api/common/cpp/audioapi/core/effects/ConvolverNode.cpp
  • packages/react-native-audio-api/common/cpp/audioapi/core/utils/AudioFileWriter.cpp
  • packages/react-native-audio-api/common/cpp/audioapi/utils/AndroidLogTag.h
  • packages/react-native-audio-api/common/cpp/audioapi/utils/Benchmark.hpp
Files with no reviewable changes: 4

  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/CommunicationDeviceCallbacks.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/DeviceChangeListener.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/MediaSessionManager.kt
  • packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/system/VolumeChangeListener.kt

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant