Jack2  1.9.8
netjack_packet.h
1 
2 /*
3  * NetJack - Packet Handling functions
4  *
5  * used by the driver and the jacknet_client
6  *
7  * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
24  *
25  */
26 
27 #ifndef __JACK_NET_PACKET_H__
28 #define __JACK_NET_PACKET_H__
29 
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #endif
34 
35 #include <jack/jack.h>
36 #include <jack/types.h>
37 #include <jack/jslist.h>
38 #include <jack/midiport.h>
39 
40 // The Packet Header.
41 
42 #define CELT_MODE 1000 // Magic bitdepth value that indicates CELT compression
43 #define MASTER_FREEWHEELS 0x80000000
44 
46 
48  // General AutoConf Data
49  jack_nframes_t capture_channels_audio;
50  jack_nframes_t playback_channels_audio;
51  jack_nframes_t capture_channels_midi;
52  jack_nframes_t playback_channels_midi;
53  jack_nframes_t period_size;
54  jack_nframes_t sample_rate;
55 
56  // Transport Sync
57  jack_nframes_t sync_state;
58  jack_nframes_t transport_frame;
59  jack_nframes_t transport_state;
60 
61  // Packet loss Detection, and latency reduction
62  jack_nframes_t framecnt;
63  jack_nframes_t latency;
64 
65  jack_nframes_t reply_port;
66  jack_nframes_t mtu;
67  jack_nframes_t fragment_nr;
68  };
69 
70  typedef union _int_float int_float_t;
71 
72  union _int_float {
73  uint32_t i;
74  float f;
75  };
76 
77  // fragment reorder cache.
78  typedef struct _cache_packet cache_packet;
79 
80  struct _cache_packet {
81  int valid;
82  int num_fragments;
83  int packet_size;
84  int mtu;
85  jack_time_t recv_timestamp;
86  jack_nframes_t framecnt;
87  char * fragment_array;
88  char * packet_buf;
89  };
90 
91  typedef struct _packet_cache packet_cache;
92 
93  struct _packet_cache {
94  int size;
95  cache_packet *packets;
96  int mtu;
97  struct sockaddr_in master_address;
98  int master_address_valid;
99  jack_nframes_t last_framecnt_retreived;
100  int last_framecnt_retreived_valid;
101  };
102 
103  // fragment cache function prototypes
104  // XXX: Some of these are private.
105  packet_cache *packet_cache_new(int num_packets, int pkt_size, int mtu);
106  void packet_cache_free(packet_cache *pkt_cache);
107 
108  cache_packet *packet_cache_get_packet(packet_cache *pkt_cache, jack_nframes_t framecnt);
109  cache_packet *packet_cache_get_oldest_packet(packet_cache *pkt_cache);
110  cache_packet *packet_cache_get_free_packet(packet_cache *pkt_cache);
111 
112  void cache_packet_reset(cache_packet *pack);
113  void cache_packet_set_framecnt(cache_packet *pack, jack_nframes_t framecnt);
114  void cache_packet_add_fragment(cache_packet *pack, char *packet_buf, int rcv_len);
115  int cache_packet_is_complete(cache_packet *pack);
116 
117  void packet_cache_drain_socket( packet_cache *pcache, int sockfd );
118  void packet_cache_reset_master_address( packet_cache *pcache );
119  float packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt );
120  int packet_cache_retreive_packet_pointer( packet_cache *pcache, jack_nframes_t framecnt, char **packet_buf, int pkt_size, jack_time_t *timestamp );
121  int packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt );
122  int packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt );
123  int packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt );
124  int packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt );
125 
126  // Function Prototypes
127 
128  int netjack_poll_deadline (int sockfd, jack_time_t deadline);
129  void netjack_sendto(int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu);
130  int get_sample_size(int bitdepth);
131  void packet_header_hton(jacknet_packet_header *pkthdr);
132  void packet_header_ntoh(jacknet_packet_header *pkthdr);
133  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 );
134  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 );
135 
136  // XXX: This is sort of deprecated:
137  // This one waits forever. an is not using ppoll
138  int netjack_poll(int sockfd, int timeout);
139 
140  void decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf);
141  void encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 #endif
147