From 98de736975e764896993c8733bf35f5323df4fa0 Mon Sep 17 00:00:00 2001 From: Hassan Sahibzada Date: Thu, 19 Jan 2023 14:17:56 -0500 Subject: [PATCH] remove gst based storage sample --- samples/CMakeLists.txt | 9 +- samples/kvsWebRTCClientStorageSession.c | 512 ------------------------ 2 files changed, 2 insertions(+), 519 deletions(-) delete mode 100644 samples/kvsWebRTCClientStorageSession.c diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 437f8c0306..f9224c3675 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -76,13 +76,7 @@ if(GST_FOUND) ) target_link_libraries(kvsWebrtcClientMasterGstSample kvsWebrtcClient kvsWebrtcSignalingClient ${GST_SAMPLE_LIBRARIES} kvsCommonLws kvspicUtils websockets) - add_executable( - kvsWebrtcClientStorageSession - Common.c - kvsWebRTCClientStorageSession.c) - target_link_libraries( kvsWebrtcClientStorageSession kvsWebrtcClient kvsWebrtcSignalingClient ${GST_SAMPLE_LIBRARIES} kvsCommonLws kvspicUtils websockets) - - install(TARGETS kvsWebrtcClientMasterGstSample kvsWebrtcClientStorageSession + install(TARGETS kvsWebrtcClientMasterGstSample RUNTIME DESTINATION bin ) endif() @@ -90,3 +84,4 @@ endif() install(TARGETS kvsWebrtcClientMaster kvsWebrtcClientViewer discoverNatBehavior RUNTIME DESTINATION bin ) + diff --git a/samples/kvsWebRTCClientStorageSession.c b/samples/kvsWebRTCClientStorageSession.c deleted file mode 100644 index 179c1c58b8..0000000000 --- a/samples/kvsWebRTCClientStorageSession.c +++ /dev/null @@ -1,512 +0,0 @@ -#include "Samples.h" -#include -#include - -extern PSampleConfiguration gSampleConfiguration; - -typedef enum { - CLIP_TYPE_PERSISTENT, - CLIP_TYPE_EVENT, -} CLIP_TYPE; - -// #define VERBOSE - -GstFlowReturn on_new_sample(GstElement* sink, gpointer data, UINT64 trackid) -{ - GstBuffer* buffer; - BOOL isDroppable, delta; - GstFlowReturn ret = GST_FLOW_OK; - GstSample* sample = NULL; - GstMapInfo info; - GstSegment* segment; - GstClockTime buf_pts; - Frame frame; - STATUS status; - PSampleConfiguration pSampleConfiguration = (PSampleConfiguration) data; - PSampleStreamingSession pSampleStreamingSession = NULL; - PRtcRtpTransceiver pRtcRtpTransceiver = NULL; - UINT32 i; - - if (pSampleConfiguration == NULL) { - DLOGE("operation returned status code: 0x%08x", STATUS_NULL_ARG); - goto CleanUp; - } - - info.data = NULL; - sample = gst_app_sink_pull_sample(GST_APP_SINK(sink)); - - buffer = gst_sample_get_buffer(sample); - isDroppable = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_CORRUPTED) || GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DECODE_ONLY) || - (GST_BUFFER_FLAGS(buffer) == GST_BUFFER_FLAG_DISCONT) || - (GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DISCONT) && GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT)) || - // drop if buffer contains header only and has invalid timestamp - !GST_BUFFER_PTS_IS_VALID(buffer); - - if (!isDroppable) { - delta = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT); - - frame.flags = delta ? FRAME_FLAG_NONE : FRAME_FLAG_KEY_FRAME; - // convert from segment timestamp to running time in live mode. - segment = gst_sample_get_segment(sample); - buf_pts = gst_segment_to_running_time(segment, GST_FORMAT_TIME, buffer->pts); - if (!GST_CLOCK_TIME_IS_VALID(buf_pts)) { - DLOGE("Frame contains invalid PTS dropping the frame."); - } - - if (!(gst_buffer_map(buffer, &info, GST_MAP_READ))) { - DLOGE("Gst buffer mapping failed"); - goto CleanUp; - } - - frame.trackId = trackid; - frame.duration = 0; - frame.version = FRAME_CURRENT_VERSION; - frame.size = (UINT32) info.size; - frame.frameData = (PBYTE) info.data; - - MUTEX_LOCK(pSampleConfiguration->streamingSessionListReadLock); - for (i = 0; i < pSampleConfiguration->streamingSessionCount; ++i) { - pSampleStreamingSession = pSampleConfiguration->sampleStreamingSessionList[i]; - frame.index = (UINT32) ATOMIC_INCREMENT(&pSampleStreamingSession->frameIndex); - - if (trackid == DEFAULT_AUDIO_TRACK_ID) { - pRtcRtpTransceiver = pSampleStreamingSession->pAudioRtcRtpTransceiver; - // YC_TBD, - // frame.presentationTs = pSampleStreamingSession->audioTimestamp; - frame.presentationTs = buf_pts * DEFAULT_TIME_UNIT_IN_NANOS; - frame.decodingTs = frame.presentationTs; - pSampleStreamingSession->audioTimestamp += - SAMPLE_AUDIO_FRAME_DURATION; // assume audio frame size is 20ms, which is default in opusenc - } else { - pRtcRtpTransceiver = pSampleStreamingSession->pVideoRtcRtpTransceiver; - // YC_TBD. - // frame.presentationTs = pSampleStreamingSession->videoTimestamp; - frame.presentationTs = buf_pts * DEFAULT_TIME_UNIT_IN_NANOS; - frame.decodingTs = frame.presentationTs; - pSampleStreamingSession->videoTimestamp += SAMPLE_VIDEO_FRAME_DURATION; // assume video fps is 25 - } - - status = writeFrame(pRtcRtpTransceiver, &frame); - - if (status != STATUS_SRTP_NOT_READY_YET && status != STATUS_SUCCESS) { -#ifdef VERBOSE - DLOGE("failed with 0x%08x", status); -#endif - } - } - MUTEX_UNLOCK(pSampleConfiguration->streamingSessionListReadLock); - } - -CleanUp: - - if (info.data != NULL) { - gst_buffer_unmap(buffer, &info); - } - - if (sample != NULL) { - gst_sample_unref(sample); - } - - if (ATOMIC_LOAD_BOOL(&pSampleConfiguration->appTerminateFlag)) { - ret = GST_FLOW_EOS; - } - - return ret; -} - -GstFlowReturn on_new_sample_video(GstElement* sink, gpointer data) -{ - return on_new_sample(sink, data, DEFAULT_VIDEO_TRACK_ID); -} - -GstFlowReturn on_new_sample_audio(GstElement* sink, gpointer data) -{ - return on_new_sample(sink, data, DEFAULT_AUDIO_TRACK_ID); -} - -PVOID sendGstreamerAudioVideo(PVOID args) -{ - STATUS retStatus = STATUS_SUCCESS; - GstElement *appsinkVideo = NULL, *appsinkAudio = NULL, *pipeline = NULL; - GstBus* bus; - GstMessage* msg; - GError* error = NULL; - PSampleConfiguration pSampleConfiguration = (PSampleConfiguration) args; - BOOL locked = FALSE; - UINT32 i; - - if (pSampleConfiguration == NULL) { - DLOGE("operation returned status code: 0x%08x", STATUS_NULL_ARG); - goto CleanUp; - } - - switch (pSampleConfiguration->mediaType) { - case SAMPLE_STREAMING_VIDEO_ONLY: - pipeline = gst_parse_launch( - "videotestsrc is-live=TRUE ! clockoverlay ! queue ! videoconvert ! video/x-raw,width=1280,height=720,framerate=30/1 ! " - "x264enc bframes=0 speed-preset=veryfast bitrate=512 byte-stream=TRUE tune=zerolatency ! " - "video/x-h264,stream-format=byte-stream,alignment=au,profile=baseline ! appsink sync=TRUE emit-signals=TRUE name=appsink-video", - &error); - break; - - case SAMPLE_STREAMING_AUDIO_VIDEO: - pipeline = gst_parse_launch( - "videotestsrc is-live=TRUE ! clockoverlay ! queue ! videoconvert ! video/x-raw,width=1280,height=720,framerate=30/1 ! " - "x264enc bframes=0 speed-preset=veryfast bitrate=512 byte-stream=TRUE tune=zerolatency ! " - "video/x-h264,stream-format=byte-stream,alignment=au,profile=baseline ! appsink sync=TRUE " - "emit-signals=TRUE name=appsink-video audiotestsrc is-live=TRUE ! " - "queue leaky=2 max-size-buffers=400 ! audioconvert ! audioresample ! opusenc ! " - "audio/x-opus,rate=48000,channels=2 ! appsink sync=TRUE emit-signals=TRUE name=appsink-audio", - &error); - break; - } - - if (pipeline == NULL) { - DLOGE("Failed to launch gstreamer, operation returned status code: 0x%08x", STATUS_INTERNAL_ERROR); - goto CleanUp; - } - - appsinkVideo = gst_bin_get_by_name(GST_BIN(pipeline), "appsink-video"); - appsinkAudio = gst_bin_get_by_name(GST_BIN(pipeline), "appsink-audio"); - - if (!(appsinkVideo != NULL || appsinkAudio != NULL)) { - DLOGE("cant find appsink, operation returned status code: 0x%08x", STATUS_INTERNAL_ERROR); - goto CleanUp; - } - - if (appsinkVideo != NULL) { - g_signal_connect(appsinkVideo, "new-sample", G_CALLBACK(on_new_sample_video), (gpointer) pSampleConfiguration); - } - - if (appsinkAudio != NULL) { - g_signal_connect(appsinkAudio, "new-sample", G_CALLBACK(on_new_sample_audio), (gpointer) pSampleConfiguration); - } - - gst_element_set_state(pipeline, GST_STATE_PLAYING); - - /* block until error or EOS */ - bus = gst_element_get_bus(pipeline); - msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS); - /* issue the termination signals to the app */ - MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock); - locked = TRUE; - - // scan and cleanup terminated streaming session - DLOGD("gstreamer terminated"); - for (i = 0; i < pSampleConfiguration->streamingSessionCount; ++i) { - ATOMIC_STORE_BOOL(&pSampleConfiguration->sampleStreamingSessionList[i]->terminateFlag, TRUE); - } - CVAR_BROADCAST(pSampleConfiguration->cvar); - MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock); - locked = FALSE; - - /* Free resources */ - if (msg != NULL) { - gst_message_unref(msg); - } - gst_object_unref(bus); - gst_element_set_state(pipeline, GST_STATE_NULL); - gst_object_unref(pipeline); - gst_object_unref(appsinkAudio); - gst_object_unref(appsinkVideo); - -CleanUp: - if (locked) { - MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock); - } - - if (error != NULL) { - DLOGE("%s", error->message); - g_clear_error(&error); - } - - return (PVOID) (ULONG_PTR) retStatus; -} - -VOID onGstAudioFrameReady(UINT64 customData, PFrame pFrame) -{ - GstFlowReturn ret; - GstBuffer* buffer; - GstElement* appsrcAudio = (GstElement*) customData; - - /* Create a new empty buffer */ - buffer = gst_buffer_new_and_alloc(pFrame->size); - gst_buffer_fill(buffer, 0, pFrame->frameData, pFrame->size); - - /* Push the buffer into the appsrc */ - g_signal_emit_by_name(appsrcAudio, "push-buffer", buffer, &ret); - - /* Free the buffer now that we are done with it */ - gst_buffer_unref(buffer); -} - -VOID onSampleStreamingSessionShutdown(UINT64 customData, PSampleStreamingSession pSampleStreamingSession) -{ - (void) (pSampleStreamingSession); - GstElement* appsrc = (GstElement*) customData; - GstFlowReturn ret; - - g_signal_emit_by_name(appsrc, "end-of-stream", &ret); -} - -PVOID receiveGstreamerAudioVideo(PVOID args) -{ - STATUS retStatus = STATUS_SUCCESS; - GstElement *pipeline = NULL, *appsrcAudio = NULL; - GstBus* bus; - GstMessage* msg; - GError* error = NULL; - PSampleStreamingSession pSampleStreamingSession = (PSampleStreamingSession) args; - gchar *videoDescription = "", *audioDescription = "", *audioVideoDescription; - - if (pSampleStreamingSession == NULL) { - DLOGE("operation returned status code: 0x%08x", STATUS_NULL_ARG); - goto CleanUp; - } - - // TODO: Wire video up with gstreamer pipeline - - switch (pSampleStreamingSession->pAudioRtcRtpTransceiver->receiver.track.codec) { - case RTC_CODEC_OPUS: - audioDescription = "appsrc name=appsrc-audio ! opusparse ! decodebin ! autoaudiosink"; - break; - - case RTC_CODEC_MULAW: - case RTC_CODEC_ALAW: - audioDescription = "appsrc name=appsrc-audio ! rawaudioparse ! decodebin ! autoaudiosink"; - break; - default: - break; - } - - audioVideoDescription = g_strjoin(" ", audioDescription, videoDescription, NULL); - - pipeline = gst_parse_launch(audioVideoDescription, &error); - - appsrcAudio = gst_bin_get_by_name(GST_BIN(pipeline), "appsrc-audio"); - if (appsrcAudio == NULL) { - DLOGE("gst_bin_get_by_name(): cant find appsrc, operation returned status code: 0x%08x", STATUS_INTERNAL_ERROR); - goto CleanUp; - } - - transceiverOnFrame(pSampleStreamingSession->pAudioRtcRtpTransceiver, (UINT64) appsrcAudio, onGstAudioFrameReady); - - retStatus = streamingSessionOnShutdown(pSampleStreamingSession, (UINT64) appsrcAudio, onSampleStreamingSessionShutdown); - if (retStatus != STATUS_SUCCESS) { - DLOGE("streamingSessionOnShutdown(): operation returned status code: 0x%08x", STATUS_INTERNAL_ERROR); - goto CleanUp; - } - - g_free(audioVideoDescription); - - if (pipeline == NULL) { - DLOGE("Failed to launch gstreamer, operation returned status code: 0x%08x", STATUS_INTERNAL_ERROR); - goto CleanUp; - } - - gst_element_set_state(pipeline, GST_STATE_PLAYING); - - /* block until error or EOS */ - bus = gst_element_get_bus(pipeline); - msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS); - - /* Free resources */ - if (msg != NULL) { - gst_message_unref(msg); - } - gst_object_unref(bus); - gst_element_set_state(pipeline, GST_STATE_NULL); - gst_object_unref(pipeline); - gst_object_unref(appsrcAudio); - -CleanUp: - if (error != NULL) { - DLOGE("%s", error->message); - g_clear_error(&error); - } - - return (PVOID) (ULONG_PTR) retStatus; -} - -INT32 main(INT32 argc, CHAR* argv[]) -{ - STATUS retStatus = STATUS_SUCCESS; - PSampleConfiguration pSampleConfiguration = NULL; - PCHAR pChannelName; - - SET_INSTRUMENTED_ALLOCATORS(); - - signal(SIGINT, sigintHandler); - - // do trickle-ice by default - DLOGI("Using trickleICE by default"); - -#ifdef IOT_CORE_ENABLE_CREDENTIALS - CHK_ERR((pChannelName = getenv(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_THING_NAME must be set"); -#else - pChannelName = argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME; -#endif - - retStatus = createSampleConfiguration(pChannelName, SIGNALING_CHANNEL_ROLE_TYPE_MASTER, TRUE, TRUE, TRUE, &pSampleConfiguration); - if (retStatus != STATUS_SUCCESS) { - DLOGE("createSampleConfiguration(): operation returned status code: 0x%08x", retStatus); - goto CleanUp; - } - - DLOGI("Created signaling channel %s", pChannelName); - - if (pSampleConfiguration->enableFileLogging) { - retStatus = - createFileLogger(FILE_LOGGING_BUFFER_SIZE, MAX_NUMBER_OF_LOG_FILES, (PCHAR) FILE_LOGGER_LOG_FILE_DIRECTORY_PATH, TRUE, TRUE, NULL); - if (retStatus != STATUS_SUCCESS) { - DLOGE("[KVS Master] createFileLogger(): operation returned status code: 0x%08x", retStatus); - pSampleConfiguration->enableFileLogging = FALSE; - } - } - - pSampleConfiguration->videoSource = sendGstreamerAudioVideo; - pSampleConfiguration->mediaType = SAMPLE_STREAMING_VIDEO_ONLY; - pSampleConfiguration->receiveAudioVideoSource = receiveGstreamerAudioVideo; - pSampleConfiguration->onDataChannel = onDataChannel; - pSampleConfiguration->customData = (UINT64) pSampleConfiguration; - pSampleConfiguration->useTestSrc = FALSE; - /* Initialize GStreamer */ - gst_init(&argc, &argv); - DLOGI("Finished initializing GStreamer"); - - if (argc > 2) { - if (STRCMP(argv[2], "video-only") == 0) { - pSampleConfiguration->mediaType = SAMPLE_STREAMING_VIDEO_ONLY; - DLOGI("Streaming video only"); - } else if (STRCMP(argv[2], "audio-video") == 0) { - pSampleConfiguration->mediaType = SAMPLE_STREAMING_AUDIO_VIDEO; - DLOGI("Streaming audio and video"); - } else { - DLOGI("Unrecognized streaming type. Default to video-only"); - } - } else { - DLOGI("Streaming video only"); - } - - if (argc > 3) { - pSampleConfiguration->mediaStorageClipLength = strtoull(argv[3], NULL, 10) * GST_SECOND; - if (pSampleConfiguration->mediaStorageClipLength == 0ULL) { - pSampleConfiguration->mediaStorageClipLength = GST_CLOCK_TIME_NONE; - DLOGI("persistent streaming"); - } else { - DLOGI("periodic event-driven streaming"); - } - } - - switch (pSampleConfiguration->mediaType) { - case SAMPLE_STREAMING_VIDEO_ONLY: - DLOGI("streaming type video-only"); - break; - case SAMPLE_STREAMING_AUDIO_VIDEO: - DLOGI("streaming type audio-video"); - break; - } - - // Initialize KVS WebRTC. This must be done before anything else, and must only be done once. - retStatus = initKvsWebRtc(); - if (retStatus != STATUS_SUCCESS) { - DLOGE("initKvsWebRtc(): operation returned status code: 0x%08x", retStatus); - goto CleanUp; - } - DLOGI("KVS WebRTC initialization completed successfully"); - - pSampleConfiguration->signalingClientCallbacks.messageReceivedFn = signalingMessageReceived; - - strcpy(pSampleConfiguration->clientInfo.clientId, SAMPLE_MASTER_CLIENT_ID); - - retStatus = createSignalingClientSync(&pSampleConfiguration->clientInfo, &pSampleConfiguration->channelInfo, - &pSampleConfiguration->signalingClientCallbacks, pSampleConfiguration->pCredentialProvider, - &pSampleConfiguration->signalingClientHandle); - - if (retStatus != STATUS_SUCCESS) { - DLOGE("createSignalingClientSync(): operation returned status code: 0x%08x", retStatus); - } - DLOGI("Signaling client created successfully"); - - // Enable the processing of the messages - retStatus = signalingClientFetchSync(pSampleConfiguration->signalingClientHandle); - if (retStatus != STATUS_SUCCESS) { - DLOGE("[KVS Master] signalingClientFetchSync(): operation returned status code: 0x%08x", retStatus); - goto CleanUp; - } - - retStatus = signalingClientConnectSync(pSampleConfiguration->signalingClientHandle); - if (retStatus != STATUS_SUCCESS) { - DLOGE("signalingClientConnectSync(): operation returned status code: 0x%08x", retStatus); - goto CleanUp; - } - DLOGI("Signaling client connection to socket established"); - - DLOGI("Beginning streaming...check the stream over channel %s", pChannelName); - - if (pSampleConfiguration->channelInfo.useMediaStorage == TRUE) { - DLOGD("invoke join storage session"); - retStatus = signalingClientJoinSessionSync(pSampleConfiguration->signalingClientHandle); - if (retStatus != STATUS_SUCCESS) { - printf("[KVS Master] signalingClientConnectSync(): operation returned status code: 0x%08x", retStatus); - goto CleanUp; - } - printf("[KVS Master] Signaling client connection to socket established"); - if (SAMPLE_PERIODIC_JOIN_SESSION) { - CHK_LOG_ERR(retStatus = timerQueueAddTimer(pSampleConfiguration->timerQueueHandle, SAMPLE_PERIODIC_JOIN_SESSION_PERIOD, - SAMPLE_PERIODIC_JOIN_SESSION_PERIOD, joinSessionTimerCallback, (UINT64) pSampleConfiguration, - &pSampleConfiguration->joinSessionTimerId)); - } - } - - gSampleConfiguration = pSampleConfiguration; - - // Checking for termination - retStatus = sessionCleanupWait(pSampleConfiguration); - if (retStatus != STATUS_SUCCESS) { - DLOGI("sessionCleanupWait(): operation returned status code: 0x%08x", retStatus); - goto CleanUp; - } - - DLOGI("Streaming session terminated"); - -CleanUp: - - if (retStatus != STATUS_SUCCESS) { - DLOGE("Terminated with status code 0x%08x", retStatus); - } - - DLOGI("Cleaning up...."); - - if (pSampleConfiguration != NULL) { - // Kick of the termination sequence - ATOMIC_STORE_BOOL(&pSampleConfiguration->appTerminateFlag, TRUE); - - if (pSampleConfiguration->mediaSenderTid != INVALID_TID_VALUE) { - THREAD_JOIN(pSampleConfiguration->mediaSenderTid, NULL); - } - - if (pSampleConfiguration->enableFileLogging) { - freeFileLogger(); - } - retStatus = freeSignalingClient(&pSampleConfiguration->signalingClientHandle); - if (retStatus != STATUS_SUCCESS) { - DLOGE("freeSignalingClient(): operation returned status code: 0x%08x", retStatus); - } - - retStatus = freeSampleConfiguration(&pSampleConfiguration); - if (retStatus != STATUS_SUCCESS) { - DLOGE("freeSampleConfiguration(): operation returned status code: 0x%08x", retStatus); - } - } - DLOGI("Cleanup done"); - - RESET_INSTRUMENTED_ALLOCATORS(); - - // https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html - // We can only return with 0 - 127. Some platforms treat exit code >= 128 - // to be a success code, which might give an unintended behaviour. - // Some platforms also treat 1 or 0 differently, so it's better to use - // EXIT_FAILURE and EXIT_SUCCESS macros for portability. - return STATUS_FAILED(retStatus) ? EXIT_FAILURE : EXIT_SUCCESS; -}