Jack2 1.9.10

netjack_packet.h

00001 
00002 /*
00003  * NetJack - Packet Handling functions
00004  *
00005  * used by the driver and the jacknet_client
00006  *
00007  * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022  *
00023  * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
00024  *
00025  */
00026 
00027 #ifndef __JACK_NET_PACKET_H__
00028 #define __JACK_NET_PACKET_H__
00029 
00030 #ifdef __cplusplus
00031 extern "C"
00032 {
00033 #endif
00034 
00035 #include <jack/jack.h>
00036 #include <jack/types.h>
00037 #include <jack/jslist.h>
00038 #include <jack/midiport.h>
00039 
00040 // The Packet Header.
00041 
00042 #define CELT_MODE 1000   // Magic bitdepth value that indicates CELT compression
00043 #define OPUS_MODE  999   // Magic bitdepth value that indicates OPUS compression
00044 #define MASTER_FREEWHEELS 0x80000000
00045 
00046     typedef struct _jacknet_packet_header jacknet_packet_header;
00047 
00048     struct _jacknet_packet_header {
00049         // General AutoConf Data
00050         jack_nframes_t capture_channels_audio;
00051         jack_nframes_t playback_channels_audio;
00052         jack_nframes_t capture_channels_midi;
00053         jack_nframes_t playback_channels_midi;
00054         jack_nframes_t period_size;
00055         jack_nframes_t sample_rate;
00056 
00057         // Transport Sync
00058         jack_nframes_t sync_state;
00059         jack_nframes_t transport_frame;
00060         jack_nframes_t transport_state;
00061 
00062         // Packet loss Detection, and latency reduction
00063         jack_nframes_t framecnt;
00064         jack_nframes_t latency;
00065 
00066         jack_nframes_t reply_port;
00067         jack_nframes_t mtu;
00068         jack_nframes_t fragment_nr;
00069     };
00070 
00071     typedef union _int_float int_float_t;
00072 
00073     union _int_float {
00074         uint32_t i;
00075         float    f;
00076     };
00077 
00078     // fragment reorder cache.
00079     typedef struct _cache_packet cache_packet;
00080 
00081     struct _cache_packet {
00082         int                 valid;
00083         int                 num_fragments;
00084         int                 packet_size;
00085         int                 mtu;
00086         jack_time_t         recv_timestamp;
00087         jack_nframes_t  framecnt;
00088         char *      fragment_array;
00089         char *      packet_buf;
00090     };
00091 
00092     typedef struct _packet_cache packet_cache;
00093 
00094     struct _packet_cache {
00095         int size;
00096         cache_packet *packets;
00097         int mtu;
00098         struct sockaddr_in master_address;
00099         int master_address_valid;
00100         jack_nframes_t last_framecnt_retreived;
00101         int last_framecnt_retreived_valid;
00102     };
00103 
00104     // fragment cache function prototypes
00105     // XXX: Some of these are private.
00106     packet_cache *packet_cache_new(int num_packets, int pkt_size, int mtu);
00107     void              packet_cache_free(packet_cache *pkt_cache);
00108 
00109     cache_packet *packet_cache_get_packet(packet_cache *pkt_cache, jack_nframes_t framecnt);
00110     cache_packet *packet_cache_get_oldest_packet(packet_cache *pkt_cache);
00111     cache_packet *packet_cache_get_free_packet(packet_cache *pkt_cache);
00112 
00113     void        cache_packet_reset(cache_packet *pack);
00114     void        cache_packet_set_framecnt(cache_packet *pack, jack_nframes_t framecnt);
00115     void        cache_packet_add_fragment(cache_packet *pack, char *packet_buf, int rcv_len);
00116     int cache_packet_is_complete(cache_packet *pack);
00117 
00118     void packet_cache_drain_socket( packet_cache *pcache, int sockfd );
00119     void packet_cache_reset_master_address( packet_cache *pcache );
00120     float packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt );
00121     int packet_cache_retreive_packet_pointer( packet_cache *pcache, jack_nframes_t framecnt, char **packet_buf, int pkt_size, jack_time_t *timestamp );
00122     int packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt );
00123     int packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt );
00124     int packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt );
00125     int packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt );
00126 
00127     // Function Prototypes
00128 
00129     int netjack_poll_deadline (int sockfd, jack_time_t deadline);
00130     void netjack_sendto(int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu);
00131     int get_sample_size(int bitdepth);
00132     void packet_header_hton(jacknet_packet_header *pkthdr);
00133     void packet_header_ntoh(jacknet_packet_header *pkthdr);
00134     void render_payload_to_jack_ports(int bitdepth, void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes, int dont_htonl_floats );
00135     void render_jack_ports_to_payload(int bitdepth, JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up, int dont_htonl_floats );
00136 
00137     // XXX: This is sort of deprecated:
00138     //      This one waits forever. an is not using ppoll
00139     int netjack_poll(int sockfd, int timeout);
00140 
00141     void decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf);
00142     void encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf);
00143 
00144 #ifdef __cplusplus
00145 }
00146 #endif
00147 #endif
00148