Skip to content

Commit

Permalink
Build on windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
sobomax committed Jun 20, 2024
1 parent 6c1b1c3 commit 3fedac3
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 32 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ jobs:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
compiler: ['gcc', 'clang']
os: [macos, ubuntu]
include:
- python-version: '3.10'
compiler: microsoft
os: windows

runs-on: ${{ matrix.os }}-latest
env:
COMPILER: ${{ matrix.compiler }}
PYTHON_CMD: "python${{ matrix.python-version }}"

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand All @@ -49,17 +52,21 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install setuptools wheel
shell: bash

- name: build
run: CC=${COMPILER} LDSHARED="${COMPILER} -shared" ${PYTHON_CMD} setup.py build sdist
run: CC=${COMPILER} LDSHARED="${COMPILER} -shared" python setup.py build sdist
shell: bash

- name: install
run: pip install dist/[Rr]tp*.gz
shell: bash

- name: test
run: |
CC=${COMPILER} ${PYTHON_CMD} setup.py runctest
${PYTHON_CMD} tests/test_jbuf.py
CC=${COMPILER} python setup.py runctest
python tests/test_jbuf.py
shell: bash

publish_wheels:
needs: build_and_test_python
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include setup/__init__.py setup/RunCTest.py
include src/rsth_timeops.h src/rtp.h src/rtp_info.h src/rtpjbuf.h src/rtpsynth.h src/Symbol.map
include src/winnet.h python/RtpSynth_mod.c
include README.md
6 changes: 4 additions & 2 deletions python/RtpSynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@

from ctypes import cdll, c_void_p, c_int, create_string_buffer, c_uint
from math import modf
import sys, os, site, sysconfig
import sys, os, site
from sysconfig import get_config_var, get_platform

from .env import RSTH_MOD_NAME

_esuf = sysconfig.get_config_var('EXT_SUFFIX')
_esuf = get_config_var('EXT_SUFFIX')
if not _esuf:
_esuf = '.so'
try:
Expand All @@ -48,6 +49,7 @@
else:
_rsth = cdll.LoadLibrary('librtpsynth.so')

print(f'{dir(_rsth)=}')
_rsth.rsynth_ctor.argtypes = [c_int, c_int]
_rsth.rsynth_ctor.restype = c_void_p
_rsth.rsynth_dtor.argtypes = [c_void_p,]
Expand Down
75 changes: 75 additions & 0 deletions python/RtpSynth_mod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <assert.h>
#include <stdbool.h>

#include <Python.h>

#define MODULE_BASENAME _rtpsynth

#define CONCATENATE_DETAIL(x, y) x##y
#define CONCATENATE(x, y) CONCATENATE_DETAIL(x, y)

#if !defined(DEBUG_MOD)
#define MODULE_NAME MODULE_BASENAME
#else
#define MODULE_NAME CONCATENATE(MODULE_BASENAME, _debug)
#endif

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

#define MODULE_NAME_STR TOSTRING(MODULE_NAME)
#define PY_INIT_FUNC CONCATENATE(PyInit_, MODULE_NAME)

typedef struct {
PyObject_HEAD
} PyRtpSynth;

static int PyRtpSynth_init(PyRtpSynth* self, PyObject* args, PyObject* kwds) {

return 0;
}

// The __del__ method for PyRtpSynth objects
static void PyRtpSynth_dealloc(PyRtpSynth* self) {
Py_TYPE(self)->tp_free((PyObject*)self);
}

static PyMethodDef PyRtpSynth_methods[] = {
{NULL} // Sentinel
};

static PyTypeObject PyRtpSynthType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = MODULE_NAME_STR "." MODULE_NAME_STR,
.tp_doc = "[TODO]",
.tp_basicsize = sizeof(PyRtpSynth),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = (initproc)PyRtpSynth_init,
.tp_dealloc = (destructor)PyRtpSynth_dealloc,
.tp_methods = PyRtpSynth_methods,
};

