Skip to content

Commit c0306de

Browse files
author
Mofizur Rahman
authored
Merge pull request #214 from opentok/hls-dvr
Added HLS parameter to Broadcast request
2 parents b62d892 + 26a4d14 commit c0306de

File tree

6 files changed

+175
-42
lines changed

6 files changed

+175
-42
lines changed

src/main/java/com/opentok/Broadcast.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public String getSessionId() {
8585
return sessionId;
8686
}
8787

88-
8988
/**
9089
* The OpenTok API key associated with the broadcast.
9190
*/
@@ -99,12 +98,14 @@ public int getProjectId() {
9998
public long getCreatedAt() {
10099
return createdAt;
101100
}
101+
102102
/**
103103
* The time at which the broadcast was updated, in milliseconds since the Unix epoch.
104104
*/
105105
public long getUpdatedAt() {
106106
return updatedAt;
107107
}
108+
108109
/**
109110
* The broadcast resolution.
110111
*/
@@ -118,6 +119,7 @@ public String getResolution() {
118119
public String getStatus() {
119120
return status;
120121
}
122+
121123
/**
122124
* Details on the HLS and RTMP broadcast streams. For an HLS stream, the URL is provided.
123125
* See the <a href="https://tokbox.com/developer/guides/broadcast/live-streaming/">OpenTok
@@ -140,18 +142,21 @@ private void unpack(Map<String,Object> broadcastUrls) {
140142
this.rtmpList.add(rtmp);
141143
}
142144
}
145+
143146
/**
144147
* The HLS URL (if there is one) of the broadcast.
145148
*/
146149
public String getHls() {
147150
return hls;
148151
}
152+
149153
/**
150154
* A list of RTMP URLs (if there are any) of the broadcast.
151155
*/
152156
public List<Rtmp> getRtmpList() {
153157
return rtmpList;
154158
}
159+
155160
@Override
156161
public String toString() {
157162
try {

src/main/java/com/opentok/BroadcastProperties.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public class BroadcastProperties {
2424
private int maxDuration;
2525
private boolean hasHls;
2626
private List<RtmpProperties> rtmpList;
27-
private String resolution = null;
27+
private String resolution;
2828
private StreamMode streamMode;
29-
29+
private Hls hls;
3030

3131
private BroadcastProperties(Builder builder) {
3232
this.layout = builder.layout;
3333
this.maxDuration = builder.maxDuration;
3434
this.hasHls = builder.hasHls;
35+
this.hls = builder.hls;
3536
this.rtmpList = builder.rtmpList;
3637
this.resolution = builder.resolution;
3738
this.streamMode = builder.streamMode;
@@ -46,6 +47,8 @@ public static class Builder {
4647
private BroadcastLayout layout = new BroadcastLayout(BroadcastLayout.Type.BESTFIT);
4748
private int maxDuration = 7200;
4849
private boolean hasHls = false;
50+
51+
private Hls hls;
4952
private List<RtmpProperties> rtmpList = new ArrayList<>();
5053
private String resolution = "640x480";
5154
private StreamMode streamMode = StreamMode.AUTO;
@@ -72,7 +75,7 @@ public Builder layout(BroadcastLayout layout){
7275
* @return The BroadcastProperties.Builder object with the maxDuration setting.
7376
*/
7477
public Builder maxDuration(int maxDuration) throws InvalidArgumentException {
75-
if(maxDuration < 60 || maxDuration > 36000) {
78+
if (maxDuration < 60 || maxDuration > 36000) {
7679
throw new InvalidArgumentException("maxDuration value must be between 60 and 36000 (inclusive).");
7780
}
7881
this.maxDuration = maxDuration;
@@ -90,6 +93,18 @@ public Builder hasHls(boolean hasHls) {
9093
this.hasHls = hasHls;
9194
return this;
9295
}
96+
97+
/**
98+
* Call this method to include HLS options. This will set <code>hasHls</code> to <code>true</code>.
99+
*
100+
* @param hls The HLS object.
101+
*
102+
* @return The BroadcastProperties.Builder object with HLS populated.
103+
*/
104+
public Builder hls(Hls hls) {
105+
return hasHls((this.hls = hls) != null);
106+
}
107+
93108
/**
94109
* Call this method to set a list of RTMP broadcast streams. There is a limit of
95110
* 5 RTMP streams.
@@ -109,7 +124,8 @@ public Builder addRtmpProperties (RtmpProperties rtmpProps) throws InvalidArgume
109124
/**
110125
* Sets the resolution of the broadcast stream.
111126
*
112-
* @param resolution The resolution of the broadcast, either "640x480" (SD, the default) or "1280x720" (HD).
127+
* @param resolution The resolution of the broadcast, either "640x480" (SD, the default),
128+
* "1280x720" (HD) or "1920x1080" (FHD).
113129
*
114130
* @return The BroadcastProperties.Builder object with the resolution setting.
115131
*/
@@ -158,6 +174,7 @@ public BroadcastProperties build() {
158174
public BroadcastLayout layout() {
159175
return layout;
160176
}
177+
161178
/**
162179
* The maximum duration in seconds of the broadcast.
163180
*/
@@ -172,10 +189,17 @@ public boolean hasHls() {
172189
return hasHls;
173190
}
174191

192+
/**
193+
* The HLS configuration object, or <code>null</code> if {@link BroadcastProperties#hasHls} is false.
194+
*/
195+
public Hls hls() {
196+
return hls;
197+
}
198+
175199
/**
176200
* Returns the RtmpProperties list.
177201
*/
178-
public List<RtmpProperties> getRtmpList() {
202+
public List<RtmpProperties> rtmpList() {
179203
return rtmpList;
180204
}
181205
/**

src/main/java/com/opentok/Hls.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2022 Vonage.
4+
* http://www.tokbox.com
5+
*
6+
* Licensed under The MIT License (MIT). See LICENSE file for more information.
7+
*/
8+
package com.opentok;
9+
10+
/**
11+
* POJO for the 'hls' object node in the 'outputs' section of a Broadcast request's body.
12+
*/
13+
public class Hls {
14+
15+
private final boolean dvr;
16+
private final boolean lowLatency;
17+
18+
public static class Builder {
19+
private boolean dvr = false;
20+
private boolean lowLatency = false;
21+
22+
public Builder dvr(boolean dvr) {
23+
this.dvr = dvr;
24+
return this;
25+
}
26+
27+
public Builder lowLatency(boolean lowLatency) {
28+
this.lowLatency = lowLatency;
29+
return this;
30+
}
31+
32+
public Hls build() {
33+
return new Hls(this);
34+
}
35+
}
36+
37+
protected Hls(Builder builder) {
38+
// Non-short-circuiting for setter
39+
if ((this.dvr = builder.dvr) & (this.lowLatency = builder.lowLatency)) {
40+
throw new IllegalArgumentException("Cannot set both dvr and lowLatency on HLS");
41+
}
42+
}
43+
44+
public boolean dvr() {
45+
return dvr;
46+
}
47+
48+
public boolean lowLatency() {
49+
return lowLatency;
50+
}
51+
}

src/main/java/com/opentok/OpenTok.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
123123
* @return The token string.
124124
*/
125125
public String generateToken(String sessionId, TokenOptions tokenOptions) throws OpenTokException {
126-
List<String> sessionIdParts = null;
127-
if (sessionId == null || sessionId == "") {
126+
List<String> sessionIdParts;
127+
if (sessionId == null || sessionId.isEmpty()) {
128128
throw new InvalidArgumentException("Session not valid");
129129
}
130130

@@ -582,14 +582,13 @@ public void setArchiveLayout(String archiveId, ArchiveProperties properties) thr
582582
* including the broadcast ID.
583583
*/
584584
public Broadcast startBroadcast(String sessionId, BroadcastProperties properties) throws OpenTokException {
585-
if (StringUtils.isEmpty(sessionId) || (properties == null)) {
585+
if (StringUtils.isEmpty(sessionId) || properties == null) {
586586
throw new InvalidArgumentException("Session not valid or broadcast properties is null");
587587
}
588588

589589
String broadcast = client.startBroadcast(sessionId, properties);
590590
try {
591-
return broadcastReader.readValue(
592-
broadcast);
591+
return broadcastReader.readValue(broadcast);
593592
} catch (Exception e) {
594593
throw new RequestException("Exception mapping json: " + e.getMessage());
595594
}

src/main/java/com/opentok/util/HttpClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public String startBroadcast(String sessionId, BroadcastProperties properties)
526526
ObjectNode requestJson = nodeFactory.objectNode();
527527
requestJson.put("sessionId", sessionId);
528528
requestJson.put("streamMode", properties.streamMode().toString());
529-
if(properties.layout() != null) {
529+
if (properties.layout() != null) {
530530
ObjectNode layout = requestJson.putObject("layout");
531531
screenshareType = properties.layout().getScreenshareType();
532532
String type = properties.layout().getType().toString();
@@ -549,10 +549,16 @@ public String startBroadcast(String sessionId, BroadcastProperties properties)
549549
}
550550
ObjectNode outputs = requestJson.putObject("outputs");
551551
if (properties.hasHls()) {
552-
outputs.set("hls", nodeFactory.objectNode());
552+
ObjectNode hlsNode = nodeFactory.objectNode();
553+
outputs.set("hls", hlsNode);
554+
Hls hlsPojo = properties.hls();
555+
if (hlsPojo != null) {
556+
hlsNode.put("dvr", hlsPojo.dvr());
557+
hlsNode.put("lowLatency", hlsPojo.lowLatency());
558+
}
553559
}
554560
ArrayNode rtmp = outputs.putArray("rtmp");
555-
for (RtmpProperties prop : properties.getRtmpList()) {
561+
for (RtmpProperties prop : properties.rtmpList()) {
556562
ObjectNode rtmpProps = nodeFactory.objectNode();
557563
rtmpProps.put("id", prop.id());
558564
rtmpProps.put("serverUrl", prop.serverUrl());

0 commit comments

Comments
 (0)