Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / capture / video / mac / video_capture_device_mac.h
blobc36248eb29323ef0837fc59b30788ad7e1080ca0
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.
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.
10 #ifndef MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_MAC_H_
11 #define MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_MAC_H_
13 #import <Foundation/Foundation.h>
15 #include <string>
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/base/video_capture_types.h"
22 #include "media/capture/video/video_capture_device.h"
24 @protocol PlatformVideoCapturingMac;
26 namespace base {
27 class SingleThreadTaskRunner;
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_;
40 - (id)initWithName:(NSString*)name transportType:(int32_t)transportType;
42 - (NSString*)deviceName;
43 - (int32_t)transportType;
44 @end
46 namespace media {
48 enum {
49 // Unknown transport type, addition to the kIOAudioDeviceTransportType*
50 // family for QTKit devices where this attribute isn't published.
51 kIOAudioDeviceTransportTypeUnknown = 'unkn'
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 ~VideoCaptureDeviceMac() override;
61 // VideoCaptureDevice implementation.
62 void AllocateAndStart(const VideoCaptureParams& params,
63 scoped_ptr<VideoCaptureDevice::Client> client) override;
64 void StopAndDeAllocate() override;
66 bool Init(VideoCaptureDevice::Name::CaptureApiType capture_api_type);
68 // Called to deliver captured video frames.
69 void ReceiveFrame(const uint8* video_frame,
70 int video_frame_length,
71 const VideoCaptureFormat& frame_format,
72 int aspect_numerator,
73 int aspect_denominator);
75 // Forwarder to VideoCaptureDevice::Client::OnError().
76 void ReceiveError(const std::string& reason);
78 // Forwarder to VideoCaptureDevice::Client::OnLog().
79 void LogMessage(const std::string& message);
81 private:
82 void SetErrorState(const std::string& reason);
83 bool UpdateCaptureResolution();
85 // Flag indicating the internal state.
86 enum InternalState { kNotInitialized, kIdle, kCapturing, kError };
88 Name device_name_;
89 scoped_ptr<VideoCaptureDevice::Client> client_;
91 VideoCaptureFormat capture_format_;
92 // These variables control the two-step configure-start process for QTKit HD:
93 // the device is first started with no configuration and the captured frames
94 // are inspected to check if the camera really supports HD. AVFoundation does
95 // not need this process so |final_resolution_selected_| is false then.
96 bool final_resolution_selected_;
97 bool tried_to_square_pixels_;
99 // Only read and write state_ from inside this loop.
100 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
101 InternalState state_;
103 id<PlatformVideoCapturingMac> capture_device_;
105 // Used with Bind and PostTask to ensure that methods aren't called after the
106 // VideoCaptureDeviceMac is destroyed.
107 // NOTE: Weak pointers must be invalidated before all other member variables.
108 base::WeakPtrFactory<VideoCaptureDeviceMac> weak_factory_;
110 DISALLOW_COPY_AND_ASSIGN(VideoCaptureDeviceMac);
113 } // namespace media
115 #endif // MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_MAC_H_