root/branches/streaming-rework/tests/SytMonitor.cpp

Revision 384, 6.1 kB (checked in by pieterpalmers, 17 years ago)

- temporary commit as backup measure
- rewrote synchronisation code
- receive streaming based on SYT works
- transmit streaming synced to received stream sort of works, still

have to iron out some issues.

NOTE: all devices but the bebob's are disabled in this code,

because they still have to be ported to the new sync
mechanism.

Line 
1 /* $Id$ */
2
3 /*
4  *   FreeBob Streaming API
5  *   FreeBob = Firewire (pro-)audio for linux
6  *
7  *   http://freebob.sf.net
8  *
9  *   Copyright (C) 2005,2006,2007 Pieter Palmers <pieterpalmers@users.sourceforge.net>
10  *
11  *   This program is free software {} you can redistribute it and/or modify
12  *   it under the terms of the GNU General Public License as published by
13  *   the Free Software Foundation {} either version 2 of the License, or
14  *   (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY {} without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with this program {} if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  *
26  *
27  */
28
29 #include "SytMonitor.h"
30 #include "src/libstreaming/IsoStream.h"
31
32 #include <netinet/in.h>
33 #include <assert.h>
34
35
36 IMPL_DEBUG_MODULE( SytMonitor, SytMonitor, DEBUG_LEVEL_NORMAL );
37
38
39 /* --------------------- RECEIVE ----------------------- */
40
41 SytMonitor::SytMonitor(int port)
42     : IsoStream(IsoStream::EST_Receive, port) {
43     m_cinfo_buffer=freebob_ringbuffer_create(16384*sizeof(struct cycle_info));
44
45 }
46
47 SytMonitor::~SytMonitor() {
48         freebob_ringbuffer_free(m_cinfo_buffer);
49
50 }
51
52 enum raw1394_iso_disposition
53 SytMonitor::putPacket(unsigned char *data, unsigned int length,
54                   unsigned char channel, unsigned char tag, unsigned char sy,
55                   unsigned int cycle, unsigned int dropped) {
56     enum raw1394_iso_disposition retval=RAW1394_ISO_OK;
57    
58     struct iec61883_packet *packet = (struct iec61883_packet *) data;
59     unsigned int syt_timestamp=ntohs(packet->syt);
60     unsigned int m_full_timestamp;
61    
62     bool wraparound_occurred=false;
63     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"%2u,%2u: CY=%4u, SYT=%08X (%3u secs + %4u cycles + %04u ticks)\n",
64         m_port,channel,
65         cycle,syt_timestamp,
66         CYCLE_TIMER_GET_SECS(syt_timestamp),
67         CYCLE_TIMER_GET_CYCLES(syt_timestamp),
68         CYCLE_TIMER_GET_OFFSET(syt_timestamp)
69        
70         );
71    
72     // reconstruct the full cycle
73     unsigned int cc=m_handler->getCycleTimer();
74     unsigned int cc_ticks=CYCLE_TIMER_TO_TICKS(cc);
75    
76     unsigned int cc_cycles=CYCLE_TIMER_GET_CYCLES(cc);
77    
78     unsigned int cc_seconds=CYCLE_TIMER_GET_SECS(cc);
79    
80    
81     // the cycletimer has wrapped since this packet was received
82     // we want cc_seconds to reflect the 'seconds' at the point this
83     // was received
84     if (cycle>cc_cycles) cc_seconds--;
85
86      // reconstruct the top part of the timestamp using the current cycle number
87     unsigned int now_cycle_masked=cycle & 0xF;
88     unsigned int syt_cycle=CYCLE_TIMER_GET_CYCLES(syt_timestamp);
89    
90     // if this is true, wraparound has occurred, undo this wraparound
91     if(syt_cycle<now_cycle_masked) syt_cycle += 0x10;
92    
93     // this is the difference in cycles wrt the cycle the
94     // timestamp was received
95     unsigned int delta_cycles=syt_cycle-now_cycle_masked;
96    
97     // reconstruct the cycle part of the timestamp
98     unsigned int new_cycles=cycle + delta_cycles;
99    
100     // if the cycles cause a wraparound of the cycle timer,
101     // perform this wraparound
102     if(new_cycles>7999) {
103         debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
104             "Detected wraparound: %d + %d = %d\n",
105             cycle,delta_cycles,new_cycles);
106        
107         new_cycles-=8000; // wrap around
108         wraparound_occurred=true;
109        
110     }
111    
112     m_full_timestamp = (new_cycles) << 12;
113    
114     // now add the offset part on top of that
115     m_full_timestamp |= (syt_timestamp & 0xFFF);
116    
117     // and the seconds part
118     // if the timestamp causes a wraparound of the cycle timer,
119     // the timestamp lies in the next second
120     // if it didn't, the timestamp lies in this second
121     if (wraparound_occurred) {
122         m_full_timestamp |= ((cc_seconds+1 << 25) & 0xFE000000);
123     } else {
124         m_full_timestamp |= ((cc_seconds << 25) & 0xFE000000);
125     }
126
127     struct cycle_info cif;
128     cif.cycle=cycle;
129     cif.seconds=cc_seconds;
130    
131     cif.syt=packet->syt;
132     cif.full_syt=m_full_timestamp;
133    
134     // now we reconstruct the presentation time
135     cif.pres_seconds = CYCLE_TIMER_GET_SECS(m_full_timestamp);
136     cif.pres_cycle   = CYCLE_TIMER_GET_CYCLES(m_full_timestamp);
137     cif.pres_offset  = CYCLE_TIMER_GET_OFFSET(m_full_timestamp);
138     cif.pres_ticks   = CYCLE_TIMER_TO_TICKS(m_full_timestamp);
139    
140     if (wraparound_occurred) {
141         debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"    (P: %08X, R: %08X)\n",
142             m_full_timestamp, syt_timestamp);
143     }
144    
145     if (cif.pres_offset != (syt_timestamp & 0xFFF)) {
146         debugOutput(DEBUG_LEVEL_NORMAL,"P-offset error: %04X != %04X (P: %08X, R: %08X)\n",
147         cif.pres_offset, syt_timestamp & 0xFFF, m_full_timestamp, syt_timestamp);
148     }
149     if ((cif.pres_cycle & 0xF) != ((syt_timestamp & 0xF000)>>12)) {
150         debugOutput(DEBUG_LEVEL_NORMAL,"P-cycle error: %01X != %01X (P: %08X, R: %08X)\n",
151             cif.pres_cycle &0xF, (syt_timestamp & 0xF000)>>12, m_full_timestamp, syt_timestamp);
152     }
153    
154     putCycleInfo(&cif);
155
156     return retval;
157 }
158
159 bool SytMonitor::putCycleInfo(struct cycle_info *cif) {
160     if(freebob_ringbuffer_write(m_cinfo_buffer,(char *)cif,sizeof(struct cycle_info))
161         !=sizeof(struct cycle_info)) {
162         return false;
163     }
164     return true;   
165 }
166
167 bool SytMonitor::readNextCycleInfo(struct cycle_info *cif) {
168     if(freebob_ringbuffer_peek(m_cinfo_buffer,(char *)cif,sizeof(struct cycle_info))
169         !=sizeof(struct cycle_info)) {
170         return false;
171     }
172     return true;
173    
174 }
175
176 bool SytMonitor::consumeNextCycleInfo() {
177     struct cycle_info cif;
178     if(freebob_ringbuffer_read(m_cinfo_buffer,(char *)&cif,sizeof(struct cycle_info))
179         !=sizeof(struct cycle_info)) {
180         return false;
181     }
182     return true;
183 }
184
185 void SytMonitor::dumpInfo()
186 {
187     IsoStream::dumpInfo();
188         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Ringbuffer fill: %d\n",
189            freebob_ringbuffer_read_space(m_cinfo_buffer)/sizeof(struct cycle_info));
190 };
Note: See TracBrowser for help on using the browser.