Is it possible to consume rtmp and send via websocket?

1,225 views
Skip to first unread message

falco...@googlemail.com

unread,
Jun 26, 2015, 7:40:26 AM6/26/15
to monas...@googlegroups.com
I found mona via google, because im searching for a way to consume a rtmp stream and send this via websockets to the clients.

The rtmp stream is pushed to a server, i can't connect to it.

Cheers

Thomas Jammet

unread,
Jun 27, 2015, 10:09:30 AM6/27/15
to monas...@googlegroups.com, falco...@googlemail.com, falco...@googlemail.com
Hi,

First of all, can you tell us what you want to do exactly with the rtmp stream?

It is possible to send raw media to websocket clients with some lua script code, here is a little sample :

local wsclients = {}

-- Write an integer in LSB order using width bytes.
function numbertobytes(num, width)
 
local function _n2b(width, num, rem)
    rem
= rem * 256
   
if width == 0 then return rem end
   
return rem, _n2b(width-1, math.modf(num/256))
 
end
 
return string.char(_n2b(width-1, math.modf(num/256)))
end

function onConnection(client)
   
 
if client.protocol == "WebSocket" then
    NOTE
("Added client websocket"); wsclients[client] = client.writer
 
end
   
 
function client:onPublish(publication)

   
function publication:onVideo(time,packet)
     
for c,w in pairs(wsclients) do
        w
:writeRaw("\x09"..time..packet)
     
end
   
end
   
   
function publication:onAudio(time,packet)
     
for c,w in pairs(wsclients) do
        w
:writeRaw("\x08"..time..packet)
     
end
   
end
 
end
end
function onDisconnection(client)
 
if wsclients[client] ~= nil then
    wsclients
[client] = nil
 
end
end


This server's lua script send all video/audio data (of any publication) to websocket clients. The packet send is composed of :
- 1 byte for media type ("\x09" = video, "\x08" = audio)
- 4 bytes for time
- and the rest for payload data

On the client side you just need to implement correctly the reception function, here is an example that log the first 16 bytes of data :

function onMessage(msg){
       
var reader = new FileReader();
        reader
.addEventListener("loadend", function() {
         
if (reader.result[0] == '\x09') {
            console
.log("video : "+reader.result.substr(1,17));
         
} else if (reader.result[0] == '\x08') {
            console
.log("audio : "+reader.result.substr(1,17));  
         
}
       
});
        reader
.readAsBinaryString(msg.data);
}

This is basics for what you want to do. If you want more informations or if you want a complete API for subscription with WebSocket please contact us directly.

Regards

KawaiiWoIf

unread,
Sep 1, 2015, 2:48:53 PM9/1/15
to MonaServer, falco...@googlemail.com
I can think of a good use. I've used monaserver for the last few months for small streaming over rtmpe, but sadly it requires a flash plugin. Later on when HLS or WebRTC are implemented we might have a way to stream in a stable way to a browser without need for separate plugins. Some day, but probably not yet; WebSocket isn't quite stable enough on it's own without putting in a lot more work to do buffering and smoothing.

Thomas Jammet

unread,
Sep 6, 2015, 5:48:27 AM9/6/15
to MonaServer, falco...@googlemail.com
Hi KawaiiWolf,

Yes flash plugin is always required. For now our alternatives are HTTP and RTSP (always in beta).

I think that HLS for live streaming can be implemented in lua by creating the m3u8 files directly. If anyone is interested to make a try on it it could be useful!
Though we have found that HLS is not really fitted for real-time streaming because is structure introduce delay by nature. :/

And WebRTC is always our objective but we need investors.

Nick Name

unread,
Sep 6, 2015, 6:14:01 AM9/6/15
to KawaiiWoIf, MonaServer
Hi,

meanwhile i've coded a websocket solution. In my case, flash is not an option. HLS or DASH are not possible because of their delay.

Thanks for all your hints / code and help in general! 
Reply all
Reply to author
Forward
0 new messages