Skip to content

Commit

Permalink
Merge branch 'master' of github.com:john-preston/tgcalls into macos-s…
Browse files Browse the repository at this point in the history
…table

- screen capture for macos
  • Loading branch information
overtake committed Aug 25, 2020
2 parents 4f3b1fb + db23f73 commit e8e9dd8
Show file tree
Hide file tree
Showing 24 changed files with 207 additions and 133 deletions.
15 changes: 8 additions & 7 deletions tgcalls/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ namespace tgcalls {

class VideoCaptureInterface;

#ifndef _WIN32
using FilePath = std::string;
#else
using FilePath = std::wstring;
#endif

struct Proxy {
std::string host;
uint16_t port = 0;
Expand Down Expand Up @@ -99,13 +105,8 @@ struct Config {
bool enableAGC = false;
bool enableCallUpgrade = false;
bool enableVolumeControl = false;
#ifndef _WIN32
std::string logPath;
std::string statsLogPath;
#else
std::wstring logPath;
std::wstring statsLogPath;
#endif
FilePath logPath;
FilePath statsLogPath;
int maxApiLayer = 0;
bool enableHighBitrateVideo = false;
std::vector<std::string> preferredVideoCodecs;
Expand Down
23 changes: 0 additions & 23 deletions tgcalls/InstanceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,6 @@ void InstanceImpl::stop(std::function<void(FinalState)> completion) {
});
}

/*void InstanceImpl::controllerStateCallback(Controller::State state) {
if (onStateUpdated_) {
const auto mappedState = [&] {
switch (state) {
case Controller::State::WaitInit:
return State::WaitInit;
case Controller::State::WaitInitAck:
return State::WaitInitAck;
case Controller::State::Established:
return State::Estabilished;
case Controller::State::Failed:
return State::Failed;
case Controller::State::Reconnecting:
return State::Reconnecting;
default:
return State::Estabilished;
}
}();
onStateUpdated_(mappedState);
}
}*/

int InstanceImpl::GetConnectionMaxLayer() {
return 92; // TODO: retrieve from LayerBase
}
Expand Down
1 change: 0 additions & 1 deletion tgcalls/InstanceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class InstanceImpl final : public Instance {
TrafficStats getTrafficStats() override;
PersistentState getPersistentState() override;
void stop(std::function<void(FinalState)> completion) override;
//void controllerStateCallback(Controller::State state);

private:
std::unique_ptr<ThreadLocalObject<Manager>> _manager;
Expand Down
39 changes: 21 additions & 18 deletions tgcalls/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ rtc::Thread *makeMediaThread() {
return value.get();
}

void dumpStatsLog(const std::string path, const CallStats &stats) {
void dumpStatsLog(const FilePath &path, const CallStats &stats) {
if (path.empty()) {
return;
}
std::ofstream file;
file.open(path);

file << "{";
file << "\"v\":\"" << 1 << "\"";
file << ",";

file << "\"codec\":\"" << stats.outgoingCodec << "\"";
file << ",";

file << "\"bitrate\":[";
bool addComma = false;
for (auto &it : stats.bitrateRecords) {
Expand All @@ -52,7 +55,7 @@ void dumpStatsLog(const std::string path, const CallStats &stats) {
}
file << "]";
file << ",";

file << "\"network\":[";
addComma = false;
for (auto &it : stats.networkRecords) {
Expand All @@ -69,9 +72,9 @@ void dumpStatsLog(const std::string path, const CallStats &stats) {
addComma = true;
}
file << "]";

file << "}";

file.close();
}

Expand Down Expand Up @@ -122,7 +125,7 @@ _dataSaving(descriptor.config.dataSaving) {
assert(_thread->IsCurrent());
assert(_stateUpdated != nullptr);
assert(_signalingDataEmitted != nullptr);

_preferredCodecs = descriptor.config.preferredVideoCodecs;

_sendSignalingMessage = [=](const Message &message) {
Expand Down Expand Up @@ -207,7 +210,7 @@ void Manager::start() {
strong->_mediaManager->perform(RTC_FROM_HERE, [=](MediaManager *mediaManager) {
mediaManager->setIsConnected(state.isReadyToSendData);
});

if (isFirstConnection) {
strong->sendInitialSignalingMessages();
}
Expand Down Expand Up @@ -354,7 +357,7 @@ void Manager::setIsLocalNetworkLowCost(bool isLocalNetworkLowCost) {
_networkManager->perform(RTC_FROM_HERE, [isLocalNetworkLowCost](NetworkManager *networkManager) {
networkManager->setIsLocalNetworkLowCost(isLocalNetworkLowCost);
});

_localNetworkIsLowCost = isLocalNetworkLowCost;
updateCurrentResolvedNetworkStatus();
}
Expand All @@ -363,16 +366,16 @@ void Manager::setIsLocalNetworkLowCost(bool isLocalNetworkLowCost) {
void Manager::getNetworkStats(std::function<void (TrafficStats, CallStats)> completion) {
_networkManager->perform(RTC_FROM_HERE, [thread = _thread, weak = std::weak_ptr<Manager>(shared_from_this()), completion = std::move(completion), statsLogPath = _statsLogPath](NetworkManager *networkManager) {
auto networkStats = networkManager->getNetworkStats();

CallStats callStats;
networkManager->fillCallStats(callStats);

thread->PostTask(RTC_FROM_HERE, [weak, networkStats, completion = std::move(completion), callStats = std::move(callStats), statsLogPath = statsLogPath] {
const auto strong = weak.lock();
if (!strong) {
return;
}

strong->_mediaManager->perform(RTC_FROM_HERE, [networkStats, completion = std::move(completion), callStatsValue = std::move(callStats), statsLogPath = statsLogPath](MediaManager *mediaManager) {
CallStats callStats = std::move(callStatsValue);
mediaManager->fillCallStats(callStats);
Expand All @@ -397,14 +400,14 @@ void Manager::updateCurrentResolvedNetworkStatus() {
default:
break;
}

ResolvedNetworkStatus localStatus;
localStatus.isLowCost = _localNetworkIsLowCost;
localStatus.isLowDataRequested = localIsLowDataRequested;

if (!_currentResolvedLocalNetworkStatus.has_value() || *_currentResolvedLocalNetworkStatus != localStatus) {
_currentResolvedLocalNetworkStatus = localStatus;

switch (_protocolVersion) {
case ProtocolVersion::V1:
if (_didConnectOnce) {
Expand All @@ -415,11 +418,11 @@ void Manager::updateCurrentResolvedNetworkStatus() {
break;
}
}

ResolvedNetworkStatus status;
status.isLowCost = _localNetworkIsLowCost && _remoteNetworkIsLowCost;
status.isLowDataRequested = localIsLowDataRequested || _remoteIsLowDataRequested;

if (!_currentResolvedNetworkStatus.has_value() || *_currentResolvedNetworkStatus != status) {
_currentResolvedNetworkStatus = status;
_mediaManager->perform(RTC_FROM_HERE, [status](MediaManager *mediaManager) {
Expand Down
8 changes: 4 additions & 4 deletions tgcalls/Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Manager final : public std::enable_shared_from_this<Manager> {
struct ResolvedNetworkStatus {
bool isLowCost = false;
bool isLowDataRequested = false;

bool operator==(const ResolvedNetworkStatus &rhs);
bool operator!=(const ResolvedNetworkStatus &rhs);
};

public:
static rtc::Thread *getMediaThread();

Expand All @@ -35,7 +35,7 @@ class Manager final : public std::enable_shared_from_this<Manager> {
void setIsLowBatteryLevel(bool isLowBatteryLevel);
void setIsLocalNetworkLowCost(bool isLocalNetworkLowCost);
void getNetworkStats(std::function<void(TrafficStats, CallStats)> completion);


void setAudioInputDevice(std::string id);
void setAudioOutputDevice(std::string id);
Expand All @@ -55,7 +55,7 @@ class Manager final : public std::enable_shared_from_this<Manager> {
bool _enableTCP = false;
bool _enableStunMarking = false;
ProtocolVersion _protocolVersion = ProtocolVersion::V0;
std::string _statsLogPath;
FilePath _statsLogPath;
std::vector<RtcServer> _rtcServers;
MediaDevicesConfig _mediaDevicesConfig;
std::shared_ptr<VideoCaptureInterface> _videoCapture;
Expand Down
54 changes: 37 additions & 17 deletions tgcalls/MediaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class VideoSinkInterfaceProxyImpl : public rtc::VideoSinkInterface<webrtc::Video
VideoSinkInterfaceProxyImpl(bool rewriteRotation) :
_rewriteRotation(rewriteRotation) {
}

virtual ~VideoSinkInterfaceProxyImpl() {
}

virtual void OnFrame(const webrtc::VideoFrame& frame) override {
if (_impl) {
if (_rewriteRotation) {
Expand All @@ -65,21 +65,21 @@ class VideoSinkInterfaceProxyImpl : public rtc::VideoSinkInterface<webrtc::Video
}
}
}

virtual void OnDiscardedFrame() override {
if (_impl) {
_impl->OnDiscardedFrame();
}
}

void setSink(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) {
_impl = impl;
}

private:
bool _rewriteRotation = false;
std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> _impl;

};

rtc::Thread *MediaManager::getWorkerThread() {
Expand Down Expand Up @@ -120,7 +120,7 @@ _enableHighBitrateVideo(enableHighBitrateVideo) {
break;
}
_incomingVideoSinkProxy.reset(new VideoSinkInterfaceProxyImpl(rewriteFrameRotation));

_ssrcAudio.incoming = isOutgoing ? ssrcAudioIncoming : ssrcAudioOutgoing;
_ssrcAudio.outgoing = (!isOutgoing) ? ssrcAudioIncoming : ssrcAudioOutgoing;
_ssrcAudio.fecIncoming = isOutgoing ? ssrcAudioFecIncoming : ssrcAudioFecOutgoing;
Expand Down Expand Up @@ -159,9 +159,12 @@ _enableHighBitrateVideo(enableHighBitrateVideo) {
preferredCodecs);

mediaDeps.audio_processing = webrtc::AudioProcessingBuilder().Create();
_audioDeviceModule = mediaDeps.adm = webrtc::AudioDeviceModule::Create(
webrtc::AudioDeviceModule::kPlatformDefaultAudio,
_taskQueueFactory.get());

_audioDeviceModule = createAudioDeviceModule();
if (!_audioDeviceModule) {
return;
}
mediaDeps.adm = _audioDeviceModule;

_mediaEngine = cricket::CreateMediaEngine(std::move(mediaDeps));
_mediaEngine->Init();
Expand All @@ -181,7 +184,7 @@ _enableHighBitrateVideo(enableHighBitrateVideo) {
audioOptions.echo_cancellation = true;
audioOptions.noise_suppression = true;
audioOptions.audio_jitter_buffer_fast_accelerate = true;

std::vector<std::string> streamIds;
streamIds.push_back("1");

Expand Down Expand Up @@ -236,10 +239,27 @@ _enableHighBitrateVideo(enableHighBitrateVideo) {
_audioChannel->SetPlayout(true);

_videoChannel->SetInterface(_videoNetworkInterface.get(), webrtc::MediaTransportConfig());

adjustBitratePreferences(true);
}

rtc::scoped_refptr<webrtc::AudioDeviceModule> MediaManager::createAudioDeviceModule() {
const auto check = [&](webrtc::AudioDeviceModule::AudioLayer layer) {
auto result = webrtc::AudioDeviceModule::Create(
layer,
_taskQueueFactory.get());
return (result && (result->Init() == 0)) ? result : nullptr;
};
if (auto result = check(webrtc::AudioDeviceModule::kPlatformDefaultAudio)) {
return result;
#ifdef WEBRTC_LINUX
} else if (auto result = check(webrtc::AudioDeviceModule::kLinuxAlsaAudio)) {
return result;
#endif // WEBRTC_LINUX
}
return nullptr;
}

void MediaManager::start() {
_sendSignalingMessage({ _myVideoFormats });

Expand Down Expand Up @@ -367,7 +387,7 @@ void MediaManager::collectStats() {
if (_signalBarsUpdated) {
_signalBarsUpdated((int)(adjustedQuality * signalBarsNorm));
}

_bitrateRecords.push_back(CallStatsBitrateRecord { (int32_t)(rtc::TimeMillis() / 1000), stats.send_bandwidth_bps / 1000 });

beginStatsTimer(2000);
Expand Down Expand Up @@ -502,7 +522,7 @@ void MediaManager::configureSendingVideoIfNeeded() {
} else {
_videoChannel->AddSendStream(cricket::StreamParams::CreateLegacy(_ssrcVideo.outgoing));
}

adjustBitratePreferences(true);
}

Expand All @@ -526,7 +546,7 @@ void MediaManager::checkIsSendingVideoChanged(bool wasSending) {
_videoChannel->SetVideoSend(_ssrcVideo.outgoing, NULL, nullptr);
_videoChannel->SetVideoSend(_ssrcVideo.fecOutgoing, NULL, nullptr);
}

adjustBitratePreferences(true);
}

Expand All @@ -550,7 +570,7 @@ void MediaManager::adjustBitratePreferences(bool resetStartBitrate) {
preferences.start_bitrate_bps = 400000;
}
preferences.max_bitrate_bps = getMaxVideoBitrate();

_call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences);
} else {
webrtc::BitrateConstraints preferences;
Expand All @@ -569,7 +589,7 @@ void MediaManager::adjustBitratePreferences(bool resetStartBitrate) {
}
preferences.max_bitrate_bps = getMaxAudioBitrate();
}

_call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences);
}
}
Expand Down
2 changes: 2 additions & 0 deletions tgcalls/MediaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class MediaManager : public sigslot::has_slots<>, public std::enable_shared_from
void sendVideoParametersMessage();
void sendOutgoingMediaStateMessage();

rtc::scoped_refptr<webrtc::AudioDeviceModule> createAudioDeviceModule();

void beginStatsTimer(int timeoutMs);
void collectStats();

Expand Down
2 changes: 0 additions & 2 deletions tgcalls/VideoCapturerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class VideoCapturerInterface {
virtual void setPreferredCaptureAspectRatio(float aspectRatio) = 0;
virtual void setUncroppedOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) = 0;

virtual void enableScreenCast() = 0;
virtual void disableScreenCast() = 0;
};

} // namespace tgcalls
Expand Down
3 changes: 3 additions & 0 deletions tgcalls/legacy/InstanceImplLegacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ void InstanceImplLegacy::receiveSignalingData(const std::vector<uint8_t> &data)
void InstanceImplLegacy::setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) {
}

void InstanceImplLegacy::setRequestedVideoAspect(float aspect) {
}

void InstanceImplLegacy::setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) {
}

Expand Down
Loading

0 comments on commit e8e9dd8

Please sign in to comment.