GstInterpipe - Example 2: Digital Camera

From RidgeRun Developer Wiki



Previous: Example 1: CCTV Index Next: Contact Us




This page contains an example to demonstrate the usage of the interpipesink and interpipesrc elements.

This example demonstrates the dynamic change of independent pipelines properties and state without affecting the other pipelines that conform to the system. The objective of this sample is to show the simplicity that GstInterpipe offers in a real case of use where you have one source pipeline and different sink pipelines, and you want to dynamically control the sinks that are connected with the source pipeline. For example a Digital Camera system. The following sample simulates a digital camera that allows displaying a live preview, record video, and taking snapshots.

In this sample, there is 1 source pipeline labeled as: cam_src_pipe. This source pipeline is a videotestsrc that simulates a real camera sensor connected to the system. On the other side, there are 3 sink pipelines, which are labeled as live_preview_pipe, recording_pipe, snapshot_pipe. The live_preview_pipe consists of a videosink that displays on screen the live video generated from the cam_src_pipe. The recording_pipe consists of an H264 encoder, a muxer, and a filesink that records the video generated from the cam_src_pipe. The snapshot_pipe consists of a JPEG encoder and a filesink that saves a snapshot of the video generated from the cam_src_pipe.

The below diagram illustrates the concept:


CCTV Example (Dynamic Switching) diagram
CCTV Example (Dynamic Switching) diagram


As you can view from the sample code, activating or deactivating any sink pipeline, is as simple as modifying the respective pipeline state without worrying about the other pipelines state.

Sample bash script code:

#!/bin/bash
 
echo -e "\n ====== Digital Camera Example (Switch the scr_pipe to listen in runtime) ====== \n"
 
END_MSJ="!!! Digital Camera Example Finished !!!"
REC_INDEX=0
 
# Graceful cleanup upon CTRL-C
trap "gstd-client pipeline_delete cam_src_pipe; gstd-client pipeline_delete live_preview_pipe; "\
"gstd-client pipeline_delete recording_pipe; gstd-client pipeline_delete snapshot_pipe; echo -e $END_MSJ; exit" SIGINT
 
# Create pipelines
echo -e "\n ====> Create the cam_src_pipe \n"
gstd-client pipeline_create cam_src_pipe videotestsrc pattern=ball is-live=true \
! "video/x-raw, framerate=15/1, width=640, height=480" ! queue max-size-buffers=3 leaky=downstream \
! interpipesink name=cam_src caps=video/x-raw,width=640,height=480,framerate=15/1 sync=true async=false
 
echo -e "\n ====> Create the live_preview_pipe \n"
gstd-client pipeline_create live_preview_pipe interpipesrc name=live_prev_intpsrc listen-to=cam_src \
is-live=true allow-renegotiation=true stream-sync=compensate-ts ! queue max-size-buffers=3 leaky=downstream \
! fpsdisplaysink async=false sync=false
 
echo -e "\n ====> Create the recording_pipe \n"
gstd-client pipeline_create recording_pipe interpipesrc name=rec_intpsrc listen-to=cam_src is-live=false \
allow-renegotiation=true stream-sync=passthrough-ts format=time ! queue max-size-buffers=3 leaky=downstream ! x264enc \
! h264parse ! mpegtsmux ! filesink name=rec_file_sink location=/tmp/recording_$REC_INDEX.ts async=false sync=false

echo -e "\n ====> Create the snapshot_pipe \n"
gstd-client pipeline_create snapshot_pipe interpipesrc name=snap_intpsrc listen-to=cam_src num-buffers=1 \
is-live=true allow-renegotiation=true stream-sync=passthrough-ts ! queue max-size-buffers=3 leaky=downstream \
! jpegenc ! multifilesink location=/tmp/snapshot_%d.jpg async=false sync=false


# Change pipelines to PLAYING STATE
echo -e "\n ====> Start cam_src_pipe and live_preview_pipe \n"
gstd-client pipeline_play cam_src_pipe
gstd-client pipeline_play live_preview_pipe
 
# Enable recording procedure
function enable_rec {
	gstd-client element_set recording_pipe rec_file_sink location /tmp/recording_$REC_INDEX.ts
	gstd-client bus_filter recording_pipe eos
	gstd-client pipeline_play recording_pipe
}
 
# Disable recording procedure
function disable_rec {
        gstd-client bus_timeout recording_pipe 1000000000
	gstd-client event_eos recording_pipe
	# wait eos
	gstd-client bus_read recording_pipe
	gstd-client pipeline_stop recording_pipe
	REC_INDEX=$(($REC_INDEX+1))
}
 
# Take snapshot procedure
function take_snap {
	gstd-client bus_filter snapshot_pipe eos
	gstd-client bus_timeout snapshot_pipe 1000000000
	gstd-client pipeline_play snapshot_pipe
	# wait eos
	gstd-client bus_read snapshot_pipe
	gstd-client pipeline_stop snapshot_pipe
}
 
 
echo -e "\n====> Press S to take a snapshot or R to start recording video for 15 seconds \n"
echo -e "====> Type Ctrl+C to stop the example execution, otherwise, it will iterate infinitely!\n"
 
 
# Start requesting actions to the user
while :
do
	read -rsn1 input
 
	if [ "$input" = "s" ];
	then
		echo -e "\n====> Take snapshot! \n"
		take_snap
		echo -e "\n====> Snapshot Taken! \n"
		echo -e "\n====> Press S to take a snapshot or R to start recording video for 15 seconds \n"
		echo -e "====> Type Ctrl+C to stop the example execution, otherwise, it will iterate infinitely!\n"
	fi
 
	if [ "$input" = "r" ];
	then
		enable_rec
		echo -e "\n====> Start to record video! \n"
		sleep 15
		disable_rec
		echo -e "\n====> Finish to record video! \n"
		echo -e "\n====> Press S to take a snapshot or R to start recording video for 15 seconds \n"
		echo -e "====> Type Ctrl+C to stop the example execution, otherwise, it will iterate infinitely!\n"
	fi
 
done


To run the sample follow these steps:

  1. Build and install GStreamer Daemon (gstd): Building GStreamer Daemon.
  2. Copy the script code in a file
  3. Change the file permissions to be executable:
    1. chmod 777 <file.sh>
  4. Run the GStreamer Daemon:
    1. gstd &
  5. Run the script:
    1. ./<file.sh>
  6. Kill the GStreamer Daemon:
    1. killall gstd

Tested versions:

  1. GStreamer version: v1.8.3
  2. GStreamer Daemon: v0.11.3
  3. GstInterpipe: v1.1.3


Previous: Example 1: CCTV Index Next: Contact Us