Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e7950555
authored
Jul 21, 2015
by
Oliver Woodman
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Generalize MulticastDataSource to UdpDataSource
parent
4451b065
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
19 deletions
library/src/main/java/com/google/android/exoplayer/upstream/MulticastDataSource.java → library/src/main/java/com/google/android/exoplayer/upstream/UdpDataSource.java
library/src/main/java/com/google/android/exoplayer/upstream/
Multicast
DataSource.java
→
library/src/main/java/com/google/android/exoplayer/upstream/
Udp
DataSource.java
View file @
e7950555
...
...
@@ -19,24 +19,26 @@ import com.google.android.exoplayer.C;
import
java.io.IOException
;
import
java.net.DatagramPacket
;
import
java.net.DatagramSocket
;
import
java.net.InetAddress
;
import
java.net.InetSocketAddress
;
import
java.net.MulticastSocket
;
/**
* A
multicast
{@link DataSource}.
* A
UDP
{@link DataSource}.
*/
public
class
Multicast
DataSource
implements
UriDataSource
{
public
class
Udp
DataSource
implements
UriDataSource
{
/**
* Thrown when an error is encountered when trying to read from a {@link
Multicast
DataSource}.
* Thrown when an error is encountered when trying to read from a {@link
Udp
DataSource}.
*/
public
static
final
class
Multicast
DataSourceException
extends
IOException
{
public
static
final
class
Udp
DataSourceException
extends
IOException
{
public
Multicast
DataSourceException
(
String
message
)
{
public
Udp
DataSourceException
(
String
message
)
{
super
(
message
);
}
public
Multicast
DataSourceException
(
IOException
cause
)
{
public
Udp
DataSourceException
(
IOException
cause
)
{
super
(
cause
);
}
...
...
@@ -50,18 +52,21 @@ public class MulticastDataSource implements UriDataSource {
private
final
DatagramPacket
packet
;
private
DataSpec
dataSpec
;
private
MulticastSocket
socket
;
private
DatagramSocket
socket
;
private
MulticastSocket
multicastSocket
;
private
InetAddress
address
;
private
InetSocketAddress
socketAddress
;
private
boolean
opened
;
private
int
packetsReceived
;
private
byte
[]
packetBuffer
;
private
int
packetRemaining
;
public
Multicast
DataSource
(
TransferListener
transferListener
)
{
public
Udp
DataSource
(
TransferListener
transferListener
)
{
this
(
transferListener
,
DEFAULT_MAX_PACKET_SIZE
);
}
public
Multicast
DataSource
(
TransferListener
transferListener
,
int
maxPacketSize
)
{
public
Udp
DataSource
(
TransferListener
transferListener
,
int
maxPacketSize
)
{
this
.
transferListener
=
transferListener
;
packetBuffer
=
new
byte
[
maxPacketSize
];
...
...
@@ -69,17 +74,25 @@ public class MulticastDataSource implements UriDataSource {
}
@Override
public
long
open
(
DataSpec
dataSpec
)
throws
Multicast
DataSourceException
{
public
long
open
(
DataSpec
dataSpec
)
throws
Udp
DataSourceException
{
this
.
dataSpec
=
dataSpec
;
String
uri
=
dataSpec
.
uri
.
toString
();
String
host
=
uri
.
substring
(
0
,
uri
.
indexOf
(
':'
));
int
port
=
Integer
.
parseInt
(
uri
.
substring
(
uri
.
indexOf
(
':'
)
+
1
));
try
{
socket
=
new
MulticastSocket
(
port
);
socket
.
joinGroup
(
InetAddress
.
getByName
(
host
));
address
=
InetAddress
.
getByName
(
host
);
socketAddress
=
new
InetSocketAddress
(
address
,
port
);
if
(
address
.
isMulticastAddress
())
{
multicastSocket
=
new
MulticastSocket
(
socketAddress
);
multicastSocket
.
joinGroup
(
address
);
socket
=
multicastSocket
;
}
else
{
socket
=
new
DatagramSocket
(
socketAddress
);
}
}
catch
(
IOException
e
)
{
throw
new
Multicast
DataSourceException
(
e
);
throw
new
Udp
DataSourceException
(
e
);
}
opened
=
true
;
...
...
@@ -89,18 +102,30 @@ public class MulticastDataSource implements UriDataSource {
@Override
public
void
close
()
{
if
(
opened
)
{
if
(
multicastSocket
!=
null
)
{
try
{
multicastSocket
.
leaveGroup
(
address
);
}
catch
(
IOException
e
)
{
// Do nothing
}
multicastSocket
=
null
;
}
if
(
socket
!=
null
)
{
socket
.
close
();
socket
=
null
;
transferListener
.
onTransferEnd
();
packetRemaining
=
0
;
packetsReceived
=
0
;
}
address
=
null
;
socketAddress
=
null
;
packetRemaining
=
0
;
packetsReceived
=
0
;
if
(
opened
)
{
opened
=
false
;
transferListener
.
onTransferEnd
();
}
}
@Override
public
int
read
(
byte
[]
buffer
,
int
offset
,
int
readLength
)
throws
Multicast
DataSourceException
{
public
int
read
(
byte
[]
buffer
,
int
offset
,
int
readLength
)
throws
Udp
DataSourceException
{
// if we've read all the data, get another packet
if
(
packetRemaining
==
0
)
{
if
(
packetsReceived
==
TRANSFER_LISTENER_PACKET_INTERVAL
)
{
...
...
@@ -112,7 +137,7 @@ public class MulticastDataSource implements UriDataSource {
try
{
socket
.
receive
(
packet
);
}
catch
(
IOException
e
)
{
throw
new
Multicast
DataSourceException
(
e
);
throw
new
Udp
DataSourceException
(
e
);
}
packetRemaining
=
packet
.
getLength
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment