You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
273 lines
6.0 KiB
273 lines
6.0 KiB
#include "basicMediaInput.h"
|
|
|
|
#define LOG(X) m_pLog->Log("MediaInput", this, X)
|
|
#define LOG_DEBUG(X) m_pLog->Log("MediaInput", this, _LOG_DEBUG, X)
|
|
#define LOG_ERROR(X) m_pLog->Log("MediaInput", this, _LOG_ERROR, X)
|
|
|
|
static int xioctl(int fh, int request, void * arg)
|
|
{
|
|
int r;
|
|
do
|
|
{
|
|
r = ioctl(fh, request, arg);
|
|
}
|
|
while (-1 == r && EINTR == errno);
|
|
return r;
|
|
}
|
|
|
|
// Constructor implementation
|
|
basicMediaInput::basicMediaInput() {}
|
|
|
|
// Destructor implementation
|
|
basicMediaInput::~basicMediaInput()
|
|
{
|
|
m_eType = MT_UNDEFINED;
|
|
memset(m_sMediaState.m_pInputName, 0, MAX_PATH_LEN);
|
|
}
|
|
|
|
basicMediaInput::basicMediaInput(_MediaTypes mediaType)
|
|
{
|
|
m_eType = mediaType;
|
|
memset(m_sMediaState.m_pInputName, 0, MAX_PATH_LEN);
|
|
}
|
|
|
|
basicMediaInput::basicMediaInput(CSTR URI, _MediaTypes mediaType)
|
|
{
|
|
m_eType = mediaType;
|
|
LOG_DEBUG(URI);
|
|
if (URI == NULL)
|
|
{
|
|
memset(m_sMediaState.m_pInputName, 0, MAX_PATH_LEN);
|
|
}
|
|
else
|
|
{
|
|
strncpy(m_sMediaState.m_pInputName, URI, MAX_PATH_LEN);
|
|
m_sMediaState.m_pInputName[MAX_PATH_LEN - 1] = 0;
|
|
};
|
|
}
|
|
|
|
// TODO: Uncomment the copy constructor when you need it.
|
|
// inline basicMediaInput::basicMediaInput(const basicMediaInput& src)
|
|
//{
|
|
// // TODO: copy
|
|
//}
|
|
|
|
// TODO: Uncomment the assignment operator when you need it.
|
|
// inline basicMediaInput& basicMediaInput::operator=(const basicMediaInput& rhs)
|
|
//{
|
|
// if (this == &rhs) {
|
|
// return *this;
|
|
// }
|
|
//
|
|
// // TODO: assignment
|
|
//
|
|
// return *this;
|
|
//}
|
|
|
|
bool basicMediaInput::create(CSTR URI, _MediaTypes mediaType)
|
|
{
|
|
char mBuffer[CLOG];
|
|
LOG_DEBUG(URI);
|
|
if (URI == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
strncpy(m_sMediaState.m_pInputName, URI, MAX_PATH_LEN);
|
|
m_sMediaState.m_pInputName[MAX_PATH_LEN - 1] = 0;
|
|
if (mediaType > MT_UNDEFINED)
|
|
{
|
|
m_eType |= mediaType;
|
|
}
|
|
else
|
|
{
|
|
if (strncmp(m_sMediaState.m_pInputName, "/dev/", 5) == 0)
|
|
{
|
|
LOG_DEBUG("Local Device");
|
|
m_eType = MT_DEVICE;
|
|
}
|
|
else if (strncmp(m_sMediaState.m_pInputName, "rtsp://", 7) == 0)
|
|
{
|
|
m_eType = MT_NETWORK;
|
|
LOG_DEBUG("Network RTSP Device");
|
|
}
|
|
else if (strncmp(m_sMediaState.m_pInputName, "http://", 7) == 0)
|
|
{
|
|
m_eType = MT_NETWORK;
|
|
LOG_DEBUG("Network HTTP Device");
|
|
}
|
|
else
|
|
{
|
|
m_eType = MT_FILE;
|
|
LOG_DEBUG("Local File");
|
|
};
|
|
};
|
|
switch (m_eType)
|
|
{
|
|
case MT_DEVICE:
|
|
{
|
|
struct v4l2_capability cap;
|
|
struct v4l2_cropcap cropcap;
|
|
struct v4l2_crop crop;
|
|
struct v4l2_format fmt;
|
|
|
|
unsigned int min;
|
|
|
|
struct stat st;
|
|
|
|
if (-1 == stat(m_sMediaState.m_pInputName, &st))
|
|
{
|
|
sprintf(mBuffer, "Cannot identify '%s': %d, %s", m_sMediaState.m_pInputName, errno, strerror(errno));
|
|
LOG_ERROR(mBuffer);
|
|
return false;
|
|
};
|
|
|
|
if (! S_ISCHR(st.st_mode))
|
|
{
|
|
sprintf(mBuffer, "%s is no device", m_sMediaState.m_pInputName);
|
|
LOG_ERROR(mBuffer);
|
|
return false;
|
|
};
|
|
|
|
m_sMediaState.m_iFileDescriptor = open(m_sMediaState.m_pInputName, O_RDWR /* required */ | O_NONBLOCK, 0);
|
|
|
|
if (-1 == m_sMediaState.m_iFileDescriptor)
|
|
{
|
|
sprintf(mBuffer, "Cannot open '%s': %d, %s", m_sMediaState.m_pInputName, errno, strerror(errno));
|
|
LOG_ERROR(mBuffer);
|
|
return false;
|
|
}
|
|
|
|
if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_QUERYCAP, &cap))
|
|
{
|
|
if (EINVAL == errno)
|
|
{
|
|
sprintf(mBuffer, "%s is no V4L2 device", m_sMediaState.m_pInputName);
|
|
LOG_ERROR(mBuffer);
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("VIDIOC_QUERYCAP");
|
|
};
|
|
close(m_sMediaState.m_iFileDescriptor);
|
|
return false;
|
|
};
|
|
|
|
if (! (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
|
|
{
|
|
sprintf(mBuffer, "%s is no video capture device", m_sMediaState.m_pInputName);
|
|
LOG_ERROR(mBuffer);
|
|
close(m_sMediaState.m_iFileDescriptor);
|
|
return false;
|
|
}
|
|
|
|
if (! (cap.capabilities & V4L2_CAP_READWRITE))
|
|
{
|
|
sprintf(mBuffer, "%s does not support read i/o", m_sMediaState.m_pInputName);
|
|
LOG_ERROR(mBuffer);
|
|
close(m_sMediaState.m_iFileDescriptor);
|
|
return false;
|
|
};
|
|
|
|
if (! (cap.capabilities & V4L2_CAP_STREAMING))
|
|
{
|
|
sprintf(mBuffer, "%s does not support streaming i/o", m_sMediaState.m_pInputName);
|
|
LOG_ERROR(mBuffer);
|
|
close(m_sMediaState.m_iFileDescriptor);
|
|
return false;
|
|
};
|
|
|
|
CLEAR(cropcap);
|
|
|
|
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (0 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_CROPCAP, &cropcap))
|
|
{
|
|
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
crop.c = cropcap.defrect; /* reset to default */
|
|
if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_S_CROP, &crop))
|
|
{
|
|
switch (errno)
|
|
{
|
|
case EINVAL:
|
|
/* Cropping not supported. */
|
|
break;
|
|
default:
|
|
/* Errors ignored. */
|
|
break;
|
|
};
|
|
};
|
|
}
|
|
else
|
|
{
|
|
/* Errors ignored. */
|
|
};
|
|
|
|
CLEAR(fmt);
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (0)
|
|
{ // force_format ) {
|
|
LOG("Set H264");
|
|
fmt.fmt.pix.width = 640; // replace
|
|
fmt.fmt.pix.height = 480; // replace
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_H264; // replace
|
|
fmt.fmt.pix.field = V4L2_FIELD_ANY;
|
|
|
|
if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_S_FMT, &fmt))
|
|
{
|
|
LOG_ERROR("VIDIOC_S_FMT");
|
|
close(m_sMediaState.m_iFileDescriptor);
|
|
return false;
|
|
};
|
|
|
|
/* Note VIDIOC_S_FMT may change width and height. */
|
|
}
|
|
else
|
|
{
|
|
/* Preserve original settings as set by v4l2-ctl for example */
|
|
if (-1 == xioctl(m_sMediaState.m_iFileDescriptor, VIDIOC_G_FMT, &fmt))
|
|
{
|
|
LOG_ERROR("VIDIOC_G_FMT");
|
|
close(m_sMediaState.m_iFileDescriptor);
|
|
return false;
|
|
};
|
|
};
|
|
|
|
/* Buggy driver paranoia. */
|
|
min = fmt.fmt.pix.width * 2;
|
|
if (fmt.fmt.pix.bytesperline < min)
|
|
{
|
|
fmt.fmt.pix.bytesperline = min;
|
|
}
|
|
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
|
|
if (fmt.fmt.pix.sizeimage < min)
|
|
{
|
|
fmt.fmt.pix.sizeimage = min;
|
|
}
|
|
break;
|
|
};
|
|
case MT_NETWORK:
|
|
{
|
|
|
|
break;
|
|
};
|
|
case MT_FILE:
|
|
{
|
|
|
|
break;
|
|
};
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void * basicMediaInput::m_fStarterProc(void * arg) {}
|
|
|
|
// http://www.boehmel.de/lanc.htm
|
|
/*
|
|
* This program displays the names of all files in the current directory.
|
|
*/
|
|
|
|
#undef LOG
|
|
#undef LOG_DEBUG
|
|
#undef LOG_ERROR
|