Skip to content

Commit

Permalink
Merge pull request #36 from zouxiaoh/icamerasrc_slim_api
Browse files Browse the repository at this point in the history
RPL-P MR1 release
  • Loading branch information
zouxiaoh authored Sep 1, 2023
2 parents feeea6e + 9f2c02b commit 931412a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
64 changes: 44 additions & 20 deletions src/gstcambasesrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,10 @@ gst_cam_base_src_query_latency (GstCamBaseSrc * src, gboolean * live,
{
GstClockTime min;

g_return_val_if_fail (GST_IS_CAM_BASE_SRC (src), FALSE);

GST_OBJECT_LOCK (src);
if (live)
*live = src->is_live;
if (live) {
*live = gst_cam_base_src_is_live(src);
GST_LOG_OBJECT(src, "live: %s", *live == TRUE ? "TRUE" : "FALSE");
}

/* if we have a startup latency, report this one, else report 0. Subclasses
* are supposed to override the query function if they want something
Expand All @@ -720,10 +719,8 @@ gst_cam_base_src_query_latency (GstCamBaseSrc * src, gboolean * live,
if (max_latency)
*max_latency = min;

GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
GST_TIME_ARGS (min));
GST_OBJECT_UNLOCK (src);
GST_LOG_OBJECT (src, "latency: min %" GST_TIME_FORMAT ", max %"
GST_TIME_FORMAT, GST_TIME_ARGS (min), GST_TIME_ARGS (min));

return TRUE;
}
Expand Down Expand Up @@ -845,8 +842,12 @@ static gboolean
gst_cam_base_src_send_video_stream_start(GstCamBaseSrc *src, GstPad *pad)
{
gboolean ret = TRUE;
guint stream_id = CameraSrcUtils::get_stream_id_by_pad(src->priv->request_stream_map, pad);
gint stream_id = CameraSrcUtils::get_stream_id_by_pad(src->priv->request_stream_map, pad);

if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM - 1) {
GST_ERROR_OBJECT(src, "invalid stream_id: %d", stream_id);
return FALSE;
}
/* start video stream */
if (src->priv->muxPriv[stream_id].vid_stream_start_pending) {
gchar *vid_stream_id;
Expand Down Expand Up @@ -1445,7 +1446,11 @@ gst_cam_base_src_default_alloc (GstCamBaseSrc * src, GstPad *pad,
allocator = priv->allocator;
params = priv->params;
} else {
guint stream_id = CameraSrcUtils::get_stream_id_by_pad(priv->request_stream_map, pad);
gint stream_id = CameraSrcUtils::get_stream_id_by_pad(priv->request_stream_map, pad);
if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM - 1) {
GST_ERROR_OBJECT(src, "invalid stream_id: %d", stream_id);
return GST_FLOW_ERROR;
}
pool = priv->muxPriv[stream_id].vid_pool;
allocator = priv->muxPriv[stream_id].vid_allocator;
params = priv->muxPriv[stream_id].vid_params;
Expand Down Expand Up @@ -1817,7 +1822,7 @@ gst_cam_base_src_send_event (GstElement * element, GstEvent * event)
start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
GST_OBJECT_UNLOCK (src->srcpad);

if (src->is_live) {
if (gst_cam_base_src_is_live(src)) {
if (!src->live_running)
start = FALSE;
}
Expand Down Expand Up @@ -2498,7 +2503,7 @@ gst_cam_base_src_get_range (GstCamBaseSrc * src, GstPad *pad, guint64 offset,
bclass = GST_CAM_BASE_SRC_GET_CLASS (src);

again:
if (src->is_live) {
if (gst_cam_base_src_is_live(src)) {
if (G_UNLIKELY (!src->live_running)) {
ret = gst_cam_base_src_wait_playing (src);
if (ret != GST_FLOW_OK)
Expand Down Expand Up @@ -2579,7 +2584,7 @@ gst_cam_base_src_get_range (GstCamBaseSrc * src, GstPad *pad, guint64 offset,

/* no timestamp set and we are at offset 0, we can timestamp with 0 */
if (offset == 0 && src->segment.time == 0
&& (gint64)GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
&& (gint64)GST_BUFFER_DTS (res_buf) == -1 && !gst_cam_base_src_is_live(src)) {
GST_DEBUG_OBJECT (src, "%s pad: setting first timestamp to 0", padname);
res_buf = gst_buffer_make_writable (res_buf);
GST_BUFFER_DTS (res_buf) = 0;
Expand Down Expand Up @@ -2702,8 +2707,13 @@ gst_cam_base_src_video_get_range (GstCamBaseSrc * src, GstPad *pad, guint64 offs
GstClockReturn status;
GstBuffer *res_buf, *in_buf;
gchar *padname = gst_pad_get_name(pad);
guint stream_id = CameraSrcUtils::get_stream_id_by_pad(src->priv->request_stream_map, pad);
gint stream_id = CameraSrcUtils::get_stream_id_by_pad(src->priv->request_stream_map, pad);

if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM - 1) {
GST_ERROR("pad %s has no stream_id %d", padname, stream_id);
g_free(padname);
return GST_FLOW_ERROR;
}
bclass = GST_CAM_BASE_SRC_GET_CLASS(src);

if (G_UNLIKELY (!GST_CAM_BASE_SRC_IS_STARTED (src)
Expand Down Expand Up @@ -2738,7 +2748,7 @@ gst_cam_base_src_video_get_range (GstCamBaseSrc * src, GstPad *pad, guint64 offs
}

if (offset == 0 && src->mux[stream_id].vid_segment.time == 0
&& (gint64)GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
&& (gint64)GST_BUFFER_DTS (res_buf) == -1 && !gst_cam_base_src_is_live(src)) {
GST_DEBUG_OBJECT (src, "%s pad: setting first timestamp to 0", padname);
res_buf = gst_buffer_make_writable (res_buf);
GST_BUFFER_DTS (res_buf) = 0;
Expand Down Expand Up @@ -2902,7 +2912,7 @@ gst_cam_base_src_loop (GstPad * pad)
if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
goto flushing;

blocksize = src->blocksize;
blocksize = gst_cam_base_src_get_blocksize(src);

/* if we operate in bytes, we can calculate an offset */
if (src->segment.format == GST_FORMAT_BYTES) {
Expand Down Expand Up @@ -3164,8 +3174,12 @@ static void gst_cam_base_src_video_loop (GstPad * pad)
guint blocksize;
gchar *padname = gst_pad_get_name(pad);
GstCamBaseSrc *src = GST_CAM_BASE_SRC(GST_OBJECT_PARENT(pad));
guint stream_id = CameraSrcUtils::get_stream_id_by_pad(src->priv->request_stream_map, pad);
gint stream_id = CameraSrcUtils::get_stream_id_by_pad(src->priv->request_stream_map, pad);

if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM - 1) {
GST_DEBUG_OBJECT(src, "%s pad: invalid stream id: %d", padname, stream_id);
goto done;
}
/* Just leave immediately if we're flushing */
GST_VID_LIVE_LOCK (src, stream_id);
if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
Expand Down Expand Up @@ -3194,7 +3208,7 @@ static void gst_cam_base_src_video_loop (GstPad * pad)
}

GST_VID_LIVE_LOCK(src, stream_id);
blocksize = src->blocksize;
blocksize = gst_cam_base_src_get_blocksize(src);

/* we only consider the scenario of GST_FORMAT_TIME */
if (src->mux[stream_id].vid_segment.format == GST_FORMAT_TIME)
Expand Down Expand Up @@ -3364,7 +3378,12 @@ gst_cam_base_src_set_allocation (GstCamBaseSrc * basesrc, GstPad *pad,
else
gst_allocation_params_init(&priv->params);
} else {
guint stream_id = CameraSrcUtils::get_stream_id_by_pad(priv->request_stream_map, pad);
gint stream_id = CameraSrcUtils::get_stream_id_by_pad(priv->request_stream_map, pad);
if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM - 1) {
GST_ERROR_OBJECT(basesrc, "invalid stream_id: %d", stream_id);
GST_OBJECT_UNLOCK(basesrc);
return GST_FLOW_ERROR;
}
oldalloc = priv->muxPriv[stream_id].vid_allocator;
oldpool = priv->muxPriv[stream_id].vid_pool;
priv->muxPriv[stream_id].vid_allocator = allocator;
Expand Down Expand Up @@ -4318,6 +4337,11 @@ gst_cam_base_src_activate_mode (GstPad * pad, GstObject * parent,
res = gst_cam_base_src_activate_push (pad, parent, active);
} else {
int stream_id = CameraSrcUtils::get_stream_id_by_pad(src->priv->request_stream_map, pad);
if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM - 1) {
GST_ERROR("pad %s has no stream_id %d", padname, stream_id);
res = FALSE;
break;
}
src->priv->muxPriv[stream_id].vid_stream_start_pending = active;
res = gst_cam_base_src_video_activate_push (pad, parent, stream_id, active);
}
Expand Down
29 changes: 20 additions & 9 deletions src/gstcamerasrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ static GstPad *gst_camerasrc_request_new_pad (GstElement * element,
}
} else {
GST_ERROR("Invalid pad name: %s, please follow 'video_%%u' or 'still_%%u' as template to name it", name_templ);
GST_CAMSRC_LOCK(camerasrc);
GST_CAMSRC_UNLOCK(camerasrc);
return NULL;
}

Expand Down Expand Up @@ -1498,17 +1498,24 @@ static void gst_camerasrc_release_pad (GstElement * element, GstPad * pad)
Gstcamerasrc *camerasrc = GST_CAMERASRC(element);
gchar *padname = gst_pad_get_name(pad);
int stream_id = CameraSrcUtils::get_stream_id_by_pad(camerasrc->stream_map, pad);

if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM) {
GST_ERROR("CameraId=%d pad %s has no stream_id %d", camerasrc->device_id,
padname, stream_id);
g_free(padname);
return;
}
GST_INFO("CameraId=%d.", camerasrc->device_id);

GST_CAMSRC_LOCK(camerasrc);
g_hash_table_remove(camerasrc->pad_indexes, GUINT_TO_POINTER(camerasrc->stream_map[padname]));
g_hash_table_remove(camerasrc->pad_indexes, GUINT_TO_POINTER(stream_id));
camerasrc->stream_map.erase(padname);
camerasrc->number_of_activepads--;
camerasrc->streams[stream_id].activated = FALSE;
gst_pad_set_active(pad, FALSE);
gst_element_remove_pad (element, pad);
g_free (padname);
GST_CAMSRC_UNLOCK(camerasrc);
g_free (padname);
}

/**
Expand Down Expand Up @@ -2474,8 +2481,7 @@ gst_camerasrc_find_match_stream(Gstcamerasrc* camerasrc,
}

static gboolean
gst_camerasrc_get_caps_info (Gstcamerasrc* camerasrc,
GstCaps * caps, int stream_id, stream_config_t *stream_list)
gst_camerasrc_get_caps_info (Gstcamerasrc* camerasrc, GstCaps * caps, int stream_id)
{
PERF_CAMERA_ATRACE();
GstVideoInfo info;
Expand Down Expand Up @@ -2645,7 +2651,8 @@ gst_camerasrc_set_caps(GstCamBaseSrc *src, GstPad *pad, GstCaps *caps)

int stream_id = CameraSrcUtils::get_stream_id_by_pad(camerasrc->stream_map, pad);
gchar *padname = gst_pad_get_name(pad);
if (stream_id < 0) {
if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM) {
GST_ERROR("pad %s has no stream_id %d", padname, stream_id);
g_free (padname);
return FALSE;
}
Expand All @@ -2661,7 +2668,7 @@ gst_camerasrc_set_caps(GstCamBaseSrc *src, GstPad *pad, GstCaps *caps)
}

/* Get caps info from structure and match from HAL */
if (!gst_camerasrc_get_caps_info (camerasrc, caps, stream_id, &camerasrc->stream_list)) {
if (!gst_camerasrc_get_caps_info(camerasrc, caps, stream_id)) {
g_free (padname);
return FALSE;
}
Expand Down Expand Up @@ -2984,8 +2991,10 @@ gst_camerasrc_decide_allocation(GstCamBaseSrc *bsrc,GstQuery *query, GstPad *pad
const char* GST_MSDK_SELECT="gstMsdkSelect";

int stream_id = CameraSrcUtils::get_stream_id_by_pad(camerasrc->stream_map, pad);
if (stream_id < 0)
if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM) {
GST_ERROR("CameraId=%d has no stream_id %d", camerasrc->device_id, stream_id);
return FALSE;
}
GST_INFO("CameraId=%d, StreamId=%d.", camerasrc->device_id, stream_id);

active = gst_buffer_pool_is_active(GST_BUFFER_POOL_CAST(camerasrc->streams[stream_id].pool));
Expand Down Expand Up @@ -3122,8 +3131,10 @@ gst_camerasrc_fill(GstCamPushSrc *src, GstPad *pad, GstBuffer *buf)
GstClock *clock;
GstClockTime base_time, timestamp, duration;
int stream_id = CameraSrcUtils::get_stream_id_by_pad(camerasrc->stream_map, pad);
if (stream_id < 0)
if (stream_id < 0 || stream_id >= GST_CAMERASRC_MAX_STREAM_NUM) {
GST_ERROR("CameraId=%d has no stream_id %d", camerasrc->device_id, stream_id);
return GST_FLOW_ERROR;
}

GstCamerasrcBufferPool *bpool = GST_CAMERASRC_BUFFER_POOL(camerasrc->streams[stream_id].pool);

Expand Down
2 changes: 1 addition & 1 deletion src/gstcamerasrcbufferpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ gst_camerasrc_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buff
goto err_alloc_buffer;
break;
default:
break;
goto err_alloc_buffer;
}

meta->index = pool->number_allocated;
Expand Down
4 changes: 2 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void CameraSrcUtils::get_stream_info_by_caps(GstCaps *caps, const char **format,
gst_structure_get_int(structure, "height", height);
}

int CameraSrcUtils::get_stream_id_by_pad(map<string, int> streamMap, GstPad *pad)
int CameraSrcUtils::get_stream_id_by_pad(map<string, int> &streamMap, GstPad *pad)
{
int stream_id = -1;
gchar *padname = gst_pad_get_name(pad);
Expand All @@ -158,7 +158,7 @@ int CameraSrcUtils::get_stream_id_by_pad(map<string, int> streamMap, GstPad *pad
if (iter != streamMap.end())
stream_id = iter->second;
else {
GST_ERROR("failed to get StreamId: %d", stream_id);
GST_ERROR("failed to find StreamId by pad: %s", padname);
}

g_free(padname);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace CameraSrcUtils {

void get_stream_info_by_caps(GstCaps *caps, const char **format, int *width, int *height);

int get_stream_id_by_pad(map<string, int> streamMap, GstPad *pad);
int get_stream_id_by_pad(map<string, int> &streamMap, GstPad *pad);
}

#endif

0 comments on commit 931412a

Please sign in to comment.