Skip to content
This repository was archived by the owner on Oct 3, 2022. It is now read-only.

update for c-toxcore 0.2.0+ API #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions examples/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ def on_call(self, fid, audio_enabled, video_enabled):
def on_call_state(self, fid, state):
print('call state:fn=%d, state=%d' % (fid, state))

def on_bit_rate_status(self, fid,
audio_bit_rate, video_bit_rate):
print('bit rate status: fn=%d, abr=%d, vbr=%d' %
(fid, audio_bit_rate, video_bit_rate))
def on_audio_bit_rate(self, fid, video_bit_rate):
print('audio bit rate status: fn=%d, abr=%d' %
(fid, audio_bit_rate))

def on_video_bit_rate(self, fid, video_bit_rate):
print('video bit rate status: fn=%d, vbr=%d' %
(fid, video_bit_rate))

def on_audio_receive_frame(self, fid, pcm, sample_count,
channels, sampling_rate):
Expand Down
65 changes: 49 additions & 16 deletions pytox/av.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ ToxAVCore_callback_call_state(ToxAV *toxAV, uint32_t friend_number, uint32_t sta
}

static void
ToxAVCore_callback_bit_rate_status(ToxAV *toxAV, uint32_t friend_number,
uint32_t audio_bit_rate, uint32_t video_bit_rate, void *self)
ToxAVCore_callback_audio_bit_rate(ToxAV *toxAV, uint32_t friend_number,
uint32_t audio_bit_rate, void *self)
{
PyObject_CallMethod((PyObject*)self, "on_bit_rate_status", "iii",
friend_number, audio_bit_rate, video_bit_rate);
PyObject_CallMethod((PyObject*)self, "on_audio_bit_rate", "ii",
friend_number, audio_bit_rate);
}

static void
ToxAVCore_callback_video_bit_rate(ToxAV *toxAV, uint32_t friend_number,
uint32_t video_bit_rate, void *self)
{
PyObject_CallMethod((PyObject*)self, "on_video_bit_rate", "ii",
friend_number, video_bit_rate);
}

