Skip to content

Commit 9a9d602

Browse files
committed
feat: wait a bit more before segfaulting
1 parent 000e636 commit 9a9d602

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

app/src/main/cpp/NativeTrack.cpp

+17-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ typedef void*(*ZN7android7RefBaseC2Ev_t)(void* thisptr);
1717
static ZN7android7RefBaseC2Ev_t ZN7android7RefBaseC2Ev = nullptr;
1818
typedef void*(*ZN7android7RefBaseD2Ev_t)(void* thisptr);
1919
static ZN7android7RefBaseD2Ev_t ZN7android7RefBaseD2Ev = nullptr;
20+
typedef void(*ZNK7android7RefBase9incStrongEPKv_t)(void* thisptr, void* id);
21+
static ZNK7android7RefBase9incStrongEPKv_t ZNK7android7RefBase9incStrongEPKv = nullptr;
22+
typedef void(*ZNK7android7RefBase9decStrongEPKv_t)(void* thisptr, void* id);
23+
static ZNK7android7RefBase9decStrongEPKv_t ZNK7android7RefBase9decStrongEPKv = nullptr;
24+
typedef void*(*ZNK7android7RefBase10createWeakEPKv_t)(void* thisptr, void* id);
25+
static ZNK7android7RefBase10createWeakEPKv_t ZNK7android7RefBase10createWeakEPKv = nullptr;
2026
#include "aosp_stubs.h"
2127
typedef void*(*ZN7android19parcelForJavaObjectEP7_JNIEnvP8_jobject_t)(_JNIEnv*, _jobject*);
2228
static ZN7android19parcelForJavaObjectEP7_JNIEnvP8_jobject_t ZN7android19parcelForJavaObjectEP7_JNIEnvP8_jobject = nullptr;
@@ -26,19 +32,13 @@ typedef void*(*ZN7android10AudioTrackC1ERKNS_7content22AttributionSourceStateE_t
2632
static ZN7android10AudioTrackC1ERKNS_7content22AttributionSourceStateE_t ZN7android10AudioTrackC1ERKNS_7content22AttributionSourceStateE = nullptr;
2733
typedef void*(*ZN7android10AudioTrackC1Ev_t)(void* thisptr);
2834
static ZN7android10AudioTrackC1Ev_t ZN7android10AudioTrackC1Ev = nullptr;
29-
typedef void(*ZNK7android7RefBase9incStrongEPKv_t)(void* thisptr, void* id);
30-
static ZNK7android7RefBase9incStrongEPKv_t ZNK7android7RefBase9incStrongEPKv = nullptr;
31-
typedef void(*ZNK7android7RefBase9decStrongEPKv_t)(void* thisptr, void* id);
32-
static ZNK7android7RefBase9decStrongEPKv_t ZNK7android7RefBase9decStrongEPKv = nullptr;
33-
typedef void*(*ZNK7android7RefBase10createWeakEPKv_t)(void* thisptr, void* id);
34-
static ZNK7android7RefBase10createWeakEPKv_t ZNK7android7RefBase10createWeakEPKv = nullptr;
3535
typedef void(*ZN7android7RefBase12weakref_type7decWeakEPKv_t)(void* thisptr, void* id);
3636
static ZN7android7RefBase12weakref_type7decWeakEPKv_t ZN7android7RefBase12weakref_type7decWeakEPKv = nullptr;
3737
typedef int32_t(*ZN7android10AudioTrack3setE19audio_stream_type_tj14audio_format_t20audio_channel_mask_tm20audio_output_flags_tRKNS_2wpINS0_19IAudioTrackCallbackEEEiRKNS_2spINS_7IMemoryEEEb15audio_session_tNS0_13transfer_typeEPK20audio_offload_info_tRKNS_7content22AttributionSourceStateEPK18audio_attributes_tbfi_t)
3838
(void* thisptr, int32_t streamType, uint32_t sampleRate, uint32_t format, uint32_t channelMask, size_t frameCount /* = 0 */, uint32_t flags /* = 0 */, fake_wp callback /* = nullptr */, int32_t notificationFrames /* = 0 */, fake_sp sharedMemory /* = nullptr */, bool threadCanCallJava /* = false */, int32_t audioSessionId /* = 0 */, transfer_type transferType /* = TRANSFER_DEFAULT */, void* offloadInfo /* = nullptr */, void* attributionSource, void* attributes /* = nullptr */, bool doNotReconnect /* = false */, float maxRequiredSpeed /* = 1.0f */, int selectedDeviceId /* = 0 */);
3939
static ZN7android10AudioTrack3setE19audio_stream_type_tj14audio_format_t20audio_channel_mask_tm20audio_output_flags_tRKNS_2wpINS0_19IAudioTrackCallbackEEEiRKNS_2spINS_7IMemoryEEEb15audio_session_tNS0_13transfer_typeEPK20audio_offload_info_tRKNS_7content22AttributionSourceStateEPK18audio_attributes_tbfi_t ZN7android10AudioTrack3setE19audio_stream_type_tj14audio_format_t20audio_channel_mask_tm20audio_output_flags_tRKNS_2wpINS0_19IAudioTrackCallbackEEEiRKNS_2spINS_7IMemoryEEEb15audio_session_tNS0_13transfer_typeEPK20audio_offload_info_tRKNS_7content22AttributionSourceStateEPK18audio_attributes_tbfi = nullptr;
4040

41-
/*class MyCallback : virtual android::AudioTrack::IAudioTrackCallback {
41+
class MyCallback : public virtual android::AudioTrack::IAudioTrackCallback {
4242
public:
4343
MyCallback() : RefBase() {};
4444
void onUnderrun() override {
@@ -56,11 +56,11 @@ static ZN7android10AudioTrack3setE19audio_stream_type_tj14audio_format_t20audio_
5656
void onStreamEnd() override {
5757
ALOGI("MyCallback::onStreamEnd called");
5858
}
59-
};*/
59+
};
6060

