1 // Copyright 2017-present 650 Industries. All rights reserved.
2 
3 #import <AVFoundation/AVFoundation.h>
4 
5 #import <jsi/jsi.h>
6 
7 // TODO: Replace this with <ReactCommon/TurboModuleUtils.h> after we upgrade to RN 0.66
8 #import <EXAV/CallbackWrapper.h>
9 
10 namespace jsi = facebook::jsi;
11 using CallInvoker = facebook::react::CallInvoker;
12 
13 // TODO: Replace this with just facebook::react namespace after we upgrade to RN 0.66
14 using JsiCallbackWrapper = expo::CallbackWrapper;
15 using LongLivedObjectCollection = expo::LongLivedObjectCollection;
16 
17 namespace expo {
18 namespace av {
19 
20 // A class managing lifecycle of audio sample buffer callbacks
21 class AudioSampleCallbackWrapper
22 {
23   std::weak_ptr<JsiCallbackWrapper> weakWrapper;
24 public:
25   AudioSampleCallbackWrapper(jsi::Function &&callback,
26                              jsi::Runtime &runtime,
27                              std::shared_ptr<CallInvoker> jsInvoker);
28 
29   ~AudioSampleCallbackWrapper();
30 
31   void call(AudioBuffer* buffer, double timestamp);
32 
33   // static members
34 public:
35   // called when JS VM is destroyed to remove all JSI callback objects
36   static void removeAllCallbacks() {
37     callbackCollection->clear();
38   }
39 
40 private:
41   static std::shared_ptr<LongLivedObjectCollection> callbackCollection;
42 };
43 
44 } // namespace av
45 } // namespace expo
46