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_MODULES_VIDEO_RENDER_MAIN_SOURCE_INCOMING_VIDEO_STREAM_H_
12 #define WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_INCOMING_VIDEO_STREAM_H_
13 
14 #include "webrtc/modules/video_render/include/video_render.h"
15 
16 namespace webrtc {
17 class CriticalSectionWrapper;
18 class EventWrapper;
19 class ThreadWrapper;
20 class VideoRenderCallback;
21 class VideoRenderFrames;
22 
23 class IncomingVideoStream : public VideoRenderCallback {
24  public:
25   IncomingVideoStream(const int32_t module_id,
26                       const uint32_t stream_id);
27   ~IncomingVideoStream();
28 
29   int32_t ChangeModuleId(const int32_t id);
30 
31   // Get callback to deliver frames to the module.
32   VideoRenderCallback* ModuleCallback();
33   virtual int32_t RenderFrame(const uint32_t stream_id,
34                               const I420VideoFrame& video_frame);
35 
36   // Set callback to the platform dependent code.
37   int32_t SetRenderCallback(VideoRenderCallback* render_callback);
38 
39   // Callback for file recording, snapshot, ...
40   int32_t SetExternalCallback(VideoRenderCallback* render_object);
41 
42   // Start/Stop.
43   int32_t Start();
44   int32_t Stop();
45 
46   // Clear all buffers.
47   int32_t Reset();
48 
49   // Properties.
50   uint32_t StreamId() const;
51   uint32_t IncomingRate() const;
52 
53   int32_t SetStartImage(const I420VideoFrame& video_frame);
54 
55   int32_t SetTimeoutImage(const I420VideoFrame& video_frame,
56                           const uint32_t timeout);
57 
58   int32_t SetExpectedRenderDelay(int32_t delay_ms);
59 
60  protected:
61   static bool IncomingVideoStreamThreadFun(void* obj);
62   bool IncomingVideoStreamProcess();
63 
64  private:
65   enum { KEventStartupTimeMS = 10 };
66   enum { KEventMaxWaitTimeMs = 100 };
67   enum { KFrameRatePeriodMs = 1000 };
68 
69   int32_t module_id_;
70   uint32_t stream_id_;
71   // Critsects in allowed to enter order.
72   CriticalSectionWrapper& stream_critsect_;
73   CriticalSectionWrapper& thread_critsect_;
74   CriticalSectionWrapper& buffer_critsect_;
75   rtc::scoped_ptr<ThreadWrapper> incoming_render_thread_;
76   EventWrapper& deliver_buffer_event_;
77   bool running_;
78 
79   VideoRenderCallback* external_callback_;
80   VideoRenderCallback* render_callback_;
81   VideoRenderFrames& render_buffers_;
82 
83   RawVideoType callbackVideoType_;
84   uint32_t callbackWidth_;
85   uint32_t callbackHeight_;
86 
87   uint32_t incoming_rate_;
88   int64_t last_rate_calculation_time_ms_;
89   uint16_t num_frames_since_last_calculation_;
90   int64_t last_render_time_ms_;
91   I420VideoFrame temp_frame_;
92   I420VideoFrame start_image_;
93   I420VideoFrame timeout_image_;
94   uint32_t timeout_time_;
95 };
96 
97 }  // namespace webrtc
98 
99 #endif  // WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_INCOMING_VIDEO_STREAM_H_
100