aboutsummaryrefslogtreecommitdiff
path: root/webrtc/common_video/incoming_video_stream.cc
blob: 1272ecc5bbc3cca20294206f325d30cfded13259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/common_video/include/incoming_video_stream.h"

#include <assert.h>

#if defined(_WIN32)
#include <windows.h>
#elif defined(WEBRTC_LINUX)
#include <sys/time.h>
#include <time.h>
#else
#include <sys/time.h>
#endif

#include "webrtc/base/platform_thread.h"
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
#include "webrtc/common_video/video_render_frames.h"
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
#include "webrtc/system_wrappers/include/event_wrapper.h"
#include "webrtc/system_wrappers/include/tick_util.h"
#include "webrtc/system_wrappers/include/trace.h"
#include "webrtc/video_renderer.h"

namespace webrtc {

IncomingVideoStream::IncomingVideoStream(uint32_t stream_id,
                                         bool disable_prerenderer_smoothing)
    : stream_id_(stream_id),
      disable_prerenderer_smoothing_(disable_prerenderer_smoothing),
      stream_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
      thread_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
      buffer_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
      incoming_render_thread_(),
      deliver_buffer_event_(EventTimerWrapper::Create()),
      running_(false),
      external_callback_(nullptr),
      render_callback_(nullptr),
      render_buffers_(new VideoRenderFrames()),
      incoming_rate_(0),
      last_rate_calculation_time_ms_(0),
      num_frames_since_last_calculation_(0),
      last_render_time_ms_(0),
      temp_frame_(),
      start_image_(),
      timeout_image_(),
      timeout_time_() {}

IncomingVideoStream::~IncomingVideoStream() {
  Stop();
}

VideoRenderCallback* IncomingVideoStream::ModuleCallback() {
  CriticalSectionScoped cs(stream_critsect_.get());
  return this;
}

int32_t IncomingVideoStream::RenderFrame(const uint32_t stream_id,
                                         const VideoFrame& video_frame) {
  CriticalSectionScoped csS(stream_critsect_.get());

  if (!running_) {
    return -1;
  }

  // Rate statistics.
  num_frames_since_last_calculation_++;
  int64_t now_ms = TickTime::MillisecondTimestamp();
  if (now_ms >= last_rate_calculation_time_ms_ + kFrameRatePeriodMs) {
    incoming_rate_ =
        static_cast<uint32_t>(1000 * num_frames_since_last_calculation_ /
                              (now_ms - last_rate_calculation_time_ms_));
    num_frames_since_last_calculation_ = 0;
    last_rate_calculation_time_ms_ = now_ms;
  }

  // Hand over or insert frame.
  if (disable_prerenderer_smoothing_) {
    DeliverFrame(video_frame);
  } else {
    CriticalSectionScoped csB(buffer_critsect_.get());
    if (render_buffers_->AddFrame(video_frame) == 1) {
      deliver_buffer_event_->Set();
    }
  }
  return 0;
}

int32_t IncomingVideoStream::SetStartImage(const VideoFrame& video_frame) {
  CriticalSectionScoped csS(thread_critsect_.get());
  return start_image_.CopyFrame(video_frame);
}

int32_t IncomingVideoStream::SetTimeoutImage(const VideoFrame& video_frame,
                                             const uint32_t timeout) {
  CriticalSectionScoped csS(thread_critsect_.get());
  timeout_time_ = timeout;
  return timeout_image_.CopyFrame(video_frame);
}

void IncomingVideoStream::SetRenderCallback(
    VideoRenderCallback* render_callback) {
  CriticalSectionScoped cs(thread_critsect_.get());
  render_callback_ = render_callback;
}

int32_t IncomingVideoStream::SetExpectedRenderDelay(
    int32_t delay_ms) {
  CriticalSectionScoped csS(stream_critsect_.get());
  if (running_) {
    return -1;
  }
  CriticalSectionScoped cs(buffer_critsect_.get());
  return render_buffers_->SetRenderDelay(delay_ms);
}

void IncomingVideoStream::SetExternalCallback(
    VideoRenderCallback* external_callback) {
  CriticalSectionScoped cs(thread_critsect_.get());
  external_callback_ = external_callback;
}

int32_t IncomingVideoStream::Start() {
  CriticalSectionScoped csS(stream_critsect_.get());
  if (running_) {
    return 0;
  }

  if (!disable_prerenderer_smoothing_) {
    CriticalSectionScoped csT(thread_critsect_.get());
    assert(incoming_render_thread_ == NULL);

    incoming_render_thread_.reset(new rtc::PlatformThread(
        IncomingVideoStreamThreadFun, this, "IncomingVideoStreamThread"));
    if (!incoming_render_thread_) {
      return -1;
    }

    incoming_render_thread_->Start();
    incoming_render_thread_->SetPriority(rtc::kRealtimePriority);
    deliver_buffer_event_->StartTimer(false, kEventStartupTimeMs);
  }

  running_ = true;
  return 0;
}

int32_t IncomingVideoStream::Stop() {
  CriticalSectionScoped cs_stream(stream_critsect_.get());

  if (!running_) {
    return 0;
  }

  rtc::PlatformThread* thread = NULL;
  {
    CriticalSectionScoped cs_thread(thread_critsect_.get());
    if (incoming_render_thread_) {
      // Setting the incoming render thread to NULL marks that we're performing
      // a shutdown and will make IncomingVideoStreamProcess abort after wakeup.
      thread = incoming_render_thread_.release();
      deliver_buffer_event_->StopTimer();
      // Set the event to allow the thread to wake up and shut down without
      // waiting for a timeout.
      deliver_buffer_event_->Set();
    }
  }
  if (thread) {
    thread->Stop();
    delete thread;
  }
  running_ = false;
  return 0;
}

int32_t IncomingVideoStream::Reset() {
  CriticalSectionScoped cs_buffer(buffer_critsect_.get());
  render_buffers_->ReleaseAllFrames();
  return 0;
}

uint32_t IncomingVideoStream::StreamId() const {
  return stream_id_;
}

uint32_t IncomingVideoStream::IncomingRate() const {
  CriticalSectionScoped cs(stream_critsect_.get());
  return incoming_rate_;
}

bool IncomingVideoStream::IncomingVideoStreamThreadFun(void* obj) {
  return static_cast<IncomingVideoStream*>(obj)->IncomingVideoStreamProcess();
}

bool IncomingVideoStream::IncomingVideoStreamProcess() {
  if (kEventError != deliver_buffer_event_->Wait(kEventMaxWaitTimeMs)) {
    CriticalSectionScoped cs(thread_critsect_.get());
    if (incoming_render_thread_ == NULL) {
      // Terminating
      return false;
    }

    // Get a new frame to render and the time for the frame after this one.
    VideoFrame frame_to_render;
    uint32_t wait_time;
    {
      CriticalSectionScoped cs(buffer_critsect_.get());
      frame_to_render = render_buffers_->FrameToRender();
      wait_time = render_buffers_->TimeToNextFrameRelease();
    }

    // Set timer for next frame to render.
    if (wait_time > kEventMaxWaitTimeMs) {
      wait_time = kEventMaxWaitTimeMs;
    }
    deliver_buffer_event_->StartTimer(false, wait_time);

    DeliverFrame(frame_to_render);
  }
  return true;
}

void IncomingVideoStream::DeliverFrame(const VideoFrame& video_frame) {
  CriticalSectionScoped cs(thread_critsect_.get());
  if (video_frame.IsZeroSize()) {
    if (render_callback_) {
      if (last_render_time_ms_ == 0 && !start_image_.IsZeroSize()) {
        // We have not rendered anything and have a start image.
        temp_frame_.CopyFrame(start_image_);
        render_callback_->RenderFrame(stream_id_, temp_frame_);
      } else if (!timeout_image_.IsZeroSize() &&
                 last_render_time_ms_ + timeout_time_ <
                     TickTime::MillisecondTimestamp()) {
        // Render a timeout image.
        temp_frame_.CopyFrame(timeout_image_);
        render_callback_->RenderFrame(stream_id_, temp_frame_);
      }
    }

    // No frame.
    return;
  }

  // Send frame for rendering.
  if (external_callback_) {
    external_callback_->RenderFrame(stream_id_, video_frame);
  } else if (render_callback_) {
    render_callback_->RenderFrame(stream_id_, video_frame);
  }

  // We're done with this frame.
  last_render_time_ms_ = video_frame.render_time_ms();
}

}  // namespace webrtc