static struct PyModuleDef RtpSynth_module = {
PyModuleDef_HEAD_INIT,
.m_name = MODULE_NAME_STR,
.m_doc = "Python interface XXX.",
.m_size = -1,
};

// Module initialization function
PyMODINIT_FUNC PY_INIT_FUNC(void) {
PyObject* module;
if (PyType_Ready(&PyRtpSynthType) < 0)
return NULL;

module = PyModule_Create(&RtpSynth_module);
if (module == NULL)
return NULL;

Py_INCREF(&PyRtpSynthType);
PyModule_AddObject(module, MODULE_NAME_STR, (PyObject*)&PyRtpSynthType);

return module;
}
20 changes: 15 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@
from python.env import RSTH_MOD_NAME
from setup.RunCTest import RunCTest

is_win = get_platform().startswith('win')
is_mac = get_platform().startswith('macosx-')

rs_srcs = ['src/rtpsynth.c', 'src/rtp.c', 'src/rtpjbuf.c']

extra_compile_args = ['--std=c11', '-Wno-zero-length-array', '-Wall', '-pedantic', '-flto']
extra_link_args = ['-flto']
if not is_mac:
rs_srcs.append('python/RtpSynth_mod.c')

extra_compile_args = ['-Wall']
if not is_win:
extra_compile_args += ['--std=c11', '-Wno-zero-length-array',
'-flto', '-pedantic']
extra_link_args = ['-flto'] if not is_win else []
debug_opts = (('-g3', '-O0'))
if not get_platform().startswith('macosx-'):

if not is_mac and not is_win:
nodebug_opts = (('-march=native', '-O3'))
else:
nodebug_opts = (('-O3',))
nodebug_opts = (('-O3',)) if not is_win else ()
if False:
extra_compile_args.extend(debug_opts)
extra_link_args.extend(debug_opts)
Expand All @@ -32,7 +42,7 @@
RunCTest.extra_link_args = extra_link_args.copy()
RunCTest.extra_compile_args = extra_compile_args

if not get_platform().startswith('macosx-'):
if not is_mac and not is_win:
extra_link_args.append('-Wl,--version-script=src/Symbol.map')

def get_ex_mod():
Expand Down
5 changes: 5 additions & 0 deletions src/rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#if !defined(_WIN32) && !defined(_WIN64)
#include <netinet/in.h>
#else
#include "winnet.h"
#endif
#include <assert.h>
#include <stddef.h>
#include <stdint.h>

#include "rtp.h"
#include "rtp_info.h"
Expand Down
15 changes: 11 additions & 4 deletions src/rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ typedef enum rtp_type rtp_type_t;
# endif
#endif

#ifdef _MSC_VER
# define PACKED_STRUCT(name) \
__pragma(pack(push, 1)) struct name __pragma(pack(pop))
#elif defined(__GNUC__)
# define PACKED_STRUCT(name) struct __attribute__((packed)) name
#endif

