1/*
2 *  Copyright (c) 2012 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_COMMON_VIDEO_INCLUDE_INCOMING_VIDEO_STREAM_H_
12#define WEBRTC_COMMON_VIDEO_INCLUDE_INCOMING_VIDEO_STREAM_H_
13
14#include "webrtc/base/platform_thread.h"
15#include "webrtc/base/scoped_ptr.h"
16#include "webrtc/base/thread_annotations.h"
17#include "webrtc/common_video/video_render_frames.h"
18
19namespace webrtc {
20class CriticalSectionWrapper;
21class EventTimerWrapper;
22
23class VideoRenderCallback {
24 public:
25  virtual int32_t RenderFrame(const uint32_t streamId,
26                              const VideoFrame& videoFrame) = 0;
27
28 protected:
29  virtual ~VideoRenderCallback() {}
30};
31
32class IncomingVideoStream : public VideoRenderCallback {
33 public:
34  IncomingVideoStream(uint32_t stream_id, bool disable_prerenderer_smoothing);
35  ~IncomingVideoStream();
36
37  // Get callback to deliver frames to the module.
38  VideoRenderCallback* ModuleCallback();
39  virtual int32_t RenderFrame(const uint32_t stream_id,
40                              const VideoFrame& video_frame);
41
42  // Set callback to the platform dependent code.
43  void SetRenderCallback(VideoRenderCallback* render_callback);
44
45  // Callback for file recording, snapshot, ...
46  void SetExternalCallback(VideoRenderCallback* render_object);
47
48  // Start/Stop.
49  int32_t Start();
50  int32_t Stop();
51
52  // Clear all buffers.
53  int32_t Reset();
54
55  // Properties.
56  uint32_t StreamId() const;
57  uint32_t IncomingRate() const;
58
59  int32_t SetStartImage(const VideoFrame& video_frame);
60
61  int32_t SetTimeoutImage(const VideoFrame& video_frame,
62                          const uint32_t timeout);
63
64  int32_t SetExpectedRenderDelay(int32_t delay_ms);
65
66 protected:
67  static bool IncomingVideoStreamThreadFun(void* obj);
68  bool IncomingVideoStreamProcess();
69
70 private:
71  enum { kEventStartupTimeMs = 10 };
72  enum { kEventMaxWaitTimeMs = 100 };
73  enum { kFrameRatePeriodMs = 1000 };
74
75  void DeliverFrame(const VideoFrame& video_frame);
76
77  uint32_t const stream_id_;
78  const bool disable_prerenderer_smoothing_;
79  // Critsects in allowed to enter order.
80  const rtc::scoped_ptr<CriticalSectionWrapper> stream_critsect_;
81  const rtc::scoped_ptr<CriticalSectionWrapper> thread_critsect_;
82  const rtc::scoped_ptr<CriticalSectionWrapper> buffer_critsect_;
83  // TODO(pbos): Make plain member and stop resetting this thread, just
84  // start/stoping it is enough.
85  rtc::scoped_ptr<rtc::PlatformThread> incoming_render_thread_
86      GUARDED_BY(thread_critsect_);
87  rtc::scoped_ptr<EventTimerWrapper> deliver_buffer_event_;
88
89  bool running_ GUARDED_BY(stream_critsect_);
90  VideoRenderCallback* external_callback_ GUARDED_BY(thread_critsect_);
91  VideoRenderCallback* render_callback_ GUARDED_BY(thread_critsect_);
92  const rtc::scoped_ptr<VideoRenderFrames> render_buffers_
93      GUARDED_BY(buffer_critsect_);
94
95  uint32_t incoming_rate_ GUARDED_BY(stream_critsect_);
96  int64_t last_rate_calculation_time_ms_ GUARDED_BY(stream_critsect_);
97  uint16_t num_frames_since_last_calculation_ GUARDED_BY(stream_critsect_);
98  int64_t last_render_time_ms_ GUARDED_BY(thread_critsect_);
99  VideoFrame temp_frame_ GUARDED_BY(thread_critsect_);
100  VideoFrame start_image_ GUARDED_BY(thread_critsect_);
101  VideoFrame timeout_image_ GUARDED_BY(thread_critsect_);
102  uint32_t timeout_time_ GUARDED_BY(thread_critsect_);
103};
104
105}  // namespace webrtc
106
107#endif  // WEBRTC_COMMON_VIDEO_INCLUDE_INCOMING_VIDEO_STREAM_H_
108