janus-gateway的videoroom插件的RTP包录制功能源码详解

2024-02-25 15:44

本文主要是介绍janus-gateway的videoroom插件的RTP包录制功能源码详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

引:

janus-gateway在配置文件设置后,可以实现对videoroom插件的每个publisher的音频,视频,数据的RTP流录制成mjr文件。

对于音频,视频的mjr文件,可以使用自带的postprocessing工具janus-pp-rec转成mp4文件。

每个publisher音频和视频mjr文件是分立的两个文件,需要使用ffmpeg将两个合成一个mp4文件。

janus-gateway的原生代码中的录制功能是通过配置文件实现,只能配置成要么录,要么不录。如果要通过客户端的信令进行可控的频繁开关,则需要修改源码实现。

如果要对videoroom的publisher的RTP流转成RTMP流推送出去,可以使用第三方的enhanced-videoroom插件实现。

一、配置文件的录制参数设置

etc/janus/janus.plugin.videoroom.jcfg
房间中和录制相关的参数

# room-<unique room ID>: {
# description = This is my awesome room
...
# record = true|false (whether this room should be recorded, default=false)
# rec_dir = <folder where recordings should be stored, when enabled>
# lock_record = true|false (whether recording can only be started/stopped if the secret
#            is provided, or using the global enable_recording request, default=false)
#}
配置实例
room-1234: {description = "Demo Room"secret = "adminpwd"publishers = 6bitrate = 128000fir_freq = 10audiocodec = "opus"videocodec = "h264"record = truerec_dir = "/data/PJT-janus/record-samples"
}

二、录制初始化

当客户端为发布者,且发送的message为"configure"类型时,
将初始化录制, 将初始化音频、视频和数据文件的存储路径、文件名后,
打开文件以获得文件句柄后,写入文件头。

