Http live streaming to iphone - iphone

I'm trying to play an http live stream to iphone, it looks like i have looked every example, mistakes and everything that i could found on the internet and apple docs about http live stream, and i think i'm in a dead end now.. I'm using MPMoviePlayer as in most of examples. Also i have to add the i can see the stream if i open the url from vlc player.
I succeeded in playing the apple BipBop stream on my iPhone that is here but can't play my stream. I figured that my url shows not in the m3u8 file so i found this terminal command, and successfully used it.
/Applications/VLC.app/Contents/MacOS/VLC --intf=rc rtp://#239.35.86.11:10000
'--sout=#transcode{fps=25,vcodec=h264,venc=x264{aud,profile=baseline,level=30, keyint=30,bframes=0,ref=1,nocabac},acodec=mp3,ab=56,audio-sync,deinterlace}:standard{mux=ts,dst=-,access=file}' | mediastreamsegmenter -b http://192.168.1.16/~Jonas/streaming/ -f
/users/jonas/sites/streaming/ -D
Now i have a playlist m3u8 file locally on my machine. As i understand with the command i download stream divide it into smaller ts files and generate m3u8 file that is like a reference to those ts files. So i've tried to load this, but still no luck. For some reasons i can't even open the m3u8 file in vlc or itunes, it throws me errors. So i guess it is something wrong with the playlist file?
Maybe some of you can see what am i doing wrong here or have some suggestions how to find my problem? I would really appreciate it.

It looks like your iOS code is just fine and that it is your server-side code that is causing issues, primarily with regards to generating the m3u8 playlist and possibly with how you're hosting the ts files that it references.
Unfortunately my example code is a bit noisy as I wrote it a year ago (it is in python) and it does a bit more than you're asking for (it live-transcodes a video into the correct m3u8/ts stuff) but it is tested and functional.
You can take a look at the code here: https://github.com/DFTi/ScribbeoServer/blob/python/transcode.py
I will paste some of the relevant methods here for your convenience; I hope it helps you:
def start_transcoding(self, videoPath):
if DISABLE_LIVE_TRANSCODE:
print "Live transcoding is currently disabled! There is a problem with your configuration."
return
print "Initiating transcode for asset at path: "+videoPath
videoPath = unquote(videoPath)
video_md5 = md5.new(videoPath).hexdigest()
if self.sessions.has_key(video_md5): # Session already exists?
return self.m3u8_bitrates_for(video_md5)
transcodingSession = TranscodeSession(self, videoPath)
if transcodingSession.can_be_decoded():
self.sessions[transcodingSession.md5] = transcodingSession
return self.m3u8_bitrates_for(transcodingSession.md5)
else:
return "Cannot decode this file."
def m3u8_segments_for(self, md5_hash, video_bitrate):
segment = string.Template("#EXTINF:$length,\n$md5hash-$bitrate-$segment.ts\n")
partCount = math.floor(self.sessions[md5_hash].duration / 10)
m3u8_segment_file = "#EXTM3U\n#EXT-X-TARGETDURATION:10\n"
for i in range(0, int(partCount)):
m3u8_segment_file += segment.substitute(length=10, md5hash=md5_hash, bitrate=video_bitrate, segment=i)
last_segment_length = math.ceil((self.sessions[md5_hash].duration - (partCount * 10)))
m3u8_segment_file += segment.substitute(length=last_segment_length, md5hash=md5_hash, bitrate=video_bitrate, segment=i)
m3u8_segment_file += "#EXT-X-ENDLIST"
return m3u8_segment_file
def m3u8_bitrates_for(self, md5_hash):
m3u8_fudge = string.Template(
"#EXTM3U\n"
# "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=384000\n"
# "$hash-384-segments.m3u8\n"
# "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=512000\n"
# "$hash-512-segments.m3u8\n"
"#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=768000\n"
"$hash-768-segments.m3u8\n"
# "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1024000\n"
# "$hash-1024-segments.m3u8\n"
)
return m3u8_fudge.substitute(hash=md5_hash)
def segment_path(self, md5_hash, the_bitrate, segment_number):
# A segment was requested.
path = self.sessions[md5_hash].transcode(segment_number, the_bitrate)
if path:
return path
else:
raise "Segment path not found"
That project is all open source now and can be found here: https://github.com/DFTi/ScribbeoServer/tree/python
Binaries can be found here: http://scribbeo.com/server
Good luck!