static void
Expand Down Expand Up @@ -229,8 +237,8 @@ ToxAVCore_callback_video_receive_frame(ToxAV *toxAV, uint32_t friend_number, uin
* NOTE Compatibility with old toxav group calls TODO remove
*/
static void
ToxAVCore_callback_add_av_groupchat(/*Tox*/void *tox, int groupnumber, int peernumber, const int16_t *pcm,
unsigned int samples, uint8_t channels, unsigned int sample_rate, void *self)
ToxAVCore_callback_add_av_groupchat(/*Tox*/void *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t *pcm,
unsigned int samples, uint8_t channels, uint32_t sample_rate, void *self)
{
PyGILState_STATE gstate = PyGILState_Ensure();

Expand All @@ -248,8 +256,8 @@ ToxAVCore_callback_add_av_groupchat(/*Tox*/void *tox, int groupnumber, int peern
}

static void
ToxAVCore_callback_join_av_groupchat(/*Tox*/void *tox, int groupnumber, int peernumber, const int16_t *pcm,
unsigned int samples, uint8_t channels, unsigned int sample_rate, void *self)
ToxAVCore_callback_join_av_groupchat(/*Tox*/void *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t *pcm,
unsigned int samples, uint8_t channels, uint32_t sample_rate, void *self)
{
PyGILState_STATE gstate = PyGILState_Ensure();

Expand Down Expand Up @@ -298,7 +306,8 @@ static int init_helper(ToxAVCore *self, PyObject* args)

toxav_callback_call(self->av, ToxAVCore_callback_call, self);
toxav_callback_call_state(self->av, ToxAVCore_callback_call_state, self);
toxav_callback_bit_rate_status(self->av, ToxAVCore_callback_bit_rate_status, self);
toxav_callback_audio_bit_rate(self->av, ToxAVCore_callback_audio_bit_rate, self);
toxav_callback_video_bit_rate(self->av, ToxAVCore_callback_video_bit_rate, self);
toxav_callback_audio_receive_frame(self->av, ToxAVCore_callback_audio_receive_frame, self);
toxav_callback_video_receive_frame(self->av, ToxAVCore_callback_video_receive_frame, self);

Expand Down Expand Up @@ -387,20 +396,38 @@ ToxAVCore_call_control(ToxAVCore *self, PyObject* args)
}

static PyObject*
ToxAVCore_bit_rate_set(ToxAVCore *self, PyObject* args)
ToxAVCore_audio_set_bit_rate(ToxAVCore *self, PyObject* args)
{
uint32_t friend_number;
int32_t audio_bit_rate;

if (!PyArg_ParseTuple(args, "ii", &friend_number, &audio_bit_rate)) {
return NULL;
}

TOXAV_ERR_BIT_RATE_SET err = 0;
bool ret = toxav_audio_set_bit_rate(self->av, friend_number, audio_bit_rate, &err);
if (ret == false) {
PyErr_Format(ToxOpError, "toxav audio bit rate set error: %d", err);
return NULL;
}
return PyBool_FromLong(ret);
}

static PyObject*
ToxAVCore_video_set_bit_rate(ToxAVCore *self, PyObject* args)
{
uint32_t friend_number;
int32_t video_bit_rate;

if (!PyArg_ParseTuple(args, "iii", &friend_number, &audio_bit_rate, &video_bit_rate)) {
if (!PyArg_ParseTuple(args, "ii", &friend_number, &video_bit_rate)) {
return NULL;
}

TOXAV_ERR_BIT_RATE_SET err = 0;
bool ret = toxav_bit_rate_set(self->av, friend_number, audio_bit_rate, video_bit_rate, &err);
bool ret = toxav_video_set_bit_rate(self->av, friend_number, video_bit_rate, &err);
if (ret == false) {
PyErr_Format(ToxOpError, "toxav bit rate set error: %d", err);
PyErr_Format(ToxOpError, "toxav video bit rate set error: %d", err);
return NULL;
}
return PyBool_FromLong(ret);
Expand Down Expand Up @@ -581,9 +608,15 @@ static PyMethodDef ToxAVCore_methods[] = {
"Returns True on success.\n\n"
},
{
"bit_rate_set", (PyCFunction)ToxAVCore_bit_rate_set, METH_VARARGS,
"bit_rate_set(friend_number, audio_bit_rate, video_bit_rate)\n"
"Set the bit rate to be used in subsequent audio/video frames."
"audio_set_bit_rate", (PyCFunction)ToxAVCore_audio_set_bit_rate, METH_VARARGS,
"audio_set_bit_rate(friend_number, audio_bit_rate)\n"
"Set the bit rate to be used in subsequent audio frames."
"Returns True on success.\n\n"
},
{
"video_set_bit_rate", (PyCFunction)ToxAVCore_video_set_bit_rate, METH_VARARGS,
"video_set_bit_rate(friend_number, video_bit_rate)\n"
"Set the bit rate to be used in subsequent video frames."
"Returns True on success.\n\n"
},
{
Expand Down
41 changes: 20 additions & 21 deletions pytox/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ static void callback_conference_message(Tox *tox, uint32_t conference_number,
peer_number, type, message, length - (message[length - 1] == 0));
}

static void callback_conference_namelist_change(Tox *tox, uint32_t conference_number,
uint32_t peer_number, TOX_CONFERENCE_STATE_CHANGE change, void* self)
static void callback_conference_peer_name(Tox *tox, uint32_t conference_number,
uint32_t peer_number, const uint8_t *name, size_t length, void* self)
{
PyObject_CallMethod((PyObject*)self, "on_conference_namelist_change", "iii",
conference_number, peer_number, change);
PyObject_CallMethod((PyObject*)self, "on_conference_peer_name", "iis#",
conference_number, peer_number, name, length);
}

static void callback_conference_peer_list_changed(Tox *tox, uint32_t conference_number, void* self)
{
PyObject_CallMethod((PyObject*)self, "on_conference_peer_list_changed", "i", conference_number);
}

static void callback_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number,
Expand Down Expand Up @@ -271,7 +276,8 @@ static int init_helper(ToxCore* self, PyObject* args)
tox_callback_friend_connection_status(tox, callback_friend_connection_status);
tox_callback_conference_invite(tox, callback_conference_invite);
tox_callback_conference_message(tox, callback_conference_message);
tox_callback_conference_namelist_change(tox, callback_conference_namelist_change);
tox_callback_conference_peer_name(tox, callback_conference_peer_name);
tox_callback_conference_peer_list_changed(tox, callback_conference_peer_list_changed);
tox_callback_file_chunk_request(tox, callback_file_chunk_request);
tox_callback_file_recv_control(tox, callback_file_recv_control);
tox_callback_file_recv(tox, callback_file_recv);
Expand Down Expand Up @@ -1507,20 +1513,16 @@ static PyMethodDef Tox_methods[] = {
"nothing."
},
{
"on_conference_namelist_change", (PyCFunction)ToxCore_callback_stub, METH_VARARGS,
"on_conference_namelist_change(conference_number, peer_number, change)\n"
"on_conference_peer_name", (PyCFunction)ToxCore_callback_stub, METH_VARARGS,
"on_conference_peer_name(conference_number, peer_number, peer_name)\n"
"Callback for receiving conference peer name changes, default implementation does "
"nothing.\n"
},
{
"on_conference_peer_list_changed", (PyCFunction)ToxCore_callback_stub, METH_VARARGS,
"on_conference_peer_list_changed(conference_number)\n"
"Callback for joins/parts/name changes, default implementation does "
"nothing.\n\n"
"There are there possible *change* values:\n\n"
"+----------------------------------------------+----------------------+\n"
"| change | description |\n"
"+==============================================+======================+\n"
"| Tox.CONFERENCE_STATE_CHANGE_PEER_JOIN | a peer is added |\n"
"+----------------------------------------------+----------------------+\n"
"| Tox.CONFERENCE_STATE_CHANGE_PEER_EXIT | a peer is deleted |\n"
"+----------------------------------------------+----------------------+\n"
"| Tox.CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE | name of peer changed |\n"
"+----------------------------------------------+----------------------+\n"
"nothing.\n"
},
{
"on_file_recv", (PyCFunction)ToxCore_callback_stub, METH_VARARGS,
Expand Down Expand Up @@ -1938,9 +1940,6 @@ void ToxCore_install_dict()
SET(USER_STATUS_NONE)
SET(USER_STATUS_AWAY)
SET(USER_STATUS_BUSY)
SET(CONFERENCE_STATE_CHANGE_PEER_JOIN)
SET(CONFERENCE_STATE_CHANGE_PEER_EXIT)
SET(CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE)
SET(FILE_KIND_DATA)
SET(FILE_KIND_AVATAR)
SET(FILE_CONTROL_RESUME)
Expand Down
8 changes: 1 addition & 7 deletions pytox/pytox.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
#include "core.h"
#include "util.h"

#ifdef ENABLE_AV
#include "av.h"
#endif
#include "av.h"

#if PY_MAJOR_VERSION >= 3
struct PyModuleDef moduledef = {
Expand Down Expand Up @@ -60,9 +58,7 @@ PyMODINIT_FUNC initpytox(void)
}

ToxCore_install_dict();
#ifdef ENABLE_AV
ToxAVCore_install_dict();
#endif

/* Initialize toxcore */
if (PyType_Ready(&ToxCoreType) < 0) {
Expand All @@ -76,7 +72,6 @@ PyMODINIT_FUNC initpytox(void)
ToxOpError = PyErr_NewException("pytox.OperationFailedError", NULL, NULL);
PyModule_AddObject(m, "OperationFailedError", (PyObject*)ToxOpError);

#ifdef ENABLE_AV
/* Initialize toxav */
if (PyType_Ready(&ToxAVCoreType) < 0) {
fprintf(stderr, "Invalid PyTypeObject `ToxAVCoreType'\n");
Expand All @@ -85,7 +80,6 @@ PyMODINIT_FUNC initpytox(void)

Py_INCREF(&ToxAVCoreType);
PyModule_AddObject(m, "ToxAV", (PyObject*)&ToxAVCoreType);
#endif

#if PY_MAJOR_VERSION >= 3
return m;
Expand Down
20 changes: 4 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
from subprocess import Popen, PIPE


def supports_av():
h = Popen("ld $LDFLAGS -ltoxav", shell=True, stderr=PIPE)
out, err = h.communicate()
return 'toxav' not in str(err)

sources = ["pytox/pytox.c", "pytox/core.c", "pytox/util.c"]
sources = ["pytox/pytox.c", "pytox/core.c", "pytox/av.c", "pytox/util.c"]
libraries = [
"opus",
"sodium",
"toxcore",
# "toxcrypto",
# "toxdht",
"toxdns",
"toxencryptsave",
# "toxdns",
# "toxencryptsave",
# "toxfriends",
# "toxgroup",
# "toxmessenger",
Expand All @@ -33,16 +28,9 @@ def supports_av():
"-fno-strict-aliasing",
]

if supports_av():
libraries.append("toxav")
sources.append("pytox/av.c")
cflags.append("-DENABLE_AV")
else:
print("Warning: AV support not found, disabled.")

setup(
name="PyTox",
version="0.0.23",
version="0.0.24",
description='Python binding for Tox the skype replacement',
author='Wei-Ning Huang (AZ)',
author_email='[email protected]',
Expand Down
18 changes: 8 additions & 10 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def test_conference(self):
t:conference_join
t:on_conference_invite
t:on_conference_message
t:on_conference_namelist_change
t:on_conference_peer_list_changed
"""
self.bob_add_alice_as_friend()

Expand All @@ -530,12 +530,11 @@ def on_conference_invite(self, fid, type_, data):

AliceTox.on_conference_invite = on_conference_invite

def on_conference_namelist_change(self, gid, peer_number, change):
def on_conference_peer_list_changed(self, gid):
assert gid == group_id
assert change == Tox.CONFERENCE_STATE_CHANGE_PEER_JOIN
self.gn = True

AliceTox.on_conference_namelist_change = on_conference_namelist_change
AliceTox.on_conference_peer_list_changed = on_conference_peer_list_changed

self.alice.gi = False
self.alice.gn = False
Expand All @@ -545,7 +544,7 @@ def on_conference_namelist_change(self, gid, peer_number, change):
assert self.wait_callbacks(self.alice, ['gi', 'gn'])

AliceTox.on_conference_invite = Tox.on_conference_invite
AliceTox.on_conference_namelist_change = Tox.on_conference_namelist_change
AliceTox.on_conference_peer_list_change = Tox.on_conference_peer_list_changed

#: Test group number of peers
self.loop(50)
Expand All @@ -555,15 +554,14 @@ def on_conference_namelist_change(self, gid, peer_number, change):
self.alice.self_set_name('Alice')
self.bob.self_set_name('Bob')

def on_conference_namelist_change(self, gid, peer_number, change):
if change == Tox.CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE:
self.gn = True
def on_conference_peer_list_changed(self, gid):
self.gn = True

AliceTox.on_conference_namelist_change = on_conference_namelist_change
AliceTox.on_conference_peer_list_changed = on_conference_peer_list_changed
self.alice.gn = False

assert self.wait_callback(self.alice, 'gn')
AliceTox.on_conference_namelist_change = Tox.on_conference_namelist_change
AliceTox.on_conference_peer_list_changed = Tox.on_conference_peer_list_changed

peernames = [self.bob.conference_peer_get_name(group_id, i) for i in
range(self.bob.conference_peer_count(group_id))]
Expand Down