PacketHeader.cpp 3.88 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
//*****************************************************************
/*
jcaceres's avatar
jcaceres committed
3
  JackTrip: A System for High-Quality Audio Network Performance
jcaceres's avatar
jcaceres committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  over the Internet

  Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe.
  SoundWIRE group at CCRMA, Stanford University.
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/
//*****************************************************************

/**
 * \file PacketHeader.cpp
 * \author Juan-Pablo Caceres
 * \date July 2008
 */

#include "PacketHeader.h"
#include "JackAudioInterface.h"

jcaceres's avatar
jcaceres committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <sys/time.h>

#include <iostream>

using std::cout; using std::endl;



//#######################################################################
//####################### PacketHeader ##################################
//#######################################################################
uint64_t PacketHeader::usecTime()
{
  struct timeval tv;
  gettimeofday (&tv, NULL);
  return ( (tv.tv_sec * 1000000)  + // seconds
	   (tv.tv_usec) );  // plus the microseconds. Type suseconds_t, range [-1, 1000000]
}



jcaceres's avatar
jcaceres committed
62
63
64
65

//#######################################################################
//####################### DefaultHeader #################################
//#######################################################################
jcaceres's avatar
jcaceres committed
66
67
68
69
70
71
72
73
74
75
DefaultHeader::DefaultHeader()
{
  mHeader.TimeStamp = 0;
  mHeader.SeqNumber = 0;  
  mHeader.BufferSize = 0; 
  mHeader.SamplingRate = 0;
  mHeader.NumInChannels = 0;
  mHeader.NumOutChannels = 0;
}

jcaceres's avatar
jcaceres committed
76
77

//***********************************************************************
78
void DefaultHeader::fillHeaderCommonFromJack(const JackAudioInterface& JackAudio)
jcaceres's avatar
jcaceres committed
79
{
80
  mHeader.BufferSize = JackAudio.getBufferSizeInSamples();
jcaceres's avatar
jcaceres committed
81
82
83
84
85
  mHeader.SamplingRate = JackAudio.getSampleRateType ();
  mHeader.NumInChannels = JackAudio.getNumInputChannels();
  mHeader.NumOutChannels = JackAudio.getNumOutputChannels();
  mHeader.SeqNumber = 0;
  mHeader.TimeStamp = PacketHeader::usecTime();
jcaceres's avatar
jcaceres committed
86
87
  //cout << mHeader.TimeStamp << endl;
  //printHeader();
jcaceres's avatar
jcaceres committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
}


//***********************************************************************
void DefaultHeader::printHeader() const
{
  cout << "Default Packet Header:" << endl;
  cout << "Buffer Size               = " << static_cast<int>(mHeader.BufferSize) << endl;
  // Get the sample rate in Hz form the JackAudioInterface::samplingRateT
  int sample_rate = 
    JackAudioInterface::getSampleRateFromType
    ( static_cast<JackAudioInterface::samplingRateT>(mHeader.SamplingRate) );
  cout << "Sampling Rate             = " << sample_rate << endl;
  cout << "Number of Input Channels  = " << static_cast<int>(mHeader.NumInChannels) << endl;
  cout << "Number of Output Channels = " << static_cast<int>(mHeader.NumOutChannels) << endl;
  cout << "Sequence Number           = " << static_cast<int>(mHeader.SeqNumber) << endl;
  cout << "Time Stamp                = " << mHeader.TimeStamp << endl;
  cout << gPrintSeparator << endl;
  cout << sizeof(mHeader) << endl;

jcaceres's avatar
jcaceres committed
108
}