Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
sarantli
Posts: 122
Registered: ‎09-15-2010
My Device: BB - 9800

problem with playing rtsp media link

Hi all......

 

         In the code below whenever we click "OK" button, the 3gp file has to 

 

downloaded and stored in the sd card and it'll b played from the sd card.

 

Its working fine if I'm giving a http link but the problem arises when its 

 

rtsp link.  I dont know exactly whether this the correct way to fetch a rtsp 

 

link in-order  to play the media content. Actually I'm trying to play a streaming 

 

video using rtsp protocol. If this code runs as expected I can look forward. 

 

So any help in this regard would be highly appreciated.  

 

 

 

 

package downloading;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.GUIControl;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.control.VolumeControl;

import net.rim.device.api.io.transport.ConnectionDescriptor;
import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;


public class downloading extends UiApplication 
{
	public static void main(String[] args)
	{
		downloading theapp=new downloading();
		theapp.enterEventDispatcher();
	}
	public downloading()
	{
		pushScreen(new myClass());
	}
}
final class myClass extends MainScreen implements FieldChangeListener
{
	ButtonField ok = new ButtonField("OK");
	//String url = "http://www.pocketjourney.com/downloads/pj/video/famous.3gp";
	String url = "rtsp://streaming.rim.com/streaming_video.3gp";
	
	LabelField label = new LabelField("downloading...........");
	
	DataInputStream dis=null;
	DataInputStream dis1=null;
	DataOutputStream dos=null;
	HttpConnection hc;
	Player player;
	VideoControl vc;
	VolumeControl vol;
	Display display;
	
	public myClass()
	{
		
		setTitle("displaying Media");
		add(ok);
		ok.setChangeListener(this);
	}
	public void savingMedia() 
	{
		delete(ok);
		try
		{
		ConnectionFactory connfac = new ConnectionFactory();
		ConnectionDescriptor conndesc;
		FileConnection FileConn = null;
		String path = "file:///SDCard/BlackBerry/videos/streaming_video.3gp";
		
		add(label);
		conndesc = connfac.getConnection(url);
			if(conndesc!=null)
			{
				hc = (HttpConnection)conndesc.getConnection();
				final int responsecode = hc.getResponseCode();
				Dialog.alert("response code"+Integer.toString(responsecode));
				dis = hc.openDataInputStream();
				//to download the media file into sd card
				int lenght = (int)hc.getLength();
				byte[] b = new byte[lenght];
				dis.readFully(b);
				FileConn = (FileConnection)Connector.open(path, Connector.READ_WRITE);
				if(!FileConn.exists())
					FileConn.create();
				dos = FileConn.openDataOutputStream();
				dos.write(b, 0, b.length);
				//to play the downloaded media file
				Dialog.alert("download complete, click to watch the video.......");
				dis1 = FileConn.openDataInputStream();
				player = Manager.createPlayer(dis1,"video/3gp");
			   player.realize();
			   vc = (VideoControl)player.getControl("VideoControl");
			   vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,this);
			   GUIControl gc;
			   if ((gc = (GUIControl)player.getControl("GUIControl"))!=null)
			   add((Field) gc.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE,null)); 
			   vc.setDisplayLocation(0, 0);
			   vc.setDisplaySize(display.getWidth(),display.getHeight());
			   vc.setVisible(true);
			   vol = (VolumeControl) 
			   player.getControl("VolumeControl"); 
			   vol.setLevel(50);
			   player.start();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		} 
		
	}
	public void fieldChanged(Field field, int context) 
	{
		Dialog.alert("getting the data.......");
		savingMedia();
	}
	
}
 

thanks in advance 

 

 

regards, 

$aran.............

Please use plain text.
Regular Contributor
Abhijit_RIM
Posts: 98
Registered: ‎08-31-2010
My Device: BB-9700

Re: problem with playing rtsp media link

RTSP is a streming protocol and  it should not work the way you are trying to do it. It seems from your code that you are trying to download a file from the RTSP URL.

 

RTSP is a streaming protocol and not provide a downloadable contains for the video file. RTSP uses some commands which need to be send to server like DESCRIBE, OPTIONS, PLAY etc.

 

You need to establish all these commands and then when you fire the PLAY method, server would start sending you RTP packets on different port number of the socket. From that point you need to read and parse RTP packets. Those RTP packets actually contains the media data which you can save into the file.

 

Thanks

 

Regards

-Abhijit

 

Please mark kudos if you get the solution

Please use plain text.
Developer
sarantli
Posts: 122
Registered: ‎09-15-2010
My Device: BB - 9800

Re: problem with playing rtsp media link

Hi abhijet, thanks for ur posting..........for the past one week I'm working on 

 

rtsp protocols and I fed enough with the links I gone through and I thought 

 

lets give a try myself and working on my own. but I'm not able to get ride 

 

of the problem. I already gone through some links like the one displayed below. 

http://supportforums.blackberry.com/t5/Java-Development/Streaming-media-Start-to-finish/ta-p/488255

 

but no use. I'm getting lot of errors and I'm not able to trace the code. So can u provide 

 

any sample code through which we can achieve streaming video.

 

 

