-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcapture_utils.cpp
More file actions
119 lines (102 loc) · 3.64 KB
/
Copy pathcapture_utils.cpp
File metadata and controls
119 lines (102 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2025 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "capture_utils.h"
#include <array>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <random>
#include <thread>
#include "livekit/livekit.h"
using namespace livekit;
// Test utils to run a capture loop to publish noisy audio frames to the room
void runNoiseCaptureLoop(const std::shared_ptr<AudioSource>& source, std::atomic<bool>& running_flag) {
const int sample_rate = source->sample_rate();
const int num_channels = source->num_channels();
const int frame_ms = 10;
const int samples_per_channel = sample_rate * frame_ms / 1000;
// White noise generator (keep amplitude conservative to avoid clipping)
std::mt19937 rng{std::random_device{}()};
std::uniform_int_distribution<int> dist(-3000, 3000);
using Clock = std::chrono::steady_clock;
auto next_deadline = Clock::now();
while (running_flag.load(std::memory_order_relaxed)) {
AudioFrame frame = AudioFrame::create(sample_rate, num_channels, samples_per_channel);
auto& pcm = frame.data();
for (auto& s : pcm) {
s = static_cast<int16_t>(dist(rng));
}
try {
source->captureFrame(frame);
} catch (const std::exception& e) {
std::cerr << "Error in captureFrame (noise): " << e.what() << std::endl;
break;
}
next_deadline += std::chrono::milliseconds(frame_ms);
std::this_thread::sleep_until(next_deadline);
}
try {
source->clearQueue();
} catch (...) {
std::cerr << "Error in clearQueue (noise)" << std::endl;
}
}
// Fake video source: solid color cycling
void runFakeVideoCaptureLoop(const std::shared_ptr<VideoSource>& source, std::atomic<bool>& running_flag) {
auto frame = VideoFrame::create(1280, 720, VideoBufferType::RGBA);
const double framerate = 1.0 / 30.0;
while (running_flag.load(std::memory_order_relaxed)) {
static auto start = std::chrono::high_resolution_clock::now();
float t = std::chrono::duration<float>(std::chrono::high_resolution_clock::now() - start).count();
// Cycle every 4 seconds: 0=red, 1=green, 2=blue, 3=black
int stage = static_cast<int>(t) % 4;
std::array<uint8_t, 4> rgb{};
switch (stage) {
case 0: // red
rgb = {255, 0, 0, 0};
break;
case 1: // green
rgb = {0, 255, 0, 0};
break;
case 2: // blue
rgb = {0, 0, 255, 0};
break;
case 3: // black
default:
rgb = {0, 0, 0, 0};
break;
}
// ARGB
uint8_t* data = frame.data();
const size_t size = frame.dataSize();
for (size_t i = 0; i < size; i += 4) {
data[i + 0] = 255; // A
data[i + 1] = rgb[0]; // R
data[i + 2] = rgb[1]; // G
data[i + 3] = rgb[2]; // B
}
try {
// If VideoSource is ARGB-capable, pass frame.
// If it expects I420, pass i420 instead.
source->captureFrame(frame, 0, VideoRotation::VIDEO_ROTATION_0);
} catch (const std::exception& e) {
std::cerr << "Error in captureFrame (fake video): " << e.what() << std::endl;
break;
}
std::this_thread::sleep_for(std::chrono::duration<double>(framerate));
}
}