Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(videomode): Added possible video shift
Browse files Browse the repository at this point in the history
  • Loading branch information
Diadlo committed Jun 25, 2016
1 parent 2d861ee commit fd701df
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/persistence/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void Settings::loadGlobal()

s.beginGroup("Video");
videoDev = s.value("videoDev", "").toString();
camVideoRes = s.value("camVideoRes",QSize()).toSize();
camVideoRes = s.value("camVideoRes", QRect()).toRect();
camVideoFPS = s.value("camVideoFPS", 0).toUInt();
s.endGroup();

Expand Down Expand Up @@ -1412,13 +1412,13 @@ void Settings::setOutVolume(int volume)
outVolume = volume;
}

QSize Settings::getCamVideoRes() const
QRect Settings::getCamVideoRes() const
{
QMutexLocker locker{&bigLock};
return camVideoRes;
}

void Settings::setCamVideoRes(QSize newValue)
void Settings::setCamVideoRes(QRect newValue)
{
QMutexLocker locker{&bigLock};
camVideoRes = newValue;
Expand Down
6 changes: 3 additions & 3 deletions src/persistence/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ public slots:
QString getVideoDev() const;
void setVideoDev(const QString& deviceSpecifier);

QSize getCamVideoRes() const;
void setCamVideoRes(QSize newValue);
QRect getCamVideoRes() const;
void setCamVideoRes(QRect newValue);

unsigned short getCamVideoFPS() const;
void setCamVideoFPS(unsigned short newValue);
Expand Down Expand Up @@ -434,7 +434,7 @@ private slots:

// Video
QString videoDev;
QSize camVideoRes;
QRect camVideoRes;
unsigned short camVideoFPS;

struct friendProp
Expand Down
4 changes: 2 additions & 2 deletions src/video/cameradevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ CameraDevice* CameraDevice::open(QString devName, AVDictionary** options)
format = iformat;
}

if (avformat_open_input(&fctx, devName.toStdString().c_str(), format, options)<0)
if (avformat_open_input(&fctx, devName.toStdString().c_str(), format, options) < 0)
goto out;

// Fix avformat_find_stream_info hanging on garbage input
Expand Down Expand Up @@ -105,7 +105,7 @@ CameraDevice* CameraDevice::open(QString devName, AVDictionary** options)

CameraDevice* CameraDevice::open(QString devName)
{
VideoMode mode{0,0,0,0};
VideoMode mode = VideoMode();
return open(devName, mode);
}

