Search in sources :

Example 1 with StorageUnavailableException

use of net.majorkernelpanic.streaming.exceptions.StorageUnavailableException in project libstreaming by fyhertz.

the class Session method syncStart.

/** 
	 * Starts a stream in a synchronous manner. <br />
	 * Throws exceptions in addition to calling a callback.
	 * @param id The id of the stream to start
	 **/
public void syncStart(int id) throws CameraInUseException, StorageUnavailableException, ConfNotSupportedException, InvalidSurfaceException, UnknownHostException, IOException {
    Stream stream = id == 0 ? mAudioStream : mVideoStream;
    if (stream != null && !stream.isStreaming()) {
        try {
            InetAddress destination = InetAddress.getByName(mDestination);
            stream.setTimeToLive(mTimeToLive);
            stream.setDestinationAddress(destination);
            stream.start();
            if (getTrack(1 - id) == null || getTrack(1 - id).isStreaming()) {
                postSessionStarted();
            }
            if (getTrack(1 - id) == null || !getTrack(1 - id).isStreaming()) {
                mHandler.post(mUpdateBitrate);
            }
        } catch (UnknownHostException e) {
            postError(ERROR_UNKNOWN_HOST, id, e);
            throw e;
        } catch (CameraInUseException e) {
            postError(ERROR_CAMERA_ALREADY_IN_USE, id, e);
            throw e;
        } catch (StorageUnavailableException e) {
            postError(ERROR_STORAGE_NOT_READY, id, e);
            throw e;
        } catch (ConfNotSupportedException e) {
            postError(ERROR_CONFIGURATION_NOT_SUPPORTED, id, e);
            throw e;
        } catch (InvalidSurfaceException e) {
            postError(ERROR_INVALID_SURFACE, id, e);
            throw e;
        } catch (IOException e) {
            postError(ERROR_OTHER, id, e);
            throw e;
        } catch (RuntimeException e) {
            postError(ERROR_OTHER, id, e);
            throw e;
        }
    }
}
Also used : CameraInUseException(net.majorkernelpanic.streaming.exceptions.CameraInUseException) ConfNotSupportedException(net.majorkernelpanic.streaming.exceptions.ConfNotSupportedException) UnknownHostException(java.net.UnknownHostException) StorageUnavailableException(net.majorkernelpanic.streaming.exceptions.StorageUnavailableException) AudioStream(net.majorkernelpanic.streaming.audio.AudioStream) VideoStream(net.majorkernelpanic.streaming.video.VideoStream) IOException(java.io.IOException) InetAddress(java.net.InetAddress) InvalidSurfaceException(net.majorkernelpanic.streaming.exceptions.InvalidSurfaceException)

Example 2 with StorageUnavailableException

use of net.majorkernelpanic.streaming.exceptions.StorageUnavailableException in project libstreaming by fyhertz.

the class H264Stream method testMediaRecorderAPI.

