MCPcopy Create free account
hub / github.com/RGAA-Software/GoDesk / Encode

Method Encode

src/render/plugins/opus_encoder/opus_encoder_plugin.cpp:62–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60 }
61
62 void OpusEncoderPlugin::Encode(const std::shared_ptr<Data> &data, int samples, int channels, int bits) {
63 if (!opus_encoder_) {
64 // audio cache
65 audio_cache_ = Data::Make(nullptr, 1024*16);
66 LOGI("audio format, samples: {}, channels: {}, bits: {}", samples, channels, bits);
67 opus_encoder_ = std::make_shared<OpusAudioEncoder>(samples, channels, bits, OPUS_APPLICATION_AUDIO);
68 if (!opus_encoder_->valid()) {
69 opus_encoder_ = nullptr;
70 return;
71 }
72 opus_encoder_->SetComplexity(8);
73 }
74
75 if (debug_opus_decoder_) {
76 static auto pcm_file = File::OpenForWriteB("1.opus.encoder.plugin.origin.pcm");
77 pcm_file->Append((char*)data->DataAddr(), data->Size());
78 }
79
80 PostWorkTask([=, this]() {
81 audio_cache_->Append(data->DataAddr(), data->Size());
82 // 2 or 6
83 if (++audio_callback_count_ < 2) {
84 return;
85 }
86
87 //int frame_size = data->Size() / 2 / 2;
88 int frame_size = audio_cache_->Offset()/2/2;
89 auto encoded_frames = opus_encoder_->Encode(audio_cache_->CStr(), audio_cache_->Offset(), frame_size);
90 for (const auto& ef : encoded_frames) {
91 auto encoded_data = Data::Make((char*)ef.data(), ef.size());
92
93 auto event = std::make_shared<GrPluginEncodedAudioFrameEvent>();
94 event->sample_rate_ = samples;
95 event->channels_ = channels;
96 event->bits_ = bits;
97 event->frame_size_ = frame_size;
98 event->data_ = encoded_data;
99 CallbackEventDirectly(event);
100
101 if (debug_opus_decoder_) {
102 if (!opus_decoder_) {
103 opus_decoder_ = std::make_shared<OpusAudioDecoder>(opus_encoder_->SampleRate(), opus_encoder_->Channels());
104 }
105 std::vector<unsigned char> buffer(ef.begin(), ef.end());
106 auto pcm_data = opus_decoder_->Decode(buffer, frame_size, false);
107 static auto pcm_file = File::OpenForWriteB("1.test.pcm");
108 pcm_file->Append((char*)pcm_data.data(), pcm_data.size()*2);
109 }
110 }
111
112 audio_cache_->Reset();
113 audio_callback_count_ = 0;
114
115 });
116 }
117
118}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected