# % macro, P, Device Prefix
# % macro, R, Device Suffix
# % macro, PORT, Asyn Port name
# % macro, QUALITY, Jpeg Quality in percent
# % macro, MAXW, Maximum Jpeg Width
# % macro, MAXH, Maximum Jpeg Height
# % gui, $(PORT), edmtab, ffmpegStream.edl, P=$(P),R=$(R)

# # \file 
# # ffmpegServer contains a very simple http server based on
# # <a href="http://www.nulllogic.ca/httpd">nullhttpd</a>. If a file named 
# # \<port\>.jpg is requested for any ffmpegStream object named \<port\>, then it 
# # will return the last NDArray received by the object compressed to jpeg. If 
# # \<port\>.mjpg is requested then it will return an mjpg stream over http to the
# # client. Otherwise, it will return an index page listing all the available 
# # streams.
# # \section ffmpegStream_setup Setup
# # - In the database, an instance of NDPluginBase is required, followed by an
# # instance of this template. 
# # - In the startup scripts, call ffmpegServerConfigure() once per IOC to setup 
# # the http server, then call ffmpegStreamConfigure() once per ffmpegStream
# # instance to setup the areadetector plugin
# # - see \ref build_instructions
# # \section ffmpegStream_screenshots Screenshots
# # \image html server.png "Index page for ffmpegServer"
# # \image html ffmpegStreamEdm.png "Edm screen for ffmpegStream"

# All tags starting with gdatag are DLS specific for GDA
# % gdatag, template, ffmpegStream, $(PORT)_ffmpegStream, $(PORT) ffmpegStream class instance

# # JPEG Quality
# % gdatag, pv, rw, $(PORT)_ffmpegStream, QUALITY, Set QUALITY
record(ao, "$(P)$(R)QUALITY") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT) 0)FFMPEG_QUALITY")
  field(VAL, "$(QUALITY=100)")
  field(DRVH, "100")
  field(DRVL, "0")          
  field(PINI, "1")  
}

# # JPEG Quality Readback from driver
# % gdatag, pv, ro, $(PORT)_ffmpegStream, QUALITY_RBV, Readback for QUALITY
record(ai, "$(P)$(R)QUALITY_RBV") {
  field(SCAN, "I/O Intr")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT) 0)FFMPEG_QUALITY")
}

# # Maximum jpg width
# % gdatag, pv, rw, $(PORT)_ffmpegStream, MAXW, Set MAXW
record(ao, "$(P)$(R)MAXW") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT) 0)FFMPEG_MAXW")
  field(VAL, "$(MAXW=1600)")
  field(PINI, "1")  
}

# # Maximum jpg width Readback from driver
# % gdatag, pv, ro, $(PORT)_ffmpegStream, MAXW_RBV, Readback for MAXW
record(ai, "$(P)$(R)MAXW_RBV") {
  field(SCAN, "I/O Intr")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT) 0)FFMPEG_MAXW")
}

# # Maximum jpg width
# % gdatag, pv, rw, $(PORT)_ffmpegStream, MAXH, Set MAXH
record(ao, "$(P)$(R)MAXH") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT) 0)FFMPEG_MAXH")
  field(VAL, "$(MAXH=1200)")
  field(PINI, "1")  
}

# # Maximum jpg width Readback from driver
# % gdatag, pv, ro, $(PORT)_ffmpegStream, MAXH_RBV, Readback for MAXH
record(ai, "$(P)$(R)MAXH_RBV") {
  field(SCAN, "I/O Intr")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT) 0)FFMPEG_MAXH")
}


# # Toggle False colour mode 
# % gdatag, binary, rw, $(PORT)_ffmpegStream, FALSE_COL, Set FALSE_COL
record(bo, "$(P)$(R)FALSE_COL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT) 0)FFMPEG_FALSE_COL")
  field(ZNAM, "Off")
  field(ONAM, "On")
  field(VAL, "0")
  field(PINI, "1")  
}

# # False colour mode readback from driver
# % gdatag, binary, ro, $(PORT)_ffmpegStream, FALSE_COL_RBV, Readback for FALSE_COL
record(bi, "$(P)$(R)FALSE_COL_RBV") {
  field(SCAN, "I/O Intr")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT) 0)FFMPEG_FALSE_COL")
  field(ZNAM, "Off")
  field(ONAM, "On") 
}

