Home | History | Annotate | Download | only in webrtc
      1 /*
      2  *  Copyright (c) 2013 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_VIDEO_SEND_STREAM_H_
     12 #define WEBRTC_VIDEO_SEND_STREAM_H_
     13 
     14 #include <map>
     15 #include <string>
     16 
     17 #include "webrtc/common_types.h"
     18 #include "webrtc/config.h"
     19 #include "webrtc/frame_callback.h"
     20 #include "webrtc/video_renderer.h"
     21 
     22 namespace webrtc {
     23 
     24 class VideoEncoder;
     25 
     26 // Class to deliver captured frame to the video send stream.
     27 class VideoSendStreamInput {
     28  public:
     29   // These methods do not lock internally and must be called sequentially.
     30   // If your application switches input sources synchronization must be done
     31   // externally to make sure that any old frames are not delivered concurrently.
     32   virtual void SwapFrame(I420VideoFrame* video_frame) = 0;
     33 
     34  protected:
     35   virtual ~VideoSendStreamInput() {}
     36 };
     37 
     38 class VideoSendStream {
     39  public:
     40   struct Stats {
     41     Stats()
     42         : input_frame_rate(0),
     43           encode_frame_rate(0),
     44           avg_delay_ms(0),
     45           max_delay_ms(0),
     46           suspended(false) {}
     47 
     48     int input_frame_rate;
     49     int encode_frame_rate;
     50     int avg_delay_ms;
     51     int max_delay_ms;
     52     bool suspended;
     53     std::string c_name;
     54     std::map<uint32_t, StreamStats> substreams;
     55   };
     56 
     57   struct Config {
     58     Config()
     59         : pre_encode_callback(NULL),
     60           post_encode_callback(NULL),
     61           local_renderer(NULL),
     62           render_delay_ms(0),
     63           target_delay_ms(0),
     64           suspend_below_min_bitrate(false) {}
     65     std::string ToString() const;
     66 
     67     struct EncoderSettings {
     68       EncoderSettings() : payload_type(-1), encoder(NULL) {}
     69       std::string ToString() const;
     70 
     71       std::string payload_name;
     72       int payload_type;
     73 
     74       // Uninitialized VideoEncoder instance to be used for encoding. Will be
     75       // initialized from inside the VideoSendStream.
     76       webrtc::VideoEncoder* encoder;
     77     } encoder_settings;
     78 
     79     static const size_t kDefaultMaxPacketSize = 1500 - 40;  // TCP over IPv4.
     80     struct Rtp {
     81       Rtp()
     82           : max_packet_size(kDefaultMaxPacketSize),
     83             min_transmit_bitrate_bps(0) {}
     84       std::string ToString() const;
     85 
     86       std::vector<uint32_t> ssrcs;
     87 
     88       // Max RTP packet size delivered to send transport from VideoEngine.
     89       size_t max_packet_size;
     90 
     91       // Padding will be used up to this bitrate regardless of the bitrate
     92       // produced by the encoder. Padding above what's actually produced by the
     93       // encoder helps maintaining a higher bitrate estimate.
     94       int min_transmit_bitrate_bps;
     95 
     96       // RTP header extensions to use for this send stream.
     97       std::vector<RtpExtension> extensions;
     98 
     99       // See NackConfig for description.
    100       NackConfig nack;
    101 
    102       // See FecConfig for description.
    103       FecConfig fec;
    104 
    105       // Settings for RTP retransmission payload format, see RFC 4588 for
    106       // details.
    107       struct Rtx {
    108         Rtx() : payload_type(-1), pad_with_redundant_payloads(false) {}
    109         std::string ToString() const;
    110         // SSRCs to use for the RTX streams.
    111         std::vector<uint32_t> ssrcs;
    112 
    113         // Payload type to use for the RTX stream.
    114         int payload_type;
    115         // Use redundant payloads to pad the bitrate. Instead of padding with
    116         // randomized packets, we will preemptively retransmit media packets on
    117         // the RTX stream.
    118         bool pad_with_redundant_payloads;
    119       } rtx;
    120 
    121       // RTCP CNAME, see RFC 3550.
    122       std::string c_name;
    123     } rtp;
    124 
    125     // Called for each I420 frame before encoding the frame. Can be used for
    126     // effects, snapshots etc. 'NULL' disables the callback.
    127     I420FrameCallback* pre_encode_callback;
    128 
    129     // Called for each encoded frame, e.g. used for file storage. 'NULL'
    130     // disables the callback.
    131     EncodedFrameObserver* post_encode_callback;
    132 
    133     // Renderer for local preview. The local renderer will be called even if
    134     // sending hasn't started. 'NULL' disables local rendering.
    135     VideoRenderer* local_renderer;
    136 
    137     // Expected delay needed by the renderer, i.e. the frame will be delivered
    138     // this many milliseconds, if possible, earlier than expected render time.
    139     // Only valid if |local_renderer| is set.
    140     int render_delay_ms;
    141 
    142     // Target delay in milliseconds. A positive value indicates this stream is
    143     // used for streaming instead of a real-time call.
    144     int target_delay_ms;
    145 
    146     // True if the stream should be suspended when the available bitrate fall
    147     // below the minimum configured bitrate. If this variable is false, the
    148     // stream may send at a rate higher than the estimated available bitrate.
    149     bool suspend_below_min_bitrate;
    150   };
    151 
    152   // Gets interface used to insert captured frames. Valid as long as the
    153   // VideoSendStream is valid.
    154   virtual VideoSendStreamInput* Input() = 0;
    155 
    156   virtual void Start() = 0;
    157   virtual void Stop() = 0;
    158 
    159   // Set which streams to send. Must have at least as many SSRCs as configured
    160   // in the config. Encoder settings are passed on to the encoder instance along
    161   // with the VideoStream settings.
    162   virtual bool ReconfigureVideoEncoder(const std::vector<VideoStream>& streams,
    163                                        const void* encoder_settings) = 0;
    164 
    165   virtual Stats GetStats() const = 0;
    166 
    167  protected:
    168   virtual ~VideoSendStream() {}
    169 };
    170 
    171 }  // namespace webrtc
    172 
    173 #endif  // WEBRTC_VIDEO_SEND_STREAM_H_
    174