Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no audio coming while video is coming for the other participant. #2013

Open
4 tasks done
Naveen12345-alt opened this issue Aug 12, 2023 · 0 comments
Open
4 tasks done

Comments

@Naveen12345-alt
Copy link

Naveen12345-alt commented Aug 12, 2023

  • I have verified that the issue occurs with the latest twilio-video.js release and is not marked as a known issue in the CHANGELOG.md.
  • I reviewed the Common Issues and open GitHub issues and verified that this report represents a potentially new issue.
  • I verified that the Quickstart application works in my environment.
  • I am not sharing any Personally Identifiable Information (PII)
    or sensitive account information (API keys, credentials, etc.) when reporting this issue.

Code to reproduce the issue:

'use strict';

// Import the Twilio Video library
const Video = require('twilio-video');

// Map to store track class names based on their kind (audio or video)
const trackClassName = {
  audio: 'RemoteAudioTrack',
  video: 'RemoteVideoTrack'
};

// Initialize variables to manage room, state, and closing behavior
let room = null;
let shouldClose = false;
let isClosing = false;

// Function to add indentation to a string
function indent(str, n) {
  return str.split('\n').map(line => `  ${line}`).join('\n');
}

// Listen for unhandled errors and rejections in the window context
window.addEventListener('error', event => {
  error(`\n\n${indent(event.error.stack)}\n`);
});

window.onunhandledrejection = event => {
  error(`\n\n${indent(event.reason.stack)}\n`);
};

// Main function responsible for connecting to a Twilio Video room
async function main(token, roomSid) {
  // Log the room SID
  info(roomSid);
  // Log the attempt to connect to the room
  info('Connecting to Room...');
  // Connect to the Twilio Video room using the provided token and room name
    room = await Video.connect(token, {
      name: roomSid,
      // tracks:[],
      // audio:true,
      // video:false
    });

  info(`Connected to Room ${room.sid} as LocalParticipant ${room.localParticipant.sid}.`);
  if (shouldClose) {
    close();
    return;
  }

  // Handle participants in the room
  const participants = [...room.participants.values()];
  if (!participants.length) {
    info('There are no RemoteParticipants in the Room.');
  } else {
    let message = `There ${participants.length > 1 ? 'are' : 'is'} \
${participants.length} RemoteParticipant${participants.length > 1 ? 's' : ''} \
in the Room:\n\n`;
    participants.forEach(participant => {
      message += `  - RemoteParticipant ${participant.sid}\n`;
      participant.tracks.forEach(track => {
        if (track.kind === 'data') {
          return;
        }
        message += `    - ${trackClassName[track.kind]} ${track.trackSid}\n`;
      });
    });
    info(message);
    participants.forEach(participant => {
      participant.tracks.forEach(track => trackSubscribed(track, participant));
    });
  }

  // Set up event handlers for various room and track events
  room.on('participantConnected', participant => {
    info(`RemoteParticipant ${participant.sid} connected.`);
  });

  room.on('participantDisconnected', participant => {
    info(`RemoteParticipant ${participant.sid} disconnected.`);
  });

  room.on('trackSubscribed', (track,_, participant) => {
    if (track.kind === 'data') {
      return;
    }
    info(`Subscribed to ${trackClassName[track.kind]} ${track.sid} published \
by RemoteParticipant ${participant.sid}`);
    trackSubscribed(track, participant);
  });

  room.on('trackUnsubscribed', (track,_, participant) => {
    if (track.kind === 'data') {
      return;
    }
    info(`Unsubscribed from ${trackClassName[track.kind]} ${track.sid} \
published by RemoteParticipant ${participant.sid}`);
    trackUnsubscribed(track, participant);
  });

  // Handle disconnection from the room
  room.once('disconnected', (room, error) => {
    info(`Disconnected from Room.`);
    close(error);
  });

  // Return information about the connected room and local participant
  return {
    roomSid: room.sid,
    localParticipantSid: room.localParticipant.sid
  };
}