// Should not be called by the UI thread
private MP4Config testMediaRecorderAPI() throws RuntimeException, IOException {
    String key = PREF_PREFIX + "h264-mr-" + mRequestedQuality.framerate + "," + mRequestedQuality.resX + "," + mRequestedQuality.resY;
    if (mSettings != null && mSettings.contains(key)) {
        String[] s = mSettings.getString(key, "").split(",");
        return new MP4Config(s[0], s[1], s[2]);
    }
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        throw new StorageUnavailableException("No external storage or external storage not ready !");
    }
    final String TESTFILE = Environment.getExternalStorageDirectory().getPath() + "/spydroid-test.mp4";
    Log.i(TAG, "Testing H264 support... Test file saved at: " + TESTFILE);
    try {
        File file = new File(TESTFILE);
        file.createNewFile();
    } catch (IOException e) {
        throw new StorageUnavailableException(e.getMessage());
    }
    // Save flash state & set it to false so that led remains off while testing h264
    boolean savedFlashState = mFlashEnabled;
    mFlashEnabled = false;
    boolean previewStarted = mPreviewStarted;
    boolean cameraOpen = mCamera != null;
    createCamera();
    // Stops the preview if needed
    if (mPreviewStarted) {
        lockCamera();
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
        }
        mPreviewStarted = false;
    }
    try {
        Thread.sleep(100);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    unlockCamera();
    try {
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setCamera(mCamera);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mMediaRecorder.setVideoEncoder(mVideoEncoder);
        mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
        mMediaRecorder.setVideoSize(mRequestedQuality.resX, mRequestedQuality.resY);
        mMediaRecorder.setVideoFrameRate(mRequestedQuality.framerate);
        mMediaRecorder.setVideoEncodingBitRate((int) (mRequestedQuality.bitrate * 0.8));
        mMediaRecorder.setOutputFile(TESTFILE);
        mMediaRecorder.setMaxDuration(3000);
        // We wait a little and stop recording
        mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {

            public void onInfo(MediaRecorder mr, int what, int extra) {
                Log.d(TAG, "MediaRecorder callback called !");
                if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                    Log.d(TAG, "MediaRecorder: MAX_DURATION_REACHED");
                } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
                    Log.d(TAG, "MediaRecorder: MAX_FILESIZE_REACHED");
                } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN) {
                    Log.d(TAG, "MediaRecorder: INFO_UNKNOWN");
                } else {
                    Log.d(TAG, "WTF ?");
                }
                mLock.release();
            }
        });
        // Start recording
        mMediaRecorder.prepare();
        mMediaRecorder.start();
        if (mLock.tryAcquire(6, TimeUnit.SECONDS)) {
            Log.d(TAG, "MediaRecorder callback was called :)");
            Thread.sleep(400);
        } else {
            Log.d(TAG, "MediaRecorder callback was not called after 6 seconds... :(");
        }
    } catch (IOException e) {
        throw new ConfNotSupportedException(e.getMessage());
    } catch (RuntimeException e) {
        throw new ConfNotSupportedException(e.getMessage());
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        try {
            mMediaRecorder.stop();
        } catch (Exception e) {
        }
        mMediaRecorder.release();
        mMediaRecorder = null;
        lockCamera();
        if (!cameraOpen)
            destroyCamera();
        // Restore flash state
        mFlashEnabled = savedFlashState;
        if (previewStarted) {
            // If the preview was started before the test, we try to restart it.
            try {
                startPreview();
            } catch (Exception e) {
            }
        }
    }
    // Retrieve SPS & PPS & ProfileId with MP4Config
    MP4Config config = new MP4Config(TESTFILE);
    // Delete dummy video
    File file = new File(TESTFILE);
    if (!file.delete())
        Log.e(TAG, "Temp file could not be erased");
    Log.i(TAG, "H264 Test succeded...");
    // Save test result
    if (mSettings != null) {
        Editor editor = mSettings.edit();
        editor.putString(key, config.getProfileLevel() + "," + config.getB64SPS() + "," + config.getB64PPS());
        editor.commit();
    }
    return config;
}
Also used : ConfNotSupportedException(net.majorkernelpanic.streaming.exceptions.ConfNotSupportedException) MP4Config(net.majorkernelpanic.streaming.mp4.MP4Config) IOException(java.io.IOException) ConfNotSupportedException(net.majorkernelpanic.streaming.exceptions.ConfNotSupportedException) IOException(java.io.IOException) StorageUnavailableException(net.majorkernelpanic.streaming.exceptions.StorageUnavailableException) SuppressLint(android.annotation.SuppressLint) StorageUnavailableException(net.majorkernelpanic.streaming.exceptions.StorageUnavailableException) MediaRecorder(android.media.MediaRecorder) Editor(android.content.SharedPreferences.Editor) File(java.io.File)

Aggregations

IOException (java.io.IOException)2 ConfNotSupportedException (net.majorkernelpanic.streaming.exceptions.ConfNotSupportedException)2 StorageUnavailableException (net.majorkernelpanic.streaming.exceptions.StorageUnavailableException)2 SuppressLint (android.annotation.SuppressLint)1 Editor (android.content.SharedPreferences.Editor)1 MediaRecorder (android.media.MediaRecorder)1 File (java.io.File)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 AudioStream (net.majorkernelpanic.streaming.audio.AudioStream)1 CameraInUseException (net.majorkernelpanic.streaming.exceptions.CameraInUseException)1 InvalidSurfaceException (net.majorkernelpanic.streaming.exceptions.InvalidSurfaceException)1 MP4Config (net.majorkernelpanic.streaming.mp4.MP4Config)1 VideoStream (net.majorkernelpanic.streaming.video.VideoStream)1