/*
 * h224handler.h
 *
 * H.224 protocol handler implementation for the OpenH323 Project.
 *
 * Copyright (c) 2006 Network for Educational Technology, ETH Zurich.
 * Written by Hannes Friederich.
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * Contributor(s): ______________________________________.
 *
 * $Log: h224handler.h,v $
 * Revision 1.10  2016/05/04 22:01:50  joseph
 * Add support for SIP FECC (both directions)
 *
 * Revision 1.9  2016/02/25 00:07:22  joseph
 * VGW-1938 - implement FECC for H.323
 * -added H235 encryption for inbound FECC
 * -removed some warnings
 *
 * Revision 1.8  2016/02/18 18:27:34  joseph
 * VGW-1938 - implement FECC for H.323
 * - more changes related to the sessionID issue from the previous check-in.
 *
 * Revision 1.7  2016/02/05 22:12:58  joseph
 * VGW-1938 - implement FECC for H.323
 * - Fixed a bug in the q922 HDLC decoder.  The decoder had a bug parsing an FECC messages when it contained two sets of 5 consecutive one's was separated by a 0 (i.e. this pattern 11111011111).
 * - Fixed the FECC ExtraCapabilitiesMessages such that now it advertises one "main camera" as being able to pan,tilt,zoom and focus.  This was preventing the Cisco EX60 endpoint from enabling it's FECC control panel.
 *
 * Revision 1.6  2016/01/27 12:06:41  joseph
 * Inbound FECC support for OTN
 *
 * Revision 1.5  2014/11/09 08:57:26  roni
 * sdk 2.2 and 64 bit
 *
 * Revision 1.1.1.1  2014/07/11 15:30:12  roni
 * Initial import
 *
 * Revision 1.4  2011/05/10 04:21:46  joseph
 * Removed a comment.
 *
 * Revision 1.3  2011-05-10 03:14:24  joseph
 * Added FECC (Far End Camera Control) feature to the GW.
 *
 * Revision 1.2  2009-12-11 20:01:52  roni
 * *** empty log message ***
 *
 * Revision 1.1.1.1  2009-01-02 16:23:15  roni
 * adding openH323
 *
 * Revision 1.2  2008/10/23 19:58:55  roni
 * *** empty log message ***
 *
 * Revision 1.1  2007/08/06 20:50:48  shorne
 * First commit of h323plus
 *
 * Revision 1.1  2006/06/22 11:07:22  shorne
 * Backport of FECC (H.224) from Opal
 *
 * Revision 1.2  2006/04/23 18:52:19  dsandras
 * Removed warnings when compiling with gcc on Linux.
 *
 * Revision 1.1  2006/04/20 16:48:17  hfriederich
 * Initial version of H.224/H.281 implementation.
 *
 */

#ifndef __H323_H224HANDLER_H
#define __H323_H224HANDLER_H

#ifdef P_USE_PRAGMA
#pragma interface
#endif

#include <ptlib.h>
//#include <opal/connection.h>
//#include <transports.h>
#include <rtp.h>
#include <h281handler.h>
#include <channels.h>

#define H281_CLIENT_ID 0x01

class H224_Frame;
class OpalH224Handler;

class OpalH224ReceiverThread : public PThread
{
  PCLASSINFO(OpalH224ReceiverThread, PThread);
	
public:
	
  OpalH224ReceiverThread(OpalH224Handler *h224Handler, RTP_Session & rtpSession);
  ~OpalH224ReceiverThread();
	
  virtual void Main();
	
  void Close();
	
private:
		
  OpalH224Handler *h224Handler;
  mutable PMutex inUse;
  unsigned timestamp;
  RTP_Session & rtpSession;
  BOOL terminate;
};

class OpalH281Handler;
class H323Connection;

class OpalH224Handler : public PObject
{
  PCLASSINFO(OpalH224Handler, PObject);
	
public:
	
  OpalH224Handler();
  OpalH224Handler(H323Channel::Directions dir, H323Connection & connection, unsigned sessionID);
  ~OpalH224Handler();
	
  virtual void StartTransmit();
  virtual void StopTransmit();
  virtual void StartReceive();
  virtual void StopReceive();
	
  BOOL SendClientList();
  BOOL SendExtraCapabilities();
  BOOL SendClientListCommand();
  BOOL SendExtraCapabilitiesCommand(BYTE clientID);

  BOOL SendExtraCapabilitiesMessage(BYTE clientID, BYTE *data, PINDEX length);

  BOOL TransmitClientFrame(BYTE clientID, H224_Frame & frame);
	
  virtual BOOL OnReceivedFrame(H224_Frame & frame);
  virtual BOOL OnReceivedCMEMessage(H224_Frame & frame);
  virtual BOOL OnReceivedClientList(H224_Frame & frame);
  virtual BOOL OnReceivedClientListCommand();
  virtual BOOL OnReceivedExtraCapabilities(H224_Frame & frame);
  virtual BOOL OnReceivedExtraCapabilitiesCommand();
  virtual void TransmitFrame(H224_Frame & frame);
	
  PMutex & GetTransmitMutex() { return transmitMutex; }
	
  RTP_Session * GetSession() const { return session; }
	
  virtual OpalH224ReceiverThread * CreateH224ReceiverThread();
	
  OpalH281Handler *GetH281Handler() { return h281Handler; }
  void setSessionKey(unsigned char *sessionKey_);
  unsigned char *getSessionKey() { return sessionKey; }
	
protected:

  RTP_Session * session;

  BOOL canTransmit;
  PMutex transmitMutex;
  RTP_DataFrame *transmitFrame;
  BYTE transmitBitIndex;
  PTime *transmitStartTime;
	
  OpalH224ReceiverThread *receiverThread;
	
  OpalH281Handler *h281Handler;
  unsigned char *sessionKey;
  unsigned char buf[16];
	
private:
		
  //void TransmitFrame(H224_Frame & frame);
	
};

#endif // __H323_H224HANDLER_H

