Page 1 of 1

Streaming with MPlayer using stdin?

Posted: Sun May 10, 2009 4:33 am
by Joakim Christiansen
EDIT: Ok, got it working, see my second post. :)

I've been trying to understand this, but it seems I can't get the hang of it... sometimes it just hangs (WriteProgramData freezes?!) and I have to close the console window to get rest of the strings from mplayer. :S I know I must somehow read the feedback and not send mplayer more data than it wants.

If you want to test just get the mplayer files, which for example is included in this player:
http://smplayer.sourceforge.net/

Code: Select all

movie$ = "C:\Downloads\Californication\Californication.S01E01.avi";OpenFileRequester("","","",0)

If movie$
  handle = RunProgram("C:\Program Files\SMPlayer\mplayer\mplayer.exe","-cache 1192 -","",#PB_Program_Open|#PB_Program_Write|#PB_Program_Read)
  ReadFile(0,movie$)
  *buffer = AllocateMemory(1192000)
  While Not Eof(0) And ProgramRunning(handle)
    If AvailableProgramOutput(handle)
      result$ = Trim(ReadProgramString(handle))
      Debug result$
      If LCase(Left(result$,Len("Cache fill"))) = "cache fill" Or Left(result$,2) = "A:"
        dataRead = ReadData(0,*buffer,1192000)
        WriteProgramData(handle,*buffer,dataRead)
        Debug "read"
      EndIf
    EndIf
    ;Debug "read"

    Delay(100)
  Wend
  Debug "closed"
EndIf
lol, this is so weird...

Posted: Sun May 10, 2009 3:09 pm
by Joakim Christiansen
This seems to work.
I just had to remove the read flag. And now it wont make WriteProgramData() return before it needs more data, so I can't flood it now.

Code: Select all

movie$ = OpenFileRequester("Open a avi file","","avi|*.avi",0)
If movie$ And ReadFile(0,movie$)
  handle = RunProgram("C:\Program Files\SMPlayer\mplayer\mplayer.exe"," -","",#PB_Program_Open|#PB_Program_Write)
  If IsProgram(handle)
    *buffer = AllocateMemory(1192000)
    While Eof(0)=#False And ProgramRunning(handle)
      dataRead = ReadData(0,*buffer,1192000)
      If ProgramRunning(handle) And WriteProgramData(handle,*buffer,dataRead) ;this halts until a new piece is needed btw
        Debug "Whole piece read"
      EndIf
    Wend
    Debug "Closed"
  Else
    Debug "Unable to start MPlayer"
  EndIf
  CloseFile(0)
EndIf

Posted: Sun May 10, 2009 6:56 pm
by utopiomania
What are you trying to do here?

Posted: Sun May 10, 2009 7:41 pm
by Joakim Christiansen
utopiomania wrote:What are you trying to do here?
Sending a buffer containing a part of the video file to MPlayer and repeating the process until is has streamed the whole video.

And in the last snippet I managed to do just that, I was just very confused first about MPlayer's behavior when using the #PB_Program_Read flag. But I guess I don't care too much about that now since I got it working.

Posted: Sun May 10, 2009 8:09 pm
by utopiomania
Ok, I don't want to talk about this anymore! Your programming skills far superceedes mine
so please go away. :shock: :)

Posted: Sun May 10, 2009 8:17 pm
by Joakim Christiansen
utopiomania wrote:Ok, I don't want to talk about this anymore! Your programming skills far superceedes mine
so please go away. :shock: :)
:lol:

Re: Streaming with MPlayer using stdin?

Posted: Mon May 11, 2009 12:54 am
by AND51
Joakim Christiansen wrote:sometimes it just hangs (WriteProgramData freezes?!)
I found this bug on Linux, maybe the bug also affects the windows version. Could you have a look, JLC ?
http://www.purebasic.fr/english/viewtopic.php?t=37364

// edit:
My personal test on windows is negative, this bug is only on Linux.

// edit 2:
Well, did you consider writing an EOF into the stdin? Can't see it. Please read the manual at WriteProgramData() and write a #PB_Program_EOF into the input.