1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_CALL_AUDIO_RECEIVE_STREAM_H_
12 #define WEBRTC_CALL_AUDIO_RECEIVE_STREAM_H_
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "webrtc/api/call/transport.h"
20 #include "webrtc/base/optional.h"
21 #include "webrtc/base/scoped_ref_ptr.h"
22 #include "webrtc/modules/audio_coding/codecs/audio_decoder_factory.h"
23 #include "webrtc/common_types.h"
24 #include "webrtc/config.h"
25 #include "webrtc/typedefs.h"
26 
27 namespace webrtc {
28 class AudioSinkInterface;
29 
30 // WORK IN PROGRESS
31 // This class is under development and is not yet intended for for use outside
32 // of WebRtc/Libjingle. Please use the VoiceEngine API instead.
33 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4690
34 
35 class AudioReceiveStream {
36  public:
37   struct Stats {
38     uint32_t remote_ssrc = 0;
39     int64_t bytes_rcvd = 0;
40     uint32_t packets_rcvd = 0;
41     uint32_t packets_lost = 0;
42     float fraction_lost = 0.0f;
43     std::string codec_name;
44     rtc::Optional<int> codec_payload_type;
45     uint32_t ext_seqnum = 0;
46     uint32_t jitter_ms = 0;
47     uint32_t jitter_buffer_ms = 0;
48     uint32_t jitter_buffer_preferred_ms = 0;
49     uint32_t delay_estimate_ms = 0;
50     int32_t audio_level = -1;
51     float expand_rate = 0.0f;
52     float speech_expand_rate = 0.0f;
53     float secondary_decoded_rate = 0.0f;
54     float accelerate_rate = 0.0f;
55     float preemptive_expand_rate = 0.0f;
56     int32_t decoding_calls_to_silence_generator = 0;
57     int32_t decoding_calls_to_neteq = 0;
58     int32_t decoding_normal = 0;
59     int32_t decoding_plc = 0;
60     int32_t decoding_cng = 0;
61     int32_t decoding_plc_cng = 0;
62     int32_t decoding_muted_output = 0;
63     int64_t capture_start_ntp_time_ms = 0;
64   };
65 
66   struct Config {
67     std::string ToString() const;
68 
69     // Receive-stream specific RTP settings.
70     struct Rtp {
71       std::string ToString() const;
72 
73       // Synchronization source (stream identifier) to be received.
74       uint32_t remote_ssrc = 0;
75 
76       // Sender SSRC used for sending RTCP (such as receiver reports).
77       uint32_t local_ssrc = 0;
78 
79       // Enable feedback for send side bandwidth estimation.
80       // See
81       // https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions
82       // for details.
83       bool transport_cc = false;
84 
85       // See NackConfig for description.
86       NackConfig nack;
87 
88       // RTP header extensions used for the received stream.
89       std::vector<RtpExtension> extensions;
90     } rtp;
91 
92     Transport* rtcp_send_transport = nullptr;
93 
94     // Underlying VoiceEngine handle, used to map AudioReceiveStream to lower-
95     // level components.
96     // TODO(solenberg): Remove when VoiceEngine channels are created outside
97     // of Call.
98     int voe_channel_id = -1;
99 
100     // Identifier for an A/V synchronization group. Empty string to disable.
101     // TODO(pbos): Synchronize streams in a sync group, not just one video
102     // stream to one audio stream. Tracked by issue webrtc:4762.
103     std::string sync_group;
104 
105     // Decoders for every payload that we can receive. Call owns the
106     // AudioDecoder instances once the Config is submitted to
107     // Call::CreateReceiveStream().
108     // TODO(solenberg): Use unique_ptr<> once our std lib fully supports C++11.
109     std::map<uint8_t, AudioDecoder*> decoder_map;
110 
111     rtc::scoped_refptr<AudioDecoderFactory> decoder_factory;
112   };
113 
114   // Starts stream activity.
115   // When a stream is active, it can receive, process and deliver packets.
116   virtual void Start() = 0;
117   // Stops stream activity.
118   // When a stream is stopped, it can't receive, process or deliver packets.
119   virtual void Stop() = 0;
120 
121   virtual Stats GetStats() const = 0;
122 
123   // Sets an audio sink that receives unmixed audio from the receive stream.
124   // Ownership of the sink is passed to the stream and can be used by the
125   // caller to do lifetime management (i.e. when the sink's dtor is called).
126   // Only one sink can be set and passing a null sink clears an existing one.
127   // NOTE: Audio must still somehow be pulled through AudioTransport for audio
128   // to stream through this sink. In practice, this happens if mixed audio
129   // is being pulled+rendered and/or if audio is being pulled for the purposes
130   // of feeding to the AEC.
131   virtual void SetSink(std::unique_ptr<AudioSinkInterface> sink) = 0;
132 
133   // Sets playback gain of the stream, applied when mixing, and thus after it
134   // is potentially forwarded to any attached AudioSinkInterface implementation.
135   virtual void SetGain(float gain) = 0;
136 
137  protected:
~AudioReceiveStream()138   virtual ~AudioReceiveStream() {}
139 };
140 }  // namespace webrtc
141 
142 #endif  // WEBRTC_CALL_AUDIO_RECEIVE_STREAM_H_
143