Skip to content

Commit

Permalink
Merge pull request wtsnz#60 from wtsnz/feature/refactor-portal
Browse files Browse the repository at this point in the history
Refactor portal lib
  • Loading branch information
wtsnz authored Feb 4, 2021
2 parents 79317aa + ff24742 commit bd4d29e
Show file tree
Hide file tree
Showing 26 changed files with 1,780 additions and 1,088 deletions.
6 changes: 3 additions & 3 deletions CI/install-build-obs-macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ cd ..
echo "[obs-ios-camera-plugin] Cloning obs-studio from GitHub.."
git clone https://github.com/obsproject/obs-studio
cd obs-studio
# OBSLatestTag=$(git describe --tags --abbrev=0)
# git checkout $OBSLatestTag
git checkout 24.0.6
OBSLatestTag=$(git describe --tags --abbrev=0)
git checkout $OBSLatestTag
#git checkout 24.0.6
mkdir build && cd build
echo "[obs-ios-camera-plugin] Building obs-studio.."
cmake .. \
Expand Down
16 changes: 11 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(CMAKE_PREFIX_PATH "${QTDIR}")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set (CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)

if (WIN32 OR APPLE)
include(external/FindLibObs.cmake)
Expand Down Expand Up @@ -152,16 +152,18 @@ endif()
set(portal_HEADERS
deps/portal/src/Channel.hpp
deps/portal/src/Device.hpp
deps/portal/src/Portal.hpp
deps/portal/src/Protocol.hpp
deps/portal/src/logging.h
deps/portal/src/DeviceConnection.hpp
deps/portal/src/DeviceManager.hpp
)

set(portal_SOURCES
deps/portal/src/Channel.cpp
deps/portal/src/Device.cpp
deps/portal/src/Portal.cpp
deps/portal/src/Protocol.cpp
deps/portal/src/DeviceConnection.cpp
deps/portal/src/DeviceManager.cpp
)

include_directories(portal include
Expand All @@ -188,7 +190,9 @@ set(obs-ios-camera-source_SOURCES
src/VideoDecoder.cpp
src/FFMpegVideoDecoder.cpp
src/FFMpegAudioDecoder.cpp
src/Thread.cpp)
src/Thread.cpp
src/DeviceApplicationConnectionController.cpp
)

set(obs-ios-camera-source_HEADERS
src/obs-ios-camera-source.h
Expand All @@ -197,7 +201,9 @@ set(obs-ios-camera-source_HEADERS
src/FFMpegVideoDecoder.h
src/FFMpegAudioDecoder.h
src/Thread.hpp
src/Queue.hpp)
src/Queue.hpp
src/DeviceApplicationConnectionController.hpp
)

if(APPLE)

Expand Down
187 changes: 118 additions & 69 deletions deps/portal/src/Channel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
portal
Copyright (C) 2018 Will Townsend <[email protected]>
Copyright (C) 2018 Will Townsend <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -17,90 +17,139 @@
*/

#include "Channel.hpp"
#include <iostream>

