@@ -333,25 +333,40 @@ class NativeTrack(context: Context, attributes: AudioAttributes, streamType: Int
333333 proxy = try {
334334 getProxy(ptr, this .sessionId)
335335 } catch (t: Throwable ) {
336- Log .e(TAG , Log .getStackTraceString(t)); null
336+ try {
337+ dtor(ptr)
338+ } catch (t2: Throwable ) {
339+ throw NativeTrackException (" dtor() threw exception after getProxy() threw exception: " +
340+ Log .getStackTraceString(t2), t)
341+ }
342+ throw NativeTrackException (" getProxy() threw exception" , t)
343+ }
344+ if (proxy == null ) {
345+ try {
346+ dtor(ptr)
347+ } catch (t: Throwable ) {
348+ throw NativeTrackException (" dtor() threw exception after getProxy() returned null, " +
349+ " check prior logs" , t)
350+ }
351+ throw NativeTrackException (" getProxy() returned null, check prior logs" )
337352 }
338353 routingListener = object : AudioRouting .OnRoutingChangedListener {
339354 override fun onRoutingChanged (router : AudioRouting ? ) {
340355 this @NativeTrack.onRoutingChanged()
341356 }
342357 }
343- proxy? .addOnRoutingChangedListener(routingListener, null )
358+ proxy.addOnRoutingChangedListener(routingListener, null )
344359 } else {
345360 proxy = null
346361 routingListener = null
347362 }
348- if (proxy != null && Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
363+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
349364 codecListener = object : AudioTrack .OnCodecFormatChangedListener {
350365 override fun onCodecFormatChanged (audioTrack : AudioTrack , info : AudioMetadataReadMap ? ) {
351366 this @NativeTrack.onCodecFormatChanged(info)
352367 }
353368 }
354- proxy.addOnCodecFormatChangedListener(MoreExecutors .directExecutor(), codecListener)
369+ proxy!! .addOnCodecFormatChangedListener(MoreExecutors .directExecutor(), codecListener)
355370 } else codecListener = null
356371 myState = State .ALIVE
357372 Log .e(" hi" , " dump:${AfFormatTracker .dumpInternal(getRealPtr(ptr))} " )
@@ -387,20 +402,20 @@ class NativeTrack(context: Context, attributes: AudioAttributes, streamType: Int
387402 doNotReconnect : Boolean , transferMode : Int , contentId : Int , syncId : Int ,
388403 encapsulationMode : Int ): Int
389404 private external fun getRealPtr (@Suppress(" unused" ) ptr : Long ): Long
390- private external fun flagsFromOffset (@Suppress(" unused" ) ptr : Long ): Int
405+ private external fun flagsFromOffset (@Suppress(" unused" ) ptr : Long , @Suppress( " unused " ) proxy : AudioTrack ? ): Int
391406 private external fun notificationFramesActFromOffset (@Suppress(" unused" ) ptr : Long ): Int
392407 private external fun dtor (@Suppress(" unused" ) ptr : Long )
393408 @RequiresApi(Build .VERSION_CODES .N )
394- private external fun getProxy (@Suppress(" unused" ) ptr : Long , @Suppress(" unused" ) sessionId : Int ): AudioTrack
409+ private external fun getProxy (@Suppress(" unused" ) ptr : Long , @Suppress(" unused" ) sessionId : Int ): AudioTrack ?
395410
396411 fun release () {
397412 myState = State .RELEASED
398413 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R && codecListener != null ) {
399- proxy? .removeOnCodecFormatChangedListener(codecListener)
414+ proxy!! .removeOnCodecFormatChangedListener(codecListener)
400415 }
401416 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
402- proxy? .removeOnRoutingChangedListener(routingListener)
403- proxy? .release() // this doesn't free native obj because we hold extra strong ref, cleared in dtor()
417+ proxy!! .removeOnRoutingChangedListener(routingListener)
418+ proxy.release() // this doesn't free native obj because we hold extra strong ref, cleared in dtor()
404419 }
405420 dtor(ptr)
406421 }
@@ -447,10 +462,7 @@ class NativeTrack(context: Context, attributes: AudioAttributes, streamType: Int
447462 // TODO document L/M not stripping all flags, only direct / offload / fast
448463 if (myState == State .RELEASED )
449464 throw IllegalStateException (" state is $myState " )
450- return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P )
451- AfFormatTracker .getFlagsFromDump(dump())
452- ? : throw IllegalStateException (" getFlags failed, check prior logs" )
453- else flagsFromOffset(ptr) // TODO use proxy get_flags on N/O
465+ return flagsFromOffset(ptr, proxy)
454466 }
455467
456468 fun notificationFramesAct (): Int {
0 commit comments