Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / video / capture / mac / video_capture_device_mac.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // MacOSX implementation of generic VideoCaptureDevice, using either QTKit or
6 // AVFoundation as native capture API. QTKit is available in all OSX versions,
7 // although namely deprecated in 10.9, and AVFoundation is available in versions
8 // 10.7 (Lion) and later.
9
10 #ifndef MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_MAC_H_
11 #define MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_MAC_H_
12
13 #import <Foundation/Foundation.h>
14
15 #include <string>
16
17 #include "base/compiler_specific.h"
18 #include "base/mac/scoped_nsobject.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/weak_ptr.h"
21 #include "media/video/capture/video_capture_device.h"
22 #include "media/video/capture/video_capture_types.h"
23
24 @protocol PlatformVideoCapturingMac;
25
26 namespace base {
27 class SingleThreadTaskRunner;
28 }
29
30 // Small class to bundle device name and connection type into a dictionary.
31 MEDIA_EXPORT
32 @interface DeviceNameAndTransportType : NSObject {
33  @private
34   base::scoped_nsobject<NSString> deviceName_;
35   // The transport type of the device (USB, PCI, etc), values are defined in
36   // <IOKit/audio/IOAudioTypes.h> as kIOAudioDeviceTransportType*.
37   int32_t transportType_;
38 }
39
40 - (id)initWithName:(NSString*)name transportType:(int32_t)transportType;
41
42 - (NSString*)deviceName;
43 - (int32_t)transportType;
44 @end
45
46 namespace media {
47
48 enum {
49   // Unknown transport type, addition to the kIOAudioDeviceTransportType*
50   // family for QTKit devices where this attribute isn't published.
51   kIOAudioDeviceTransportTypeUnknown = 'unkn'
52 };
53
54 // Called by VideoCaptureManager to open, close and start, stop Mac video
55 // capture devices.
56 class VideoCaptureDeviceMac : public VideoCaptureDevice {
57  public:
58   explicit VideoCaptureDeviceMac(const Name& device_name);
59   virtual ~VideoCaptureDeviceMac();
60
61   // VideoCaptureDevice implementation.
62   virtual void AllocateAndStart(
63       const VideoCaptureParams& params,
64       scoped_ptr<VideoCaptureDevice::Client> client) OVERRIDE;
65   virtual void StopAndDeAllocate() OVERRIDE;
66
67   bool Init(VideoCaptureDevice::Name::CaptureApiType capture_api_type);
68
69   // Called to deliver captured video frames.
70   void ReceiveFrame(const uint8* video_frame,
71                     int video_frame_length,
72                     const VideoCaptureFormat& frame_format,
73                     int aspect_numerator,
74                     int aspect_denominator);
75
76   void ReceiveError(const std::string& reason);
77
78  private:
79   void SetErrorState(const std::string& reason);
80   void LogMessage(const std::string& message);
81   bool UpdateCaptureResolution();
82
83   // Flag indicating the internal state.
84   enum InternalState {
85     kNotInitialized,
86     kIdle,
87     kCapturing,
88     kError
89   };
90
91   Name device_name_;
92   scoped_ptr<VideoCaptureDevice::Client> client_;
93
94   VideoCaptureFormat capture_format_;
95   // These variables control the two-step configure-start process for QTKit HD:
96   // the device is first started with no configuration and the captured frames
97   // are inspected to check if the camera really supports HD. AVFoundation does
98   // not need this process so |final_resolution_selected_| is false then.
99   bool final_resolution_selected_;
100   bool tried_to_square_pixels_;
101
102   // Only read and write state_ from inside this loop.
103   const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
104   InternalState state_;
105
106   id<PlatformVideoCapturingMac> capture_device_;
107
108   // Used with Bind and PostTask to ensure that methods aren't called after the
109   // VideoCaptureDeviceMac is destroyed.
110   // NOTE: Weak pointers must be invalidated before all other member variables.
111   base::WeakPtrFactory<VideoCaptureDeviceMac> weak_factory_;
112
113   DISALLOW_COPY_AND_ASSIGN(VideoCaptureDeviceMac);
114 };
115
116 }  // namespace media
117
118 #endif  // MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_MAC_H_