6161
struct track_holder {
6262
void* track;
63-
void* callback;
63+
MyCallback* callback;
6464
void* ats;
6565
};
6666

@@ -113,19 +113,13 @@ Java_org_akanework_gramophone_logic_utils_NativeTrack_create(
113113
memset(ats, 0, ATTRIBUTION_SOURCE_SIZE);
114114
ZN7android7content22AttributionSourceState14readFromParcelEPKNS_6ParcelE(ats, myParcel);
115115
ZN7android10AudioTrackC1ERKNS_7content22AttributionSourceStateE(theTrack, ats);
116-
//ZN7android7RefBaseC2Ev(theTrack); // TODO
117116
holder->ats = ats;
118117
} else {
119118
ZN7android10AudioTrackC1Ev(theTrack);
120119
}
121-
ALOGE("0");
122-
auto callback = nullptr;//new MyCallback();
123-
//ZN7android7RefBaseC2Ev(callback); // TODO
124-
ALOGE("1");
125-
ZNK7android7RefBase9incStrongEPKv(theTrack, holder);
126-
ALOGE("2");
127-
//ZNK7android7RefBase9incStrongEPKv(callback, holder);
128-
ALOGE("3");
120+
auto callback = new MyCallback();
121+
callback->incStrong(holder);
122+
((android::AudioTrack*)theTrack)->incStrong(holder);
129123
holder->track = theTrack;
130124
holder->callback = callback;
131125
return (intptr_t)holder;
@@ -134,9 +128,9 @@ Java_org_akanework_gramophone_logic_utils_NativeTrack_create(
134128
extern "C" JNIEXPORT jint JNICALL
135129
Java_org_akanework_gramophone_logic_utils_NativeTrack_doSet(
136130
JNIEnv *, jobject, jlong ptr) {
137-
return 0;
138131
auto holder = (track_holder*) ptr;
139-
auto refs = ZNK7android7RefBase10createWeakEPKv(holder->callback, holder);
132+
auto refs = holder->callback->createWeak(holder);
133+
ALOGE("calling set on %p", holder->track);
140134
auto ret = ZN7android10AudioTrack3setE19audio_stream_type_tj14audio_format_t20audio_channel_mask_tm20audio_output_flags_tRKNS_2wpINS0_19IAudioTrackCallbackEEEiRKNS_2spINS_7IMemoryEEEb15audio_session_tNS0_13transfer_typeEPK20audio_offload_info_tRKNS_7content22AttributionSourceStateEPK18audio_attributes_tbfi(
141135
holder->track,
142136
/* streamType = */ 3 /* AUDIO_STREAM_MUSIC */,
@@ -145,7 +139,7 @@ Java_org_akanework_gramophone_logic_utils_NativeTrack_doSet(
145139
/* channelMask = */ 1,
146140
/* frameCount = */ 0 /* default */,
147141
/* flags = */ 0 /* AUDIO_OUTPUT_FLAG_NONE */,
148-
/* callback = */ { .thePtr = holder->callback, .refs = refs },
142+
/* callback = */ { .thePtr = nullptr }, // TODO add back ptr/refs
149143
/* notificationFrames = */ 0 /* default */,
150144
/* sharedBuffer = */ { .thePtr = nullptr },
151145
/* threadCanCallJava = */ true,
@@ -171,7 +165,7 @@ Java_org_akanework_gramophone_logic_utils_NativeTrack_dtor(
171165
JNIEnv *, jobject, jlong ptr) {
172166
auto holder = (track_holder*) ptr;
173167
// RefBase will call the dtor
174-
ZNK7android7RefBase9decStrongEPKv(holder->track, holder);
175-
ZNK7android7RefBase9decStrongEPKv(holder->callback, holder);
168+
((android::AudioTrack*)holder->track)->decStrong(holder);
169+
holder->callback->decStrong(holder);
176170
delete holder;
177171
}

app/src/main/cpp/aosp_stubs.h

+31-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ enum transfer_type {
3939
TRANSFER_SHARED, // shared memory
4040
TRANSFER_SYNC_NOTIF_CALLBACK, // synchronous write(), notif EVENT_CAN_WRITE_MORE_DATA
4141
};
42-
/*
42+
4343
namespace android {
4444
// NOLINTBEGIN
4545
class RefBase {
@@ -54,6 +54,21 @@ namespace android {
5454
ZN7android7RefBaseD2Ev(this);
5555
}
5656

57+
inline void incStrong(void* id) {
58+
ALOGI("fake base impl of incStrong says hello");
59+
ZNK7android7RefBase9incStrongEPKv(this, id);
60+
}
61+
62+
inline void decStrong(void* id) {
63+
ALOGI("fake base impl of decStrong says hello");
64+
ZNK7android7RefBase9decStrongEPKv(this, id);
65+
}
66+
67+
inline void* createWeak(void* id) {
68+
ALOGI("fake base impl of createWeak says hello");
69+
return ZNK7android7RefBase10createWeakEPKv(this, id);
70+
}
71+
5772
virtual void onFirstRef();
5873

5974
virtual void onLastStrongRef(const void *id);
@@ -65,6 +80,19 @@ namespace android {
6580
void *mRefs;
6681
};
6782
// NOLINTEND
83+
class AudioSystem {
84+
public:
85+
class AudioDeviceCallback : public virtual RefBase
86+
{
87+
public:
88+
89+
AudioDeviceCallback() {}
90+
virtual ~AudioDeviceCallback() {}
91+
92+
virtual void onAudioDeviceUpdate(int32_t audioIo,
93+
int32_t deviceId) = 0;
94+
};
95+
};
6896
class AudioTimestamp {
6997
public:
7098
AudioTimestamp() : mPosition(0), mTime({ .tv_sec = 0, .tv_nsec = 0 }) {
@@ -73,7 +101,7 @@ namespace android {
73101
uint32_t mPosition;
74102
struct timespec mTime;
75103
};
76-
class AudioTrack {
104+
class AudioTrack : public virtual AudioSystem::AudioDeviceCallback {
77105
public:
78106
AudioTrack() {
79107
ALOGE("if you see this, expect a segfault. this class AudioTrack never was supposed to be instantiated");
@@ -171,4 +199,4 @@ inline bool android::RefBase::onIncStrongAttempted(uint32_t flags, const void*)
171199
inline void android::RefBase::onLastWeakRef(const void*)
172200
{
173201
ALOGI("fake base impl of onLastWeakRef says hello");
174-
}*/
202+
}

0 commit comments

Comments
 (0)