Ejemplo n.º 1
0
		void SetupReaderAndWriter ()
		{
			NSError error = null;

			// Create asset reader and asset writer
			assetReader = AVAssetReader.FromAsset (asset, out error);
			if (assetReader == null)
				throw new NSErrorException (error);

			assetWriter = AVAssetWriter.FromUrl (outputURL, AVFileType.QuickTimeMovie, out error);
			if (assetWriter == null)
				throw new NSErrorException (error);

			// Create asset reader outputs and asset writer inputs for the first audio track and first video track of the asset
			// Grab first audio track and first video track, if the asset has them
			AVAssetTrack audioTrack = asset.TracksWithMediaType (AVMediaType.Audio).FirstOrDefault ();
			AVAssetTrack videoTrack = asset.TracksWithMediaType (AVMediaType.Video).FirstOrDefault ();

			SetupAssetReaderWriterForAudio (audioTrack);
			SetupAssetReaserWriterForVideo (videoTrack);
		}
Ejemplo n.º 2
0
        void ReadSampleBuffers(AVAsset asset)
        {
            NSError error;
            assetReader = AVAssetReader.FromAsset (asset, out error);

            if (error != null)
                Console.WriteLine ("Error creating Asset Reader: {0}", error.Description);

            AVAssetTrack[] videoTracks = asset.TracksWithMediaType (AVMediaType.Video);
            AVAssetTrack videoTrack = videoTracks [0];
            CreateDecompressionSession (videoTrack);
            var videoTrackOutput = AVAssetReaderTrackOutput.Create (videoTrack, (AVVideoSettingsUncompressed)null);

            if (assetReader.CanAddOutput (videoTrackOutput))
                assetReader.AddOutput (videoTrackOutput);

            if (!assetReader.StartReading ())
                return;

            while (assetReader.Status == AVAssetReaderStatus.Reading) {
                CMSampleBuffer sampleBuffer = videoTrackOutput.CopyNextSampleBuffer ();
                if (sampleBuffer != null) {
                    VTDecodeFrameFlags flags = VTDecodeFrameFlags.EnableAsynchronousDecompression;
                    VTDecodeInfoFlags flagOut;
                    decompressionSession.DecodeFrame (sampleBuffer, flags, IntPtr.Zero, out flagOut);

                    sampleBuffer.Dispose ();
                    if (presentationTimes.Count >= 5)
                        bufferSemaphore.Wait ();

                } else if (assetReader.Status == AVAssetReaderStatus.Failed) {
                    Console.WriteLine ("Asset Reader failed with error: {0}", assetReader.Error.Description);
                } else if (assetReader.Status == AVAssetReaderStatus.Completed) {
                    Console.WriteLine("Reached the end of the video.");
                    ChangeStatus ();
                    ReadSampleBuffers (asset);
                }
            }
        }
Ejemplo n.º 3
0
		void ReadingAndWritingDidFinish (bool success, NSError error)
		{
			if (!success) {
				assetReader.CancelReading ();
				assetWriter.CancelWriting ();
			}

			// Tear down
			assetReader.Dispose ();
			assetReader = null;

			assetWriter.Dispose ();
			assetWriter = null;

			audioSampleBufferChannel = null;
			videoSampleBufferChannel = null;
			cancellationTokenSrc = null;

			completionProc (error);
		}
Ejemplo n.º 4
0
		private void SetupReaderAndWriter()
		{
			AVAsset localAsset = _asset;
			NSUrl localOutputURL = _outputURL;
			NSError error = null;

			// Create asset reader and asset writer
			_assetReader = new AVAssetReader (localAsset, out error);
			if (_assetReader == null)
				throw new NSErrorException(error);

			_assetWriter = new AVAssetWriter (localOutputURL, AVFileType.QuickTimeMovie, out error);
			if (_assetWriter == null)
				throw new NSErrorException(error);

			// Create asset reader outputs and asset writer inputs for the first audio track and first video track of the asset
			// Grab first audio track and first video track, if the asset has them
			AVAssetTrack audioTrack = localAsset.TracksWithMediaType (AVMediaType.Audio).FirstOrDefault ();
			AVAssetTrack videoTrack = localAsset.TracksWithMediaType (AVMediaType.Video).FirstOrDefault ();

			SetupAssetReaderWriterForAudio (audioTrack);
			SetupAssetReaserWriterForVideo (videoTrack);
		}