Coil Video Decoder Contribution

As developers, we use open-source libraries every day — but how often do we give back?

We use a lot of open-source libraries. For networking, we might use Retrofit or Ktor. For image loading, we might use Glide, Picasso or Coil. For persisting data we might use Sqlite, Datastore and many more. But, sometimes we take it for granted and we do not support the work being done in open-source.

I felt this with myself too, and I wanted to contribute to Coil, the most used Compose-Multiplatform image loading library. Below, I am gonna share the exact story from finding a proper issue and getting my PR merged. And, I am writing this in hope that other developers also benefit from this story and get a motivation to contribute to open-source.

Finding an issue

The first step is finding the proper issue from Coil. I took a look at the contributing section of the library in Github. As you can see below, they clearly stated how people should contribute to the project, and also stated that the 'help wanted' tagged issues are the best candidates to contribute.

Well. Then, I opened the issues tab on the Coil Github and then checked only help-wanted issues. Then, I found one issue that I can contribute to.

The issue was related to the video decoder. You know, Coil can show the videos as an image using Video Decoder. The logic behind is that the user can configure which frame index/percentage to get as a thumbnail. But the issue reporter is recommending to add an option to use embedded thumbnails if possible. Because, some video formats support thumbnail embedding, and it can be retrieved by the Android MediaRetriever API.

Contribution

I forked the codebase, and opened a new branch to work on my changes. When you are contributing or working on any project, you need to always stick to the style or architecture that is being used in the project. Otherwise, the project will be hard to maintain and understand with different styles and approaches.

So, when I was adding a new option to the video decoder, I checked how other options are being used in the project. And, to keep the style the same, I also added my new option in the same way

Option is added, next step is how to use this option while decoding the video.

Well, I applied all the required changes. Now, I need a way to test my code. Firstly, I checked for any visual ways of testing this code in the 'coil-example' app. Unfortunately, since the MediaRetriever API is Android-specific, Coil example app did not have any usage of the decoder. They are testing this decoder only with unit tests and integration tests (UI tests).

So, I also needed to create a few tests with my new code. Strategy is simple.

I needed 4 test cases:

  1. The video has an embedded thumbnail, and an option is enabled. Decoder should return embedded thumbnail
  2. Video has an embedded thumbnail, and option is disabled. Decoder should fallback to other options (like index based frame or percentage based).
  3. Video does not have an embedded thumbnail, option is enabled. Decoder should fallback to other options (like index based frame or percentage based). Because no embedded thumb.
  4. Video does not have an embedded thumbnail, option is disabled. Decoder should fallback to other options (like index based frame or percentage based).

To create a video with an embedded thumbnail, I used ffmpeg. Then wrote my test cases:

And I ran ./test.sh script to verify everything is OK. Then I created my PR.

After that, my code was tested on a wide range of devices, and my few test cases failed on API 26 devices. Also, I got some feedback on my implementation of decoder logic. I fixed the issues and re-sent my changes. You can take a look at my [PR], to get insights on what feedback I received and how I changed my code. Hopefully, then my PR got merged.

Conclusion

I hope, this story will be helpful.

If you’ve never contributed to open source before, start small. Pick a library you already use, browse its open issues, and see if there’s something you can help with. Even small improvements — fixing a bug, adding a test, or improving documentation — can make a big difference for the community.