Related

Xuggler write and read video via H.264 to/from Sockets

I want to be able to send BufferedImages generated from my java program over the local network in real time, some my second application can show them.
I have been looking through a lot of websites over the last 2 days but I wasn't able to find anything. Only thing I found was this:
Can I use Xuggler to encode video/audio to a byte array?
I tried implementing the URLHandler but problem is, MediaWriter still wants an URL and as soon as I add a VideoStream, it opens the container a second time with the url and then in crashes.
I hope you can help me and thanks in advance.
Code I have right now:
val clientSocket = serverSocket.accept()
connectedClients.add(clientSocket)
val container = IContainer.make()
val writer = ToolFactory.makeWriter("localhost", container)
container.open(VTURLProtocolHandler(clientSocket.getOutputStream()), IContainer.Type.WRITE, IContainerFormat.make())
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, width, height)

BlackBerry 10 Native SDK - Writing Text Files

I am battling to write some data to a simple text file
Here is my code:
QFile file(app->applicationDirPath() + "/data/testfile.txt");
if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream << "DATA HERE \n";
}
The app compiles and runs fine.
Just I cant find the file, or more likely: it is not being created
Where am I going wrong? :)
Thanks
Extra Info:
Run: on my device (BlackBerry Z10)
IDE: QNX IDE (Native SDK) / (Cascades)
Example code is located in: TestApp::TestApp(bb::cascades::Application *app)
: QObject(app)
Okay, I kinda stumbled upon the answer myself:
QFile file(QDir::currentPath() + "/shared/documents/yourfile.txt");
if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream << "DATA HERE \n";
}
Turns out each application has access to its own working directory. So the file was being created, I just could not see it on the device:
making the path: "/shared/documents/" made the file in a place where I could see it in the file manager
(hope this helps anyone who has a similar problem in the future)
This is a useful link, which explains the directories & current path.

Matlab: Writing avi file

I am having a problem when creating an avi file using Matlab. My aim is to use an edge filter on an entire video and save the file as an avi. The filter works fine, my problem is the writing of the avi file.
My code:
vidFile = VideoReader('video.avi');
edgeMov = avifile('edges','fps',30);
for i = 1:vidFile.numberofframes
frameI = read(vidFile,i);
frameIgray = rgb2gray(frameI);
edgeI = edge(frameIgray,'canny',0.6);
edgeIuint8 = im2uint8(edgeI);
edgeIuint8(:,:,2) = edgeIuint8(:,:,1);
edgeIuint8(:,:,3) = edgeIuint8(:,:,1);
edgeMov = addframe(edgeMov,edgeIuint8);
end
edgeMov = close(edgeMov)
When the loop finishes and the avifile is closed, I go to play the video and it says "Windows Media Player encountered a problem while playing this file". I've also tried, without success, Media Player Classic and VLC which lead me to believe that the problem must be the file itself. Using GSpot I checked the file and it said that the AVI header is corrupt.
Retrying the loop again returned exactly the same problem. What's confusing me is when I run the loop for a smaller number of frames, 30 for example, the video writes fine and I can watch it. The size of the video I am trying to convert is in excess of 1000 frames so I don't know if size is a problem?
Any help would be greatly appreciated, thank you.
I've used the following to create AVI
edgeMov = avifile('video.avi','compression','Indeo5','fps',15,'quality',95);
Give it a try.

How can I get file size in Perl before processing an upload request?