// Expose the main function to the window context
window.main = main;

// Function to close the bot and disconnect from the room
function close(error) {
  if (isClosing) {
    return;
  }
  isClosing = true;

  // Cleanup and unsubscribe from tracks
  recorders.forEach((recorder, track) => {
    trackUnsubscribed(track);
  });

  // Disconnect from the room if connected
  if (room && room.state !== 'disconnected') {
    info('Disconnecting from Room...');
    room.disconnect();
  }
}

// Expose the close function to the window context
window.close = close;

// Handle subscribed track
function trackSubscribed(track, participant) {
  // Handle subscribed track (e.g., recording)
  if (track.kind === 'data') {
    return;
  }
  let subscriptionCount = subscriptionCounts.get(track) || 0;
  subscriptionCount++;
  subscriptionCounts.set(track, subscriptionCount);
  const filepath = [
    room.sid,
    room.localParticipant.sid,
    track?.trackSid??track.sid,
    `${subscriptionCount}.webm`
  ];

  info(track.kind);
  if(track.mediaStreamTrack){
    info(JSON.stringify(track))
    record(track.mediaStreamTrack, filepath);
  }
}

// Handle unsubscribed track
function trackUnsubscribed(track) {
  // Handle unsubscribed track (e.g., stop recording)
  const recorder = recorders.get(track);
  recorders.delete(track);
  if (recorder) {
    // info(`Stop recording ${recorder.filename}.`);
    recorder.stop();
  }
}

// Map to store MediaRecorder instances for tracks
const recorders = new Map();
// Map to track subscription counts for tracks
const subscriptionCounts = new Map();

// Function to start recording a track
function record(track, filepath) {
  // Create filename for the recording
  const filename = filepath.join('/');
  info(`Begin recording ${filename}.`);
  createRecording(filepath);

  // Create a MediaStream containing the track
  const stream = new MediaStream([track]);

  // special case for video tracks to work around Chromium bug
  if (track.kind === 'video') {
    // NOTE(mroberts): This is a hack to workaround the following bug:
    //
    //   https://bugs.chromium.org/p/chromium/issues/detail?id=760760
    //
    const audioContext = new AudioContext();
    const destinationNode = audioContext.createMediaStreamDestination();
    const oscillatorNode = audioContext.createOscillator();
    oscillatorNode.frequency.setValueAtTime(0, audioContext.currentTime);
    oscillatorNode.connect(destinationNode);
    const [audioTrack] = destinationNode.stream.getAudioTracks();
    stream.addTrack(audioTrack);
  }

  // Create a MediaRecorder for the track and start recording
  const mimeType = `${track.kind}/webm`;
  const recorder = new MediaRecorder(stream, { mimeType });
  recorder.filename = filename;

  recorders.set(track, recorder);

  // Handle data available events from the recorder
  recorder.ondataavailable = event => {
    info(JSON.stringify(event))
    if (!event.data.size) {
      return;
    }
    const fileReader = new FileReader();
    fileReader.onload = event => {
      const buffer = event.target.result;
      appendRecording(filepath, arrayBufferToString(buffer));
    };
    fileReader.readAsArrayBuffer(event.data);
  };

  recorder.start(100);
}

// Function to convert an ArrayBuffer to a string
function arrayBufferToString(buffer) {
  const bufView = new Uint8Array(buffer);
  const length = bufView.length;

  let result = '';
  let addition = Math.pow(2, 8) - 1;

  for (let i = 0; i < length; i += addition) {
    if (i + addition > length){
        addition = length - i;
    }
    result += String.fromCharCode.apply(null, bufView.subarray(i, i + addition));
  }

  return result;
}

Expected behavior:

i should receive audio back for other participant.

Actual behavior:

i am not receiving audio back for other participant. i observed that audioLevel for other participant is coming as 0.

@Naveen12345-alt Naveen12345-alt changed the title no audio coming while video is coming no audio coming while video is coming for the other participant. Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant