Repository navigation
Expand file tree
/
Copy pathgroup_call_join.cpp
More file actions
101 lines (91 loc) · 3.61 KB
/
Copy pathgroup_call_join.cpp
File metadata and controls
101 lines (91 loc) · 3.61 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
// Joins the active group call of a chat. CppGram does the phone.* signaling. The media engine is a separate program.
//
// Environment:
// TG_API_ID, TG_API_HASH API credentials
// TG_SESSION session file of an account that is already logged in
// CPPGRAM_JOIN_PAYLOAD file holding the join payload (JSON) that the media engine produced
// CPPGRAM_AUDIO_SSRC the audio ssrc declared in that payload (needed later by leave)
// CPPGRAM_CONNECTION_OUT file the server's connection parameters are written to, for the media engine
// Usage: group_call_join <username|me>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
#include "cppgram/client.h"
#include "cppgram/group_call.h"
namespace {
const char* env_or_throw(const char* name) {
const char* v = std::getenv(name);
if (v == nullptr || *v == '\0')
throw std::runtime_error(std::string("set ") + name);
return v;
}
// The media engine lives outside this process. This adapter reads its join payload from a file and writes the server's
// answer to another file, so the engine (for example an NTgCalls-based helper) can pick it up.
class FileVoiceEngine : public cppgram::GroupCallVoiceEngine {
public:
FileVoiceEngine(std::string payload_path, std::string connection_path, std::int32_t source)
: payload_path_(std::move(payload_path))
, connection_path_(std::move(connection_path))
, source_(source) {
}
std::string join_payload() override {
std::ifstream in(payload_path_);
if (!in)
throw std::runtime_error("cannot read join payload: " + payload_path_);
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
}
std::int32_t audio_source() const override {
return source_;
}
void apply_connection(std::string_view params_json) override {
std::ofstream out(connection_path_, std::ios::trunc);
if (!out)
throw std::runtime_error("cannot write connection file: " + connection_path_);
out << params_json;
}
void stop() noexcept override {
std::cout << "media engine stopped\n";
}
private:
std::string payload_path_;
std::string connection_path_;
std::int32_t source_;
};
} // namespace
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "usage: group_call_join <username|me>\n";
return 2;
}
try {
cppgram::ClientOptions options;
options.api_id = std::stoi(env_or_throw("TG_API_ID"));
options.api_hash = env_or_throw("TG_API_HASH");
options.session_path = env_or_throw("TG_SESSION");
cppgram::Client client(options);
client.connect();
const cppgram::PeerRef chat = client.resolve(argv[1]);
const auto call = client.find_group_call(chat);
if (!call) {
std::cerr << "no active group call in that chat\n";
client.disconnect();
return 1;
}
FileVoiceEngine engine(env_or_throw("CPPGRAM_JOIN_PAYLOAD"), env_or_throw("CPPGRAM_CONNECTION_OUT"),
std::stoi(env_or_throw("CPPGRAM_AUDIO_SSRC")));
cppgram::GroupCallSession session(client, *call, engine);
session.join(client.resolve("me"), /*muted=*/true);
std::cout << "joined call " << call->id << "; press Enter to leave\n";
std::cin.get();
session.leave();
client.disconnect();
} catch (const std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}