Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Water
Level 7

Sending Video URL with http POST to my roku app

I'm trying to get my android app to send a Video URL by http POST to the launch params. I'm using connectSDK as the android app. Could anyone help out on how this works, and if using the launch params sample I have would work with this. I've looked at Roku's DIAL documentation but I still cannot figure it out.


public void postVideoURL() throws IOException {
String appRunLocation = "";
URL obj = new URL("http://10.50.0.105:8060/dial/dev?url=http%3A%2F%2Fvideo.ted.com%2Ftalks%2Fpodcast%2FDavidBrooks_2011.mp4");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add request header
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/plain; charset=\"utf-8\"");

String urlParameters = "videoUrl=" + URLEncoder.encode("video-url", "UTF-8");
urlParameters += "&streamFormat=hls";
String play_start = "0";
urlParameters += "&play_start=" + play_start;

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
try {
wr.writeBytes(urlParameters);
} catch (IOException e) {
e.printStackTrace();
}
try {
wr.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}

//BufferedReader in = new BufferedReader(
// new InputStreamReader(con.getInputStream()));
// String inputLine;
// StringBuffer response = new StringBuffer();

//appRunLocation = getHeader(con, RUN_LOCATION);
// try {
// in.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
}






This is the roku app i've running for the launch parameters

sub Main(launchParameters)
' don't do anything unless we got a url
if launchParameters.url <> invalid
' setup a poster screen and assign it a message port
port = CreateObject("roMessagePort")
screen = CreateObject("roVideoScreen")
screen.SetMessagePort(port)

' build a content-meta-data using the passed in URL
screen.SetContent({
stream: { url: launchParameters.url }
})


screen.SetPositionNotificationPeriod(1)
'screen.SetContent({
' stream: { url: launchParameters.url }
' })
msg = Wait(10000, port)
' play the video
'waitMess = print "Waiting for video..."
screen.Show()


end
end sub
0 Kudos
6 Replies
Veeta
Level 7

Re: Sending Video URL with http POST to my roku app

I haven't used DIAL, nor the connectSDK, but could it be that you forgot to replace the string literal in this line


String urlParameters = "videoUrl=" + URLEncoder.encode("video-url", "UTF-8");


... with the actual URL you wish to launch?
0 Kudos
Water
Level 7

Re: Sending Video URL with http POST to my roku app

Do I even need the parameters? All I want is the video to send and play, could I leave all that out like this?
I'm not sure what way the video url should look like, on roku website they have the url looking like the way I have it but thats through the terminal, Should I be taking out the %2F's?




    public void postVideoURL() throws IOException {
String appRunLocation = "";
URL obj = new URL("http://10.50.0.105:8060/dial/dev?url=http%3A%2F%2Fvideo.ted.com%2Ftalks%2Fpodcast%2FDavidBrooks_2011.mp4");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add request header
con.setRequestMethod("POST");

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());

try {
wr.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}

}
0 Kudos
Veeta
Level 7

Re: Sending Video URL with http POST to my roku app

Since you've added the Roku side of your code I can see that it won't work. You should look at the SimpleVideoPlayer sample in the SDK for some working brightscript video playback. You need to be calling Show() before you loop on Wait().

I recommend hard-coding your URL within the Roku channel code until you have it successfully playing. After that you can try adding the DIAL remote launching.
0 Kudos
belltown
Level 7

Re: Sending Video URL with http POST to my roku app

I'm not sure about the Java thing you're using, but here's a working example in C# (error-checking omitted for clarity):


using System.Net;
using System.Web;

class Program
{
static void Main()
{
string rokuIp = "192.168.0.6";
string channelId = "dev";
string videoUrl = "http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4";

// Note that the query string parameters should be url-encoded
string requestUrl = $"http://{rokuIp}:8060/launch/{channelId}?url={HttpUtility.UrlEncode(videoUrl)}";

using (var wc = new WebClient())
{
// POST the query string with no data
wc.UploadString(requestUrl, "");
}
}
}


and the BrightScript code:

Sub Main(params)

' Url to play if no launch parameters specified
urlParam = ""

' Check that launch parameters are specified
If Type(params) = "roAssociativeArray"
' Extract the url (if specified) from the launch parameters
If params.url <> Invalid
urlParam = params.url
End If
End If

' Play the video
port = CreateObject("roMessagePort")
ui = CreateObject ("roVideoScreen")
ui.SetMessagePort(port)
ui.SetContent ({Stream: {Url: urlParam, Bitrate: 0, Quality: False}})
ui.Show ()
While True
msg = Wait (0, port)
If Type (msg) = "roVideoScreenEvent"
If msg.IsScreenClosed ()
Exit While
End If
End If
End While

End Sub


Note that the query string parameters should be url-encoded in the client, but automatically appear in their decoded form when received by your Roku channel.

Here's the Wireshark trace:

POST /launch/dev?url=http%3a%2f%2fvideo.ted.com%2ftalk%2fpodcast%2f2016%2fNone%2fMoranCerf_2016.mp4 HTTP/1.1
Host: 192.168.0.6:8060
Content-Length: 0
Connection: Keep-Alive

HTTP/1.1 200 OK
Server: Roku UPnP/1.0 MiniUPnPd/1.4
Content-Length: 0
https://github.com/belltown/
0 Kudos
Water
Level 7

Re: Sending Video URL with http POST to my roku app

Thanks a lot for your BrightScript that i'm now using & the tip about the encoding . I still can't manage to get it to work, I feel like its something simple i'm missing out on but can't figure it out. Googling it looks like i've pretty much what I need for it to work but not sure whats wrong



public void postVideoURL() throws IOException {

String encodedUrl = URLEncoder.encode("http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4", "UTF-8");
URL obj = new URL("http://10.50.0.105:8060/launch/dev?url="+encodedUrl);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add request header
con.setRequestMethod("POST");

// Send post request
con.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
System.out.println(con.getResponseCode());
System.out.println(con.getResponseMessage());
wr.flush();
wr.close();

wr.write("");
}
0 Kudos
belltown
Level 7

Re: Sending Video URL with http POST to my roku app

Make sure you write an empty body before you flush and close the stream.
https://github.com/belltown/
0 Kudos