blob: 47fc0a79187d02c150bd6f4653b82e8d0e3f2a1b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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// This sub-API supports the following functionalities:
12//
13// - Creating and deleting VideoEngine instances.
14// - Creating and deleting channels.
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +000015// - Connect a video channel with a corresponding voice channel for audio/video
16// synchronization.
niklase@google.com470e71d2011-07-07 08:21:25 +000017// - Start and stop sending and receiving.
18
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +000019#ifndef WEBRTC_VIDEO_ENGINE_INCLUDE_VIE_BASE_H_
20#define WEBRTC_VIDEO_ENGINE_INCLUDE_VIE_BASE_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000021
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000022#include "webrtc/common_types.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000024#if defined(ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
25#include <jni.h>
26#endif
27
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +000028namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000029
andresp@webrtc.org7707d062013-05-13 10:50:50 +000030class Config;
niklase@google.com470e71d2011-07-07 08:21:25 +000031class VoiceEngine;
32
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000033// CpuOveruseObserver is called when a system overuse is detected and
34// VideoEngine cannot keep up the encoding frequency.
35class CpuOveruseObserver {
36 public:
37 // Called as soon as an overuse is detected.
38 virtual void OveruseDetected() = 0;
39 // Called periodically when the system is not overused any longer.
40 virtual void NormalUsage() = 0;
41
42 protected:
43 virtual ~CpuOveruseObserver() {}
44};
45
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000046// Limits on standard deviation for under/overuse.
47#ifdef WEBRTC_ANDROID
48const float kOveruseStdDevMs = 32.0f;
49const float kNormalUseStdDevMs = 27.0f;
50#elif WEBRTC_LINUX
51const float kOveruseStdDevMs = 20.0f;
52const float kNormalUseStdDevMs = 14.0f;
53#elif WEBRTC_MAC
54const float kOveruseStdDevMs = 27.0f;
55const float kNormalUseStdDevMs = 21.0f;
56#elif WEBRTC_WIN
57const float kOveruseStdDevMs = 20.0f;
58const float kNormalUseStdDevMs = 14.0f;
59#else
60const float kOveruseStdDevMs = 30.0f;
61const float kNormalUseStdDevMs = 20.0f;
62#endif
63
64struct CpuOveruseOptions {
65 CpuOveruseOptions()
66 : enable_capture_jitter_method(true),
67 low_capture_jitter_threshold_ms(kNormalUseStdDevMs),
68 high_capture_jitter_threshold_ms(kOveruseStdDevMs),
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +000069 enable_encode_usage_method(false),
70 low_encode_usage_threshold_percent(60),
71 high_encode_usage_threshold_percent(90),
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000072 frame_timeout_interval_ms(1500),
73 min_frame_samples(120),
74 min_process_count(3),
75 high_threshold_consecutive_count(2) {}
76
77 // Method based on inter-arrival jitter of captured frames.
78 bool enable_capture_jitter_method;
79 float low_capture_jitter_threshold_ms; // Threshold for triggering underuse.
80 float high_capture_jitter_threshold_ms; // Threshold for triggering overuse.
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +000081 // Method based on encode time of frames.
82 bool enable_encode_usage_method;
83 int low_encode_usage_threshold_percent; // Threshold for triggering underuse.
84 int high_encode_usage_threshold_percent; // Threshold for triggering overuse.
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +000085 // General settings.
86 int frame_timeout_interval_ms; // The maximum allowed interval between two
87 // frames before resetting estimations.
88 int min_frame_samples; // The minimum number of frames required.
89 int min_process_count; // The number of initial process times required before
90 // triggering an overuse/underuse.
91 int high_threshold_consecutive_count; // The number of consecutive checks
92 // above the high threshold before
93 // triggering an overuse.
94
95 bool Equals(const CpuOveruseOptions& o) const {
96 return enable_capture_jitter_method == o.enable_capture_jitter_method &&
97 low_capture_jitter_threshold_ms == o.low_capture_jitter_threshold_ms &&
98 high_capture_jitter_threshold_ms ==
99 o.high_capture_jitter_threshold_ms &&
asapersson@webrtc.orgce12f1f2014-03-24 21:59:16 +0000100 enable_encode_usage_method == o.enable_encode_usage_method &&
101 low_encode_usage_threshold_percent ==
102 o.low_encode_usage_threshold_percent &&
103 high_encode_usage_threshold_percent ==
104 o.high_encode_usage_threshold_percent &&
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000105 frame_timeout_interval_ms == o.frame_timeout_interval_ms &&
106 min_frame_samples == o.min_frame_samples &&
107 min_process_count == o.min_process_count &&
108 high_threshold_consecutive_count == o.high_threshold_consecutive_count;
109 }
110};
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000111
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000112struct CpuOveruseMetrics {
113 CpuOveruseMetrics()
114 : capture_jitter_ms(-1),
115 avg_encode_time_ms(-1),
116 encode_usage_percent(-1),
117 capture_queue_delay_ms_per_s(-1) {}
118
119 int capture_jitter_ms; // The current estimated jitter in ms based on
120 // incoming captured frames.
121 int avg_encode_time_ms; // The average encode time in ms.
122 int encode_usage_percent; // The average encode time divided by the average
123 // time difference between incoming captured frames.
124 int capture_queue_delay_ms_per_s; // The current time delay between an
125 // incoming captured frame until the frame
126 // is being processed. The delay is
127 // expressed in ms delay per second.
128};
129
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000130class WEBRTC_DLLEXPORT VideoEngine {
131 public:
132 // Creates a VideoEngine object, which can then be used to acquire sub‐APIs.
133 static VideoEngine* Create();
andresp@webrtc.org7707d062013-05-13 10:50:50 +0000134 static VideoEngine* Create(const Config& config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000136 // Deletes a VideoEngine instance.
137 static bool Delete(VideoEngine*& video_engine);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000139 // Specifies the amount and type of trace information, which will be created
140 // by the VideoEngine.
141 static int SetTraceFilter(const unsigned int filter);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000143 // Sets the name of the trace file and enables non‐encrypted trace messages.
144 static int SetTraceFile(const char* file_nameUTF8,
145 const bool add_file_counter = false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000147 // Installs the TraceCallback implementation to ensure that the VideoEngine
148 // user receives callbacks for generated trace messages.
149 static int SetTraceCallback(TraceCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
fischman@webrtc.org4e65e072013-10-03 18:23:13 +0000151#if defined(ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000152 // Android specific.
fischman@webrtc.org95127192014-06-06 18:40:44 +0000153 static int SetAndroidObjects(JavaVM* java_vm, jobject context);
fischman@webrtc.org4e65e072013-10-03 18:23:13 +0000154#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000156 protected:
157 VideoEngine() {}
158 virtual ~VideoEngine() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000159};
160
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000161class WEBRTC_DLLEXPORT ViEBase {
162 public:
163 // Factory for the ViEBase sub‐API and increases an internal reference
164 // counter if successful. Returns NULL if the API is not supported or if
165 // construction fails.
166 static ViEBase* GetInterface(VideoEngine* video_engine);
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000168 // Releases the ViEBase sub-API and decreases an internal reference counter.
169 // Returns the new reference count. This value should be zero
170 // for all sub-API:s before the VideoEngine object can be safely deleted.
171 virtual int Release() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000173 // Initiates all common parts of the VideoEngine.
174 virtual int Init() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000176 // Connects a VideoEngine instance to a VoiceEngine instance for audio video
177 // synchronization.
178 virtual int SetVoiceEngine(VoiceEngine* voice_engine) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000180 // Creates a new channel.
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000181 virtual int CreateChannel(int& video_channel) = 0;
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000182
183 // Creates a new channel grouped together with |original_channel|. The channel
184 // can both send and receive video. It is assumed the channel is sending
185 // and/or receiving video to the same end-point.
186 // Note: |CreateReceiveChannel| will give better performance and network
187 // properties for receive only channels.
188 virtual int CreateChannel(int& video_channel,
189 int original_channel) = 0;
190
191 // Creates a new channel grouped together with |original_channel|. The channel
192 // can only receive video and it is assumed the remote end-point is the same
193 // as for |original_channel|.
194 virtual int CreateReceiveChannel(int& video_channel,
195 int original_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000197 // Deletes an existing channel and releases the utilized resources.
198 virtual int DeleteChannel(const int video_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000200 // Registers an observer to be called when an overuse is detected, see
201 // 'CpuOveruseObserver' for details.
202 // NOTE: This is still very experimental functionality.
203 virtual int RegisterCpuOveruseObserver(int channel,
204 CpuOveruseObserver* observer) = 0;
205
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000206 // Sets options for cpu overuse detector.
207 // TODO(asapersson): Remove default implementation.
208 virtual int SetCpuOveruseOptions(int channel,
209 const CpuOveruseOptions& options) {
210 return -1;
211 }
212
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000213 // Gets cpu overuse measures.
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000214 // TODO(asapersson): Remove default implementation.
asapersson@webrtc.orgab6bf4f2014-05-27 07:43:15 +0000215 virtual int GetCpuOveruseMetrics(int channel,
216 CpuOveruseMetrics* metrics) {
217 return -1;
218 }
219 // TODO(asapersson): Remove this function when libjingle has been updated.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000220 virtual int CpuOveruseMeasures(int channel,
221 int* capture_jitter_ms,
222 int* avg_encode_time_ms,
223 int* encode_usage_percent,
224 int* capture_queue_delay_ms_per_s) {
225 return -1;
226 }
227
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000228 // Specifies the VoiceEngine and VideoEngine channel pair to use for
229 // audio/video synchronization.
230 virtual int ConnectAudioChannel(const int video_channel,
231 const int audio_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000233 // Disconnects a previously paired VideoEngine and VoiceEngine channel pair.
234 virtual int DisconnectAudioChannel(const int video_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000236 // Starts sending packets to an already specified IP address and port number
237 // for a specified channel.
238 virtual int StartSend(const int video_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000239
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000240 // Stops packets from being sent for a specified channel.
241 virtual int StopSend(const int video_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000243 // Prepares VideoEngine for receiving packets on the specified channel.
244 virtual int StartReceive(const int video_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000246 // Stops receiving incoming RTP and RTCP packets on the specified channel.
247 virtual int StopReceive(const int video_channel) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000248
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000249 // Retrieves the version information for VideoEngine and its components.
250 virtual int GetVersion(char version[1024]) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000251
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000252 // Returns the last VideoEngine error code.
253 virtual int LastError() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000255 protected:
256 ViEBase() {}
257 virtual ~ViEBase() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000258};
259
mflodman@webrtc.orgd5a4d9b2012-01-02 13:04:05 +0000260} // namespace webrtc
261
niklase@google.com470e71d2011-07-07 08:21:25 +0000262#endif // #define WEBRTC_VIDEO_ENGINE_MAIN_INTERFACE_VIE_BASE_H_