Twitch SDK (Internal)
streamer.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
13 #include "twitchsdk/core/timer.h"
19 
20 namespace ttv
21 {
22  class TaskRunner;
23  class TrackingContext;
24 
25  namespace broadcast
26  {
27  class Streamer;
28  struct StreamerContext;
29  class AudioStreamer;
30  class FlvMuxer;
31  class FrameWriter;
32  class StreamStats;
33  class VideoFrame;
34  class VideoStreamer;
35  class IVideoEncoder;
36  class IVideoCapture;
37  class IAudioCapture;
38  class IAudioMixer;
39  class StreamerListenerProxy;
40  class IBandwidthStatListener;
41  class IMuxer;
42  }
43 }
44 
45 
47 {
49  : isBusy(false)
51  {}
52 
53  std::atomic_bool isBusy;
54  std::shared_ptr<TrackingContext> sharedTrackingContext;
56  std::string category;
57  std::string title;
58 };
59 
60 
72 {
73 public:
74  enum class StreamerState
75  {
76  Stopped,
77  Starting,
78  Started,
79  Stopping
80  };
81 
82  struct StartParams
83  {
87  std::wstring outputFile;
88  };
89 
90  using StartCallback = std::function<void(TTV_ErrorCode ec)>;
91  using StopCallback = std::function<void(TTV_ErrorCode ec)>;
92  using FrameWriterDelayStateChangedCallback = std::function<void(FrameWriter::DelayState state)>;
93 
94  class IListener
95  {
96  public:
100  virtual void OnStartComplete(Streamer* source, TTV_ErrorCode ec) = 0;
104  virtual void OnStopComplete(Streamer* source, TTV_ErrorCode ec) = 0;
108  virtual void OnStreamerStateChanged(Streamer* source, StreamerState state, TTV_ErrorCode ec) = 0;
112  virtual void OnBandwidthWarning(Streamer* source, TTV_ErrorCode ec, uint32_t backupMilliseconds) = 0;
116  virtual void OnStreamInfoFetched(Streamer* source, TTV_ErrorCode ec, const StreamInfo& streamInfo) = 0;
117  };
118 
119 public:
120  Streamer(const std::shared_ptr<User>& user, const std::shared_ptr<StreamerContext>& context);
121  virtual ~Streamer();
122 
123  void AddListener(const std::shared_ptr<IListener>& listener);
124  void RemoveListener(const std::shared_ptr<IListener>& listener);
125 
126  void AddBandwidthStatListener(const std::shared_ptr<IBandwidthStatListener>& listener);
127  void RemoveBandwidthStatListener(const std::shared_ptr<IBandwidthStatListener>& listener);
128 
132  TTV_ErrorCode SetVideoEncoder(const std::shared_ptr<IVideoEncoder>& encoder);
133  TTV_ErrorCode SetAudioEncoder(const std::shared_ptr<IAudioEncoder>& encoder);
134  TTV_ErrorCode SetCustomMuxer(const std::shared_ptr<IMuxer>& muxer);
135 
139  void SetBroadcasterSoftware(const std::string& str) { mBroadcasterSoftware = str; }
140 
144  void SetForceArchiveBroadcast(bool forceArchive) { mForceArchiveBroadcast = forceArchive; }
145 
146  void SetFrameWriterDelayStateChangedCallback(const FrameWriterDelayStateChangedCallback& callback) { mFrameWriterDelayStateChangedCallback = callback; }
147 
148  // Component overrides
149  virtual void SetTaskRunner(std::shared_ptr<TaskRunner> taskRunner) override;
150  virtual TTV_ErrorCode Initialize() override;
151  virtual void Update() override;
152  virtual TTV_ErrorCode Shutdown() override;
153 
154  // UserComponent overrides
155  virtual std::string GetLoggerName() const override;
156  virtual bool CheckShutdown() override;
157  virtual void CompleteShutdown() override;
158 
162  TTV_ErrorCode Start(const StartParams& params, StartCallback&& callback);
163 
167  TTV_ErrorCode Stop(const std::string& reason, StopCallback&& callback);
168 
169  TTV_ErrorCode GetVolume(AudioLayerId layer, float& volume) const;
170  TTV_ErrorCode SetVolume(AudioLayerId layer, float volume);
171  void SetAudioCapturer(AudioLayerId layerId, const std::shared_ptr<IAudioCapture>& capturer);
172  void SetAudioCapturerEnabled(AudioLayerId layer, bool enabled);
173  void GetAudioCapturers(std::vector<std::shared_ptr<IAudioCapture>>& result) const;
174  std::shared_ptr<IAudioCapture> GetAudioCapturer(AudioLayerId layer) const;
175 
176  void SetVideoCapturer(std::shared_ptr<IVideoCapture> capturer);
177  std::shared_ptr<IVideoCapture> GetVideoCapturer() const;
178 
179  uint64_t GetStreamTime() const;
180 
181  std::shared_ptr<StreamStats> GetStreamStats() { return mStreamStats; }
182  std::shared_ptr<const StreamStats> GetStreamStats() const { return mStreamStats; }
183 
184  VideoParams GetVideoParams() const { return mStartParams.videoParams; }
185 
186  TTV_ErrorCode ValidateParams(const VideoParams& videoParams, const IngestServer& ingestServer) const;
187 
188  StreamerState GetStreamerState() const;
189 
190  //TTV_ErrorCode SendMetaData(const char* authToken,
191  // TwitchAPI::MetaDataEventClass eventClass, const char* name,
192  // int64_t streamTime, unsigned long sequenceNumber,
193  // const char* humanDescription, const char* data,
194  // TTV_TaskCallback callback, void* userData);
195 
196  static std::string GetComponentName() { return "ttv::Streamer"; }
197  static void GetRequiredAuthScopes(std::vector<std::string>& scopes);
198 
199 private:
200  TTV_ErrorCode FetchChannelInfo(std::function<void(TTV_ErrorCode ec)>&& callback);
201  TTV_ErrorCode AddVideoFrame(const std::shared_ptr<VideoFrame>& frame);
202  TTV_ErrorCode GetStreamInfo();
203  TTV_ErrorCode SetIngestServer(const IngestServer& ingestServer);
204  TTV_ErrorCode SetStreamName(const std::string& streamKey, bool absEnabled);
205 
206  TTV_ErrorCode KickOffStart(const StartParams& params, StartCallback&& callback);
207  TTV_ErrorCode InternalStart(const StartParams& params);
208  TTV_ErrorCode InternalStop(TTV_ErrorCode ec, bool solicited, StopCallback&& callback);
209  TTV_ErrorCode UpdateBandwidthWarningState();
210 
217  TTV_ErrorCode TrackStartFailure(TTV_ErrorCode ec, bool synchronous);
218  TTV_ErrorCode TrackMinuteBroadcast();
219 
220  void NotifyStreamerStateChanged();
221 
222 private:
223  std::unique_ptr<StreamInfo> mStreamInfo;
224 
225 private:
229  std::shared_ptr<StreamerContext> mContext;
230  std::string mOutputStreamName;
231  std::shared_ptr<FlvMuxer> mFlvMuxer;
232  std::shared_ptr<FrameWriter> mFrameWriter;
233  std::shared_ptr<StreamStats> mStreamStats;
234  std::shared_ptr<VideoStreamer> mVideoStreamer;
235  std::shared_ptr<AudioStreamer> mAudioStreamer;
239 
241  std::string mStreamKey;
242 
243  std::string mBroadcasterSoftware;
244  uint64_t mInitialTime;
245  mutable std::mutex mStateMutex;
252 
254 
255  std::shared_ptr<IVideoEncoder> mVideoEncoder;
256  std::shared_ptr<IAudioEncoder> mAudioEncoder;
257 
258  std::shared_ptr<IVideoCapture> mVideoCapturer;
259 
260  std::shared_ptr<IMuxer> mCustomMuxer;
261  std::shared_ptr<TrackingContext> mTrackingContext;
262 
264 
268 };
269 
270 
275 {
276 public:
277  typedef std::function<void(Streamer* source, TTV_ErrorCode ec)> StartCompleteFunc;
278  typedef std::function<void(Streamer* source, TTV_ErrorCode ec)> StopCompleteFunc;
279  typedef std::function<void(Streamer* source, Streamer::StreamerState state, TTV_ErrorCode ec)> StreamerStateChangedFunc;
280  typedef std::function<void(Streamer* source, TTV_ErrorCode ec, uint32_t backupMilliseconds)> BandwidthWarningFunc;
281  typedef std::function<void(Streamer* source, TTV_ErrorCode ec, const StreamInfo& streamInfo)> StreamInfoFetchedFunc;
282 
283 public:
284  virtual void OnStartComplete(Streamer* source, TTV_ErrorCode ec) override
285  {
286  if (mStartCompleteFunc != nullptr)
287  {
288  mStartCompleteFunc(source, ec);
289  }
290  }
291 
292  virtual void OnStopComplete(Streamer* source, TTV_ErrorCode ec) override
293  {
294  if (mStopCompleteFunc != nullptr)
295  {
296  mStopCompleteFunc(source, ec);
297  }
298  }
299 
300  virtual void OnStreamerStateChanged(Streamer* source, Streamer::StreamerState state, TTV_ErrorCode ec) override
301  {
302  if (mStreamerStateChangedFunc != nullptr)
303  {
304  mStreamerStateChangedFunc(source, state, ec);
305  }
306  }
307 
308  virtual void OnBandwidthWarning(Streamer* source, TTV_ErrorCode ec, uint32_t backupMilliseconds) override
309  {
310  if (mBandwidthWarningFunc != nullptr)
311  {
312  mBandwidthWarningFunc(source, ec, backupMilliseconds);
313  }
314  }
315 
316  virtual void OnStreamInfoFetched(Streamer* source, TTV_ErrorCode ec, const StreamInfo& streamInfo) override
317  {
318  if (mStreamInfoFetchedFunc != nullptr)
319  {
320  mStreamInfoFetchedFunc(source, ec, streamInfo);
321  }
322  }
323 
324 public:
325  StartCompleteFunc mStartCompleteFunc;
326  StopCompleteFunc mStopCompleteFunc;
327  StreamerStateChangedFunc mStreamerStateChangedFunc;
328  BandwidthWarningFunc mBandwidthWarningFunc;
329  StreamInfoFetchedFunc mStreamInfoFetchedFunc;
330 };
Definition: timer.h:134
std::shared_ptr< IAudioEncoder > mAudioEncoder
Definition: streamer.h:256
std::string title
Definition: streamer.h:57
std::string category
Definition: streamer.h:56
TTV_ErrorCode mBandwidthWarningState
Definition: streamer.h:251
StartParams mStartParams
Definition: streamer.h:226
std::unique_ptr< StreamInfo > mStreamInfo
Definition: streamer.h:223
WaitForExpiry mMinuteBroadcastTrackingTimer
Definition: streamer.h:253
Definition: streamer.h:71
ConnectionType connectionType
Definition: streamer.h:55
std::function< void(Streamer *source, TTV_ErrorCode ec)> StartCompleteFunc
Definition: streamer.h:277
bool mFirstFrameSubmitted
Definition: streamer.h:265
AudioParams mAudioParams
Definition: streamer.h:227
StreamStartFlags flags
Definition: streamer.h:86
virtual void OnStartComplete(Streamer *source, TTV_ErrorCode ec) override
Definition: streamer.h:284
StreamerState mStreamerState
Definition: streamer.h:246
std::function< void(TTV_ErrorCode ec)> StartCallback
Definition: streamer.h:90
std::shared_ptr< TrackingContext > mTrackingContext
Definition: streamer.h:261
void SetBroadcasterSoftware(const std::string &str)
Definition: streamer.h:139
WaitForExpiry mBandwidthWarningTimer
Definition: streamer.h:250
uint32_t AudioLayerId
Definition: broadcasttypes.h:192
Definition: streamer.h:274
StreamStartFlags
Definition: broadcasttypes.h:225
Definition: coretypes.h:130
std::string mStreamKey
The key that grants stream access to the channel.
Definition: streamer.h:241
std::shared_ptr< FrameWriter > mFrameWriter
Definition: streamer.h:232
StreamerState mLastReportedStreamerState
The state last reported to the client.
Definition: streamer.h:248
void SetFrameWriterDelayStateChangedCallback(const FrameWriterDelayStateChangedCallback &callback)
Definition: streamer.h:146
std::shared_ptr< StreamerContext > mContext
Definition: streamer.h:229
virtual void OnBandwidthWarning(Streamer *source, TTV_ErrorCode ec, uint32_t backupMilliseconds) override
Definition: streamer.h:308
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
std::shared_ptr< FlvMuxer > mFlvMuxer
Definition: streamer.h:231
virtual void OnStreamerStateChanged(Streamer *source, Streamer::StreamerState state, TTV_ErrorCode ec) override
Definition: streamer.h:300
uint64_t mInitialTime
The number of system ticks at the time of stream start.
Definition: streamer.h:244
std::shared_ptr< AudioStreamer > mAudioStreamer
Definition: streamer.h:235
virtual void OnStopComplete(Streamer *source, TTV_ErrorCode ec) override
Definition: streamer.h:292
Definition: timer.h:52
Definition: iaudioencoder.h:29
bool mBandwidthTestMode
Definition: streamer.h:266
bool mForceArchiveBroadcast
If true appends recorder=1 to the RTMP URL forcing the server to record the broadcast.
Definition: streamer.h:267
Definition: eventsource.h:18
StreamerStateChangedFunc mStreamerStateChangedFunc
Definition: streamer.h:327
ConnectionType
Definition: broadcasttypes.h:154
ttv::ConcurrentQueue< BandwidthStat > mStatQueue
Definition: streamer.h:237
std::string mOutputStreamName
Definition: streamer.h:230
void SetForceArchiveBroadcast(bool forceArchive)
Definition: streamer.h:144
StreamInfoFetchedFunc mStreamInfoFetchedFunc
Definition: streamer.h:329
Definition: broadcasttypes.h:80
std::atomic_bool isBusy
Definition: streamer.h:53
virtual void OnStreamInfoFetched(Streamer *source, TTV_ErrorCode ec, const StreamInfo &streamInfo) override
Definition: streamer.h:316
Definition: broadcasttypes.h:32
StopCompleteFunc mStopCompleteFunc
Definition: streamer.h:326
IngestServer mSelectedIngestServer
Definition: streamer.h:228
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
static std::string GetComponentName()
Definition: streamer.h:196
Definition: component.h:87
RetryTimer mStreamInfoFetchTimer
Definition: streamer.h:247
BandwidthWarningFunc mBandwidthWarningFunc
Definition: streamer.h:328
EventSource< IListener > mListeners
Definition: streamer.h:236
std::shared_ptr< IVideoCapture > mVideoCapturer
Definition: streamer.h:258
ChannelId mChannelId
The name of the channel to broadcast to.
Definition: streamer.h:240
std::function< void(FrameWriter::DelayState state)> FrameWriterDelayStateChangedCallback
Definition: streamer.h:92
Definition: concurrentqueue.h:16
std::function< void(TTV_ErrorCode ec)> StopCallback
Definition: streamer.h:91
std::shared_ptr< TrackingContext > sharedTrackingContext
Definition: streamer.h:54
std::shared_ptr< IVideoEncoder > mVideoEncoder
Definition: streamer.h:255
FrameWriterDelayStateChangedCallback mFrameWriterDelayStateChangedCallback
Definition: streamer.h:263
std::shared_ptr< StreamStats > mStreamStats
Definition: streamer.h:233
std::function< void(Streamer *source, TTV_ErrorCode ec, uint32_t backupMilliseconds)> BandwidthWarningFunc
Definition: streamer.h:280
std::wstring outputFile
Definition: streamer.h:87
std::shared_ptr< IMuxer > mCustomMuxer
Definition: streamer.h:260
Definition: streamer.h:94
uint32_t ChannelId
Definition: coretypes.h:23
StartCompleteFunc mStartCompleteFunc
Definition: streamer.h:325
std::function< void(Streamer *source, TTV_ErrorCode ec, const StreamInfo &streamInfo)> StreamInfoFetchedFunc
Definition: streamer.h:281
std::mutex mStateMutex
Definition: streamer.h:245
VideoParams GetVideoParams() const
Definition: streamer.h:184
std::shared_ptr< StreamStats > GetStreamStats()
Definition: streamer.h:181
IngestServer ingestServer
Definition: streamer.h:85
std::function< void(Streamer *source, TTV_ErrorCode ec)> StopCompleteFunc
Definition: streamer.h:278
std::function< void(Streamer *source, Streamer::StreamerState state, TTV_ErrorCode ec)> StreamerStateChangedFunc
Definition: streamer.h:279
TTV_ErrorCode mStateChangeError
The cause of the last state change.
Definition: streamer.h:249
EventSource< IBandwidthStatListener > mBandwidthStatListeners
Definition: streamer.h:238
VideoParams videoParams
Definition: streamer.h:84
Definition: streamer.h:46
std::string mBroadcasterSoftware
The broadcaster_software parameter to send over RTMP.
Definition: streamer.h:243
std::shared_ptr< VideoStreamer > mVideoStreamer
Definition: streamer.h:234
StreamerState
Definition: streamer.h:74
StreamerContext()
Definition: streamer.h:48
Definition: streamer.h:82
std::shared_ptr< const StreamStats > GetStreamStats() const
Definition: streamer.h:182