Revision 47576630 plugins/janus_audiobridge.c

View differences:

plugins/janus_audiobridge.c
541 541
		}
542 542
		const char *request_text = json_string_value(request);
543 543
		json_t *event = NULL;
544
		if(!strcasecmp(request_text, "join")) {
544
		if(!strcasecmp(request_text, "create")) {
545
			/* Create a new audiobridge */
546
			JANUS_PRINT("Creating a new audiobridge\n");
547
			json_t *desc = json_object_get(root, "description");
548
			if(desc && !json_is_string(desc)) {
549
				JANUS_DEBUG("JSON error: invalid element (desc)\n");
550
				sprintf(error_cause, "JSON error: invalid element (desc)");
551
				goto error;
552
			}
553
			json_t *sampling = json_object_get(root, "sampling");
554
			if(sampling && !json_is_integer(sampling)) {
555
				JANUS_DEBUG("JSON error: invalid element (sampling)\n");
556
				sprintf(error_cause, "JSON error: invalid element (sampling)");
557
				goto error;
558
			}
559
			json_t *record = json_object_get(root, "record");
560
			if(record && !json_is_boolean(record)) {
561
				JANUS_DEBUG("JSON error: invalid element (record)\n");
562
				sprintf(error_cause, "JSON error: invalid value (record)");
563
				goto error;
564
			}
565
			/* Create the audio bridge room */
566
			janus_audiobridge_room *audiobridge = calloc(1, sizeof(janus_audiobridge_room));
567
			if(audiobridge == NULL) {
568
				JANUS_DEBUG("Memory error!\n");
569
				sprintf(error_cause, "Memory error");
570
				goto error;
571
			}
572
			/* Generate a random ID */
573
			guint64 room_id = 0;
574
			while(room_id == 0) {
575
				room_id = g_random_int();
576
				if(g_hash_table_lookup(rooms, GUINT_TO_POINTER(room_id)) != NULL) {
577
					/* Room ID already taken, try another one */
578
					room_id = 0;
579
				}
580
			}
581
			audiobridge->room_id = room_id;
582
			char *description = NULL;
583
			if(desc != NULL) {
584
				description = g_strdup(json_string_value(desc));
585
			} else {
586
				char roomname[255];
587
				sprintf(roomname, "Room %"SCNu64"", audiobridge->room_id);
588
				description = g_strdup(roomname);
589
			}
590
			if(description == NULL) {
591
				JANUS_DEBUG("Memory error!\n");
592
				continue;
593
			}
594
			audiobridge->room_name = description;
595
			if(sampling)
596
				audiobridge->sampling_rate = json_integer_value(sampling);
597
			else
598
				audiobridge->sampling_rate = 16000;
599
			if(audiobridge->sampling_rate != 16000) {
600
				JANUS_DEBUG("We currently only support 16kHz (wideband) as a sampling rate for audio rooms, changing %"SCNu32" to 16000...\n", audiobridge->sampling_rate);
601
				audiobridge->sampling_rate = 16000;
602
			}
603
			audiobridge->record = FALSE;
604
			if(record && json_is_true(record))
605
				audiobridge->record = TRUE;
606
			audiobridge->recording = NULL;
607
			audiobridge->destroy = 0;
608
			audiobridge->participants = g_hash_table_new(NULL, NULL);
609
			janus_mutex_init(&audiobridge->mutex);
610
			g_hash_table_insert(rooms, GUINT_TO_POINTER(audiobridge->room_id), audiobridge);
611
			JANUS_PRINT("Created audiobridge: %"SCNu64" (%s)\n", audiobridge->room_id, audiobridge->room_name);
612
			/* We need a thread for the mix */
613
			g_thread_new("audiobridge mixer thread", &janus_audiobridge_mixer_thread, audiobridge);
614
			/* Show updated rooms list */
615
			GList *rooms_list = g_hash_table_get_values(rooms);
616
			GList *r = rooms_list;
617
			while(r) {
618
				janus_audiobridge_room *ar = (janus_audiobridge_room *)r->data;
619
				JANUS_PRINT("  ::: [%"SCNu64"][%s] %"SCNu32" (%s be recorded)\n",
620
					ar->room_id, ar->room_name, ar->sampling_rate, ar->record ? "will" : "will NOT");
621
				r = r->next;
622
			}
623
			g_list_free(rooms_list);
624
			/* Send info back */
625
			event = json_object();
626
			json_object_set(event, "audiobridge", json_string("created"));
627
			json_object_set(event, "room", json_integer(audiobridge->room_id));
628
		} else if(!strcasecmp(request_text, "join")) {
545 629
			JANUS_PRINT("Configuring new participant\n");
546 630
			json_t *room = json_object_get(root, "room");
547 631
			if(!room || !json_is_integer(room)) {

Also available in: Unified diff