PacketHeader.h 4.99 KB
Newer Older
1
2
//*****************************************************************
/*
jcaceres's avatar
jcaceres committed
3
  JackTrip: A System for High-Quality Audio Network Performance
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
  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.h
 * \author Juan-Pablo Caceres
jcaceres's avatar
jcaceres committed
35
 * \date July 2008
36
37
38
39
40
 */

#ifndef __PACKETHEADER_H__
#define __PACKETHEADER_H__

jcaceres's avatar
jcaceres committed
41
42
#include <iostream>
#include <tr1/memory> // for shared_ptr
jcaceres's avatar
jcaceres committed
43

44
#include "types.h"
jcaceres's avatar
jcaceres committed
45
#include "globals.h"
jcaceres's avatar
jcaceres committed
46
class JackAudioInterface; // Forward Declaration
47
48


49
//---------JAMLINK HEADER DRAFT----------------------------
50
51
52
53
54
55
56
struct JamLinkHeaderStuct
{
  // watch out for alignment -- need to be on 4 byte chunks
  unsigned short i_head;
  unsigned short seqnum;
  unsigned int  timeStamp;
};
57
//---------------------------------------------------------
58
59


jcaceres's avatar
jcaceres committed
60
61
62
63
//#######################################################################
//####################### PacketHeader ##################################
//#######################################################################
/** \brief Base class for header type. Subclass this struct to
64
65
66
67
68
69
70
 * create a new header.
 */
class PacketHeader
{
public:
  PacketHeader() {};
  virtual ~PacketHeader() {};
jcaceres's avatar
jcaceres committed
71
72
73
74
75
76
  
  /** \brief Return a time stamp in microseconds
   * \return Time stamp: microseconds since midnight (0 hour), January 1, 1970
   */
  static uint64_t usecTime();

77
78
  /// \todo Implement this using a JackTrip Method (Mediator) member instead of the 
  /// reference to JackAudio
79
  virtual void fillHeaderCommonFromJack(const JackAudioInterface& JackAudio) = 0;
jcaceres's avatar
jcaceres committed
80
81
  virtual void parseHeader() = 0;
  virtual void increaseSequenceNumber() = 0;
82
83
  virtual int getHeaderSizeInBytes() const = 0;
  virtual void putHeaderInPacket(int8_t* full_packet) = 0;
84
85
86
};


jcaceres's avatar
jcaceres committed
87
88
89
90
91


//#######################################################################
//####################### DefaultHeader #################################
//#######################################################################
92
93
94
95
96
/** \brief Default Header
 */
class DefaultHeader : public PacketHeader
{
public:
jcaceres's avatar
jcaceres committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  //----------STRUCT-----------------------------------------
  /// \brief Default Header Struct
  struct DefaultHeaderStruct
  {
    // watch out for alignment...
    uint64_t TimeStamp; ///< Time Stamp
    uint16_t SeqNumber; ///< Sequence Number
    uint16_t BufferSize; ///< Buffer Size in Samples
    uint8_t  SamplingRate; ///< Sampling Rate in JackAudioInterface::samplingRateT
    uint8_t  NumInChannels; ///< Number of Input Channels
    uint8_t  NumOutChannels; ///<  Number of Output Channels
  };
  //---------------------------------------------------------

  DefaultHeader(); 
  virtual ~DefaultHeader() {};
113
  virtual void fillHeaderCommonFromJack(const JackAudioInterface& JackAudio);
114
  virtual void parseHeader() {};
jcaceres's avatar
jcaceres committed
115
116
117
118
119
  virtual void increaseSequenceNumber()
  {
    mHeader.SeqNumber++;
    std::cout << "Sequence Number = " << static_cast<int>(mHeader.SeqNumber) << std::endl;
  };
120
121
122
123
124
125
  virtual int getHeaderSizeInBytes() const { return sizeof(mHeader); };
  virtual void putHeaderInPacket(int8_t* full_packet)
  {
    std::memcpy(full_packet, reinterpret_cast<const void*>(&mHeader),
		getHeaderSizeInBytes() );
  };
jcaceres's avatar
jcaceres committed
126
  void printHeader() const;
127
128

private:
129
  DefaultHeaderStruct mHeader; ///< Header Struct
130
131
132
};


jcaceres's avatar
jcaceres committed
133
134
135
136
137


//#######################################################################
//####################### JamLinkHeader #################################
//#######################################################################
138
139
/** \brief JamLink Header
 */
140
/*
141
142
class JamLinkHeader : public PacketHeader
{
143

144
public:
145
146
147



jcaceres's avatar
jcaceres committed
148
149
150
  //JamLinkHeader(std::tr1::shared_ptr<JackAudioInterface> JackAdioPtr) :
  //  PacketHeader(JackAdioPtr) {};

151
152
  uint16_t head; ///< 16-bit standard header

153
  virtual void fillHeaderCommonFromJack(const JackAudioInterface& JackAudio) {};
154
  virtual void parseHeader() {};
155
  //virtual void fillHeaderStuct(HeaderStruct* hs) {};
156

157
158
};
*/
159
160

#endif