Skip to content

Commit

Permalink
Add simple player for low latency streaming from raspivid
Browse files Browse the repository at this point in the history
  • Loading branch information
popcornmix committed Dec 6, 2014
1 parent fed4730 commit bd8a299
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 0 deletions.
@@ -0,0 +1,8 @@
set(EXEC hello_video_simple.bin)
set(SRCS video.c)

add_executable(${EXEC} ${SRCS})
target_link_libraries(${EXEC} ${HELLO_PI_LIBS})

install(TARGETS ${EXEC}
RUNTIME DESTINATION bin)
@@ -0,0 +1,6 @@
OBJS=video.o
BIN=hello_video_simple.bin
LDFLAGS+=-lilclient

include ../Makefile.include

15 changes: 15 additions & 0 deletions host_applications/linux/apps/hello_pi/hello_video_simple/README
@@ -0,0 +1,15 @@
To stream with low latency from camera.

To set up camera server than runs from boot, on "camera" pi, run
sudo cp raspi-vid /etc/init.d; cd /etc/init.d; sudo chmod +x raspi-vid; sudo update-rc.d raspi-vid defaults

Alternatively to just launch on demand, run
./raspi-vid start

The camera server is based on suggestion from: https://drjohnstechtalk.com/blog/2014/02/screaming-streaming-on-the-raspberry-pi/

On the "display" pi run
nc 192.168.0.101 5001 | ./hello_video_simple

Streams with about 200ms latency and low cpu.

38 changes: 38 additions & 0 deletions host_applications/linux/apps/hello_pi/hello_video_simple/raspi-vid
@@ -0,0 +1,38 @@
#! /bin/sh
# /etc/init.d/raspi-vid


start() {
# -n means don't show preview on console; -rot 180 to make image right-side-up
# run a loop because this command dies unless it can connect to a listener
while /bin/true; do
#
# act as super-simple server listening on port 5001 using nc
# -n means don't show preview on console;
# -b (bitrate) of 1000000 (~ 1 mbit) seems adequate for our 1280x720 video image
# so is -fps 15 (15 frames per second)
# To view output fire up mplayer on a PC. I personally use this command on my PC:
# c:\apps\netcat\nc 192.168.2.100 443|c:\apps\smplayer\mplayer\mplayer -ontop -fps 60 -vo gl -cache 1024 -geometry 600:50 -noborder -msglevel all=0 -
raspivid -n -o - -t 0 -rot 0 -w 1280 -h 720 -b 1000000 -fps 15 | nc -l 5001
# or use hello_video_simple
sleep 1;
done
}

# The following part carries out specific functions depending on arguments.
case "$1" in
start)
echo "Starting rasip-vid"
start &
;;
stop)
echo "Stopping rasip-vid"
pkill 'raspi-?vid'
;;
*)
echo "Usage: /etc/init.d/rasip-vid {start|stop}"
exit 1
;;
esac

exit 0
188 changes: 188 additions & 0 deletions host_applications/linux/apps/hello_pi/hello_video_simple/video.c
@@ -0,0 +1,188 @@
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// Video deocode demo using OpenMAX IL though the ilcient helper library
// This variant avoids the clock and scheduler and just renders frames immediately
// It reads from stdin and can take a custom destination rectange
// suitable for use as player for network stream from raspivid

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "bcm_host.h"
#include "ilclient.h"

#define min(x, y) ((x)<(y)?(x):(y))

