style: add clang-format config and reformat scaffolding files

This commit is contained in:
2026-09-07 10:07:26 +02:00
parent 975e3b2974
commit 50369995c7
12 changed files with 100 additions and 64 deletions
+12
View File
@@ -83,6 +83,18 @@ meson test -C build --print-errorlogs
meson configure build meson configure build
``` ```
## Formatting
Run `clang-format` on every C++ source or header file before considering a
change complete. The project config is `.clang-format` at the repository root.
```sh
find include src tests -type f \( -name '*.cpp' -o -name '*.h' \) -exec clang-format -i {} +
```
If `clang-format` is not available, match the existing style in the file being
edited.
## References ## References
- `meson.build` (project root) - `meson.build` (project root)
+22
View File
@@ -0,0 +1,22 @@
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 120
BreakBeforeBraces: Attach
AllowShortFunctionsOnASingleLine: Empty
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
IndentCaseLabels: true
SpaceBeforeParens: ControlStatements
PointerAlignment: Left
SortIncludes: true
IncludeBlocks: Preserve
ReflowComments: true
AlignTrailingComments: true
BinPackArguments: false
BinPackParameters: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
+3
View File
@@ -253,6 +253,9 @@ reason in the plan.
- Keep headers minimal and free of unnecessary includes; forward-declare where - Keep headers minimal and free of unnecessary includes; forward-declare where
possible. possible.
- Format code with the configured formatter (`clang-format` if available). - Format code with the configured formatter (`clang-format` if available).
- Run `clang-format` on every C++ source or header file before considering a
change complete; do not commit unformatted code.
- The project formatter config is `.clang-format` at the repository root.
### Error handling ### Error handling
+5 -5
View File
@@ -7,19 +7,19 @@
namespace sc { namespace sc {
struct SendCommand { struct SendCommand {
std::string_view target = "monitor"; // monitor, window, region std::string_view target = "monitor"; // monitor, window, region
std::string_view peer_address; // optional std::string_view peer_address; // optional
int bitrate_kbps = 4000; int bitrate_kbps = 4000;
}; };
struct ReceiveCommand { struct ReceiveCommand {
std::string_view peer_address; // optional std::string_view peer_address; // optional
int local_rtp_port = 5004; int local_rtp_port = 5004;
}; };
using Command = std::variant<SendCommand, ReceiveCommand>; using Command = std::variant<SendCommand, ReceiveCommand>;
// Parse command line arguments. Prints usage and returns std::nullopt on error. // Parse command line arguments. Prints usage and returns std::nullopt on error.
std::optional<Command> parse_cli ( int argc, const char *argv[] ); std::optional<Command> parse_cli(int argc, const char* argv[]);
} // namespace sc } // namespace sc
+9 -11
View File
@@ -3,8 +3,6 @@
#include "screencast/capture/capture.h" #include "screencast/capture/capture.h"
#include "screencast/codec/decoder.h" #include "screencast/codec/decoder.h"
#include "screencast/codec/encoder.h" #include "screencast/codec/encoder.h"
#include "screencast/network/discovery.h"
#include "screencast/network/signaling.h"
#include "screencast/network/transport.h" #include "screencast/network/transport.h"
#include "screencast/render/renderer.h" #include "screencast/render/renderer.h"
@@ -30,11 +28,11 @@ struct ReceiverPipelineConfig {
// Sender pipeline: capture → encode → packetize → RTP/UDP. // Sender pipeline: capture → encode → packetize → RTP/UDP.
class SenderPipeline { class SenderPipeline {
public: public:
explicit SenderPipeline ( SenderPipelineConfig config ); explicit SenderPipeline(SenderPipelineConfig config);
~SenderPipeline (); ~SenderPipeline();
bool start (); bool start();
void stop (); void stop();
private: private:
class Impl; class Impl;
@@ -44,15 +42,15 @@ class SenderPipeline {
// Receiver pipeline: RTP/UDP → depacketize → decode → render. // Receiver pipeline: RTP/UDP → depacketize → decode → render.
class ReceiverPipeline { class ReceiverPipeline {
public: public:
explicit ReceiverPipeline ( ReceiverPipelineConfig config ); explicit ReceiverPipeline(ReceiverPipelineConfig config);
~ReceiverPipeline (); ~ReceiverPipeline();
bool start (); bool start();
void stop (); void stop();
private: private:
class Impl; class Impl;
std::unique_ptr<Impl> impl_; std::unique_ptr<Impl> impl_;
}; };
} // namespace sc } // namespace sc
+8 -7
View File
@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cstdint>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
@@ -14,22 +15,22 @@ struct DiscoveredPeer {
class DiscoveryService { class DiscoveryService {
public: public:
using PeerCallback = std::function<void ( const DiscoveredPeer & )>; using PeerCallback = std::function<void(const DiscoveredPeer&)>;
virtual ~DiscoveryService () = default; virtual ~DiscoveryService() = default;
// Announce this peer on the LAN. // Announce this peer on the LAN.
virtual bool announce ( const std::string &service_name, uint16_t signaling_port ) = 0; virtual bool announce(const std::string& service_name, uint16_t signaling_port) = 0;
// Browse for peers; callback is invoked for each newly found service. // Browse for peers; callback is invoked for each newly found service.
virtual bool browse ( PeerCallback on_peer ) = 0; virtual bool browse(PeerCallback on_peer) = 0;
virtual void stop () = 0; virtual void stop() = 0;
}; };
class DiscoveryFactory { class DiscoveryFactory {
public: public:
static std::unique_ptr<DiscoveryService> create_avahi (); static std::unique_ptr<DiscoveryService> create_avahi();
}; };
} // namespace sc } // namespace sc
+7 -6
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <optional>
#include <span> #include <span>
#include <vector> #include <vector>
@@ -13,21 +14,21 @@ struct RtpHeader {
bool extension = false; bool extension = false;
uint8_t csrc_count = 0; uint8_t csrc_count = 0;
bool marker = false; bool marker = false;
uint7_t payload_type = 96; // dynamic uint8_t payload_type = 96; // dynamic
uint16_t sequence_number = 0; uint16_t sequence_number = 0;
uint32_t timestamp = 0; uint32_t timestamp = 0;
uint32_t ssrc = 0; uint32_t ssrc = 0;
bool serialize ( std::span<std::byte, 12> out ) const noexcept; bool serialize(std::span<std::byte, 12> out) const noexcept;
static std::optional<RtpHeader> parse ( std::span<const std::byte, 12> in ) noexcept; static std::optional<RtpHeader> parse(std::span<const std::byte, 12> in) noexcept;
}; };
struct RtpPacket { struct RtpPacket {
RtpHeader header; RtpHeader header;
std::vector<std::byte> payload; std::vector<std::byte> payload;
std::vector<std::byte> serialize () const; std::vector<std::byte> serialize() const;
static std::optional<RtpPacket> parse ( std::span<const std::byte> in ) noexcept; static std::optional<RtpPacket> parse(std::span<const std::byte> in) noexcept;
}; };
} // namespace sc } // namespace sc
+11 -9
View File
@@ -2,9 +2,11 @@
#include "screencast/network/transport.h" #include "screencast/network/transport.h"
#include <cstdint>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
#include <variant>
namespace sc { namespace sc {
@@ -28,20 +30,20 @@ using SignalingMessage = std::variant<SessionOffer, SessionAnswer>;
class SignalingChannel { class SignalingChannel {
public: public:
using MessageCallback = std::function<void ( const SignalingMessage & )>; using MessageCallback = std::function<void(const SignalingMessage&)>;
virtual ~SignalingChannel () = default; virtual ~SignalingChannel() = default;
virtual bool connect ( const Endpoint &server ) = 0; virtual bool connect(const Endpoint& server) = 0;
virtual void send ( const SignalingMessage &message ) = 0; virtual void send(const SignalingMessage& message) = 0;
virtual void on_message ( MessageCallback callback ) = 0; virtual void on_message(MessageCallback callback) = 0;
virtual void disconnect () = 0; virtual void disconnect() = 0;
}; };
class SignalingFactory { class SignalingFactory {
public: public:
static std::unique_ptr<SignalingChannel> create_websocket_client (); static std::unique_ptr<SignalingChannel> create_websocket_client();
static std::unique_ptr<SignalingChannel> create_websocket_server ( uint16_t port ); static std::unique_ptr<SignalingChannel> create_websocket_server(uint16_t port);
}; };
} // namespace sc } // namespace sc
+8 -8
View File
@@ -17,27 +17,27 @@ struct Endpoint {
// UDP transport for RTP packets. Owned by the sender or receiver pipeline. // UDP transport for RTP packets. Owned by the sender or receiver pipeline.
class RtpTransport { class RtpTransport {
public: public:
using ReceiveCallback = std::function<void ( RtpPacket )>; using ReceiveCallback = std::function<void(RtpPacket)>;
virtual ~RtpTransport () = default; virtual ~RtpTransport() = default;
// Bind locally and start the receive loop. Callback is invoked on the // Bind locally and start the receive loop. Callback is invoked on the
// transport's thread. // transport's thread.
virtual bool start ( const Endpoint &local_endpoint, ReceiveCallback on_receive ) = 0; virtual bool start(const Endpoint& local_endpoint, ReceiveCallback on_receive) = 0;
// Send a packet to the configured peer. // Send a packet to the configured peer.
virtual bool send ( const RtpPacket &packet ) = 0; virtual bool send(const RtpPacket& packet) = 0;
// Set the peer endpoint dynamically (e.g. after signaling). // Set the peer endpoint dynamically (e.g. after signaling).
virtual void set_peer ( const Endpoint &peer ) = 0; virtual void set_peer(const Endpoint& peer) = 0;
// Stop the transport and close sockets. // Stop the transport and close sockets.
virtual void stop () = 0; virtual void stop() = 0;
}; };
class RtpTransportFactory { class RtpTransportFactory {
public: public:
static std::unique_ptr<RtpTransport> create (); static std::unique_ptr<RtpTransport> create();
}; };
} // namespace sc } // namespace sc
+6 -6
View File
@@ -6,14 +6,14 @@ namespace sc {
// RTP clock rate for video is 90 kHz. Convert a monotonic nanosecond timestamp // RTP clock rate for video is 90 kHz. Convert a monotonic nanosecond timestamp
// into a 32-bit RTP timestamp. Overflows wrap around intentionally. // into a 32-bit RTP timestamp. Overflows wrap around intentionally.
constexpr uint32_t rtp_timestamp_from_ns ( uint64_t nanoseconds ) noexcept { constexpr uint32_t rtp_timestamp_from_ns(uint64_t nanoseconds) noexcept {
// (nanoseconds * 90_000) / 1_000_000_000 // (nanoseconds * 90_000) / 1_000_000_000
// Simplify: 90_000 / 1_000_000_000 = 9 / 100_000 // Simplify: 90_000 / 1_000_000_000 = 9 / 100_000
return static_cast<uint32_t> ( ( nanoseconds * 9ULL ) / 100'000ULL ); return static_cast<uint32_t>((nanoseconds * 9ULL) / 100'000ULL);
} }
static_assert ( rtp_timestamp_from_ns ( 0 ) == 0 ); static_assert(rtp_timestamp_from_ns(0) == 0);
static_assert ( rtp_timestamp_from_ns ( 1'000'000'000ULL ) == 90'000 ); static_assert(rtp_timestamp_from_ns(1'000'000'000ULL) == 90'000);
static_assert ( rtp_timestamp_from_ns ( 500'000'000ULL ) == 45'000 ); static_assert(rtp_timestamp_from_ns(500'000'000ULL) == 45'000);
} // namespace sc } // namespace sc
+1 -4
View File
@@ -7,10 +7,7 @@ project('screen_cast', 'cpp',
'buildtype=release', 'buildtype=release',
]) ])
# Public and private include directories # Public and private include directories are declared in `src/meson.build`.
inc = include_directories('include')
# Core public headers / include dependency defined in src/meson.build.
subdir('src') subdir('src')
# Tests # Tests
+8 -8
View File
@@ -3,18 +3,18 @@
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
int main () { int main() {
assert ( sc::rtp_timestamp_from_ns ( 0 ) == 0u ); assert(sc::rtp_timestamp_from_ns(0) == 0u);
assert ( sc::rtp_timestamp_from_ns ( 1'000'000'000ULL ) == 90'000u ); assert(sc::rtp_timestamp_from_ns(1'000'000'000ULL) == 90'000u);
assert ( sc::rtp_timestamp_from_ns ( 500'000'000ULL ) == 45'000u ); assert(sc::rtp_timestamp_from_ns(500'000'000ULL) == 45'000u);
assert ( sc::rtp_timestamp_from_ns ( 16'666'667ULL ) == 1'500u ); assert(sc::rtp_timestamp_from_ns(16'666'667ULL) == 1'500u);
// A multiple of the wrap period should wrap back to zero. // A multiple of the wrap period should wrap back to zero.
// Period in ns = ceil(2^32 * 100_000 / 9) because the helper uses // Period in ns = ceil(2^32 * 100_000 / 9) because the helper uses
// integer floor division (ns * 9 / 100_000). // integer floor division (ns * 9 / 100_000).
constexpr uint64_t period_ns = ( ( 1ULL << 32 ) * 100'000ULL + 9ULL - 1ULL ) / 9ULL; constexpr uint64_t period_ns = ((1ULL << 32) * 100'000ULL + 9ULL - 1ULL) / 9ULL;
static_assert ( sc::rtp_timestamp_from_ns ( period_ns ) == 0u ); static_assert(sc::rtp_timestamp_from_ns(period_ns) == 0u);
static_assert ( sc::rtp_timestamp_from_ns ( period_ns + 1'000'000'000ULL ) == 90'000u ); static_assert(sc::rtp_timestamp_from_ns(period_ns + 1'000'000'000ULL) == 90'000u);
return 0; return 0;
} }