Combining implementation of autobahn websockets, gstreamers and html5 mediaSource API

狂风中的少年 提交于 2020-01-02 16:24:26

问题


I am running a websocket server using autobahn|python. on the server side, I also have a gstreamer pipeline running which I am using to capture webm frames using "appsink". The gstreamer pipeline that is implemented is:

gst-launch-1.0 v4l2src ! video/x-raw,width=640,height=480 ! videoconvert ! vp8enc ! webmmux ! appsink name="sink"

Everytime, I receive a buffer in the appsink, I send it over a websocket as a binary "message" using sendMessage.

def on_new_buffer(appsink):
    global once
    gstsample = appsink.emit('pull-sample')
    gstbuffer = gstsample.get_buffer()
    frame_data = gstbuffer.extract_dup(0,gstbuffer.get_size())
    for c in global_clients:
        c.sendMessage(frame_data,True)
        print("Directly sent: {0} bytes".format(len(frame_data)))

    return False

on the client side, I have a complicated flow of received frame_data blob. There is a FileReader, MediaSource and source buffer. whenever a frame_data is received, it is read as buffer using filereader. if the filereader is busy reading the previous frame_data, it will append it to "buffer_pool". once the frame_data is read as buffer, it is appended to "sourceBuffer". if the "sourceBuffer" is still updating the previous chunk, it will be appended to "sourceBufferpool".

    <script>
    var video = document.getElementById('v');
    var playButton = document.getElementById('playbutton');
    var mediaSource;
    var sourceBuffer;
    var buffer_pool = [];
    var sourceBufferpool = [];

    function setupVideo() {
        window.MediaSource = window.MediaSource || window.WebKitMediaSource;
        if (!!!window.MediaSource) {
            alert('MediaSource API is not available');
        }
        mediaSource = new MediaSource();
        video.src = window.URL.createObjectURL(mediaSource);
        mediaSource.addEventListener('sourceopen', function (e) {
            try {
                sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vp8"');
            } catch(e) {
                console.log('Exception calling addSourceBuffer for video', e);
                return;
            }

            //sourceBuffer.addEventListener('updatestart', function(e) { console.log('updatestart: ' + e.target + mediaSource.readyState); });
            //sourceBuffer.addEventListener('updateend', function(e) { console.log('updateend: ' + e.target + mediaSource.readyState); });
            sourceBuffer.addEventListener('error', function(e) { console.log('error: ' + e.target + mediaSource.readyState); });
            sourceBuffer.addEventListener('abort', function(e) { console.log('abort: ' + e.target + mediaSource.readyState); });

            sourceBuffer.addEventListener('update', function() {
                if (sourceBufferpool.length > 0 && !sourceBuffer.updating) {
                    try {
                        sourceBuffer.appendBuffer(sourceBufferpool.shift());
                        console.log('update: pooled buffer appended ' + sourceBufferpool.length + mediaSource.readyState);
                    }catch(e){
                        console.log('Exception calling appendBuffer for video ', e);
                        return;
                    }
                }
            },false)

            if (video.paused) {
                video.play()
            }

            startWSStreaming();
        },false)

        mediaSource.addEventListener('sourceended', function(e) { console.log('sourceended: ' + mediaSource.readyState); });
        mediaSource.addEventListener('sourceclose', function(e) { console.log('sourceclose: ' + mediaSource.readyState); });
        mediaSource.addEventListener('error', function(e) { console.log('error: ' + mediaSource.readyState); });

    }

    function startWSStreaming() {
        var reader = new FileReader();

        reader.onload = function (evt) {
            if (sourceBuffer.updating || sourceBufferpool.length > 0){
                sourceBufferpool.push(new Uint8Array(evt.target.result));
                console.log('update: pooled buffer appended ' + sourceBufferpool.length + mediaSource.readyState);
            }else{
                sourceBuffer.appendBuffer(new Uint8Array(evt.target.result));
                console.log('update: direct buffer appended ' + sourceBufferpool.length + mediaSource.readyState);
            }
        }

        reader.onloadend = function (evt) {
            if (buffer_pool.length > 0) {
                var chunk = new Blob([buffer_pool.shift()], {type: 'video/webm'});
                evt.target.readAsArrayBuffer(chunk);
                console.log('Processed buffer pool: current size ' + buffer_pool.length);
            }
        }

        ws = new WebSocket("ws://localhost:9000/");
        ws.onopen = function () {
            document.getElementById("MSG1").innerHTML = 'Websocket opened <br>';
        }
        ws.onmessage = function(e) {
            myBuffer = e.data;
            if (reader.readyState == 1 || buffer_pool.length > 0) {
                buffer_pool.push(myBuffer);
                console.log('Received buffer pooled: current size ' + buffer_pool.length);
            }else{
                var chunk = new Blob([myBuffer], {type: 'video/webm'});
                reader.readAsArrayBuffer(chunk);
                console.log('First buffer processed');
            }
        }

    }       
</script>

now, the end result is, I see only one frame on a browser window and then the video freezes. After checking chrome://media-internals/, I get the following clue:

Timestamp   Property    Value
00:00:00 00 pipeline_state  kCreated
00:00:00 00 EVENT   PIPELINE_CREATED
00:00:00 00 EVENT   WEBMEDIAPLAYER_CREATED
00:00:00 00 url blob:http%3A//localhost%3A8080/09060a78-9759-4fcd-97a2-997121ba6122
00:00:00 00 pipeline_state  kInitDemuxer
00:00:01 668    duration    unknown
00:00:01 669    pipeline_state  kInitVideoRenderer
00:00:01 685    pipeline_state  kPlaying
00:00:03 820    EVENT   PLAY
00:00:04 191    error   Got a block with a timecode before the previous block.
00:00:04 191    pipeline_error  pipeline: decode error
00:00:04 191    pipeline_state  kStopping
00:00:04 192    pipeline_state  kStopped
00:00:28 483    EVENT   WEBMEDIAPLAYER_DESTROYED

Phew, quite a long description!!! I hope you have made it till this line. now, the real questions:

  1. why is the video freezing after diplaying just one frame?
  2. is it because of websocket "sendMessage" method, as I am treating webm chunks as distinct messages whereas, this should need treatment as "sendMessageFrameData"?
  3. will I need some sequencing on the arrived frame_data so that they are received in the order they were sent?
  4. or my entire approach is incorrect?

please help!

来源:https://stackoverflow.com/questions/28997477/combining-implementation-of-autobahn-websockets-gstreamers-and-html5-mediasourc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!