UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IbmLiveStreaming.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
9
10#if defined(WITH_IBMRTMPINGEST) && LIVESTREAMING
11
12// IBM RTMP Ingest
14 extern "C" {
15 #include "rtmp_c/core/init.h"
16 #include "rtmp_c/net/rtmp/rtmpclient.h"
17 #include "rtmp_c/net/rtmp/modules/rtmpmodule_connect.h"
18 #include "rtmp_c/net/rtmp/modules/rtmpmodule_broadcaster.h"
19 #include "rtmp_c/core/rawdata.h"
20 #include "rtmp_c/core/platform/threadfunc.h"
21 #include "rtmp_c/codec/codecs.h"
22 }
24
25class FJsonObject;
26
28
30{
31public:
33
36
37 static FIbmLiveStreaming* Get();
38
42 bool Start();
46 bool Start(const FString& ClientId, const FString& ClientSecret, const FString& Channel, uint32 AudioSampleRate, uint32 AudioNumChannels, uint32 AudioBitrate);
50 void Stop();
51
52 static void StartCmd()
53 {
54 // We call Get(), so it creates the singleton
55 Get()->Start();
56 }
57
58 static void StopCmd()
59 {
60 if (Singleton)
61 {
62 Singleton->Stop();
63 }
64 }
65
66private:
67 enum class EState
68 {
69 None,
70 GettingAccessToken, // See https://developers.video.ibm.com/channel-api/getting-started.html#token-endpoint_8
71 GettingIngestSettings, // See https://developers.video.ibm.com/channel-api/channel.html#ingest-settings_77
75 };
76
77 struct FRtmpUrl
78 {
79 FString Host;
80 FString Application;
81 FString Channel;
82 FString ApplicationName;
83 FString StreamName;
84 FString StreamNamePrefix;
85 };
86
87 struct FIngest
88 {
89 FString StreamingKey;
91 };
92
93 struct FFormData
94 {
95 public:
96 FFormData();
97 void AddField(const FString& Name, const FString& Value);
98
99 // Call this in the end, to generate the HttpRequest
100 template<typename T1, typename T2>
102 {
103 auto HttpRequest = CreateHttpPostRequestImpl(URL);
104 HttpRequest->OnProcessRequestComplete().BindRaw(std::forward<T1>(TargetObj), std::forward<T2>(TargetObjHandler));
105 return HttpRequest;
106 }
107
108 private:
109 static TArray<uint8> FStringToUint8(const FString& InString);
110
111 FString BoundaryLabel;
112 FString BoundaryBegin;
113 FString BoundaryEnd;
114 FString Data;
115
117 };
118
119 // IGameplayMediaEncoderListener interface
120 void OnMediaSample(const FGameplayMediaEncoderSample& Sample) override;
121
128 void OnProcessRequestComplete(FHttpRequestPtr SourceHttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded);
129
130 static bool GetJsonField(const TSharedPtr<FJsonObject>& JsonObject, const FString& FieldName, FString& DestStr);
131 static bool GetJsonField(const TSharedPtr<FJsonObject>& JsonObject, const FString& FieldName, const TSharedPtr<FJsonObject>*& DestObj);
132
133 void Connect();
134
135 // Called to finalize a previous call to `Stop()`. This is called once
136 // the IBM library tells us the stream stopped and was deleted
139
140 //
141 // Callbacks for IBM RTMP C Ingest
142 //
143 static void OnConnectionError(RTMPModuleConnect* Module, RTMPEvent Evt, void* RejectInfoObj);
144 static void OnConnectionSuccess(RTMPModuleConnect* Module);
145 static void OnStreamPublished(RTMPModuleBroadcaster* Module);
146 static void OnStreamDeleted(RTMPModuleBroadcaster* Module);
147 static void OnStreamError(RTMPModuleBroadcaster* Module);
148 static void OnStopPublish(RTMPModuleBroadcaster* Module);
149 static void OnStreamBandwidthChanged(RTMPModuleBroadcaster* Module, unsigned long Bandwidth, int32 QueueWasEmpty);
157
158 static RawData* GetAvccHeader(const TArrayView<uint8>& DataView, const uint8** OutPpsEnd);
159 static RawData* GetVideoPacket(const TArrayView<uint8>& Data, bool bVideoKeyframe, const uint8* DataBegin);
160
161 void InjectVideo(uint32 TimestampMs, const TArrayView<uint8>& DataView, bool bIsKeyFrame);
162 void InjectAudio(uint32 TimestampMs, const TArrayView<uint8>& DataView);
163
164 template<typename T>
165 static const uint8* Bytes(T& t)
166 {
167 return reinterpret_cast<const uint8*>(&t);
168 }
169
170 // Custom log function for the IBM librayr
171 static void CustomLogMsg(const char* Msg);
172
173 // Utility functions to log an error if we are in the wrong state
174 bool CheckState(const wchar_t* FuncName, EState ExpectedState);
175 bool CheckState(const wchar_t* FuncName, EState ExpectedStateMin, EState ExpectedStateMax);
176
177 static FIbmLiveStreaming* Singleton;
178
179 // Initialization parameters
180 struct
181 {
182 FString ClientId;
183 FString ClientSecret;
184 FString Channel;
185 uint32 AudioSampleRate;
188 } Config;
189
190 FIngest Ingest;
191
192 TAtomic<EState> State = EState::None;
197 double FpsCalculationStartTime = 0;
198
199 struct
200 {
201 RTMPClient* Client = nullptr;
204 } Ctx;
206
207 // Helper function that locks Ctx and make the IBM call
209};
210
211#endif // WITH_IBMRTMPINGEST
212
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
#define THIRD_PARTY_INCLUDES_START
Definition GenericPlatformCompilerPreSetup.h:63
#define DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
Definition LogMacros.h:361
@ Stop
Definition PrecomputedVolumetricLightmapStreaming.cpp:26
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition JsonObject.h:23
Definition GameplayMediaEncoder.h:33
Definition ArrayView.h:139
Definition Array.h:670
Definition Atomic.h:538
Definition AndroidPlatformMisc.h:14
Definition SharedPointer.h:153
@ Stopping
Definition AudioMixerTypes.h:37
GeometryCollection::Facades::FMuscleActivationData Data
Definition MuscleActivationConstraints.h:15
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426
Definition Voronoi.cpp:10
@ Start
Definition GeoEnum.h:100
State
Definition PacketHandler.h:88
@ Config
Property should be loaded/saved to ini file as permanent profile.
Definition ObjectMacros.h:1053
Definition Timespan.h:76