cordova-rtc / cordova-plugin-iosrtc

Cordova iOS plugin exposing the WebRTC W3C API
MIT License
688 stars 340 forks source link

navigator.mediaDevices is undefined #751

Closed XavierTM closed 2 years ago

XavierTM commented 2 years ago

Versions affected

Description

I wrote the code below(in the steps to reproduce section) in my index.js, the one that is created initially when cordova create. The object navigator.mediaDevices is not defined.

Steps to reproduce

sudo cordova create app
cd app
sudo cordova platform add ios
sudo cordova plugin add cordova-plugin-iosrtc

Then edit app/www/js/index.js to the following contents:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

// Wait for the deviceready event before using any of Cordova's device APIs.
// See https://cordova.apache.org/docs/en/latest/cordova/events/events.html#deviceready
document.addEventListener('deviceready', onDeviceReady, false);

async function onDeviceReady() {
    // Cordova is now initialized. Have fun!

    try {

        console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);
        document.getElementById('deviceready').classList.add('ready');

        const stream = await navigator.mediaDevices.getUserMedia({ video: true });
        const video = document.createElement('video');
        video.setAttribute('autoplay', 'autoplay');
        video.setAttribute('playsinline', 'playsinline');;
        video.srcObject = stream;

        document.documentElement.append(video)

    } catch (err) {
        alert(String(err));
    }

}

Then run

sudo cordova build ios

Expected results

I expected the video to be streamed into the <video> tag

Actual results

Simulator Screen Shot - iPhone 8 - 2022-05-26 at 12 17 34

hthetiot commented 2 years ago

@XavierTM you have not read the README properly, you need to call "cordova.plugins.iosrtc.registerGlobals();" to expose WebRTC APIs, see end of "Usage" https://github.com/cordova-rtc/cordova-plugin-iosrtc#usage and sample application source https://github.com/cordova-rtc/cordova-plugin-iosrtc-sample/blob/master/www/js/common.js#L642

XavierTM commented 2 years ago

@hthetiot Thank you, I missed that.