Skip to content
Snippets Groups Projects
test_stream.c 72.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		return AST_TEST_FAIL;
    	}
    	ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
    	new_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
    	if (!new_cap) {
    		ast_test_status_update(test, "Could not allocate an empty format capabilities structure\n");
    		ast_stream_free(stream);
    		ast_stream_topology_free(topology);
    		return AST_TEST_FAIL;
    	}
    	if (ast_format_cap_append(new_cap, ast_format_alaw, 0)) {
    		ast_test_status_update(test, "Failed to append alaw format to capabilities\n");
    		ao2_cleanup(new_cap);
    		ast_stream_free(stream);
    		ast_stream_topology_free(topology);
    		return AST_TEST_FAIL;
    	}
    	ast_stream_set_formats(stream, new_cap);
    	ao2_cleanup(new_cap);
    	if (ast_stream_topology_append_stream(topology, stream) == -1) {
    		ast_test_status_update(test, "Failed to append a perfectly good stream to a topology\n");
    		ast_stream_free(stream);
    		ast_stream_topology_free(topology);
    		return AST_TEST_FAIL;
    	}
    
    
    	stream_caps = ast_format_cap_from_stream_topology(topology);
    	if (!stream_caps) {
    		ast_test_status_update(test, "Failed to create a format capabilities from a stream topology\n");
    		ast_stream_topology_free(topology);
    		return AST_TEST_FAIL;
    	}
    
    	ast_stream_topology_free(topology);
    
    	if (!ast_format_cap_identical(caps, stream_caps)) {
    		ast_test_status_update(test, "Converting format capabilities into topology and back resulted in different formats\n");
    		return AST_TEST_FAIL;
    	}
    
    	return AST_TEST_PASS;
    }
    
    
    #define topology_append_stream(topology, name, type, res, label) \
    	do { \
    		struct ast_stream *__stream = ast_stream_alloc((name), (type)); \
    		ast_test_validate_cleanup(test, __stream, res, label); \
    		if (ast_stream_topology_append_stream((topology), __stream) < 0) { \
    			ast_stream_free(__stream); \
    			res = AST_TEST_FAIL; \
    			goto label;	     \
    		} \
    	} while(0)
    
    AST_TEST_DEFINE(stream_topology_map_create)
    {
    	RAII_VAR(struct ast_stream_topology *, t0, NULL, ast_stream_topology_free);
    
    	struct ast_vector_int types = { NULL };
    	struct ast_vector_int v0 = { NULL };
    	struct ast_vector_int v1 = { NULL };
    
    	enum ast_test_result_state res = AST_TEST_PASS;
    
    	switch (cmd) {
    	case TEST_INIT:
    		info->name = "stream_topology_map_create";
    		info->category = "/main/stream/";
    		info->summary = "stream topology map creation unit test";
    		info->description =
    			"Test that creating a stream topology map works";
    		return AST_TEST_NOT_RUN;
    	case TEST_EXECUTE:
    		break;
    	}
    
    	ast_test_validate(test, AST_VECTOR_INIT(&types, 5) == 0);
    
    	/* Map a first topology and check that it mapped one to one */
    	ast_test_validate_cleanup(test, (t0 = ast_stream_topology_alloc()), res, done);
    	topology_append_stream(t0, "audio", AST_MEDIA_TYPE_AUDIO, res, done);
    	topology_append_stream(t0, "video", AST_MEDIA_TYPE_VIDEO, res, done);
    
    	ast_stream_topology_map(t0, &types, &v0, &v1);
    	ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&types) == 2, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&types, 0) == AST_MEDIA_TYPE_AUDIO, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&types, 1) == AST_MEDIA_TYPE_VIDEO, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v0, 0) == 0, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v0, 1) == 1, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v1, 0) == 0, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v1, 1) == 1, res, done);
    
    	/* Map a second topology and check that it merged */
    	ast_stream_topology_free(t0);
    	ast_test_validate_cleanup(test, (t0 = ast_stream_topology_alloc()), res, done);
    	topology_append_stream(t0, "video", AST_MEDIA_TYPE_VIDEO, res, done);
    	topology_append_stream(t0, "audio", AST_MEDIA_TYPE_AUDIO, res, done);
    
    	ast_stream_topology_map(t0, &types, &v0, &v1);
    	ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&types) == 2, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&types, 0) == AST_MEDIA_TYPE_AUDIO, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&types, 1) == AST_MEDIA_TYPE_VIDEO, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v0, 0) == 1, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v0, 1) == 0, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v1, 0) == 1, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v1, 1) == 0, res, done);
    
    	/* Map a third topology with more streams and check that it merged */
    	ast_stream_topology_free(t0);
    	ast_test_validate_cleanup(test, (t0 = ast_stream_topology_alloc()), res, done);
    	topology_append_stream(t0, "video", AST_MEDIA_TYPE_VIDEO, res, done);
    	topology_append_stream(t0, "audio", AST_MEDIA_TYPE_AUDIO, res, done);
    	topology_append_stream(t0, "audio", AST_MEDIA_TYPE_AUDIO, res, done);
    
    	ast_stream_topology_map(t0, &types, &v0, &v1);
    	ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&types) == 3, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&types, 0) == AST_MEDIA_TYPE_AUDIO, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&types, 1) == AST_MEDIA_TYPE_VIDEO, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&types, 2) == AST_MEDIA_TYPE_AUDIO, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v0, 0) == 1, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v0, 1) == 0, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v0, 2) == 2, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v1, 0) == 1, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v1, 1) == 0, res, done);
    	ast_test_validate_cleanup(test, AST_VECTOR_GET(&v1, 2) == 2, res, done);
    
    done:
    	AST_VECTOR_FREE(&v1);
    	AST_VECTOR_FREE(&v0);
    	AST_VECTOR_FREE(&types);
    
    	return res;
    }
    
    
    static int unload_module(void)
    {
    	AST_TEST_UNREGISTER(stream_create);
    	AST_TEST_UNREGISTER(stream_create_no_name);
    	AST_TEST_UNREGISTER(stream_set_type);
    	AST_TEST_UNREGISTER(stream_set_formats);
    	AST_TEST_UNREGISTER(stream_set_state);
    
    	AST_TEST_UNREGISTER(stream_topology_create);
    	AST_TEST_UNREGISTER(stream_topology_clone);
    	AST_TEST_UNREGISTER(stream_topology_clone);
    	AST_TEST_UNREGISTER(stream_topology_append_stream);
    	AST_TEST_UNREGISTER(stream_topology_set_stream);
    
    	AST_TEST_UNREGISTER(stream_topology_del_stream);
    
    	AST_TEST_UNREGISTER(stream_topology_create_from_format_cap);
    
    	AST_TEST_UNREGISTER(stream_topology_get_first_stream_by_type);
    	AST_TEST_UNREGISTER(stream_topology_create_from_channel_nativeformats);
    	AST_TEST_UNREGISTER(stream_topology_channel_set);
    
    	AST_TEST_UNREGISTER(stream_write_non_multistream);
    	AST_TEST_UNREGISTER(stream_write_multistream);
    
    	AST_TEST_UNREGISTER(stream_read_non_multistream);
    	AST_TEST_UNREGISTER(stream_read_multistream);
    
    	AST_TEST_UNREGISTER(stream_topology_change_request_from_application_non_multistream);
    	AST_TEST_UNREGISTER(stream_topology_change_request_from_channel_non_multistream);
    	AST_TEST_UNREGISTER(stream_topology_change_request_from_application);
    	AST_TEST_UNREGISTER(stream_topology_change_request_from_channel);
    
    	AST_TEST_UNREGISTER(format_cap_from_stream_topology);
    
    	AST_TEST_UNREGISTER(stream_topology_map_create);
    
    	return 0;
    }
    
    static int load_module(void)
    {
    	AST_TEST_REGISTER(stream_create);
    	AST_TEST_REGISTER(stream_create_no_name);
    	AST_TEST_REGISTER(stream_set_type);
    	AST_TEST_REGISTER(stream_set_formats);
    	AST_TEST_REGISTER(stream_set_state);
    
    	AST_TEST_REGISTER(stream_topology_create);
    	AST_TEST_REGISTER(stream_topology_clone);
    	AST_TEST_REGISTER(stream_topology_append_stream);
    	AST_TEST_REGISTER(stream_topology_set_stream);
    
    	AST_TEST_REGISTER(stream_topology_del_stream);
    
    	AST_TEST_REGISTER(stream_topology_create_from_format_cap);
    
    	AST_TEST_REGISTER(stream_topology_get_first_stream_by_type);
    	AST_TEST_REGISTER(stream_topology_create_from_channel_nativeformats);
    	AST_TEST_REGISTER(stream_topology_channel_set);
    
    	AST_TEST_REGISTER(stream_write_non_multistream);
    	AST_TEST_REGISTER(stream_write_multistream);
    
    	AST_TEST_REGISTER(stream_read_non_multistream);
    	AST_TEST_REGISTER(stream_read_multistream);
    
    	AST_TEST_REGISTER(stream_topology_change_request_from_application_non_multistream);
    	AST_TEST_REGISTER(stream_topology_change_request_from_channel_non_multistream);
    	AST_TEST_REGISTER(stream_topology_change_request_from_application);
    	AST_TEST_REGISTER(stream_topology_change_request_from_channel);
    
    	AST_TEST_REGISTER(format_cap_from_stream_topology);
    
    	AST_TEST_REGISTER(stream_topology_map_create);
    
    	return AST_MODULE_LOAD_SUCCESS;
    }
    
    AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Media Stream API test module");