static int video_decode_test(OMX_DISPLAYRECTTYPE dest_rect)
{
OMX_VIDEO_PARAM_PORTFORMATTYPE format;
COMPONENT_T *video_decode = NULL, *video_render = NULL;
COMPONENT_T *list[3];
TUNNEL_T tunnel[2];
ILCLIENT_T *client;
int status = 0;

memset(list, 0, sizeof(list));
memset(tunnel, 0, sizeof(tunnel));

if((client = ilclient_init()) == NULL)
{
return -3;
}

if(OMX_Init() != OMX_ErrorNone)
{
ilclient_destroy(client);
return -4;
}

// create video_decode
if(ilclient_create_component(client, &video_decode, "video_decode", ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS) != 0)
status = -14;
list[0] = video_decode;

// create video_render
if(status == 0 && ilclient_create_component(client, &video_render, "video_render", ILCLIENT_DISABLE_ALL_PORTS) != 0)
status = -14;
list[1] = video_render;

OMX_CONFIG_DISPLAYREGIONTYPE configDisplay;
memset(&configDisplay, 0, sizeof configDisplay);
configDisplay.nSize = sizeof configDisplay;
configDisplay.nVersion.nVersion = OMX_VERSION;
configDisplay.nPortIndex = 90;

configDisplay.set = (OMX_DISPLAYSETTYPE)(OMX_DISPLAY_SET_TRANSFORM | OMX_DISPLAY_SET_LAYER | OMX_DISPLAY_SET_NUM);
configDisplay.num = 0;
configDisplay.layer = (1<<15)-1;
configDisplay.transform = 0;

if (dest_rect.x_offset || dest_rect.y_offset || dest_rect.width || dest_rect.height)
{
configDisplay.set = (OMX_DISPLAYSETTYPE)(configDisplay.set|OMX_DISPLAY_SET_DEST_RECT|OMX_DISPLAY_SET_FULLSCREEN|OMX_DISPLAY_SET_NOASPECT);
configDisplay.dest_rect = dest_rect;
}

if (OMX_SetConfig(ILC_GET_HANDLE(video_render), OMX_IndexConfigDisplayRegion, &configDisplay) != OMX_ErrorNone)
status = -15;

set_tunnel(tunnel, video_decode, 131, video_render, 90);

if(status == 0)
ilclient_change_component_state(video_decode, OMX_StateIdle);

memset(&format, 0, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
format.nVersion.nVersion = OMX_VERSION;
format.nPortIndex = 130;
format.eCompressionFormat = OMX_VIDEO_CodingAVC;

if(status == 0 &&
OMX_SetParameter(ILC_GET_HANDLE(video_decode), OMX_IndexParamVideoPortFormat, &format) == OMX_ErrorNone &&
ilclient_enable_port_buffers(video_decode, 130, NULL, NULL, NULL) == 0)
{
OMX_BUFFERHEADERTYPE *buf;
int port_settings_changed = 0;

ilclient_change_component_state(video_decode, OMX_StateExecuting);
while((buf = ilclient_get_input_buffer(video_decode, 130, 1)) != NULL)
{
// feed data and wait until we get port settings changed
// limiting buffer size helps latency
int data_len = read(0, buf->pBuffer, min(buf->nAllocLen, 8*1024));

if(port_settings_changed == 0 &&
((data_len > 0 && ilclient_remove_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1) == 0) ||
(data_len == 0 && ilclient_wait_for_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1,
ILCLIENT_EVENT_ERROR | ILCLIENT_PARAMETER_CHANGED, 10000) == 0)))
{
port_settings_changed = 1;

if(ilclient_setup_tunnel(tunnel, 0, 0) != 0)
{
status = -7;
break;
}
ilclient_change_component_state(video_render, OMX_StateExecuting);
}
if(!data_len)
break;

buf->nFilledLen = data_len;
buf->nOffset = 0;
buf->nFlags = 0;

if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
{
status = -6;
break;
}
}

buf->nFilledLen = 0;
buf->nFlags = OMX_BUFFERFLAG_EOS;

if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
status = -20;

// wait for EOS from render
ilclient_wait_for_event(video_render, OMX_EventBufferFlag, 90, 0, OMX_BUFFERFLAG_EOS, 0,
ILCLIENT_BUFFER_FLAG_EOS, 10000);

// need to flush the renderer to allow video_decode to disable its input port
ilclient_flush_tunnels(tunnel, 0);

ilclient_disable_port_buffers(video_decode, 130, NULL, NULL, NULL);
}

ilclient_disable_tunnel(tunnel);
ilclient_teardown_tunnels(tunnel);

ilclient_state_transition(list, OMX_StateIdle);
ilclient_state_transition(list, OMX_StateLoaded);

ilclient_cleanup_components(list);

OMX_Deinit();

ilclient_destroy(client);
return status;
}

int main (int argc, char **argv)
{
OMX_DISPLAYRECTTYPE dest_rect = {0};
if (argc > 4)
dest_rect.x_offset = atoi(argv[1]), dest_rect.y_offset = atoi(argv[1]), dest_rect.width = atoi(argv[3]), dest_rect.height = atoi(argv[4]);
bcm_host_init();
return video_decode_test(dest_rect);
}


0 comments on commit bd8a299

Please sign in to comment.