Skip to content

Commit

Permalink
feat: implement onAudioTracks and onTextTracks on ios (#3503)
Browse files Browse the repository at this point in the history
* feat: implement onAudioTracks and onTextTracks on ios

* chore: lint code

* fix: rework previous fix to fix linter and be more aligned with architecture

---------

Co-authored-by: olivier <[email protected]>
  • Loading branch information
freeboub and olivier authored Feb 1, 2024
1 parent 41e9bcb commit 6a49cba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/pages/component/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This page shows the list of available callbacks to handle player notifications
|-------------------------------------------------------------------------------------------------|---------------------------|
| [onAudioBecomingNoisy](#onaudiobecomingnoisy) | Android, iOS |
| [onAudioFocusChanged](#onaudiofocuschanged) | Android |
| [onAudioTracks](#onaudiotracks) | Android |
| [onAudioTracks](#onaudiotracks) | Android, iOS |
| [onBandwidthUpdate](#onbandwidthupdate) | Android |
| [onBuffer](#onbuffer) | Android, iOS |
| [onEnd](#onend) | All |
Expand All @@ -27,7 +27,7 @@ This page shows the list of available callbacks to handle player notifications
| [onRestoreUserInterfaceForPictureInPictureStop](#onrestoreuserinterfaceforpictureinpicturestop) | iOS, visionOS |
| [onSeek](#onseek) | All |
| [onTimedMetadata](#ontimedmetadata) | Android, iOS, visionOS |
| [onTextTracks](#ontexttracks) | Android |
| [onTextTracks](#ontexttracks) | Android, iOS |
| [onVideoTracks](#onvideotracks) | Android |
| [onVolumeChange](#onvolumechange) | Android, iOS, visionOS |

Expand Down
10 changes: 10 additions & 0 deletions ios/Video/Features/RCTPlayerObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protocol RCTPlayerObserverHandler: RCTPlayerObserverHandlerObjc {
func handleVolumeChange(player: AVPlayer, change: NSKeyValueObservedChange<Float>)
func handleExternalPlaybackActiveChange(player: AVPlayer, change: NSKeyValueObservedChange<Bool>)
func handleViewControllerOverlayViewFrameChange(overlayView: UIView, change: NSKeyValueObservedChange<CGRect>)
func handleTracksChange(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<[AVPlayerItemTrack]>)
}

// MARK: - RCTPlayerObserver
Expand Down Expand Up @@ -96,6 +97,7 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate {
private var _playerViewControllerReadyForDisplayObserver: NSKeyValueObservation?
private var _playerLayerReadyForDisplayObserver: NSKeyValueObservation?
private var _playerViewControllerOverlayFrameObserver: NSKeyValueObservation?
private var _playerTracksObserver: NSKeyValueObservation?

deinit {
if let _handlers {
Expand Down Expand Up @@ -141,13 +143,21 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate {
options: [.new, .old],
changeHandler: _handlers.handlePlaybackLikelyToKeepUp
)

// observe tracks update
_playerTracksObserver = playerItem.observe(
\.tracks,
options: [.new, .old],
changeHandler: _handlers.handleTracksChange
)
}

func removePlayerItemObservers() {
_playerItemStatusObserver?.invalidate()
_playerPlaybackBufferEmptyObserver?.invalidate()
_playerPlaybackLikelyToKeepUpObserver?.invalidate()
_playerTimedMetadataObserver?.invalidate()
_playerTracksObserver?.invalidate()
}

func addPlayerViewControllerObservers() {
Expand Down
11 changes: 11 additions & 0 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
@objc var onRestoreUserInterfaceForPictureInPictureStop: RCTDirectEventBlock?
@objc var onGetLicense: RCTDirectEventBlock?
@objc var onReceiveAdEvent: RCTDirectEventBlock?
@objc var onTextTracks: RCTDirectEventBlock?
@objc var onAudioTracks: RCTDirectEventBlock?

@objc
func _onPictureInPictureStatusChanged() {
Expand Down Expand Up @@ -367,7 +369,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}

self._player = self._player ?? AVPlayer()

self._player?.replaceCurrentItem(with: playerItem)

self._playerObserver.player = self._player
self.applyModifiers()
self._player?.actionAtItemEnd = .none
Expand Down Expand Up @@ -1371,4 +1375,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH

onVideoBandwidthUpdate?(["bitrate": lastEvent.observedBitrate, "target": reactTag])
}

func handleTracksChange(playerItem _: AVPlayerItem, change _: NSKeyValueObservedChange<[AVPlayerItemTrack]>) {
all(RCTVideoUtils.getAudioTrackInfo(self._player), RCTVideoUtils.getTextTrackInfo(self._player)).then { audioTracks, textTracks in
self.onTextTracks?(["textTracks": textTracks])
self.onAudioTracks?(["audioTracks": audioTracks])
}
}
}
2 changes: 2 additions & 0 deletions ios/Video/RCTVideoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ @interface RCT_EXTERN_MODULE (RCTVideoManager, RCTViewManager)
RCT_EXPORT_VIEW_PROPERTY(onPictureInPictureStatusChanged, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onRestoreUserInterfaceForPictureInPictureStop, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onReceiveAdEvent, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onTextTracks, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onAudioTracks, RCTDirectEventBlock);

RCT_EXTERN_METHOD(save
: (NSDictionary*)options reactTag
Expand Down

0 comments on commit 6a49cba

Please sign in to comment.