aboutsummaryrefslogtreecommitdiff
path: root/webrtc/common_video/include/incoming_video_stream.h
blob: e3147eb871b1d0482f35f4b7705b28ceb3872845 (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
/*
 *  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.
 */

#ifndef WEBRTC_COMMON_VIDEO_INCLUDE_INCOMING_VIDEO_STREAM_H_
#define WEBRTC_COMMON_VIDEO_INCLUDE_INCOMING_VIDEO_STREAM_H_

#include "webrtc/base/platform_thread.h"
#include "webrtc/base/scoped_ptr.h"
#include "webrtc/base/thread_annotations.h"
#include "webrtc/common_video/video_render_frames.h"

namespace webrtc {
class CriticalSectionWrapper;
class EventTimerWrapper;

class VideoRenderCallback {
 public:
  virtual int32_t RenderFrame(const uint32_t streamId,
                              const VideoFrame& videoFrame) = 0;

 protected:
  virtual ~VideoRenderCallback() {}
};

class IncomingVideoStream : public VideoRenderCallback {
 public:
  IncomingVideoStream(uint32_t stream_id, bool disable_prerenderer_smoothing);
  ~IncomingVideoStream();

  // Get callback to deliver frames to the module.
  VideoRenderCallback* ModuleCallback();
  virtual int32_t RenderFrame(const uint32_t stream_id,
                              const VideoFrame& video_frame);

  // Set callback to the platform dependent code.
  void SetRenderCallback(VideoRenderCallback* render_callback);

  // Callback for file recording, snapshot, ...
  void SetExternalCallback(VideoRenderCallback* render_object);

  // Start/Stop.
  int32_t Start();
  int32_t Stop();

  // Clear all buffers.
  int32_t Reset();

  // Properties.
  uint32_t StreamId() const;
  uint32_t IncomingRate() const;

  int32_t SetStartImage(const VideoFrame& video_frame);

  int32_t SetTimeoutImage(const VideoFrame& video_frame,
                          const uint32_t timeout);

  int32_t SetExpectedRenderDelay(int32_t delay_ms);

 protected:
  static bool IncomingVideoStreamThreadFun(void* obj);
  bool IncomingVideoStreamProcess();

 private:
  enum { kEventStartupTimeMs = 10 };
  enum { kEventMaxWaitTimeMs = 100 };
  enum { kFrameRatePeriodMs = 1000 };

  void DeliverFrame(const VideoFrame& video_frame);

  uint32_t const stream_id_;
  const bool disable_prerenderer_smoothing_;
  // Critsects in allowed to enter order.
  const rtc::scoped_ptr<CriticalSectionWrapper> stream_critsect_;
  const rtc::scoped_ptr<CriticalSectionWrapper> thread_critsect_;
  const rtc::scoped_ptr<CriticalSectionWrapper> buffer_critsect_;
  // TODO(pbos): Make plain member and stop resetting this thread, just
  // start/stoping it is enough.
  rtc::scoped_ptr<rtc::PlatformThread> incoming_render_thread_
      GUARDED_BY(thread_critsect_);
  rtc::scoped_ptr<EventTimerWrapper> deliver_buffer_event_;

  bool running_ GUARDED_BY(stream_critsect_);
  VideoRenderCallback* external_callback_ GUARDED_BY(thread_critsect_);
  VideoRenderCallback* render_callback_ GUARDED_BY(thread_critsect_);
  const rtc::scoped_ptr<VideoRenderFrames> render_buffers_
      GUARDED_BY(buffer_critsect_);

  uint32_t incoming_rate_ GUARDED_BY(stream_critsect_);
  int64_t last_rate_calculation_time_ms_ GUARDED_BY(stream_critsect_);
  uint16_t num_frames_since_last_calculation_ GUARDED_BY(stream_critsect_);
  int64_t last_render_time_ms_ GUARDED_BY(thread_critsect_);
  VideoFrame temp_frame_ GUARDED_BY(thread_critsect_);
  VideoFrame start_image_ GUARDED_BY(thread_critsect_);
  VideoFrame timeout_image_ GUARDED_BY(thread_critsect_);
  uint32_t timeout_time_ GUARDED_BY(thread_critsect_);
};

}  // namespace webrtc

#endif  // WEBRTC_COMMON_VIDEO_INCLUDE_INCOMING_VIDEO_STREAM_H_