Avaya Client SDK

< Back to Package Overview

Working with Media Devices

This articles discusses basic concepts and features relating to working with devices. Media devices can be added or removed from the environment at any time, imposing requirements that must be understood and managed by your application.

The following articles are recommended to get you started:

Retrieve the list of media devices

You can retrieve all available media devices by using three collections: microphones, speakers and cameras. First of all get the CSMediaServicesInstance object and access the <CSAudioInterface>.

CSMediaServicesInstance *mediaServices = client.mediaServices;
NSObject *audioInterface = mediaServices.audioInterface;

Iterate the availableRecordDevices property of the <CSAudioInterface> object to get the list of microphone devices.

NSArray *microphones = audioInterface.availableRecordDevices;
if (microphones.count == 0) {

    NSLog(@"There are no microphones available");

} else {

    NSLog(@"Microphones:");

    for (CSMicrophoneDevice* microphone in microphones) {

        if ([microphone isDefault]) {

            NSLog(@"* %@ (%@)", microphone.name, microphone.guid);

        } else {

            NSLog(@"  %@ (%@)", microphone.name, microphone.guid);

        }
    }
}

Iterate the availablePlayDevices property of the <CSAudioInterface> object to get the list of speaker devices.

NSArray *speakers = audioInterface.availablePlayDevices;
if (speakers.count == 0) {

    NSLog(@"There are no speakers available");

} else {

    NSLog(@"Speakers:");

    for (CSSpeakerDevice* speaker in speakers) {

        if ([speaker isDefault]) {

            NSLog(@"* %@ (%@)", speaker.name, speaker.guid);

        } else {

            NSLog(@"  %@ (%@)", speaker.name, speaker.guid);

        }
    }
}

To get the list of video devices, instantiate the CSVideoCapturerOSX object then iterate over the availableDevices property.

CSVideoCapturerOSX *videoCapturer = [[CSVideoCapturerOSX alloc] init];
NSArray *cameras = videoCapturer.availableDevices;

if (cameras.count == 0) {

    NSLog(@"There are no cameras available");
}
else {

    NSLog(@"Cameras:");

    for (CSVideoCaptureDevice* camera in cameras) {

        if (camera.isDefaultDevice) {

            NSLog(@"* %@ (%@)", camera.modelID, camera.localizedName);

        } else {

            NSLog(@"* %@ (%@)", camera.modelID, camera.localizedName);
        }
    }
}

Setting an audio microphone to be active

Use the setUserRequestedMicrophone: method of the <CSAudioInterface> object to set microphone to be active.

[audioInterface setUserRequestedMicrophone: selectedMicrophone];

Setting an audio speaker to be active

Use the setUserRequestedSpeaker: method of the <CSAudioInterface> object to set speaker to be active.

[audioInterface setUserRequestedSpeaker: selectedSpeaker];

Setting a video camera to be active

Start video capture from selected camera to set it to be active. Use the openVideoCaptureDevice:withFormat:withCompletion: method of the CSVideoCapturerOSX object.

CSVideoCaptureFormat *format = [[CSVideoCaptureFormat alloc] init];
format.minRate = 15;
format.maxRate = 30;
format.maxWidth = 1280;
format.maxHeight = 720;
[videoCapturer openVideoCaptureDevice:selectedCamera withFormat:format
    withCompletion:^(NSError *error) {
        if (error) {

            NSLog(@"Error [%ld] - %@", 
                (long)error.code, error.localizedDescription);

        } else {

            NSLog(@"Success");

        }
    }
 ];

Listening for device change events

All three collections (availableRecordDevices, availablePlayDevices and availableDevices) are observable by using Key-Value Observing (KVO) technique. At first create own method to handle device list changes.

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context {

    NSSet *oldDeviceSet = [NSSet setWithArray:change[@"old"]];
    NSSet *newDeviceSet = [NSSet setWithArray:change[@"new"]];
    NSMutableSet *addedDevices = [NSMutableSet setWithSet:newDeviceSet];
    NSMutableSet *removedDevices = [NSMutableSet setWithSet:oldDeviceSet];
    [addedDevices minusSet:oldDeviceSet];
    [removedDevices minusSet:newDeviceSet];

    if ([keyPath isEqualToString:@"availableRecordDevices"]) {

        if (addedDevices.count > 0) {

            NSLog(@"Microphones have been added:");
            for (CSMicrophoneDevice* microphone in addedDevices) {
                NSLog(@"%@ (%@)", microphone.name, microphone.guid);
            }
        }
        if (removedDevices.count > 0) {

            NSLog(@"Microphones have been removed:");
            for (CSMicrophoneDevice* microphone in removedDevices) {
                NSLog(@"%@ (%@)", microphone.name, microphone.guid);
            }
        }

    } else if ([keyPath isEqualToString:@"availablePlayDevices"]) {

        if (addedDevices.count > 0) {

            NSLog(@"Speakers have been added:");
            for (CSSpeakerDevice* speaker in addedDevices) {
                NSLog(@"%@ (%@)", speaker.name, speaker.guid);
            }
        }
        if (removedDevices.count > 0) {

            NSLog(@"Speakers have been removed:");
            for (CSSpeakerDevice* speaker in removedDevices) {
                NSLog(@"%@ (%@)", speaker.name, speaker.guid);
            }
        }

    } else if ([keyPath isEqualToString:@"availableDevices"]) {

        if (addedDevices.count > 0) {

            NSLog(@"Cameras have been added:");
            for (CSVideoCaptureDevice* camera in addedDevices) {
                NSLog(@"%@ (%@)", camera.modelID, camera.localizedName);
            }
        }
        if (removedDevices.count > 0) {

            NSLog(@"Cameras have been removed:");
            for (CSVideoCaptureDevice* camera in removedDevices) {
                NSLog(@"%@ (%@)", camera.modelID, camera.localizedName);
            }
        }

    }
}

Then add the method as observer to the <CSAudioInterface> object.

[audioInterface addObserver:self
    forKeyPath:@"availableRecordDevices"
       options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
       context:nil];

[audioInterface addObserver:self
    forKeyPath:@"availablePlayDevices"
       options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
       context:nil];

[videoCapturer addObserver:self
    forKeyPath:@"availableDevices"
       options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
       context:nil];