/*
* RTP data header
*/
struct rtp_hdr {
PACKED_STRUCT(rtp_hdr) {
#if BYTE_ORDER == BIG_ENDIAN
unsigned int version:2; /* protocol version */
unsigned int p:1; /* padding flag */
Expand All @@ -101,13 +108,13 @@ struct rtp_hdr {
uint32_t ts; /* timestamp */
uint32_t ssrc; /* synchronization source */
uint32_t csrc[0]; /* optional CSRC list */
} __attribute__((__packed__));
};

struct rtp_hdr_ext {
PACKED_STRUCT(rtp_hdr_ext) {
uint16_t profile; /* defined by profile */
uint16_t length; /* length of the following array in 32-byte words */
uint32_t extension[0]; /* actual extension data */
} __attribute__((__packed__));
};

#if !defined(rtp_hdr_t_DEFINED)
typedef struct rtp_hdr rtp_hdr_t;
Expand Down
8 changes: 8 additions & 0 deletions src/rtpjbuf.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#if !defined(_WIN32) && !defined(_WIN64)
#include <arpa/inet.h>
#else
#include "winnet.h"
#endif

#include <stdint.h>
#include <stdlib.h>
Expand All @@ -12,7 +16,11 @@
#define LMS_DEFAULT LRS_DEFAULT

#define BOOLVAL(x) (x)
#if !defined(_WIN32) && !defined(_WIN64)
#define x_unlikely(x) (__builtin_expect((x), 0), (x))
#else
#define x_unlikely(x) (x)
#endif

struct jitter_buffer {
struct rtp_frame *head;
Expand Down
64 changes: 55 additions & 9 deletions src/rtpsynth.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,35 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#if defined(_WIN32) || defined(_WIN64)
#define _CRT_RAND_S
#endif

#define _DEFAULT_SOURCE

#if !defined(_WIN32) && !defined(_WIN64)
#include <arpa/inet.h>
#else
#include "winnet.h"
#endif

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#if defined(_WIN32) || defined(_WIN64)
static long
random(void)
{
unsigned int r;

rand_s(&r);
return r;
}
#endif

#include "rtp.h"
#include "rtpsynth.h"
#include "rsth_timeops.h"
Expand All @@ -42,11 +61,38 @@ struct rsynth_inst {
int ptime;
struct rsynth_seq l;
int ts_inc;
struct rtp_hdr model;
struct timespec last_ts;
struct rtp_hdr model;
};

void *
#if defined(_WIN32) || defined(_WIN64)
#include <profileapi.h>

static int
clock_gettime_monotonic(struct timespec *tv)
{
static LARGE_INTEGER ticksPerSec;
LARGE_INTEGER ticks;

if (!ticksPerSec.QuadPart) {
QueryPerformanceFrequency(&ticksPerSec);
if (!ticksPerSec.QuadPart) {
errno = ENOTSUP;
return -1;
}
}

QueryPerformanceCounter(&ticks);

tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart);
tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * NSEC_IN_SEC) / ticksPerSec.QuadPart);

return 0;
}
#define clock_gettime(_, x) clock_gettime_monotonic(x)
#endif

EXPORT void *
rsynth_ctor(int srate, int ptime)
{
struct rsynth_inst *rip;
Expand All @@ -67,7 +113,7 @@ rsynth_ctor(int srate, int ptime)
return ((void *)rip);
}

int
EXPORT int
rsynth_next_pkt_pa(void *_rip, int plen, int pt, char *buf, unsigned int blen,
int filled)
{
Expand Down Expand Up @@ -101,7 +147,7 @@ rsynth_next_pkt_pa(void *_rip, int plen, int pt, char *buf, unsigned int blen,
return (rs);
}

void *
EXPORT void *
rsynth_next_pkt(void *_rip, int plen, int pt)
{
struct rsynth_inst *rip;
Expand All @@ -118,7 +164,7 @@ rsynth_next_pkt(void *_rip, int plen, int pt)
return (rnp);
}

void
EXPORT void
rsynth_skip(void *_rip, int npkts)
{
struct rsynth_inst *rip;
Expand All @@ -128,7 +174,7 @@ rsynth_skip(void *_rip, int npkts)
return;
}

unsigned int
EXPORT unsigned int
rsynth_set_mbt(void *_rip, unsigned int new_st)
{
struct rsynth_inst *rip;
Expand All @@ -140,7 +186,7 @@ rsynth_set_mbt(void *_rip, unsigned int new_st)
return (old_st);
}

void
EXPORT void
rsynth_resync(void *_rip, struct rsynth_seq *rsp)
{
struct timespec curr_ts;
Expand All @@ -155,14 +201,14 @@ rsynth_resync(void *_rip, struct rsynth_seq *rsp)
rip->l.ts += timespec2un64time(&curr_ts) * rip->srate / NSEC_IN_SEC;
}

void
EXPORT void
rsynth_pkt_free(void *rnp)
{

free(rnp);
}

void
EXPORT void
rsynth_dtor(void *_rip)
{
struct rsynth_inst *rip;
Expand Down
Loading

0 comments on commit 3fedac3

Please sign in to comment.