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_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_
12#define WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_
13
14/*
15 * video_capture_impl.h
16 */
17
18#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
19#include "webrtc/common_video/rotation.h"
20#include "webrtc/modules/video_capture/video_capture.h"
21#include "webrtc/modules/video_capture/video_capture_config.h"
22#include "webrtc/system_wrappers/include/tick_util.h"
23#include "webrtc/video_frame.h"
24
25namespace webrtc
26{
27class CriticalSectionWrapper;
28
29namespace videocapturemodule {
30// Class definitions
31class VideoCaptureImpl: public VideoCaptureModule, public VideoCaptureExternal
32{
33public:
34
35    /*
36     *   Create a video capture module object
37     *
38     *   id              - unique identifier of this video capture module object
39     *   deviceUniqueIdUTF8 -  name of the device. Available names can be found by using GetDeviceName
40     */
41    static VideoCaptureModule* Create(const int32_t id,
42                                      const char* deviceUniqueIdUTF8);
43
44    /*
45     *   Create a video capture module object used for external capture.
46     *
47     *   id              - unique identifier of this video capture module object
48     *   externalCapture - [out] interface to call when a new frame is captured.
49     */
50    static VideoCaptureModule* Create(const int32_t id,
51                                      VideoCaptureExternal*& externalCapture);
52
53    static DeviceInfo* CreateDeviceInfo(const int32_t id);
54
55    // Helpers for converting between (integral) degrees and
56    // VideoRotation values.  Return 0 on success.
57    static int32_t RotationFromDegrees(int degrees, VideoRotation* rotation);
58    static int32_t RotationInDegrees(VideoRotation rotation, int* degrees);
59
60    //Call backs
61    virtual void RegisterCaptureDataCallback(
62        VideoCaptureDataCallback& dataCallback);
63    virtual void DeRegisterCaptureDataCallback();
64    virtual void RegisterCaptureCallback(VideoCaptureFeedBack& callBack);
65    virtual void DeRegisterCaptureCallback();
66
67    virtual void SetCaptureDelay(int32_t delayMS);
68    virtual int32_t CaptureDelay();
69    virtual int32_t SetCaptureRotation(VideoRotation rotation);
70    virtual bool SetApplyRotation(bool enable);
71    virtual bool GetApplyRotation() {
72      return apply_rotation_;
73    }
74
75    virtual void EnableFrameRateCallback(const bool enable);
76    virtual void EnableNoPictureAlarm(const bool enable);
77
78    virtual const char* CurrentDeviceName() const;
79
80    // Module handling
81    virtual int64_t TimeUntilNextProcess();
82    virtual int32_t Process();
83
84    // Implement VideoCaptureExternal
85    // |capture_time| must be specified in NTP time format in milliseconds.
86    virtual int32_t IncomingFrame(uint8_t* videoFrame,
87                                  size_t videoFrameLength,
88                                  const VideoCaptureCapability& frameInfo,
89                                  int64_t captureTime = 0);
90
91    // Platform dependent
92    virtual int32_t StartCapture(const VideoCaptureCapability& capability)
93    {
94        _requestedCapability = capability;
95        return -1;
96    }
97    virtual int32_t StopCapture()   { return -1; }
98    virtual bool CaptureStarted() {return false; }
99    virtual int32_t CaptureSettings(VideoCaptureCapability& /*settings*/)
100    { return -1; }
101    VideoCaptureEncodeInterface* GetEncodeInterface(const VideoCodec& /*codec*/)
102    { return NULL; }
103
104protected:
105    VideoCaptureImpl(const int32_t id);
106    virtual ~VideoCaptureImpl();
107    int32_t DeliverCapturedFrame(VideoFrame& captureFrame);
108
109    int32_t _id; // Module ID
110    char* _deviceUniqueId; // current Device unique name;
111    CriticalSectionWrapper& _apiCs;
112    int32_t _captureDelay; // Current capture delay. May be changed of platform dependent parts.
113    VideoCaptureCapability _requestedCapability; // Should be set by platform dependent code in StartCapture.
114private:
115    void UpdateFrameCount();
116    uint32_t CalculateFrameRate(const TickTime& now);
117
118    CriticalSectionWrapper& _callBackCs;
119
120    TickTime _lastProcessTime; // last time the module process function was called.
121    TickTime _lastFrameRateCallbackTime; // last time the frame rate callback function was called.
122    bool _frameRateCallBack; // true if EnableFrameRateCallback
123    bool _noPictureAlarmCallBack; //true if EnableNoPictureAlarm
124    VideoCaptureAlarm _captureAlarm; // current value of the noPictureAlarm
125
126    int32_t _setCaptureDelay; // The currently used capture delay
127    VideoCaptureDataCallback* _dataCallBack;
128    VideoCaptureFeedBack* _captureCallBack;
129
130    TickTime _lastProcessFrameCount;
131    TickTime _incomingFrameTimes[kFrameRateCountHistorySize];// timestamp for local captured frames
132    VideoRotation _rotateFrame;  // Set if the frame should be rotated by the
133                                 // capture module.
134
135    VideoFrame _captureFrame;
136
137    // Indicate whether rotation should be applied before delivered externally.
138    bool apply_rotation_;
139};
140}  // namespace videocapturemodule
141}  // namespace webrtc
142#endif  // WEBRTC_MODULES_VIDEO_CAPTURE_MAIN_SOURCE_VIDEO_CAPTURE_IMPL_H_
143