1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_AVFOUNDATION_MAC_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_AVFOUNDATION_MAC_H_
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#import <Foundation/Foundation.h>
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#import "base/mac/scoped_nsobject.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/synchronization/lock.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/threading/thread_checker.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#import "media/base/mac/avfoundation_glue.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#import "media/video/capture/mac/platform_video_capturing_mac.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/video/capture/video_capture_device.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/video/capture/video_capture_types.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace media {
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class VideoCaptureDeviceMac;
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)@class CrAVCaptureDevice;
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)@class CrAVCaptureSession;
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)@class CrAVCaptureVideoDataOutput;
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Class used by VideoCaptureDeviceMac (VCDM) for video capture using
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// AVFoundation API. This class lives inside the thread created by its owner
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// VCDM.
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * Clients (VCDM) should call +deviceNames to fetch the list of devices
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    available in the system; this method returns the list of device names that
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    have to be used with -setCaptureDevice:.
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * Previous to any use, clients (VCDM) must call -initWithFrameReceiver: to
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    initialise an object of this class and register a |frameReceiver_|.
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * Frame receiver registration or removal can also happen via explicit call
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    to -setFrameReceiver:. Re-registrations are safe and allowed, even during
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    capture using this method.
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * Method -setCaptureDevice: must be called at least once with a device
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    identifier from +deviceNames. Creates all the necessary AVFoundation
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    objects on first call; it connects them ready for capture every time.
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    This method should not be called during capture (i.e. between
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    -startCapture and -stopCapture).
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * -setCaptureWidth:height:frameRate: is called if a resolution or frame rate
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    different than the by default one set by -setCaptureDevice: is needed.
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    This method should not be called during capture. This method must be
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    called after -setCaptureDevice:.
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * -startCapture registers the notification listeners and starts the
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    capture. The capture can be stop using -stopCapture. The capture can be
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    restarted and restoped multiple times, reconfiguring or not the device in
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    between.
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * -setCaptureDevice can be called with a |nil| value, case in which it stops
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    the capture and disconnects the library objects. This step is not
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    necessary.
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//  * Deallocation of the library objects happens gracefully on destruction of
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//    the VideoCaptureDeviceAVFoundation object.
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)@interface VideoCaptureDeviceAVFoundation
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : NSObject<CrAVCaptureVideoDataOutputSampleBufferDelegate,
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)               PlatformVideoCapturingMac> {
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) @private
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The following attributes are set via -setCaptureHeight:width:frameRate:.
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int frameWidth_;
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int frameHeight_;
656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  float frameRate_;
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::Lock lock_;  // Protects concurrent setting and using of frameReceiver_.
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  media::VideoCaptureDeviceMac* frameReceiver_;  // weak.
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::scoped_nsobject<CrAVCaptureSession> captureSession_;
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |captureDevice_| is an object coming from AVFoundation, used only to be
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // plugged in |captureDeviceInput_| and to query for session preset support.
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  CrAVCaptureDevice* captureDevice_;
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |captureDeviceInput_| is owned by |captureSession_|.
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  CrAVCaptureDeviceInput* captureDeviceInput_;
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::scoped_nsobject<CrAVCaptureVideoDataOutput> captureVideoDataOutput_;
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ThreadChecker main_thread_checker_;
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ThreadChecker callback_thread_checker_;
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns a dictionary of capture devices with friendly name and unique id.
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)+ (NSDictionary*)deviceNames;
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Retrieve the capture supported formats for a given device |name|.
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)+ (void)getDevice:(const media::VideoCaptureDevice::Name&)name
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    supportedFormats:(media::VideoCaptureFormats*)formats;
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Initializes the instance and the underlying capture session and registers the
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// frame receiver.
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)- (id)initWithFrameReceiver:(media::VideoCaptureDeviceMac*)frameReceiver;
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Sets the frame receiver.
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)- (void)setFrameReceiver:(media::VideoCaptureDeviceMac*)frameReceiver;
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Sets which capture device to use by name, retrieved via |deviceNames|. Once
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// the deviceId is known, the library objects are created if needed and
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// connected for the capture, and a by default resolution is set. If deviceId is
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// nil, then the eventual capture is stopped and library objects are
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// disconnected. Returns YES on sucess, NO otherwise. This method should not be
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// called during capture.
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)- (BOOL)setCaptureDevice:(NSString*)deviceId;
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Configures the capture properties for the capture session and the video data
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// output; this means it MUST be called after setCaptureDevice:. Return YES on
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// success, else NO.
1086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)- (BOOL)setCaptureHeight:(int)height
1096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   width:(int)width
1106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)               frameRate:(float)frameRate;
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
112f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Starts video capturing and register the notification listeners. Must be
113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// called after setCaptureDevice:, and, eventually, also after
114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// setCaptureHeight:width:frameRate:. Returns YES on sucess, NO otherwise.
115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)- (BOOL)startCapture;
116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Stops video capturing and stops listening to notifications.
118f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)- (void)stopCapture;
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)@end
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // MEDIA_VIDEO_CAPTURE_MAC_VIDEO_CAPTURE_DEVICE_AVFOUNDATION_MAC_H_
123