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

Revision 399, 4.7 kB (checked in by pieterpalmers, 16 years ago)

- code cleanup
- introduce sync delay concept to fix latency issues due to intermediate ISO buffering
- made SytMonitor? use cycletimer.h functions

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_VERBOSE );
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    
61     if (syt_timestamp == 0xFFFF) {
62         return RAW1394_ISO_OK;
63     }
64    
65     uint32_t m_full_timestamp;
66     uint32_t m_full_timestamp_ticks;
67    
68     debugOutput(DEBUG_LEVEL_VERBOSE,"%2u,%2u: CY=%4u, SYT=%08X (%3u secs + %4u cycles + %04u ticks)\n",
69         m_port,channel,
70         cycle,syt_timestamp,
71         CYCLE_TIMER_GET_SECS(syt_timestamp),
72         CYCLE_TIMER_GET_CYCLES(syt_timestamp),
73         CYCLE_TIMER_GET_OFFSET(syt_timestamp)
74        
75         );
76    
77     // reconstruct the full cycle
78     unsigned int cc=m_handler->getCycleTimer();
79    
80     unsigned int cc_cycles=CYCLE_TIMER_GET_CYCLES(cc);
81    
82     unsigned int cc_seconds=CYCLE_TIMER_GET_SECS(cc);
83    
84    
85     // the cycletimer has wrapped since this packet was received
86     // we want cc_seconds to reflect the 'seconds' at the point this
87     // was received
88     if (cycle>cc_cycles) cc_seconds--;
89    
90     m_full_timestamp_ticks=sytRecvToFullTicks((uint32_t)syt_timestamp, cycle, cc);
91     m_full_timestamp=TICKS_TO_CYCLE_TIMER(m_full_timestamp_ticks);
92    
93     struct cycle_info cif;
94     cif.cycle=cycle;
95     cif.seconds=cc_seconds;
96    
97     cif.syt=packet->syt;
98     cif.full_syt=m_full_timestamp;
99    
100     // now we reconstruct the presentation time
101     cif.pres_seconds = CYCLE_TIMER_GET_SECS(m_full_timestamp);
102     cif.pres_cycle   = CYCLE_TIMER_GET_CYCLES(m_full_timestamp);
103     cif.pres_offset  = CYCLE_TIMER_GET_OFFSET(m_full_timestamp);
104     cif.pres_ticks   = m_full_timestamp_ticks;
105    
106     if (cif.pres_offset != (syt_timestamp & 0xFFF)) {
107         debugOutput(DEBUG_LEVEL_NORMAL,"P-offset error: %04X != %04X (P: %08X, R: %08X)\n",
108         cif.pres_offset, syt_timestamp & 0xFFF, m_full_timestamp, syt_timestamp);
109     }
110     if ((cif.pres_cycle & 0xF) != ((syt_timestamp & 0xF000)>>12)) {
111         debugOutput(DEBUG_LEVEL_NORMAL,"P-cycle error: %01X != %01X (P: %08X, R: %08X)\n",
112             cif.pres_cycle &0xF, (syt_timestamp & 0xF000)>>12, m_full_timestamp, syt_timestamp);
113     }
114    
115     putCycleInfo(&cif);
116
117     return retval;
118 }
119
120 bool SytMonitor::putCycleInfo(struct cycle_info *cif) {
121     if(freebob_ringbuffer_write(m_cinfo_buffer,(char *)cif,sizeof(struct cycle_info))
122         !=sizeof(struct cycle_info)) {
123         return false;
124     }
125     return true;   
126 }
127
128 bool SytMonitor::readNextCycleInfo(struct cycle_info *cif) {
129     if(freebob_ringbuffer_peek(m_cinfo_buffer,(char *)cif,sizeof(struct cycle_info))
130         !=sizeof(struct cycle_info)) {
131         return false;
132     }
133     return true;
134    
135 }
136
137 bool SytMonitor::consumeNextCycleInfo() {
138     struct cycle_info cif;
139     if(freebob_ringbuffer_read(m_cinfo_buffer,(char *)&cif,sizeof(struct cycle_info))
140         !=sizeof(struct cycle_info)) {
141         return false;
142     }
143     return true;
144 }
145
146 void SytMonitor::dumpInfo()
147 {
148     IsoStream::dumpInfo();
149         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Ringbuffer fill: %d\n",
150            freebob_ringbuffer_read_space(m_cinfo_buffer)/sizeof(struct cycle_info));
151 };
Note: See TracBrowser for help on using the browser.