Expand Down
33 changes: 23 additions & 10 deletions src/video/camerasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern "C" {
CameraSource* CameraSource::instance{nullptr};

CameraSource::CameraSource()
: deviceName{"none"}, device{nullptr}, mode(VideoMode{0,0,0,0}),
: deviceName{"none"}, device{nullptr}, mode(VideoMode()),
cctx{nullptr}, cctxOrig{nullptr}, videoStreamIndex{-1},
_isOpen{false}, streamBlocker{false}, subscriptions{0}
{
Expand Down Expand Up @@ -67,7 +67,7 @@ void CameraSource::open()

void CameraSource::open(const QString& deviceName)
{
open(deviceName, VideoMode{0,0,0,0});
open(deviceName, VideoMode());
}

void CameraSource::open(const QString& DeviceName, VideoMode Mode)
Expand Down Expand Up @@ -206,7 +206,7 @@ void CameraSource::unsubscribe()

bool CameraSource::openDevice()
{
qDebug() << "Opening device "<<deviceName;
qDebug() << "Opening device " << deviceName;

if (device)
{
Expand All @@ -216,10 +216,8 @@ bool CameraSource::openDevice()

// We need to create a new CameraDevice
AVCodec* codec;
if (mode)
device = CameraDevice::open(deviceName, mode);
else
device = CameraDevice::open(deviceName);
device = CameraDevice::open(deviceName, mode);

if (!device)
{
qWarning() << "Failed to open device!";
Expand All @@ -240,25 +238,36 @@ bool CameraSource::openDevice()
break;
}
}

if (videoStreamIndex == -1)
{
qWarning() << "Video stream not found";
return false;
}

// Get a pointer to the codec context for the video stream
cctxOrig = device->context->streams[videoStreamIndex]->codec;
codec = avcodec_find_decoder(cctxOrig->codec_id);
if(!codec)
{
qWarning() << "Codec not found";
return false;
}

// Copy context, since we apparently aren't allowed to use the original
cctx = avcodec_alloc_context3(codec);
if(avcodec_copy_context(cctx, cctxOrig) != 0)
{
qWarning() << "Can't copy context";
return false;
}

cctx->refcounted_frames = 1;

// Open codec
if(avcodec_open2(cctx, codec, nullptr)<0)
{
qWarning() << "Can't open codec";
avcodec_free_context(&cctx);
return false;
}
Expand Down Expand Up @@ -288,6 +297,7 @@ void CameraSource::closeDevice()
std::shared_ptr<VideoFrame> vframe = freelist[i].lock();
if (!vframe)
continue;

vframe->releaseFrame();
}
freelist.clear();
Expand All @@ -311,14 +321,15 @@ void CameraSource::stream()
AVFrame* frame = av_frame_alloc();
if (!frame)
return;

frame->opaque = nullptr;

AVPacket packet;
if (av_read_frame(device->context, &packet)<0)
if (av_read_frame(device->context, &packet) < 0)
return;

// Only keep packets from the right stream;
if (packet.stream_index==videoStreamIndex)
if (packet.stream_index == videoStreamIndex)
{
// Decode video frame
int frameFinished;
Expand All @@ -340,7 +351,8 @@ void CameraSource::stream()
av_packet_unref(&packet);
};

forever {
forever
{
biglock.lock();

// When a thread makes device null, it releases it, so we acquire here
Expand All @@ -357,6 +369,7 @@ void CameraSource::stream()
biglock.unlock();
while (streamBlocker)
QThread::yieldCurrentThread();

QThread::yieldCurrentThread();
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/video/netcamview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ NetCamView::NetCamView(int friendId, QWidget* parent)
videoSurface->setAvatar(pixmap);
});

VideoMode videoMode;
QSize videoSize = Settings::getInstance().getCamVideoRes();
videoMode.width = videoSize.width();
videoMode.height = videoSize.height();
QRect videoSize = Settings::getInstance().getCamVideoRes();
qDebug() << "SIZER" << videoSize;
videoMode.FPS = Settings::getInstance().getCamVideoFPS();
}

NetCamView::~NetCamView()
Expand Down
25 changes: 25 additions & 0 deletions src/video/videomode.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,36 @@
#ifndef VIDEOMODE_H
#define VIDEOMODE_H

#include <QRect>
#include <cstdint>

/// Describes a video mode supported by a device
struct VideoMode
{
unsigned short width, height; ///< Displayed video resolution (NOT frame resolution)
unsigned short x, y; ///< Coordinates of upper-left corner
float FPS; ///< Max frames per second supported by the device at this resolution
uint32_t pixel_format;

VideoMode(int width = 0, int height = 0, int x = 0, int y = 0,
int FPS = 0, int format = 0) :
width(width), height(height), x(x), y(y),
FPS(FPS), pixel_format(format)
{
}

VideoMode(QRect rect) :
width(rect.width()), height(rect.height()),
x(rect.x()), y(rect.y()),
FPS(0), pixel_format(0)
{
}

QRect toRect() const
{
return QRect(x, y, width, height);
}

/// All zeros means a default/unspecified mode
operator bool() const
{
Expand All @@ -38,6 +61,8 @@ struct VideoMode
{
return width == other.width
&& height == other.height
&& x == other.x
&& y == other.y
&& FPS == other.FPS
&& pixel_format == other.pixel_format;
}
Expand Down
18 changes: 10 additions & 8 deletions src/widget/form/settings/avform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ void AVForm::onVideoModesIndexChanged(int index)
}
QString devName = videoDeviceList[devIndex].first;
VideoMode mode = videoModes[index];
Settings::getInstance().setCamVideoRes(QSize(mode.width, mode.height));

QRect rect(mode.x, mode.y, mode.width, mode.height);
Settings::getInstance().setCamVideoRes(rect);
Settings::getInstance().setCamVideoFPS(mode.FPS);
camera.open(devName, mode);
}
Expand All @@ -158,12 +160,12 @@ void AVForm::selectBestModes(QVector<VideoMode> &allVideoModes)
{
// Identify the best resolutions available for the supposed XXXXp resolutions.
std::map<int, VideoMode> idealModes;
idealModes[120] = {160,120,0,0};
idealModes[240] = {460,240,0,0};
idealModes[360] = {640,360,0,0};
idealModes[480] = {854,480,0,0};
idealModes[720] = {1280,720,0,0};
idealModes[1080] = {1920,1080,0,0};
idealModes[120] = VideoMode(160, 120);
idealModes[240] = VideoMode(460, 240);
idealModes[360] = VideoMode(640, 360);
idealModes[480] = VideoMode(854, 480);
idealModes[720] = VideoMode(1280, 720);
idealModes[1080] = VideoMode(1920, 1080);

std::map<int, int> bestModeInds;
for (int i = 0; i < allVideoModes.size(); ++i)
Expand Down Expand Up @@ -255,7 +257,7 @@ void AVForm::fillModesComboBox()

int AVForm::searchPreferredIndex()
{
QSize prefRes = Settings::getInstance().getCamVideoRes();
QRect prefRes = Settings::getInstance().getCamVideoRes();
unsigned short prefFPS = Settings::getInstance().getCamVideoFPS();

for (int i = 0; i < videoModes.size(); i++)
Expand Down

0 comments on commit fd701df

Please sign in to comment.