namespace portal
namespace portal {

Channel::Channel(int port_, int conn_)
{
port = port_;
conn = conn_;

Channel::Channel(int port_, int conn_)
{
port = port_;
conn = conn_;
setState(State::Disconnected);
}

protocol = std::make_unique<SimpleDataPacketProtocol>();
Channel::~Channel()
{
running = false;
WaitForInternalThreadToExit();
portal_log("%s: Deallocating\n", __func__);
}

running = StartInternalThread();
}
bool Channel::start()
{
if (getState() == State::Connected) {
return false;
}

Channel::~Channel()
{
running = false;
WaitForInternalThreadToExit();
portal_log("%s: %d: Deallocating\n", __func__, conn);
}
running = StartInternalThread();

void Channel::close()
{
running = false;
WaitForInternalThreadToExit();
usbmuxd_disconnect(conn);
}
if (running == true) {
setState(State::Connecting);
}

/** Returns true if the thread was successfully started, false if there was an error starting the thread */
bool Channel::StartInternalThread()
{
_thread = std::thread(InternalThreadEntryFunc, this);
return true;
}
return running;
}

/** Will not return until the internal thread has exited. */
void Channel::WaitForInternalThreadToExit()
{
if (_thread.joinable())
{
_thread.join();
}
}
bool Channel::close()
{
running = false;

void Channel::StopInternalThread()
{
running = false;
}
//// Wait for exit if close() wasn't called on the internal thread
if (std::this_thread::get_id() != _thread.get_id()) {
WaitForInternalThreadToExit();
}

void Channel::InternalThreadEntry()
{
while (running)
{
auto ret = usbmuxd_disconnect(conn);

return ret;
}

const uint32_t numberOfBytesToAskFor = 65536; // (1 << 16); // This is the value in DarkLighting
uint32_t numberOfBytesReceived = 0;
bool Channel::send(std::vector<char> data)
{
if (getState() != State::Connected) {
return false;
}

char buffer[numberOfBytesToAskFor];
uint32_t sentBytes = 0;

int ret = usbmuxd_recv_timeout(conn, (char *)&buffer, numberOfBytesToAskFor, &numberOfBytesReceived, 100);
usbmuxd_send(conn, &data[0], data.size(), &sentBytes);
}

if (ret == 0)
{
if (numberOfBytesReceived > 0)
{
if (running) {
protocol->processData((char *)buffer, numberOfBytesReceived);
}
}
}
else
{
portal_log("%d: There was an error receiving data\n", conn);
running = false;
}
}
}
void Channel::setState(State state)
{
if (state == getState()) {
return;
}

void Channel::simpleDataPacketProtocolDelegateDidProcessPacket(std::vector<char> packet, int type, int tag)
{
std::shared_ptr<ChannelDelegate> strongDelegate = delegate.lock();
if (strongDelegate) {
strongDelegate->channelDidReceivePacket(packet, type, tag);
}
//std::cout << "Channel:setState: " << state << std::endl;
_state = state;

if (auto delegate = this->delegate.lock()) {
delegate->channelDidChangeState(state);
}
}

/** Returns true if the thread was successfully started, false if there was an error starting the thread */
bool Channel::StartInternalThread()
{
_thread = std::thread(InternalThreadEntryFunc, this);
return true;
}

/** Will not return until the internal thread has exited. */
void Channel::WaitForInternalThreadToExit()
{
running = false;
std::unique_lock<std::mutex> lock(worker_mutex);
if (_thread.joinable()) {
_thread.join();
}
lock.unlock();
}

void Channel::StopInternalThread()
{
running = false;
}

// TODO: https://stackoverflow.com/questions/58477291/function-exceeds-stack-size-consider-moving-some-data-to-heap-c6262
void Channel::InternalThreadEntry()
{
while (running) {
std::unique_lock<std::mutex> lock(worker_mutex);

const uint32_t numberOfBytesToAskFor = 1 << 18; // 262,144
uint32_t numberOfBytesReceived = 0;
auto vector = std::vector<char>(numberOfBytesToAskFor);

int ret = usbmuxd_recv_timeout(conn, vector.data(),
numberOfBytesToAskFor,
&numberOfBytesReceived, 10);

if (ret == 0) {
if (getState() == State::Connecting) {
setState(State::Connected);
}

if (numberOfBytesReceived > 0) {
vector.resize(numberOfBytesReceived);

if (auto spt = delegate.lock()) {
spt->channelDidReceiveData(
vector);
}
}
lock.unlock();
} else {
// Unlock now as the `close()` function also requires
// a lock
lock.unlock();
portal_log("There was an error receiving data");
close();
setState(State::Errored);
}
}
}

} // namespace portal
Loading

0 comments on commit bd4d29e

Please sign in to comment.