mswebcam.c 3.72 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

Simon Morlat's avatar
Simon Morlat committed
24
#include "mediastreamer2/mswebcam.h"
aymeric's avatar
aymeric committed
25 26 27 28 29
#include "mediastreamer2/msfilter.h"

static MSWebCamManager *scm=NULL;

static MSWebCamManager * create_manager(){
30
	MSWebCamManager *obj=(MSWebCamManager *)ms_new0(MSWebCamManager,1);
aymeric's avatar
aymeric committed
31 32 33 34 35 36 37
	return obj;
}

void ms_web_cam_manager_destroy(void){
	if (scm!=NULL){
		ms_list_for_each(scm->cams,(void (*)(void*))ms_web_cam_destroy);
		ms_list_free(scm->cams);
38
		ms_list_free(scm->descs);
39
		ms_free(scm);
aymeric's avatar
aymeric committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	}
	scm=NULL;
}

MSWebCamManager * ms_web_cam_manager_get(void){
	if (scm==NULL) scm=create_manager();
	return scm;
}

MSWebCam * ms_web_cam_manager_get_cam(MSWebCamManager *m, const char *id){
	MSList *elem;
	for (elem=m->cams;elem!=NULL;elem=elem->next){
		MSWebCam *cam=(MSWebCam*)elem->data;
		if (id==NULL) return cam;
		if (strcmp(ms_web_cam_get_string_id(cam),id)==0)	return cam;
	}
	if (id!=NULL) ms_warning("no camera with id %s",id);
	return NULL;
}

MSWebCam * ms_web_cam_manager_get_default_cam(MSWebCamManager *m){
61 62 63
	if (m->cams!=NULL)
  		return (MSWebCam*)m->cams->data;
  	return NULL;
aymeric's avatar
aymeric committed
64 65 66 67 68 69 70 71 72 73 74
}

const MSList * ms_web_cam_manager_get_list(MSWebCamManager *m){
	return m->cams;
}

void ms_web_cam_manager_add_cam(MSWebCamManager *m, MSWebCam *c){
	ms_message("Webcam %s added",ms_web_cam_get_string_id(c));
	m->cams=ms_list_append(m->cams,c);
}

smorlat's avatar
smorlat committed
75 76 77 78 79
void ms_web_cam_manager_prepend_cam(MSWebCamManager *m, MSWebCam *c){
	ms_message("Webcam %s prepended",ms_web_cam_get_string_id(c));
	m->cams=ms_list_prepend(m->cams,c);
}

80
static void cam_detect(MSWebCamManager *m, MSWebCamDesc *desc){
aymeric's avatar
aymeric committed
81 82 83 84
	if (desc->detect!=NULL)
		desc->detect(m);
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98
void ms_web_cam_manager_register_desc(MSWebCamManager *m, MSWebCamDesc *desc){
	m->descs=ms_list_append(m->descs,desc);
	cam_detect(m,desc);
}

void ms_web_cam_manager_reload(MSWebCamManager *m){
	MSList *elem;
	ms_list_for_each(m->cams,(void (*)(void*))ms_web_cam_destroy);
	ms_list_free(m->cams);
	m->cams=NULL;
	for(elem=m->descs;elem!=NULL;elem=elem->next)
		cam_detect(m,(MSWebCamDesc*)elem->data);
}

aymeric's avatar
aymeric committed
99
MSWebCam * ms_web_cam_new(MSWebCamDesc *desc){
100
	MSWebCam *obj=(MSWebCam *)ms_new0(MSWebCam,1);
aymeric's avatar
aymeric committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	obj->desc=desc;
	if (desc->init!=NULL)
		desc->init(obj);
	return obj;
}

const char *ms_web_cam_get_driver_type(const MSWebCam *obj){
	return obj->desc->driver_type;
}

const char *ms_web_cam_get_name(const MSWebCam *obj){
	return obj->name;
}

const char *ms_web_cam_get_string_id(MSWebCam *obj){
	if (obj->id==NULL)	obj->id=ms_strdup_printf("%s: %s",obj->desc->driver_type,obj->name);
	return obj->id;
}

struct _MSFilter * ms_web_cam_create_reader(MSWebCam *obj){
	if (obj->desc->create_reader!=NULL)
		return obj->desc->create_reader(obj);
	else ms_warning("ms_web_cam_create_reader: unimplemented by %s wrapper",obj->desc->driver_type);
	return NULL;
}

void ms_web_cam_destroy(MSWebCam *obj){
	if (obj->desc->uninit!=NULL) obj->desc->uninit(obj);
	if (obj->name!=NULL) ms_free(obj->name);
	if (obj->id!=NULL)	ms_free(obj->id);
	ms_free(obj);
}