3030import java .io .IOException ;
3131import java .io .ObjectOutputStream ;
3232import java .nio .ByteBuffer ;
33+ import java .util .concurrent .atomic .AtomicInteger ;
3334import java .util .concurrent .locks .ReentrantLock ;
3435
3536import ie .macinnes .htsp .HtspMessage ;
4243public class HtspDataSource implements DataSource , Subscriber .Listener , Closeable {
4344 private static final String TAG = HtspDataSource .class .getName ();
4445 private static final int BUFFER_SIZE = 10 *1024 *1024 ;
46+ private static final AtomicInteger sDataSourceCount = new AtomicInteger ();
4547
4648 public static class Factory implements DataSource .Factory {
4749 private static final String TAG = Factory .class .getName ();
@@ -66,6 +68,7 @@ public HtspDataSource createDataSource() {
6668 private SimpleHtspConnection mConnection ;
6769 private String mStreamProfile ;
6870
71+ private final int mDataSourceNumber ;
6972 private Subscriber mSubscriber ;
7073
7174 private DataSpec mDataSpec ;
@@ -76,12 +79,14 @@ public HtspDataSource createDataSource() {
7679 private boolean mIsOpen = false ;
7780
7881 public HtspDataSource (Context context , SimpleHtspConnection connection , String streamProfile ) {
79- Log .d (TAG , "New HtspDataSource instantiated" );
80-
8182 mContext = context ;
8283 mConnection = connection ;
8384 mStreamProfile = streamProfile ;
8485
86+ mDataSourceNumber = sDataSourceCount .incrementAndGet ();
87+
88+ Log .d (TAG , "New HtspDataSource instantiated (" +mDataSourceNumber +")" );
89+
8590 try {
8691 mBuffer = ByteBuffer .allocate (BUFFER_SIZE );
8792 mBuffer .limit (0 );
@@ -90,7 +95,7 @@ public HtspDataSource(Context context, SimpleHtspConnection connection, String s
9095 // enough memory to catch and throw this exception. We do this, as each OOM exception
9196 // message is unique (lots of #'s of bytes available/used/etc) and means crash reporting
9297 // doesn't group things nicely.
93- throw new RuntimeException ("OutOfMemoryError when allocating HtspDataSource buffer" , e );
98+ throw new RuntimeException ("OutOfMemoryError when allocating HtspDataSource buffer (" + mDataSourceNumber + ") " , e );
9499 }
95100
96101 mSubscriber = new Subscriber (mConnection );
@@ -101,13 +106,13 @@ public HtspDataSource(Context context, SimpleHtspConnection connection, String s
101106 // DataSource Methods
102107 @ Override
103108 public long open (DataSpec dataSpec ) throws IOException {
104- Log .i (TAG , "Opening HTSP DataSource" );
109+ Log .i (TAG , "Opening HTSP DataSource (" + mDataSourceNumber + ") " );
105110 mDataSpec = dataSpec ;
106111
107112 try {
108113 mSubscriber .subscribe (Long .parseLong (dataSpec .uri .getHost ()), mStreamProfile );
109114 } catch (HtspNotConnectedException e ) {
110- throw new IOException ("Failed to open DataSource, HTSP not connected" , e );
115+ throw new IOException ("Failed to open DataSource, HTSP not connected (" + mDataSourceNumber + ") " , e );
111116 }
112117
113118 mIsOpen = true ;
@@ -125,11 +130,11 @@ public int read(byte[] buffer, int offset, int readLength) throws IOException {
125130 while (mIsOpen && mBuffer .remaining () == 0 ) {
126131 try {
127132 if (Constants .DEBUG )
128- Log .v (TAG , "Blocking for more data" );
133+ Log .v (TAG , "Blocking for more data (" + mDataSourceNumber + ") " );
129134 Thread .sleep (250 );
130135 } catch (InterruptedException e ) {
131136 // Ignore.
132- Log .w (TAG , "Caught InterruptedException, ignoring" );
137+ Log .w (TAG , "Caught InterruptedException, ignoring (" + mDataSourceNumber + ") " );
133138 }
134139 }
135140
@@ -165,22 +170,18 @@ public Uri getUri() {
165170
166171 @ Override
167172 public void close () throws IOException {
168- Log .i (TAG , "Closing HTSP DataSource" );
173+ Log .i (TAG , "Closing HTSP DataSource (" + mDataSourceNumber + ") " );
169174 mIsOpen = false ;
170175
171176 mConnection .removeAuthenticationListener (mSubscriber );
172177 mSubscriber .removeSubscriptionListener (this );
173178 mSubscriber .unsubscribe ();
174- mSubscriber = null ;
175-
176- // Watch for memory leaks
177- Application .getRefWatcher (mContext ).watch (this );
178179 }
179180
180181 // Subscription.Listener Methods
181182 @ Override
182183 public void onSubscriptionStart (@ NonNull HtspMessage message ) {
183- Log .d (TAG , "Received subscriptionStart" );
184+ Log .d (TAG , "Received subscriptionStart (" + mDataSourceNumber + ") " );
184185 serializeMessageToBuffer (message );
185186 }
186187
@@ -201,7 +202,7 @@ public void onSubscriptionSpeed(@NonNull HtspMessage message) {
201202
202203 @ Override
203204 public void onSubscriptionStop (@ NonNull HtspMessage message ) {
204- Log .d (TAG , "Received subscriptionStop" );
205+ Log .d (TAG , "Received subscriptionStop (" + mDataSourceNumber + ") " );
205206 mIsOpen = false ;
206207 }
207208
@@ -244,7 +245,7 @@ private void serializeMessageToBuffer(@NonNull HtspMessage message) {
244245 mBuffer .flip ();
245246 } catch (IOException e ) {
246247 // Ignore?
247- Log .w (TAG , "Caught IOException, ignoring" , e );
248+ Log .w (TAG , "Caught IOException, ignoring (" + mDataSourceNumber + ") " , e );
248249 } finally {
249250 mLock .unlock ();
250251 try {
0 commit comments