Skip to content

Commit

Permalink
streaming code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Tolga Cakir <tolga@cevel.net>
  • Loading branch information
tolga9009 committed May 19, 2016
1 parent 3b15a9f commit 02089f2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
5 changes: 2 additions & 3 deletions src/gchd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

#include <iostream>
#include <vector>

#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -49,15 +48,15 @@ int GCHD::init() {
return 0;
}

void GCHD::stream(unsigned char *data, int length) {
void GCHD::stream(std::vector<unsigned char> *buffer, int size) {
// this function requires an initialized device
if (!isInitialized_) {
setupConfiguration();
}

int transfer;

libusb_bulk_transfer(devh_, 0x81, data, length, &transfer, TIMEOUT);
libusb_bulk_transfer(devh_, 0x81, buffer->data(), size, &transfer, TIMEOUT);
}

int GCHD::checkFirmware() {
Expand Down
3 changes: 2 additions & 1 deletion src/gchd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <cstdint>
#include <string>
#include <vector>

#include <libusb-1.0/libusb.h>

Expand Down Expand Up @@ -39,7 +40,7 @@ enum class DeviceType {
class GCHD {
public:
int init();
void stream(unsigned char *data, int length);
void stream(std::vector<unsigned char> *buffer, int size);
GCHD(Settings *settings);
~GCHD();

Expand Down
32 changes: 13 additions & 19 deletions src/gchd/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

#include <cstdio>
#include <vector>

#include <gchd.hpp>

Expand Down Expand Up @@ -194,7 +193,7 @@ void GCHD::slsi(uint16_t wIndex, uint16_t data) {
/**
* Loads firmware to the device
*
* @param file relative path to binary firmware file
* @param file path to binary firmware file
*/
void GCHD::dlfirm(const char *file) {
int transfer;
Expand All @@ -212,7 +211,7 @@ void GCHD::dlfirm(const char *file) {
unsigned char data[DATA_BUF] = {0};
auto bytes_remain = filesize - i;

if ((bytes_remain) > DATA_BUF) {
if (bytes_remain > DATA_BUF) {
bytes_remain = DATA_BUF;
}

Expand All @@ -235,8 +234,10 @@ void GCHD::uninitDevice() {
* I'm working with. Receive empty data, after setting state change to
* null transfer.
*/
std::vector<unsigned char> buffer(DATA_BUF);

for (int i = 0; i < 5000; i++) {
receiveData();
stream(&buffer, DATA_BUF);
}

/*
Expand All @@ -248,51 +249,51 @@ void GCHD::uninitDevice() {
read_config(0xbc, 0x0800, 0x2008, 2);

for (int i = 0; i < 50; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

read_config(0xbc, 0x0900, 0x0074, 2);

for (int i = 0; i < 50; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

read_config(0xbc, 0x0900, 0x01b0, 2);

for (int i = 0; i < 50; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

read_config(0xbc, 0x0800, 0x2008, 2);

for (int i = 0; i < 50; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

read_config(0xbc, 0x0800, 0x2008, 2);


for (int i = 0; i < 50; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

write_config2(0xbc, 0x0900, 0x0074, 0x00, 0x04);

for (int i = 0; i < 50; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

write_config2(0xbc, 0x0900, 0x01b0, 0x00, 0x00);

for (int i = 0; i < 50; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

// state change - stop encoding
scmd(SCMD_STATE_CHANGE, 0x00, SCMD_STATE_STOP);

for (int i = 0; i < 5; i++) {
receiveData();
stream(&buffer, DATA_BUF);;
}

read_config(0xbc, 0x0800, 0x2008, 2);
Expand Down Expand Up @@ -397,10 +398,3 @@ void GCHD::uninitDevice() {
write_config2(0xbc, 0x0900, 0x0074, 0x00, 0x04);
write_config2(0xbc, 0x0900, 0x01b0, 0x00, 0x00);
}

void GCHD::receiveData() {
int transfer;
unsigned char data[DATA_BUF] = {0};

libusb_bulk_transfer(devh_, 0x81, data, DATA_BUF, &transfer, TIMEOUT);
}
12 changes: 6 additions & 6 deletions src/streamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ void Streamer::streamToFifo(GCHD *gchd) {

// receive audio and video from device
while (process_->isActive() && fifoFd_) {
unsigned char data[DATA_BUF] = {0};
std::vector<unsigned char> buffer(DATA_BUF);

gchd->stream(data, DATA_BUF);
write(fifoFd_, (char *)data, DATA_BUF);
gchd->stream(&buffer, DATA_BUF);
write(fifoFd_, buffer.data(), DATA_BUF);
}
}

Expand All @@ -68,10 +68,10 @@ void Streamer::streamToDisk(GCHD *gchd, std::string outputPath) {

// receive audio and video from device
while (process_->isActive() && fifoFd_) {
unsigned char data[DATA_BUF] = {0};
std::vector<unsigned char> buffer(DATA_BUF);

gchd->stream(data, DATA_BUF);
output.write((char *)data, DATA_BUF);
gchd->stream(&buffer, DATA_BUF);
output.write(reinterpret_cast<char *>(buffer.data()), DATA_BUF);
}

output.close();
Expand Down

0 comments on commit 02089f2

Please sign in to comment.