summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/event.c
blob: fee0917ea7e154926dea8bf44910f4c9136b0a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* $Id$ */
/* 
 * Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com)
 *
 * 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 
 */
#include <pjmedia/event.h>
#include <pjmedia/errno.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/string.h>

#define THIS_FILE	"event.c"

#if 1
#   define TRACE_(x)	PJ_LOG(6,x)
#else
#   define TRACE_(x)
#endif

PJ_DEF(void) pjmedia_event_init( pjmedia_event *event,
                                 pjmedia_event_type type,
                                 const pj_timestamp *ts,
                                 const pjmedia_event_publisher *epub)
{
    pj_bzero(event, sizeof(*event));
    event->type = type;
    if (ts)
	event->timestamp.u64 = ts->u64;
    event->epub = epub;
    if (epub)
	event->epub_sig = epub->sig;
}

PJ_DEF(void) pjmedia_event_publisher_init(pjmedia_event_publisher *epub,
                                          pjmedia_obj_sig sig)
{
    pj_bzero(epub, sizeof(*epub));
    pj_list_init(&epub->subscription_list);
    epub->sig = sig;
}

PJ_DEF(void) pjmedia_event_subscription_init( pjmedia_event_subscription *esub,
                                              pjmedia_event_cb *cb,
                                              void *user_data)
{
    pj_bzero(esub, sizeof(*esub));
    esub->cb = cb;
    esub->user_data = user_data;
}

PJ_DEF(pj_bool_t)
pjmedia_event_publisher_has_sub(pjmedia_event_publisher *epub)
{
    PJ_ASSERT_RETURN(epub, PJ_FALSE);
    return epub->subscription_list.next &&
	    (!pj_list_empty(&epub->subscription_list));
}

PJ_DEF(pj_status_t) pjmedia_event_subscribe( pjmedia_event_publisher *epub,
                                             pjmedia_event_subscription *esub)
{
    PJ_ASSERT_RETURN(epub && esub && esub->cb, PJ_EINVAL);
    /* Must not currently subscribe to anything */
    PJ_ASSERT_RETURN(esub->subscribe_to == NULL, PJ_EINVALIDOP);

    pj_list_push_back(&epub->subscription_list, esub);
    esub->subscribe_to = epub;
    return PJ_SUCCESS;
}

PJ_DEF(pj_status_t) pjmedia_event_unsubscribe(pjmedia_event_subscription *esub)
{
    PJ_ASSERT_RETURN(esub, PJ_EINVAL);
    if (esub->subscribe_to) {
	PJ_ASSERT_RETURN(
	    pj_list_find_node(&esub->subscribe_to->subscription_list,
	                      esub)==esub, PJ_ENOTFOUND);
	pj_list_erase(esub);
	esub->subscribe_to = NULL;
    }
    return PJ_SUCCESS;
}

PJ_DEF(pj_status_t) pjmedia_event_publish( pjmedia_event_publisher *epub,
                                           pjmedia_event *event)
{
    pjmedia_event_subscription *esub;
    char event_name[5];
    char epub_name[5];
    pj_status_t err = PJ_SUCCESS;

    PJ_ASSERT_RETURN(epub && event, PJ_EINVAL);

    TRACE_((THIS_FILE, "Event %s is published by publisher %s",
		       pjmedia_fourcc_name(event->type, event_name),
		       pjmedia_fourcc_name(epub->sig, epub_name)));

    esub = epub->subscription_list.next;
    if (!esub)
	return err;

    while (esub != &epub->subscription_list) {
	pjmedia_event_subscription *next;
	pj_status_t status;

	/* just in case esub is destroyed in the callback */
	next = esub->next;

	status = (*esub->cb)(esub, event);
	if (status != PJ_SUCCESS && err == PJ_SUCCESS)
	    err = status;

	esub = next;
    }

    return err;
}

static pj_status_t republisher_cb(pjmedia_event_subscription *esub,
       				  pjmedia_event *event)
{
    return pjmedia_event_publish((pjmedia_event_publisher*)esub->user_data,
                                 event);
}

PJ_DEF(pj_status_t) pjmedia_event_republish(pjmedia_event_publisher *esrc,
                                             pjmedia_event_publisher *epub,
                                             pjmedia_event_subscription *esub)
{
    PJ_ASSERT_RETURN(esrc && epub && esub, PJ_EINVAL);

    pjmedia_event_subscription_init(esub, &republisher_cb, epub);
    return pjmedia_event_subscribe(esrc, esub);
}