// janus_videoroom.c
static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_session *session, json_t *message) {if(!strcasecmp(request_text, "create")) {/* Create a new VideoRoom *//* Added by Hank, For recording: */// if(rec_dir) {// videoroom->rec_dir = g_strdup(json_string_value(rec_dir));if (g_record_root_path != NULL) {videoroom->rec_dir = g_strdup(g_record_root_path);// 修改文件存储路径,在原有的录制根目录下,添加 /年月日/房间号/char new_rec_dir_arr[255] = {0};time_t timestamp = time(NULL); struct tm *local_time = localtime(&timestamp);char formatted_date[11]={0};strftime(formatted_date,sizeof(formatted_date), "%Y%m%d",local_time);g_snprintf(new_rec_dir_arr, 255, "%s/%s/%s/",videoroom->rec_dir, formatted_date, videoroom->room_id_str);char *old_rec_dir = videoroom->rec_dir;char *new_rec_dir = g_strdup(new_rec_dir_arr);videoroom->rec_dir = new_rec_dir;g_free(old_rec_dir);		/* END-OF-Hank */}		}
}
/* Thread to handle incoming messages * 当有房间“configure"消息时,* 进行本房间的发布者对应的视频、音频、数据录制文件创建*/
static void *janus_videoroom_handler(void *data) {while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {msg = g_async_queue_pop(messages);janus_videoroom *videoroom = NULL;janus_videoroom_publisher *participant = NULL;janus_videoroom_subscriber *subscriber = NULL;janus_mutex_lock(&sessions_mutex);janus_videoroom_session *session = janus_videoroom_lookup_session(msg->handle);janus_mutex_unlock(&sessions_mutex);if(session->participant_type == janus_videoroom_p_type_none) {...} else if(session->participant_type == janus_videoroom_p_type_publisher) {/* 当 request_text = "configure" 时 */json_t *request = json_object_get(root, "request");const char *request_text = json_string_value(request);if(!strcasecmp(request_text, "join") || !strcasecmp(request_text, "joinandconfigure")) {...} else if(!strcasecmp(request_text, "configure") || !strcasecmp(request_text, "publish")) {/* 录制相关配置,并创建本publisher的Video/Audio/Data录制文件  */gboolean record_locked = FALSE;if((record || recfile) && participant->room->lock_record && participant->room->room_secret) {JANUS_CHECK_SECRET(participant->room->room_secret, root, "secret", error_code, error_cause,JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT, JANUS_VIDEOROOM_ERROR_UNAUTHORIZED);if(error_code != 0) {/* Wrong secret provided, we'll prevent the recording state from being changed */record_locked = TRUE;}}janus_mutex_lock(&participant->rec_mutex);gboolean prev_recording_active = participant->recording_active;if(record && !record_locked) {participant->recording_active = json_is_true(record);JANUS_LOG(LOG_VERB, "Setting record property: %s (room %s, user %s)\n",participant->recording_active ? "true" : "false", participant->room_id_str, participant->user_id_str);}if(recfile && !record_locked) {participant->recording_base = g_strdup(json_string_value(recfile));JANUS_LOG(LOG_VERB, "Setting recording basename: %s (room %s, user %s)\n",participant->recording_base, participant->room_id_str, participant->user_id_str);}/* Do we need to do something with the recordings right now? */if(participant->recording_active != prev_recording_active) {/* Something changed */if(!participant->recording_active) {/* Not recording (anymore?) */janus_videoroom_recorder_close(participant);} else if(participant->recording_active && g_atomic_int_get(&participant->session->started)) {/* We've started recording, send a PLI/FIR and go on */GList *temp = participant->streams;while(temp) {janus_videoroom_publisher_stream *ps = (janus_videoroom_publisher_stream *)temp->data;janus_videoroom_recorder_create(participant, participant->audio, participant->video, participant->data); }janus_mutex_unlock(&participant->rec_mutex);。。。}}janus_videoroom_message_free(msg);continue;}}} // end of while(g_atomic_int_get(&initialized) ...)return NULL;
}
/**********  创建本发布者对应的音频、视频、数据录制文件 *******************/
static void janus_videoroom_recorder_create(janus_videoroom_publisher *participant, gboolean audio, gboolean video, gboolean data) {char filename[255];janus_recorder *rc = NULL;gint64 now = janus_get_real_time();// 设置音频文件的存储路径和文件名if(audio && participant->arc == NULL) {memset(filename, 0, 255);if(participant->recording_base) {/* Use the filename and path we have been provided */g_snprintf(filename, 255, "%s-audio", participant->recording_base);rc = janus_recorder_create(participant->room->rec_dir,janus_audiocodec_name(participant->acodec), filename);if(rc == NULL) {JANUS_LOG(LOG_ERR, "Couldn't open an audio recording file for this publisher!\n");}} else {/* Build a filename */g_snprintf(filename, 255, "videoroom-%s-user-%s-%"SCNi64"-audio",participant->room_id_str, participant->user_id_str, now);rc = janus_recorder_create(participant->room->rec_dir,janus_audiocodec_name(participant->acodec), filename);if(rc == NULL) {JANUS_LOG(LOG_ERR, "Couldn't open an audio recording file for this publisher!\n");}}/* If media is encrypted, mark it in the recording */if(participant->e2ee)janus_recorder_encrypted(rc);participant->arc = rc;}// 设置视频文件的存储路径和文件名if(video && participant->vrc == NULL) {janus_rtp_switching_context_reset(&participant->rec_ctx);janus_rtp_simulcasting_context_reset(&participant->rec_simctx);participant->rec_simctx.substream_target = 2;participant->rec_simctx.templayer_target = 2;memset(filename, 0, 255);if(participant->recording_base) {/* Use the filename and path we have been provided */g_snprintf(filename, 255, "%s-video", participant->recording_base);rc = janus_recorder_create_full(participant->room->rec_dir,janus_videocodec_name(participant->vcodec), participant->vfmtp, filename);if(rc == NULL) {JANUS_LOG(LOG_ERR, "Couldn't open an video recording file for this publisher!\n");}} else {/* Build a filename */g_snprintf(filename, 255, "videoroom-%s-user-%s-%"SCNi64"-video",participant->room_id_str, participant->user_id_str, now);rc = janus_recorder_create_full(participant->room->rec_dir,janus_videocodec_name(participant->vcodec), participant->vfmtp, filename);if(rc == NULL) {JANUS_LOG(LOG_ERR, "Couldn't open an video recording file for this publisher!\n");}}/* If media is encrypted, mark it in the recording */if(participant->e2ee)janus_recorder_encrypted(rc);participant->vrc = rc;}// 设置数据文件的存储路径和文件名if(data && participant->drc == NULL) {memset(filename, 0, 255);if(participant->recording_base) {/* Use the filename and path we have been provided */g_snprintf(filename, 255, "%s-data", participant->recording_base);rc = janus_recorder_create(participant->room->rec_dir,"text", filename);if(rc == NULL) {JANUS_LOG(LOG_ERR, "Couldn't open an data recording file for this publisher!\n");}} else {/* Build a filename */g_snprintf(filename, 255, "videoroom-%s-user-%s-%"SCNi64"-data",participant->room_id_str, participant->user_id_str, now);rc = janus_recorder_create(participant->room->rec_dir,"text", filename);if(rc == NULL) {JANUS_LOG(LOG_ERR, "Couldn't open an data recording file for this publisher!\n");}}/* Media encryption doesn't apply to data channels */participant->drc = rc;}
}
// record.c
/* Info header in the structured recording */
static const char *header = "MJR00002";
/* Frame header in the structured recording */
static const char *frame_header = "MEET";janus_recorder *janus_recorder_create(const char *dir, const char *codec, const char *filename) {/* Same as janus_recorder_create_full, but with no fmtp */return janus_recorder_create_full(dir, codec, NULL, filename);
}/* 
打开文件;
写入文件头;MJR00002
*/
janus_recorder *janus_recorder_create_full(const char *dir, const char *codec, const char *fmtp, const char *filename) {janus_recorder_medium type = JANUS_RECORDER_AUDIO;if(codec == NULL) {JANUS_LOG(LOG_ERR, "Missing codec information\n");return NULL;}if(!strcasecmp(codec, "vp8") || !strcasecmp(codec, "vp9") || !strcasecmp(codec, "h264")|| !strcasecmp(codec, "av1") || !strcasecmp(codec, "h265")) {type = JANUS_RECORDER_VIDEO;} else if(!strcasecmp(codec, "opus") || !strcasecmp(codec, "multiopus")|| !strcasecmp(codec, "g711") || !strcasecmp(codec, "pcmu") || !strcasecmp(codec, "pcma")|| !strcasecmp(codec, "g722")) {type = JANUS_RECORDER_AUDIO;} else if(!strcasecmp(codec, "text")) {/* FIXME We only handle text on data channels, so that's the only thing we can save too */type = JANUS_RECORDER_DATA;} else {/* We don't recognize the codec: while we might go on anyway, we'd rather fail instead */JANUS_LOG(LOG_ERR, "Unsupported codec '%s'\n", codec);return NULL;}/* Create the recorder */janus_recorder *rc = g_malloc0(sizeof(janus_recorder));janus_refcount_init(&rc->ref, janus_recorder_free);rc->dir = NULL;rc->filename = NULL;rc->file = NULL;rc->codec = g_strdup(codec);rc->fmtp = fmtp ? g_strdup(fmtp) : NULL;rc->created = janus_get_real_time();const char *rec_dir = NULL;const char *rec_file = NULL;char *copy_for_parent = NULL;char *copy_for_base = NULL;/* 检查路径和文件名是否合规 */if(filename != NULL) {/* Helper copies to avoid overwriting */copy_for_parent = g_strdup(filename);copy_for_base = g_strdup(filename);/* Get filename parent folder */const char *filename_parent = dirname(copy_for_parent);/* Get filename base file */const char *filename_base = basename(copy_for_base);if(!dir) {/* If dir is NULL we have to create filename_parent and filename_base */rec_dir = filename_parent;rec_file = filename_base;} else {/* If dir is valid we have to create dir and filename*/rec_dir = dir;rec_file = filename;if(strcasecmp(filename_parent, ".") || strcasecmp(filename_base, filename)) {JANUS_LOG(LOG_WARN, "Unsupported combination of dir and filename %s %s\n", dir, filename);}}}// 检查路径是否存在,如果不存在,则创建路径if(rec_dir != NULL) {/* Check if this directory exists, and create it if needed */struct stat s;int err = stat(rec_dir, &s);if(err == -1) {if(ENOENT == errno) {/* Directory does not exist, try creating it */if(janus_mkdir(rec_dir, 0755) < 0) {JANUS_LOG(LOG_ERR, "mkdir (%s) error: %d (%s)\n", rec_dir, errno, strerror(errno));janus_recorder_destroy(rc);g_free(copy_for_parent);g_free(copy_for_base);return NULL;}} else {JANUS_LOG(LOG_ERR, "stat (%s) error: %d (%s)\n", rec_dir, errno, strerror(errno));janus_recorder_destroy(rc);g_free(copy_for_parent);g_free(copy_for_base);return NULL;}} else {if(S_ISDIR(s.st_mode)) {/* Directory exists */JANUS_LOG(LOG_VERB, "Directory exists: %s\n", rec_dir);} else {/* File exists but it's not a directory? */JANUS_LOG(LOG_ERR, "Not a directory? %s\n", rec_dir);janus_recorder_destroy(rc);g_free(copy_for_parent);g_free(copy_for_base);return NULL;}}}char newname[1024];memset(newname, 0, 1024);// 给文件名加上.mjr的后缀if(rec_file == NULL) {/* Choose a random username */if(!rec_tempname) {/* Use .mjr as an extension right away */g_snprintf(newname, 1024, "janus-recording-%"SCNu32".mjr", janus_random_uint32());} else {/* Append the temporary extension to .mjr, we'll rename when closing */g_snprintf(newname, 1024, "janus-recording-%"SCNu32".mjr.%s", janus_random_uint32(), rec_tempext);}} else {/* Just append the extension */if(!rec_tempname) {/* Use .mjr as an extension right away */g_snprintf(newname, 1024, "%s.mjr", rec_file);} else {/* Append the temporary extension to .mjr, we'll rename when closing */g_snprintf(newname, 1024, "%s.mjr.%s", rec_file, rec_tempext);}}/* 打开文件,准备写入 */if(rec_dir == NULL) {/* Make sure folder to save to is not protected */if(janus_is_folder_protected(newname)) {JANUS_LOG(LOG_ERR, "Target recording path '%s' is in protected folder...\n", newname);janus_recorder_destroy(rc);g_free(copy_for_parent);g_free(copy_for_base);return NULL;}rc->file = fopen(newname, "wb");} else {char path[1024];memset(path, 0, 1024);g_snprintf(path, 1024, "%s/%s", rec_dir, newname);/* Make sure folder to save to is not protected */if(janus_is_folder_protected(path)) {JANUS_LOG(LOG_ERR, "Target recording path '%s' is in protected folder...\n", path);janus_recorder_destroy(rc);g_free(copy_for_parent);g_free(copy_for_base);return NULL;}rc->file = fopen(path, "wb");}if(rc->file == NULL) {JANUS_LOG(LOG_ERR, "fopen error: %d\n", errno);janus_recorder_destroy(rc);g_free(copy_for_parent);g_free(copy_for_base);return NULL;}if(rec_dir)rc->dir = g_strdup(rec_dir);rc->filename = g_strdup(newname);rc->type = type;/* 写入文件头: static const char *header = "MJR00002";*/size_t res = fwrite(header, sizeof(char), strlen(header), rc->file);if(res != strlen(header)) {JANUS_LOG(LOG_ERR, "Couldn't write .mjr header (%zu != %zu, %s)\n",res, strlen(header), strerror(errno));janus_recorder_destroy(rc);g_free(copy_for_parent);g_free(copy_for_base);return NULL;}g_atomic_int_set(&rc->writable, 1);/* 除了写入上面的文件头外,还需要写入信息头, 所以在这里将写入信息头的标志置0*/g_atomic_int_set(&rc->header, 0);janus_mutex_init(&rc->mutex);/* Done */g_atomic_int_set(&rc->destroyed, 0);g_free(copy_for_parent);g_free(copy_for_base);return rc;
}

三、录制数据

对每个接收到的RTP包:
首先:如果是第一个RTP包,则需要先写信息头到文件;
然后:
      写入4字节的帧头"MEET";
      写入4字节的帧时间戳;
      写入2字节的帧长度;
最后: 写入帧数据;

void janus_videoroom_incoming_rtp(janus_plugin_session *handle, janus_plugin_rtp *pkt) {
static void janus_videoroom_incoming_rtp_internal(janus_videoroom_session *session, janus_videoroom_publisher *participant, janus_plugin_rtp *pkt) {if(handle == NULL || g_atomic_int_get(&handle->stopped) || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))return;janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;if(!session || g_atomic_int_get(&session->destroyed) || session->participant_type != janus_videoroom_p_type_publisher)return;janus_videoroom_publisher *participant = janus_videoroom_session_get_publisher_nodebug(session);if(participant == NULL)return;if(g_atomic_int_get(&participant->destroyed) || participant->kicked || participant->room == NULL) {janus_videoroom_publisher_dereference_nodebug(participant);return;}janus_videoroom *videoroom = participant->room;gboolean video = pkt->video;char *buf = pkt->buffer;uint16_t len = pkt->length;/* 写入帧数据到录制文件  */if(!video || (participant->ssrc[0] == 0 && participant->rid[0] == NULL)) {janus_recorder_save_frame(video ? participant->vrc : participant->arc, buf, len);} else {/* We're simulcasting, save the best video quality */gboolean save = janus_rtp_simulcasting_context_process_rtp(&participant->rec_simctx,buf, len, participant->ssrc, participant->rid, participant->vcodec, &participant->rec_ctx);if(save) {uint32_t seq_number = ntohs(rtp->seq_number);uint32_t timestamp = ntohl(rtp->timestamp);uint32_t ssrc = ntohl(rtp->ssrc);janus_rtp_header_update(rtp, &participant->rec_ctx, TRUE, 0);/* We use a fixed SSRC for the whole recording */rtp->ssrc = participant->ssrc[0];janus_recorder_save_frame(participant->vrc, buf, len);/* Restore the header, as it will be needed by subscribers */rtp->ssrc = htonl(ssrc);rtp->timestamp = htonl(timestamp);rtp->seq_number = htons(seq_number);}}
}// record.c
int janus_recorder_save_frame(janus_recorder *recorder, char *buffer, uint length) {if(!recorder)return -1;janus_mutex_lock_nodebug(&recorder->mutex);if(!buffer || length < 1) {janus_mutex_unlock_nodebug(&recorder->mutex);return -2;}if(!recorder->file) {janus_mutex_unlock_nodebug(&recorder->mutex);return -3;}if(!g_atomic_int_get(&recorder->writable)) {janus_mutex_unlock_nodebug(&recorder->mutex);return -4;}gint64 now = janus_get_monotonic_time();// 如果是第一个包,则需要准备好信息头的数据, 将它的长度和内容写入到文件if(!g_atomic_int_get(&recorder->header)) {/* Write info header as a JSON formatted info */json_t *info = json_object();/* FIXME Codecs should be configurable in the future */const char *type = NULL;if(recorder->type == JANUS_RECORDER_AUDIO)type = "a";else if(recorder->type == JANUS_RECORDER_VIDEO)type = "v";else if(recorder->type == JANUS_RECORDER_DATA)type = "d";json_object_set_new(info, "t", json_string(type));								/* Audio/Video/Data */json_object_set_new(info, "c", json_string(recorder->codec));					/* Media codec */if(recorder->fmtp)json_object_set_new(info, "f", json_string(recorder->fmtp));				/* Codec-specific info */json_object_set_new(info, "s", json_integer(recorder->created));				/* Created time */json_object_set_new(info, "u", json_integer(janus_get_real_time()));			/* First frame written time *//* If media will be end-to-end encrypted, mark it in the recording header */if(recorder->encrypted)json_object_set_new(info, "e", json_true());gchar *info_text = json_dumps(info, JSON_PRESERVE_ORDER);json_decref(info);uint16_t info_bytes = htons(strlen(info_text));// 将信息头的长度(info_bytes)写入文件size_t res = fwrite(&info_bytes, sizeof(uint16_t), 1, recorder->file);if(res != 1) {JANUS_LOG(LOG_WARN, "Couldn't write size of JSON header in .mjr file (%zu != %zu, %s), expect issues post-processing\n",res, sizeof(uint16_t), strerror(errno));}// 将信息头的内容(info_text) 写入文件res = fwrite(info_text, sizeof(char), strlen(info_text), recorder->file);if(res != strlen(info_text)) {JANUS_LOG(LOG_WARN, "Couldn't write JSON header in .mjr file (%zu != %zu, %s), expect issues post-processing\n",res, strlen(info_text), strerror(errno));}free(info_text);/* Done */recorder->started = now;// 将是否写入信息头的标志置 1 ; g_atomic_int_set(&recorder->header, 1);}/* Write frame header (fixed part[4], timestamp[4], length[2]) 写入4个字节长度的固定内容的mjr包头:static const char *frame_header = "MEET";*/size_t res = fwrite(frame_header, sizeof(char), strlen(frame_header), recorder->file);if(res != strlen(frame_header)) {JANUS_LOG(LOG_WARN, "Couldn't write frame header in .mjr file (%zu != %zu, %s), expect issues post-processing\n",res, strlen(frame_header), strerror(errno));}// 写入4个字节长度的时间戳uint32_t timestamp = (uint32_t)(now > recorder->started ? ((now - recorder->started)/1000) : 0);timestamp = htonl(timestamp);res = fwrite(&timestamp, sizeof(uint32_t), 1, recorder->file);if(res != 1) {JANUS_LOG(LOG_WARN, "Couldn't write frame timestamp in .mjr file (%zu != %zu, %s), expect issues post-processing\n",res, sizeof(uint32_t), strerror(errno));}// 写入2个字节长度的帧长度uint16_t header_bytes = htons(recorder->type == JANUS_RECORDER_DATA ? (length+sizeof(gint64)) : length);res = fwrite(&header_bytes, sizeof(uint16_t), 1, recorder->file);if(res != 1) {JANUS_LOG(LOG_WARN, "Couldn't write size of frame in .mjr file (%zu != %zu, %s), expect issues post-processing\n",res, sizeof(uint16_t), strerror(errno));}if(recorder->type == JANUS_RECORDER_DATA) {/* If it's data, then we need to prepend timing related info, as it's not there by itself */gint64 now = htonll(janus_get_real_time());res = fwrite(&now, sizeof(gint64), 1, recorder->file);if(res != 1) {JANUS_LOG(LOG_WARN, "Couldn't write data timestamp in .mjr file (%zu != %zu, %s), expect issues post-processing\n",res, sizeof(gint64), strerror(errno));}}/* Save packet on file 写入帧数据到文件*/int temp = 0, tot = length;while(tot > 0) {temp = fwrite(buffer+length-tot, sizeof(char), tot, recorder->file);if(temp <= 0) {JANUS_LOG(LOG_ERR, "Error saving frame...\n");janus_mutex_unlock_nodebug(&recorder->mutex);return -5;}tot -= temp;}/* Done */janus_mutex_unlock_nodebug(&recorder->mutex);return 0;
}

四、录制结束

对录制文件重命名后,
关闭文件句柄;

/* Thread responsible for a specific remote publisher */
static void *janus_videoroom_remote_publisher_thread(void *user_data) {/* If we got here, the remote publisher has been removed from the* room: let's notify all other publishers in the room */janus_mutex_lock(&publisher->rec_mutex);g_free(publisher->recording_base);publisher->recording_base = NULL;// 结束录制,看是否要对录制文件进行重命名janus_videoroom_recorder_close(publisher);janus_mutex_unlock(&publisher->rec_mutex);
}	// janus_videoroom.c
static void janus_videoroom_recorder_close(janus_videoroom_publisher *participant) {if(participant->arc) {janus_recorder *rc = participant->arc;participant->arc = NULL;janus_recorder_close(rc);JANUS_LOG(LOG_INFO, "Closed audio recording %s\n", rc->filename ? rc->filename : "??");janus_recorder_destroy(rc);}if(participant->vrc) {janus_recorder *rc = participant->vrc;participant->vrc = NULL;janus_recorder_close(rc);JANUS_LOG(LOG_INFO, "Closed video recording %s\n", rc->filename ? rc->filename : "??");janus_recorder_destroy(rc);}if(participant->drc) {janus_recorder *rc = participant->drc;participant->drc = NULL;janus_recorder_close(rc);JANUS_LOG(LOG_INFO, "Closed data recording %s\n", rc->filename ? rc->filename : "??");janus_recorder_destroy(rc);}
}//record.c
// 结束录制,看是否要对录制文件进行重命名
int janus_recorder_close(janus_recorder *recorder) {if(!recorder || !g_atomic_int_compare_and_exchange(&recorder->writable, 1, 0))return -1;janus_mutex_lock_nodebug(&recorder->mutex);if(recorder->file) {fseek(recorder->file, 0L, SEEK_END);size_t fsize = ftell(recorder->file);fseek(recorder->file, 0L, SEEK_SET);JANUS_LOG(LOG_INFO, "File is %zu bytes: %s\n", fsize, recorder->filename);}if(rec_tempname) {/* We need to rename the file, to remove the temporary extension */char newname[1024];memset(newname, 0, 1024);g_snprintf(newname, strlen(recorder->filename)-strlen(rec_tempext), "%s", recorder->filename);char oldpath[1024];memset(oldpath, 0, 1024);char newpath[1024];memset(newpath, 0, 1024);if(recorder->dir) {g_snprintf(newpath, 1024, "%s/%s", recorder->dir, newname);g_snprintf(oldpath, 1024, "%s/%s", recorder->dir, recorder->filename);} else {g_snprintf(newpath, 1024, "%s", newname);g_snprintf(oldpath, 1024, "%s", recorder->filename);}if(rename(oldpath, newpath) != 0) {JANUS_LOG(LOG_ERR, "Error renaming %s to %s...\n", recorder->filename, newname);} else {JANUS_LOG(LOG_INFO, "Recording renamed: %s\n", newname);g_free(recorder->filename);recorder->filename = g_strdup(newname);}}janus_mutex_unlock_nodebug(&recorder->mutex);return 0;
}

这篇关于janus-gateway的videoroom插件的RTP包录制功能源码详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/745937

相关文章

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

Java CompletableFuture如何实现超时功能

《JavaCompletableFuture如何实现超时功能》:本文主要介绍实现超时功能的基本思路以及CompletableFuture(之后简称CF)是如何通过代码实现超时功能的,需要的... 目录基本思路CompletableFuture 的实现1. 基本实现流程2. 静态条件分析3. 内存泄露 bug

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.