Revision becc0c84 record.c

View differences:

record.c
36 36
/* Frame header in the structured recording */
37 37
static const char *frame_header = "MEETECHO";
38 38

  
39
/* Whether the filenames should have a temporary extension, while saving, or not (default=false) */
40
static gboolean rec_tempname = FALSE;
41
/* Extension to add in case tempnames is true (default="tmp" --> ".tmp") */
42
static char *rec_tempext = NULL;
43

  
44
void janus_recorder_init(gboolean tempnames, const char *extension) {
45
	JANUS_LOG(LOG_INFO, "Initializing recorder code\n");
46
	if(tempnames) {
47
		rec_tempname = TRUE;
48
		if(extension == NULL) {
49
			rec_tempext = g_strdup("tmp");
50
			JANUS_LOG(LOG_INFO, "  -- No extension provided, using default one (tmp)");
51
		} else {
52
			rec_tempext = g_strdup(extension);
53
			JANUS_LOG(LOG_INFO, "  -- Using temporary extension .%s", rec_tempext);
54
		}
55
	}
56
}
57

  
58
void janus_recorder_deinit(void) {
59
	rec_tempname = FALSE;
60
	g_free(rec_tempext);
61
}
62

  
39 63

  
40 64
janus_recorder *janus_recorder_create(const char *dir, const char *codec, const char *filename) {
41 65
	janus_recorder_medium type = JANUS_RECORDER_AUDIO;
......
101 125
	memset(newname, 0, 1024);
102 126
	if(filename == NULL) {
103 127
		/* Choose a random username */
104
		g_snprintf(newname, 1024, "janus-recording-%"SCNu32".mjr", janus_random_uint32());
128
		if(!rec_tempname) {
129
			/* Use .mjr as an extension right away */
130
			g_snprintf(newname, 1024, "janus-recording-%"SCNu32".mjr", janus_random_uint32());
131
		} else {
132
			/* Append the temporary extension to .mjr, we'll rename when closing */
133
			g_snprintf(newname, 1024, "janus-recording-%"SCNu32".mjr.%s", janus_random_uint32(), rec_tempext);
134
		}
105 135
	} else {
106 136
		/* Just append the extension */
107
		g_snprintf(newname, 1024, "%s.mjr", filename);
137
		if(!rec_tempname) {
138
			/* Use .mjr as an extension right away */
139
			g_snprintf(newname, 1024, "%s.mjr", filename);
140
		} else {
141
			/* Append the temporary extension to .mjr, we'll rename when closing */
142
			g_snprintf(newname, 1024, "%s.mjr.%s", filename, rec_tempext);
143
		}
108 144
	}
109 145
	/* Try opening the file now */
110 146
	if(dir == NULL) {
......
208 244
		fseek(recorder->file, 0L, SEEK_SET);
209 245
		JANUS_LOG(LOG_INFO, "File is %zu bytes: %s\n", fsize, recorder->filename);
210 246
	}
247
	if(rec_tempname) {
248
		/* We need to rename the file, to remove the temporary extension */
249
		char newname[1024];
250
		memset(newname, 0, 1024);
251
		g_snprintf(newname, strlen(recorder->filename)-strlen(rec_tempext), "%s", recorder->filename);
252
		if(rename(recorder->filename, newname) != 0) {
253
			JANUS_LOG(LOG_ERR, "Error renaming %s to %s...\n", recorder->filename, newname);
254
		} else {
255
			JANUS_LOG(LOG_INFO, "Recording renamed: %s\n", newname);
256
			g_free(recorder->filename);
257
			recorder->filename = g_strdup(newname);
258
		}
259
	}
211 260
	janus_mutex_unlock_nodebug(&recorder->mutex);
212 261
	return 0;
213 262
}

Also available in: Unified diff