thanks in advance.........

 

 

regards,

$aran........... 

Please use plain text.
Regular Contributor
Abhijit_RIM
Posts: 98
Registered: ‎08-31-2010
My Device: BB-9700

Re: problem with playing rtsp media link

Well, i am not able to share the code with you as this would contains the actual RTSP protocol commands, RTP stack, RTCP protocol stack etc.

 

If you could tell me what exactly the problem you are facing, i can definatly help you to get out of it.

 

Thanks

 

Regards

-Abhijit

Please use plain text.
New Contributor
saurabh4mobile
Posts: 9
Registered: ‎08-23-2012
My Device: Blackberry Curve

Re: problem with playing rtsp media link

[ Edited ]

Hi,

 

in the code below i am able to get responce from server but unable to parse RTP packets.

 

and how to feed that packets to the mediaplayer...

 

anyone help me ...

 

 

 

byte[] optionRequestBytesArray = ("OPTIONS "
+ "rtsp://URL..." + " RTSP/1.0" + "\r\nCSeq: 1").getBytes();

byte[] encodedData = Base64OutputStream.encode(
optionRequestBytesArray, 0,
optionRequestBytesArray.length, false, false);
System.out.println("Writing OPTIONS Data: "
+ new String(optionRequestBytesArray) + "\n"
+ " Encoded As : " + new String(encodedData));
dataOutputStream.write(encodedData);
dataOutputStream.flush();

result = read(dataInputStream);

System.out.println("Result for RTSP " + result);

// ----------------------------------------------------------------------------------------------------
// send DESCRIBE commands
byte[] describeRequestBytesArray = ("DESCRIBE "
+ "rtsp://URL..." + " RTSP/1.0\r\nCSeq: 2").getBytes();

byte[] describeEncodedData = Base64OutputStream.encode(
describeRequestBytesArray, 0,
describeRequestBytesArray.length, false, false);
System.out.println("Writing DESCRIBE Data: "
+ new String(describeRequestBytesArray) + "\n"
+ " Encoded As : "
+ new String(describeEncodedData));
dataOutputStream.write(describeEncodedData);
dataOutputStream.flush();

result = read(dataInputStream);

System.out.println("Result for RTSP DESCRIBE command"
+ result);

// send SETUP commands
byte[] setupRequestBytesArray = ("SETUP "
 + "rtsp://URL..."+ " RTSP/1.0\r\nCSeq: 3").getBytes();

byte[] setupEncodedData = Base64OutputStream.encode(
setupRequestBytesArray, 0,
setupRequestBytesArray.length, false, false);
System.out.println("Writing SETUP Data: "
+ new String(setupRequestBytesArray) + "\n"
+ " Encoded As : " + new String(setupEncodedData));
dataOutputStream.write(setupEncodedData);
dataOutputStream.flush();

result = read(dataInputStream);

System.out
.println("Result for RTSP SETUP command" + result);

// send PLAY commands
byte[] playRequestBytesArray = ("PLAY "
+ "rtsp://URL..." + " RTSP/1.0\r\nCSeq: 4").getBytes();

byte[] playEncodedData = Base64OutputStream.encode(
playRequestBytesArray, 0,
playRequestBytesArray.length, false, false);
System.out.println("Writing PLAY Data: "
+ new String(playRequestBytesArray) + "\n"
+ " Encoded As : " + new String(playEncodedData));
dataOutputStream.write(playEncodedData);
dataOutputStream.flush();
result = read(dataInputStream);
// readInputStream(dataInputStream);
synchronized (Application.getEventLock()) {
mediaPlayer(dataInputStream);
}

 

 

read code.....

 

 

private String read(InputStream is) throws Throwable {
String result = "";
byte[] buffer = new byte[1500];
long cursor = 0;
int offset = 0;
try {
do {
offset = is.read(buffer);
result += new String(buffer, 0, offset);
// System.out
// .println(">>>>>>>>>>>>>>>>>>Result from the server inside the while loop"
// + result);
cursor += offset;
} while (!result.startsWith("HTTP/1.0 200 OK"));
// is.close();
} catch (Exception e) {
System.out
.println(">>>>>>>>>>>>>>>Excecption inside the read method:"
+ e.toString());
}
System.out.println(">>>>>>>>>>>>>>>>>>Result from the server is"
+ result);
return new String(result);
}

 

Please use plain text.
New Contributor
saurabh4mobile
Posts: 9
Registered: ‎08-23-2012
My Device: Blackberry Curve

Re: problem with playing rtsp media link

hi Abhijit,

can help me,
to share any link or code to create RTP parser and how to feed that packet to the player.
Please use plain text.
Trusted Contributor
ankitagrawal
Posts: 141
Registered: ‎02-15-2012
My Device: 9800

Re: problem with playing rtsp media link

Hi sarantli,

 

You have no need to do any thing for rtsp link.

This rtsp link is used to play the video directly with media player.

 

Just try this code to use it:

 

BrowserSession browserSession = Browser.getDefaultSession();
browserSession.displayPage("rtsp link");

 

Please press like, if you likes this solution.

Please use plain text.