Twitch SDK (Internal)
audiostreamer.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 
15 #include <queue>
16 
17 namespace ttv
18 {
19  class IThread;
20 
21  namespace broadcast
22  {
23  class IAudioCapture;
24  class PcmAudioMixer;
25  class AudioStreamer;
26  }
27 }
28 
29 
31 {
32 public:
33  AudioStreamer();
34  virtual ~AudioStreamer();
35 
36 public:
37  TTV_ErrorCode SetEncoder(const std::shared_ptr<IAudioEncoder>& encoder);
38  TTV_ErrorCode Initialize(const AudioParams& audioParams);
40  void Stop();
41 
42  void SetInitialTime(uint64_t initialTime);
43 
44  float GetVolume(AudioLayerId layer) const;
45  void SetVolume(AudioLayerId layer, float volume);
46 
47  void SetCapturer(AudioLayerId layerId, const std::shared_ptr<IAudioCapture>& capturer);
48  void SetCapturerEnabled(AudioLayerId layer, bool enabled);
49  void GetCapturers(std::vector<std::shared_ptr<IAudioCapture>>& result) const;
50  std::shared_ptr<IAudioCapture> GetCapturer(AudioLayerId layer) const;
51  void GetEnabledCapturers(std::vector<std::shared_ptr<IAudioCapture>>& result) const;
52  bool HasEnabledCapturers() const;
53 
54 private:
55  class CaptureContext;
56 
57  // Mapping of request time to the desired volume
58  typedef std::queue<std::pair<uint64_t, float>> volumeChanges_t;
59 
61  {
63 
64  std::shared_ptr<IAudioCapture> capturer;
65  volumeChanges_t volumeChanges;
66  uint64_t initialTime;
67  float volume;
68  bool enabled;
69  };
70 
71 private:
72  void StartCapturers(const std::shared_ptr<IAudioMixer>& mixer);
74  void StopCapture();
75  TTV_ErrorCode WritePcmAudioFrames(const std::shared_ptr<CaptureContext>& context, uint64_t latestTimeToProcess);
76  TTV_ErrorCode WritePassThroughAudioFrames(const std::shared_ptr<CaptureContext>& context, uint64_t latestTimeToProcess);
77  TTV_ErrorCode SubmitPcmSamplesToMixer(AudioLayerId audioLayerId, const int16_t* samples, size_t numSamples, uint64_t sampleTime);
78  void ProcessCapturers();
79 
80 private:
81  uint32_t mNumPcmSamplesPerChannel; // If the audio format being captured is PCM this is the number of samples per channel.
82  uint64_t mInitialTime;
83  std::vector<int16_t> mMixedSamplesBuffer;
84 
85  std::shared_ptr<IThreadSync> mInitializingThreadSync;
87 
88  std::map<AudioLayerId, LayerProperties> mLayers;
89  std::shared_ptr<IAudioEncoder> mAudioEncoder;
90  std::shared_ptr<PcmAudioMixer> mAudioMixer;
91  std::shared_ptr<IThread> mDeviceProcessingThread;
92 
94 
95  std::atomic_bool mDoProcessing;
96 };
TTV_ErrorCode Initialize(const AudioParams &audioParams)
void SetCapturerEnabled(AudioLayerId layer, bool enabled)
TTV_ErrorCode WritePcmAudioFrames(const std::shared_ptr< CaptureContext > &context, uint64_t latestTimeToProcess)
std::shared_ptr< IAudioEncoder > mAudioEncoder
Definition: audiostreamer.h:89
void SetInitialTime(uint64_t initialTime)
std::shared_ptr< PcmAudioMixer > mAudioMixer
Definition: audiostreamer.h:90
void SetCapturer(AudioLayerId layerId, const std::shared_ptr< IAudioCapture > &capturer)
std::vector< int16_t > mMixedSamplesBuffer
Definition: audiostreamer.h:83
uint32_t AudioLayerId
Definition: broadcasttypes.h:192
std::atomic_bool mDoProcessing
Definition: audiostreamer.h:95
void GetEnabledCapturers(std::vector< std::shared_ptr< IAudioCapture >> &result) const
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
std::shared_ptr< IThreadSync > mInitializingThreadSync
Definition: audiostreamer.h:85
float volume
Definition: audiostreamer.h:67
uint64_t initialTime
Definition: audiostreamer.h:66
Definition: iaudioencoder.h:29
TTV_ErrorCode SetEncoder(const std::shared_ptr< IAudioEncoder > &encoder)
volumeChanges_t volumeChanges
Definition: audiostreamer.h:65
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
std::shared_ptr< IAudioCapture > capturer
Definition: audiostreamer.h:64
std::shared_ptr< IThread > mDeviceProcessingThread
Definition: audiostreamer.h:91
std::queue< std::pair< uint64_t, float > > volumeChanges_t
Definition: audiostreamer.h:55
uint64_t mInitialTime
Definition: audiostreamer.h:82
std::map< AudioLayerId, LayerProperties > mLayers
Definition: audiostreamer.h:88
uint32_t mNumPcmSamplesPerChannel
Definition: audiostreamer.h:81
bool enabled
Definition: audiostreamer.h:68
float GetVolume(AudioLayerId layer) const
AudioParams mAudioParams
Definition: audiostreamer.h:93
TTV_ErrorCode WritePassThroughAudioFrames(const std::shared_ptr< CaptureContext > &context, uint64_t latestTimeToProcess)
Definition: audiostreamer.h:30
void StartCapturers(const std::shared_ptr< IAudioMixer > &mixer)
void GetCapturers(std::vector< std::shared_ptr< IAudioCapture >> &result) const
void SetVolume(AudioLayerId layer, float volume)
std::shared_ptr< IAudioCapture > GetCapturer(AudioLayerId layer) const
TTV_ErrorCode SubmitPcmSamplesToMixer(AudioLayerId audioLayerId, const int16_t *samples, size_t numSamples, uint64_t sampleTime)
volatile TTV_ErrorCode mInitializeResult
Definition: audiostreamer.h:86