JackTripWorker.h 4.04 KB
Newer Older
1
2
3
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
//*****************************************************************
/*
  JackTrip: A System for High-Quality Audio Network Performance
  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.
*/
//*****************************************************************

32
33
34
35
36
37
38
39
40
/**
 * \file JackTripWorker.h
 * \author Juan-Pablo Caceres
 * \date September 2008
 */

#ifndef __JACKTRIPWORKER_H__
#define __JACKTRIPWORKER_H__

41
42
#include <iostream>

43
#include <QThreadPool>
44
#include <QObject>
45
46
#include <QEventLoop>
#include <QHostAddress>
47
#include <QMutex>
48

49
50
51
#include "JackTrip.h"

//class JackTrip; // forward declaration
52
class UdpMasterListener; // forward declaration
53

54
55
56
57
58
59
60
61
62
63
64
65

/** \brief Prototype of the worker class that will be cloned through sending threads to the 
 * Thread Pool
 *
 * This class can be send to the ThreadPool using the start() method. Each time 
 * it is sent, it'll became "independent" of the prototype, which means
 * that the prototype state can be changed, and used to send and start
 * another thread into the pool. setAutoDelete must be set to false
 * in order for this to work.
 */
// Note that it is not possible to start run() as an event loop. That has to be implemented
// inside a QThread
66
class JackTripWorker : public QObject, public QRunnable
67
68
{
  Q_OBJECT; // QRunnable is not a QObject, so I have to inherit from QObject as well
69
  
70
public:
71
  /// \brief The class constructor
72
  JackTripWorker(UdpMasterListener* udpmasterlistener);
73
74
75
  /// \brief The class destructor
  virtual ~JackTripWorker();
  
76
77
  /// \brief Implements the Thread Loop.
  /// To start the thread, call start() ( DO NOT CALL run() ).
78
  void run();
79
80
  /// \brief Check if the Thread is Spawning
  /// \return true is it is spawning, false if it's already running
81
  bool isSpawning();
82
83
84
  /// \brief Sets the JackTripWorker properties
  /// \param id ID number
  /// \param address
85
86
87
  void setJackTrip(int id, uint32_t client_address,
		   uint16_t server_port, uint16_t client_port,
		   int num_channels);
88
  /// Stop and remove thread from pool
89
  void stopThread();
jcacerec's avatar
jcacerec committed
90
91
92
93
  int getID()
  {
    return mID;
  }
94

95
96

private slots:
97
  void slotTest()
jcacerec's avatar
jcacerec committed
98
  { std::cout << "--- JackTripWorker TEST SLOT ---" << std::endl; }
99

100
101
102

signals:
  void signalRemoveThread();
103

104
105

private:
106
  int setJackTripFromClientHeader(JackTrip& jacktrip);
107
  JackTrip::connectionModeT getConnectionModeFromHeader();
108
109

  UdpMasterListener* mUdpMasterListener; ///< Master Listener Socket
110
111
  //QHostAddress mClientAddress; ///< Client Address
  uint32_t mClientAddress;
112
  uint16_t mServerPort; ///< Server Ephemeral Incomming Port to use with Client
113

114
115
  /// Client Outgoing Port. By convention, the receving port will be <tt>mClientPort -1</tt> 
  uint16_t mClientPort;
116

117
118
119
  /// Thread spawning internal lock.
  /// If true, the prototype is working on creating (spawning) a new thread
  volatile bool mSpawning;
120
121
  QMutex mMutex; ///< Mutex to protect mSpawning

122
123
124
125
126
127
  int mID; ///< ID thread number
  int mNumChans; ///< Number of Channels
};


#endif //__JACKTRIPWORKER_H__