I want to get file size I'm doing this:
my $filename=$query->param("upload_file");
my $filesize = (-s $filename);
print "Size: $filesize ";`
Yet it is not working. Note that I did not upload the file. I want to check its size before uploading it. So to limit it to max of 1 MB.
You can't know the size of something before uploading. But you can check the Content-Length request header sent by the browser, if there is one. Then, you can decide whether or not you want to believe it. Note that the Content-Length will be the length of the entire request stream, including other form fields, and not just the file upload itself. But it's sufficient to get you a ballpark figure for conformant clients.
Since you seem to be running under plain CGI, you should be able to get the request body length in $ENV{CONTENT_LENGTH}.
Also want to sanity check against possibly already having post max set (from perldoc CGI):
$CGI::POST_MAX
If set to a non-negative integer, this variable puts a ceiling on the size of
POSTings, in bytes. If CGI.pm detects a POST that is greater than the ceiling,
it will immediately exit with an error message. This value will affect both
ordinary POSTs and multipart POSTs, meaning that it limits the maximum size of
file uploads as well. You should set this to a reasonably high value, such as
1 megabyte.
The uploaded file is stashed in a tmp location on the server when the form is submitted, check the file size there.
Supply the value for $field.
my $upload_filehandle = $query->upload($field);
my $tmpfilename = $query->tmpFileName($upload_filehandle);
my $file_size = (-s $tmpfilename);
This has nothing to do with Perl.
You are trying to read the filesize of a file on the user's computer using commands that read files on your server, what you want can't be done using Perl.
This is something that has to be done in the browser, and looking briefly at these questions it's either very hard or impossible.
Your best bet is to allow the user to start the upload and abort if the file is too big.
If you want to check before you process the request, you might be better off checking on the web page that triggers the request. I don't think the web browser can do it on it's own, but if you don't mind Flash, there are many Flash upload tools that can check things like size (as well as file types) and prevent uploading.
A good one to start with is the YUI Uploader. Lots more here: What is the best multiple file JavaScript / Flash file uploader?
Obviously you would want to check on the server side too, but by the time the user has started sending the request to the server, you are already using up your CPU cycles and bandwidth.
Thanks everyone for your replies; I just found out why $filesize = (-s $filename); was not working before, it is due that I was checking file size while sending Ajax request and not while re submitting the page.That's why I was having size to be zero. I fixed that to submit the page and it worked. Thanks.
Just read this post but while checking the content-length is a good approximate pre-check you could also save the file to temporary folder and then perform any kind of check on it. If it doesn't meet your criteria just delete and don't send it to it's final destination.
Look at the perl documentation for file stats -X - perldoc.perl.org and stat-perldoc.perl.org. Also, you can look at this upload script which is doing the similar thing what you are trying to do.

Playing a Sound With Monotouch

No matter what I try (build -> content, NSUrl, filename) I get a 'null exception': file not found when I try to play a .caf sound file in monotouch.
//var path = NSBundle.MainBundle.PathForResource("MatchGame", "caf");
//var gameSong = SystemSound.FromFile( new NSUrl(path, false));
var gameSong = SystemSound.FromFile("MatchGame.caf");
gameSong.PlaySystemSound();
I also try combinations using the folder name "images/MatchGame.caf" and moving MatchGame.caf into the root folder.
What am I missing? Thanks a lot.
Here is a link to a video of adding the sound in monotouch. http://www.screencast.com/t/MmE0ZmFh What is wrong?
Bryan,
From looking at your screencast - you are trying to play a mp3 file and not a caf file. Mp3 files are encoded differently and will not play with the SystemSound class that's there (I can't remember if you can do this in Obj-C or not.)
You'll want to use the AVFoundation Namespace and AVAudioPlayer class.
using Monotouch.AVFoundation;
var mediaFile = NSUrl.FromFilename("myMp3.mp3");
var audioPlayer = AVAudioPlayer.FromUrl(mediaFile);
audioPlayer.FinishedPlaying += delegate { audioPlayer.Dispose(); };
audioPlayer.Play();
You might need to tweak the code above - I don't have MonoDevelop to hand but that should help you a little further.
Cheers,
ChrisNTR
You need the first line:
var path = NSBundle.MainBundle.PathForResource("MatchGame", "caf");
Then make sure that your audio file is included in the application by making sure that your CAF file is flagged as "Content" in the Properties pane, otherwise the file is not copied to the resulting application package (your .app)
You can follow the steps documented here (they are for images, but apply the same to audio files):
http://wiki.monotouch.net/HowTo/Images/Add_an_Image_to_your_Project
Additionally, this is a good resource for where you should store files:
http://wiki.monotouch.net/HowTo/Files/HowTo%3a_Store_Files