# # Always produce jpegs, even when no-one is listening
# % gdatag, binary, rw, $(PORT)_ffmpegStream, ALWAYS_ON, Set ALWAYS_ON
record(bo, "$(P)$(R)ALWAYS_ON") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT) 0)FFMPEG_ALWAYS_ON")
  field(ZNAM, "Off")
  field(ONAM, "On") 
  field(VAL, "0")  
  field(PINI, "1")  
}

# # Always on readback from driver
# % gdatag, binary, ro, $(PORT)_ffmpegStream, ALWAYS_ON_RBV, Readback for ALWAYS_ON
record(bi, "$(P)$(R)ALWAYS_ON_RBV") {
  field(SCAN, "I/O Intr")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT) 0)FFMPEG_ALWAYS_ON")
  field(ZNAM, "Off")
  field(ONAM, "On") 
}

# # HTTP Port to connect an mjpg viewer to
# % gdatag, pv, ro, $(PORT)_ffmpegStream, HTTP_PORT_RBV, Readback for HTTP_PORT
record(ai, "$(P)$(R)HTTP_PORT_RBV")
{
    field(PINI, "1")
    field(DTYP, "asynInt32")
    field(INP,  "@asyn($(PORT) 0)FFMPEG_HTTP_PORT")
    field(SCAN, "I/O Intr")
}

# # Hostname of the HTTP server
# % gdatag, pv, ro, $(PORT)_ffmpegStream, HOST_RBV, Readback for HOST
record(stringin, "$(P)$(R)HOST_RBV")
{
    field(PINI, "1")
    field(DTYP, "asynOctetRead")
    field(INP,  "@asyn($(PORT) 0)FFMPEG_HOST")
    field(SCAN, "I/O Intr")
}

# # Number of clients
# % gdatag, pv, ro, $(PORT)_ffmpegStream, CLIENTS_RBV, Readback for CLIENTS
record(ai, "$(P)$(R)CLIENTS_RBV")
{
    field(PINI, "1")
    field(DTYP, "asynInt32")
    field(INP,  "@asyn($(PORT) 0)FFMPEG_CLIENTS")
    field(SCAN, "I/O Intr")
}

# # URL of the jpg snapshot
# % gdatag, pv, ro, $(PORT)_ffmpegStream, JPG_URL_RBV, Readback for JPG URL
record(waveform, "$(P)$(R)JPG_URL_RBV")
{
    field(DTYP, "asynOctetRead")
    field(INP,  "@asyn($(PORT) 0)FFMPEG_JPG_URL")
    field(FTVL, "UCHAR")
    field(NELM, "256")
    field(SCAN, "I/O Intr")
}

# # URL of the mjpg stream
# % gdatag, pv, ro, $(PORT)_ffmpegStream, MJPG_URL_RBV, Readback for MJPG URL
record(waveform, "$(P)$(R)MJPG_URL_RBV")
{
    field(DTYP, "asynOctetRead")
    field(INP,  "@asyn($(PORT) 0)FFMPEG_MJPG_URL")
    field(FTVL, "UCHAR")
    field(NELM, "256")
    field(SCAN, "I/O Intr")
}

#% autosave 2
#% archiver 10 Monitor
record(longout, "$(P)$(R)GX") {
    field(VAL, "100")
    field(PINI, "1")
}

#% autosave 2
#% archiver 10 Monitor
record(longout, "$(P)$(R)GY") {
    field(VAL, "100")
    field(PINI, "1")
}

#% autosave 2
#% archiver 10 Monitor
record(bo, "$(P)$(R)GRID") {
    field(VAL, "0")
    field(ZNAM, "Off")
    field(ONAM, "On")
    field(PINI, "1")
}

#% autosave 2
#% archiver 10 Monitor
# default white
record(longout, "$(P)$(R)GCOL") {
    field(VAL, "0xFFFFFF")
    field(PINI, "1")
}

#% autosave 2
#% archiver 10 Monitor
record(longout, "$(P)$(R)GS") {
    field(VAL, "200")
    field(DRVL, "10")    
    field(DRVH, "2000")
    field(PINI, "1")
}
